Coverage Report - org.kuali.rice.krms.framework.engine.TermResolutionEngineImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
TermResolutionEngineImpl
93%
125/134
86%
69/80
3.231
TermResolutionEngineImpl$1
85%
6/7
N/A
3.231
TermResolutionEngineImpl$InvalidResolutionPathException
0%
0/2
N/A
3.231
TermResolutionEngineImpl$TermResolverKey
94%
51/54
76%
29/38
3.231
TermResolutionEngineImpl$ToVisit
57%
16/28
33%
6/18
3.231
TermResolutionEngineImpl$Visited
100%
18/18
N/A
3.231
 
 1  
 package org.kuali.rice.krms.framework.engine;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Arrays;
 5  
 import java.util.Collection;
 6  
 import java.util.Collections;
 7  
 import java.util.HashMap;
 8  
 import java.util.HashSet;
 9  
 import java.util.Iterator;
 10  
 import java.util.LinkedList;
 11  
 import java.util.List;
 12  
 import java.util.Map;
 13  
 import java.util.Map.Entry;
 14  
 import java.util.PriorityQueue;
 15  
 import java.util.Set;
 16  
 
 17  
 import org.apache.commons.collections.CollectionUtils;
 18  
 import org.apache.commons.lang.ArrayUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.krms.api.engine.Term;
 21  
 import org.kuali.rice.krms.api.engine.TermResolutionEngine;
 22  
 import org.kuali.rice.krms.api.engine.TermResolutionException;
 23  
 import org.kuali.rice.krms.api.engine.TermResolver;
 24  
 import org.kuali.rice.krms.api.engine.TermSpecification;
 25  
 
 26  70
 public class TermResolutionEngineImpl implements TermResolutionEngine {
 27  1
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TermResolutionEngineImpl.class);
 28  
         
 29  70
         private final Map<TermSpecification, List<TermResolver<?>>> termResolversByOutput = new HashMap<TermSpecification, List<TermResolver<?>>>();
 30  70
         private final Map<TermResolverKey, TermResolver<?>> termResolversByKey = new HashMap<TermResolverKey, TermResolver<?>>(); 
 31  
         
 32  
         // should this use soft refs?  Will require some refactoring to check if the referenced object is around;
 33  70
         private final Map<Term, Object> termCache = new HashMap<Term, Object>();
 34  
 
 35  
         @Override
 36  
         public void addTermValue(Term term, Object value) {
 37  73
                 termCache.put(term, value);
 38  73
         }
 39  
         
 40  
         @Override
 41  
         public void addTermResolver(TermResolver<?> termResolver) {
 42  68
                 if (termResolver == null) throw new IllegalArgumentException("termResolver is reuqired");
 43  68
                 if (termResolver.getOutput() == null) throw new IllegalArgumentException("termResolver.getOutput() must not be null");
 44  
 
 45  68
                 List<TermResolver<?>> termResolvers = termResolversByOutput.get(termResolver.getOutput());
 46  68
                 if (termResolvers == null) {
 47  63
                         termResolvers = new LinkedList<TermResolver<?>>();
 48  63
                         termResolversByOutput.put(termResolver.getOutput(), termResolvers);
 49  
                 }
 50  68
                 termResolversByKey.put(new TermResolverKey(termResolver), termResolver);
 51  68
                 termResolvers.add(termResolver);
 52  68
         }
 53  
 
 54  
         @SuppressWarnings("unchecked")
 55  
         @Override
 56  
         public <T> T resolveTerm(Term term) throws TermResolutionException {
 57  49
                 LOG.debug("+--> resolveTerm(" + term + ")");
 58  49
                 if (termCache.containsKey(term)) return (T)termCache.get(term);
 59  
                 
 60  41
                 TermSpecification termSpec = term.getSpecification();
 61  
                 
 62  
                 // build plan w/ term spec for correct TermResolver selection
 63  41
                 List<TermResolverKey> resolutionPlan = buildTermResolutionPlan(termSpec);
 64  
                 // TODO: could cache these plans somewhere, since future agendas will likely require the same plans
 65  
                 
 66  41
                 LOG.debug("resolutionPlan: " + (resolutionPlan == null ? "null" : StringUtils.join(resolutionPlan.iterator(), ", ")));
 67  
                 
 68  41
                 if (resolutionPlan != null) {
 69  38
                         LOG.debug("executing plan");
 70  38
                         for (TermResolverKey resolverKey : resolutionPlan) {
 71  50
                                 TermResolver<?> resolver = termResolversByKey.get(resolverKey);
 72  
                                 
 73  
                                 // build prereqs
 74  50
                                 Map<TermSpecification, Object> resolvedPrereqs = new HashMap<TermSpecification, Object>();
 75  
                                 
 76  
                                 // The plan order should guarantee these prereqs exist and are cached.
 77  50
                                 for (TermSpecification prereq : resolver.getPrerequisites()) {
 78  25
                                         Object resolvedPrereq = termCache.get(new Term(prereq, null));
 79  25
                                         resolvedPrereqs.put(prereq, resolvedPrereq);
 80  25
                                 }
 81  
                                 
 82  50
                                 Map<String, String> providedParameters = Collections.emptyMap();
 83  
                                 // The destination Term (and only the dest Term) can be parameterized
 84  50
                                 if (termSpec.equals(resolver.getOutput())) {
 85  38
                                         providedParameters = term.getParameters();
 86  
                                         
 87  
                                         // throw a TermResolutionException if the params doen't match up
 88  38
                                         validateTermParameters(resolver, providedParameters);
 89  
                                 }
 90  
                                 
 91  49
                                 Object resolvedTerm = resolver.resolve(resolvedPrereqs, providedParameters);
 92  49
                                 if (termSpec.equals(resolver.getOutput())) {
 93  37
                                         termCache.put(term, resolvedTerm);
 94  
                                 } else {
 95  12
                                         if (!CollectionUtils.isEmpty(resolver.getParameterNames())) {
 96  
                                                 // Shouldn't happen due to checks in buildResolutionPlan
 97  0
                                                 throw new TermResolutionException("TermResolvers requiring parameters cannot be intermediates in the Term resolution plan", resolver, providedParameters);
 98  
                                         }
 99  12
                                         termCache.put(new Term(resolver.getOutput(), null), resolvedTerm);
 100  
                                 }
 101  49
                         }                
 102  
                 } else {
 103  3
                         throw new TermResolutionException("Unable to plan the resolution of " + term, null, null);
 104  
                 }
 105  37
                 return (T)termCache.get(term);
 106  
         }
 107  
 
 108  
         /**
 109  
          * This method checks that the required parameters (as returned by the {@link TermResolver} via 
 110  
          * {@link TermResolver#getParameterNames()}) are met in the {@link Map} of provided parameters.
 111  
          * 
 112  
          * @param resolver
 113  
          * @param providedParameters
 114  
          * @throws TermResolutionException
 115  
          */
 116  
         private void validateTermParameters(TermResolver<?> resolver,
 117  
                         Map<String, String> providedParameters)
 118  
                         throws TermResolutionException {
 119  
                 // validate that params match what the TermResolver requires
 120  38
                 if (!providedParameters.keySet().equals(resolver.getParameterNames())) {
 121  1
                         StringBuilder sb = new StringBuilder();
 122  
 
 123  1
                         boolean first = true;
 124  1
                         for (Entry<String,String> param : providedParameters.entrySet()) {
 125  0
                                 if (first) {
 126  0
                                         first = false;
 127  
                                 } else {
 128  0
                                         sb.append(",");
 129  
                                 }
 130  0
                                 sb.append(param.getKey());
 131  0
                                 sb.append("=");
 132  0
                                 sb.append(param.getValue());
 133  
                         }
 134  
 
 135  1
                         throw new TermResolutionException("provided parameters ("+ sb
 136  
                                         +") do not match requirements ("
 137  
                                         + StringUtils.join(resolver.getParameterNames(), ",") +")", resolver, providedParameters);
 138  
                 }
 139  37
         }
 140  
 
 141  
         protected List<TermResolverKey> buildTermResolutionPlan(TermSpecification term) {
 142  
                 // our result
 143  42
                 List<TermResolverKey> resolutionPlan = null;
 144  
 
 145  
                 // Holds the resolvers we've visited, along with the needed metadata for generating our final plan
 146  42
                 Map<TermResolverKey, Visited> visitedByKey = new HashMap<TermResolverKey, Visited>();
 147  
 
 148  
                 // this holds a least cost first list of nodes remaining to be explored
 149  42
                 PriorityQueue<ToVisit> toVisits = new PriorityQueue<ToVisit>(); // nice grammar there cowboy
 150  
 
 151  
                 // dummy resolver to be the root of this tree
 152  
                 // Do I really need this?  Yes, because there may be more than one resolver that resolves to the desired term,
 153  
                 // so this destination unifies the trees of those candidate resolvers
 154  42
                 TermResolver destination = createDestination(term); // problem is we can't get this one out of the registry
 155  42
                 TermResolverKey destinationKey = new TermResolverKey(destination);
 156  
 
 157  42
                 LOG.debug("Beginning resolution tree search for " + term);
 158  
 
 159  
                 // seed our queue of resolvers to visit
 160  
                 // need to be aware of null parent for root ToVisit
 161  42
                 toVisits.add(new ToVisit(0, destination, null));
 162  
 
 163  
                 // there may not be a viable plan
 164  42
                 boolean plannedToDestination = false;
 165  
 
 166  
                 // We'll do a modified Dijkstra's shortest path algorithm, where at each leaf we see if we've planned out
 167  
                 // term resolution all the way up to the root, our destination.  If so, we just reconstruct our plan.
 168  146
                 while (!plannedToDestination && toVisits.size() > 0) {
 169  
                         // visit least cost node remaining
 170  104
                         ToVisit visiting = toVisits.poll();
 171  
 
 172  104
                         LOG.debug("visiting " + visiting.getTermResolverKey());
 173  
 
 174  
                         // the resolver is the edge in our tree -- we don't get it directly from the termResolversByKey Map, because it could be our destination
 175  104
                         TermResolver resolver = getResolver(visiting.getTermResolverKey(), destination, destinationKey);
 176  104
                         TermResolver parent = getResolver(visiting.getParentKey(), destination, destinationKey);
 177  
 
 178  104
                         if (visitedByKey.containsKey(visiting.getTermResolverKey())) {
 179  1
                                 continue; // We've already visited this one
 180  
                         }
 181  
 
 182  103
                         Visited parentVisited = visitedByKey.get(visiting.getParentKey());
 183  
 
 184  103
                         if (resolver == null) throw new RuntimeException("Unable to get TermResolver by its key");
 185  103
                         Set<TermSpecification> prereqs = resolver.getPrerequisites();
 186  
                         // keep track of any prereqs that we already have handy
 187  103
                         List<TermSpecification> metPrereqs = new LinkedList<TermSpecification>();
 188  
 
 189  
                         // see what prereqs we have already, and which we'll need to visit
 190  103
                         if (prereqs != null) for (TermSpecification prereq : prereqs) {
 191  78
                                 if (!termCache.containsKey(new Term(prereq, null))) {
 192  
                                         // enqueue all resolvers in toVisits
 193  64
                                         List<TermResolver<?>> prereqResolvers = termResolversByOutput.get(prereq);
 194  64
                                         if (prereqResolvers != null) for (TermResolver prereqResolver : prereqResolvers) {
 195  
                                                 // Only TermResolvers that don't take paramaterized terms can be chained, so:
 196  
                                                 // if the TermResolver doesn't take parameters, or it resolves the output term
 197  66
                                                 if (CollectionUtils.isEmpty(prereqResolver.getParameterNames()) ||
 198  
                                                                 term.equals(prereqResolver.getOutput()))
 199  
                                                 {
 200  
                                                         // queue it up for visiting
 201  65
                                                         toVisits.add(new ToVisit(visiting.getCost() /* cost to get to this resolver */, prereqResolver, resolver));
 202  
                                                 }
 203  
                                         }
 204  64
                                 } else {
 205  14
                                         metPrereqs.add(prereq);
 206  
                                 }
 207  
                         }
 208  
 
 209  
                         // Build visited info
 210  103
                         Visited visited = buildVisited(resolver, parentVisited, metPrereqs);
 211  103
                         visitedByKey.put(visited.getResolverKey(), visited);
 212  
 
 213  103
                         plannedToDestination = isPlannedBackToDestination(visited, destinationKey, visitedByKey);
 214  103
                 }
 215  
 
 216  42
                 if (plannedToDestination) {
 217  
                         // build result from Visited tree.
 218  39
                         resolutionPlan = new LinkedList<TermResolverKey>();
 219  
 
 220  39
                         assembleLinearResolutionPlan(visitedByKey.get(destinationKey), visitedByKey, resolutionPlan);
 221  
                 }
 222  42
                 return resolutionPlan;
 223  
         }
 224  
 
 225  
         /**
 226  
          *  @return the Visited object for the resolver we just, er, well, visited.
 227  
          */
 228  
         private Visited buildVisited(TermResolver resolver, Visited parentVisited, Collection<TermSpecification> metPrereqs) {
 229  103
                 Visited visited = null;
 230  
 
 231  103
                 List<TermResolverKey> pathTo = new ArrayList<TermResolverKey>(1 + (parentVisited == null ? 0 : parentVisited.pathTo.size()));
 232  103
                 if (parentVisited != null && parentVisited.getPathTo() != null) pathTo.addAll(parentVisited.getPathTo());
 233  103
                 if (parentVisited != null) pathTo.add(parentVisited.getResolverKey());
 234  103
                 TermResolverKey resolverKey = new TermResolverKey(resolver);
 235  
 
 236  103
                 visited = new Visited(resolverKey, pathTo, resolver.getPrerequisites(), resolver.getCost() + (parentVisited == null ? 0 : parentVisited.getCost()));
 237  103
                 for (TermSpecification metPrereq : metPrereqs) { visited.addPlannedPrereq(metPrereq); }
 238  
 
 239  103
                 return visited;
 240  
         }
 241  
 
 242  
         /**
 243  
          * our dummy destination isn't allowed to pollute termResolversByKey, hence the ugly conditional encapsulated herein
 244  
          */
 245  
         private TermResolver getResolver(TermResolverKey resolverKey,
 246  
                         TermResolver destination, TermResolverKey destinationKey) {
 247  
                 TermResolver resolver;
 248  208
                 if (destinationKey.equals(resolverKey)) {
 249  86
                         resolver = destination;
 250  
                 } else {
 251  122
                         resolver = termResolversByKey.get(resolverKey);
 252  
                 }
 253  208
                 return resolver;
 254  
         }
 255  
 
 256  
         /**
 257  
          * @param visited
 258  
          * @param destinationKey
 259  
          * @param visitedByKey
 260  
          * @param plannedToDestination
 261  
          * @return
 262  
          */
 263  
         private boolean isPlannedBackToDestination(Visited visited,
 264  
                         TermResolverKey destinationKey,
 265  
                         Map<TermResolverKey, Visited> visitedByKey) {
 266  103
                 boolean plannedToDestination = false;
 267  103
                 if (visited.isFullyPlanned()) {
 268  42
                         LOG.debug("Leaf! this resolver's prereqs are all avialable.");
 269  
                         // no traversing further yet, instead we need to check how far up the tree is fully planned out
 270  
                         // step backwards toward the root of the tree and see if we're good all the way to our objective.
 271  
                         // if a node fully planned, move up the tree (towards the root) to see if its parent is fully planned, and so on.
 272  
 
 273  42
                         if (visited.getPathTo().size() > 0) {
 274  
                                 // reverse the path to
 275  42
                                 List<TermResolverKey> reversePathTo = new ArrayList<TermResolverKey>(visited.getPathTo());
 276  42
                                 Collections.reverse(reversePathTo);
 277  
 
 278  
                                 // we use this to propagate resolutions up the tree
 279  42
                                 Visited previousAncestor = visited;
 280  
 
 281  42
                                 for (TermResolverKey ancestorKey : reversePathTo) {
 282  
 
 283  51
                                         Visited ancestorVisited = visitedByKey.get(ancestorKey);
 284  51
                                         ancestorVisited.addPlannedPrereq(previousAncestor.getResolverKey());
 285  
 
 286  51
                                         LOG.debug("checking ancestor " + ancestorKey);
 287  
 
 288  51
                                         if (ancestorVisited.isFullyPlanned() && ancestorKey.equals(destinationKey)) {
 289  
                                                 // Woot! Job's done!
 290  39
                                                 plannedToDestination = true;
 291  39
                                                 break;
 292  12
                                         } else if (!ancestorVisited.isFullyPlanned()) { // if the ancestor isn't fully planned, we're done
 293  3
                                                 LOG.debug("Still have planning to do.");
 294  3
                                                 break;
 295  
                                         }
 296  
                                         // update previousAncestor reference for next iteration
 297  9
                                         previousAncestor = ancestorVisited;
 298  9
                                 }
 299  42
                         } else {
 300  
                                 // we're done already! do a jig?
 301  0
                                 LOG.debug("Trivial plan.");
 302  0
                                 plannedToDestination = true;
 303  
                         }
 304  
                 }
 305  103
                 return plannedToDestination;
 306  
         }
 307  
 
 308  
         private void assembleLinearResolutionPlan(Visited visited, Map<TermResolverKey, Visited> visitedByKey, List<TermResolverKey> plan) {
 309  
                 // DFS
 310  90
                 for (TermResolverKey prereqResolverKey : visited.getPrereqResolvers()) {
 311  51
                         Visited prereqVisited = visitedByKey.get(prereqResolverKey);
 312  51
                         assembleLinearResolutionPlan(prereqVisited, visitedByKey, plan);
 313  51
                         plan.add(prereqResolverKey);
 314  51
                 }
 315  90
         }
 316  
 
 317  
         /**
 318  
          * Create our dummy destination resolver
 319  
          * @param term
 320  
          */
 321  
         private TermResolver<? extends Object> createDestination(final TermSpecification term) {
 322  42
                 TermResolver<?> destination = new TermResolver<Object>() {
 323  42
                         final TermSpecification dest = new TermSpecification("", "");
 324  
                         @Override
 325  
                         public int getCost() {
 326  84
                                 return 0;
 327  
                         }
 328  
                         @Override
 329  
                         public TermSpecification getOutput() {
 330  171
                                 return dest;
 331  
                         }
 332  
                         @Override
 333  
                         public Set<TermSpecification> getPrerequisites() {
 334  255
                                 return Collections.<TermSpecification>singleton(term);
 335  
                         }
 336  
                         @Override
 337  
                         public Set<String> getParameterNames() {
 338  171
                                 return Collections.emptySet();
 339  
                         }
 340  
                         @Override
 341  
                         public Object resolve(Map<TermSpecification, Object> resolvedPrereqs, Map<String, String> parameters) throws TermResolutionException {
 342  0
                                 return null;
 343  
                         }
 344  
                 };
 345  42
                 return destination;
 346  
         }
 347  
 
 348  
 
 349  21
         private static class ToVisit implements Comparable<ToVisit> {
 350  
 
 351  
                 private final int precost;
 352  
                 private final int addcost;
 353  
                 private final TermResolverKey resolverKey;
 354  
 
 355  
                 // the parent key is not being used for comparison purposes currently
 356  
                 private final TermResolverKey parentKey;
 357  
 
 358  
                 /**
 359  
                  * @param precost
 360  
                  * @param resolver
 361  
                  */
 362  
                 public ToVisit(int precost, TermResolver resolver, TermResolver parent) {
 363  107
                         super();
 364  107
                         this.precost = precost;
 365  107
                         this.addcost = resolver.getCost();
 366  107
                         this.resolverKey = new TermResolverKey(resolver);
 367  
 
 368  107
                         if (parent != null) {
 369  65
                                 this.parentKey = new TermResolverKey(parent);
 370  
                         } else {
 371  42
                                 this.parentKey = null;
 372  
                         }
 373  107
                 }
 374  
 
 375  
                 public int getCost() {
 376  133
                         return precost + addcost;
 377  
                 }
 378  
 
 379  
                 @Override
 380  
                 public boolean equals(Object obj) {
 381  0
                         if (this == obj) return true;
 382  0
                         if (obj == null) return false;
 383  0
                         if (getClass() != obj.getClass())
 384  0
                                 return false;
 385  0
                         ToVisit other = (ToVisit)obj;
 386  0
                         return this.compareTo(other) == 0;
 387  
                 }
 388  
 
 389  
                 /* (non-Javadoc)
 390  
                  * @see java.lang.Object#hashCode()
 391  
                  */
 392  
                 @Override
 393  
                 public int hashCode() {
 394  0
                         final int prime = 31;
 395  0
                         int result = 1;
 396  0
                         result = prime * result + getCost();
 397  0
                         result = prime * result + ((resolverKey == null) ? 0 : resolverKey.hashCode());
 398  0
                         return result;
 399  
                 }
 400  
 
 401  
                 /**
 402  
                  * {@inheritDoc Comparable}
 403  
                  */
 404  
                 @Override
 405  
                 public int compareTo(ToVisit o) {
 406  21
                         if (o == null) return 1;
 407  21
                         if (getCost() > o.getCost()) return 1;
 408  13
                         if (getCost() < o.getCost()) return -1;
 409  13
                         return resolverKey.compareTo(o.resolverKey);
 410  
                 }
 411  
 
 412  
                 public TermResolverKey getTermResolverKey() {
 413  312
                         return resolverKey;
 414  
                 }
 415  
 
 416  
                 public TermResolverKey getParentKey() {
 417  207
                         return parentKey;
 418  
                 }
 419  
 
 420  
                 @Override
 421  
                 public String toString() {
 422  0
                         return getClass().getSimpleName()+"("+getTermResolverKey()+")";
 423  
                 }
 424  
         }
 425  
 
 426  0
         protected static class TermResolverKey implements Comparable<TermResolverKey> {
 427  
                 private final List<TermSpecification> data;
 428  
                 private final String [] params;
 429  
 
 430  
                 // just used for toArray call
 431  1
                 private static final TermSpecification[] TERM_SPEC_TYPER = new TermSpecification[0];
 432  1
                 private static final String[] STRING_TYPER = new String[0];
 433  
 
 434  
                 public TermResolverKey(TermResolver resolver) {
 435  385
                         this(resolver.getOutput(), resolver.getParameterNames(), resolver.getPrerequisites());
 436  385
                 }
 437  
 
 438  385
                 private TermResolverKey(TermSpecification dest, Set<String> paramSet, Set<TermSpecification> prereqs) {
 439  385
                         if (dest == null) throw new IllegalArgumentException("dest parameter must not be null");
 440  385
                         data = new ArrayList<TermSpecification>(1 + ((prereqs == null) ? 0 : prereqs.size())); // size ArrayList perfectly
 441  385
                         data.add(dest);
 442  385
                         if (!CollectionUtils.isEmpty(paramSet)) {
 443  10
                                 params = paramSet.toArray(STRING_TYPER);
 444  10
                                 Arrays.sort(params);
 445  
                         } else {
 446  375
                                 params = STRING_TYPER; // just assigning a handy zero length String array
 447  
                         }
 448  385
                         if (prereqs != null) {
 449  
                                 // this is painful, but to be able to compare we need a defined order
 450  385
                                 TermSpecification [] prereqsArray = prereqs.toArray(TERM_SPEC_TYPER);
 451  385
                                 Arrays.sort(prereqsArray);
 452  705
                                 for (TermSpecification prereq : prereqsArray) {
 453  320
                                         data.add(prereq);
 454  
                                 }
 455  
                         }
 456  385
                 }
 457  
 
 458  
                 public TermSpecification getOutput() {
 459  102
                         return data.get(0);
 460  
                 }
 461  
 
 462  
                 @Override
 463  
                 public int compareTo(TermResolverKey o) {
 464  458
                         if (o == null) return 1;
 465  
 
 466  458
                         Iterator<TermSpecification> mDataIter = data.iterator();
 467  458
                         Iterator<TermSpecification> oDataIter = o.data.iterator();
 468  
 
 469  1141
                         while (mDataIter.hasNext() && oDataIter.hasNext()) {
 470  785
                                 int itemCompareResult = mDataIter.next().compareTo(oDataIter.next());
 471  785
                                 if (itemCompareResult != 0) return itemCompareResult;
 472  683
                         }
 473  
 
 474  356
                         if (mDataIter.hasNext()) return 1;
 475  356
                         if (oDataIter.hasNext()) return -1;
 476  
 
 477  356
                         return 0;
 478  
                 }
 479  
 
 480  
                 /* (non-Javadoc)
 481  
                  * @see java.lang.Object#hashCode()
 482  
                  */
 483  
                 @Override
 484  
                 public int hashCode() {
 485  607
                         return data.hashCode();
 486  
                 }
 487  
 
 488  
                 /* (non-Javadoc)
 489  
                  * @see java.lang.Object#equals(java.lang.Object)
 490  
                  */
 491  
                 @Override
 492  
                 public boolean equals(Object obj) {
 493  487
                         if (this == obj)
 494  0
                                 return true;
 495  487
                         if (obj == null)
 496  42
                                 return false;
 497  445
                         if (getClass() != obj.getClass())
 498  0
                                 return false;
 499  445
                         TermResolverKey other = (TermResolverKey) obj;
 500  445
                         return this.compareTo(other) == 0;
 501  
                 }
 502  
 
 503  
                 @Override
 504  
                 public String toString() {
 505  206
                         StringBuilder sb = new StringBuilder();
 506  206
                         sb.append(getClass().getSimpleName());
 507  206
                         sb.append("(");
 508  206
                         Iterator<TermSpecification> iter = data.iterator();
 509  206
                         sb.append(iter.next().toString());
 510  206
                         if (params.length != 0) {
 511  6
                                 sb.append("+");
 512  6
                                 ArrayUtils.toString(params);
 513  
                         }
 514  
 
 515  206
                         if (iter.hasNext()) sb.append(" <- ");
 516  206
                         boolean first = true;
 517  369
                         while (iter.hasNext()) {
 518  163
                                 if (first) first = false;
 519  15
                                 else sb.append(", ");
 520  163
                                 sb.append(iter.next().toString());
 521  
                         }
 522  
 
 523  
 
 524  206
                         return sb.toString();
 525  
                 }
 526  
         }
 527  
 
 528  
 
 529  61
         private static class Visited {
 530  
                 private TermResolverKey resolverKey;
 531  
                 private List<TermResolverKey> pathTo;
 532  
                 private Set<TermSpecification> remainingPrereqs;
 533  
                 private Map<TermSpecification, TermResolverKey> prereqResolvers;
 534  
                 private int cost;
 535  
 
 536  
                 /**
 537  
                  * @param resolver
 538  
                  * @param pathTo
 539  
                  * @param fullyPlanned
 540  
                  * @param cost
 541  
                  */
 542  
                 public Visited(TermResolverKey resolverKey, List<TermResolverKey> pathTo, Set<TermSpecification> prereqs, int cost) {
 543  103
                         super();
 544  103
                         this.resolverKey = resolverKey;
 545  103
                         this.pathTo = pathTo;
 546  103
                         this.remainingPrereqs = new HashSet<TermSpecification>(prereqs);
 547  103
                         this.prereqResolvers = new HashMap<TermSpecification, TermResolverKey>();
 548  103
                         this.cost = cost;
 549  103
                 }
 550  
 
 551  
                 /**
 552  
                  * @return the path from the goal node down to this resolver
 553  
                  */
 554  
                 public List<TermResolverKey> getPathTo() {
 555  206
                         return pathTo;
 556  
                 }
 557  
 
 558  
                 public TermResolverKey getResolverKey() {
 559  215
                         return resolverKey;
 560  
                 }
 561  
 
 562  
                 public Collection<TermResolverKey> getPrereqResolvers() {
 563  90
                         return prereqResolvers.values();
 564  
                 }
 565  
 
 566  
                 /**
 567  
                  * @return true if resolution of all the prerequisites has been planned
 568  
                  */
 569  
                 public boolean isFullyPlanned() {
 570  166
                         return remainingPrereqs.isEmpty();
 571  
                 }
 572  
 
 573  
                 public int getCost() {
 574  61
                         return cost;
 575  
                 }
 576  
 
 577  
                 public void addPlannedPrereq(TermResolverKey termResolverKey) {
 578  51
                         remainingPrereqs.remove(termResolverKey.getOutput());
 579  51
                         prereqResolvers.put(termResolverKey.getOutput(), termResolverKey);
 580  51
                 }
 581  
 
 582  
                 public void addPlannedPrereq(TermSpecification termSpec) {
 583  14
                         remainingPrereqs.remove(termSpec);
 584  14
                 }
 585  
         }
 586  
 
 587  70
         private static class InvalidResolutionPathException extends Exception {
 588  
                 private static final long serialVersionUID = 1L;
 589  
 
 590  0
                 public InvalidResolutionPathException() {
 591  0
                 }
 592  
         }
 593  
 
 594  
 }
 595  
 
 596