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.LinkedList; |
10 | |
import java.util.List; |
11 | |
import java.util.Map; |
12 | |
import java.util.PriorityQueue; |
13 | |
import java.util.Set; |
14 | |
|
15 | |
import org.apache.commons.lang.StringUtils; |
16 | |
import org.kuali.rice.krms.api.Asset; |
17 | |
import org.kuali.rice.krms.api.AssetResolutionEngine; |
18 | |
import org.kuali.rice.krms.api.AssetResolutionException; |
19 | |
import org.kuali.rice.krms.api.AssetResolver; |
20 | |
|
21 | 0 | public class AssetResolutionEngineImpl implements AssetResolutionEngine { |
22 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetResolutionEngineImpl.class); |
23 | |
|
24 | 0 | private final Map<Asset, List<AssetResolver<?>>> assetResolversByOutput = new HashMap<Asset, List<AssetResolver<?>>>(); |
25 | 0 | private final Map<AssetResolverKey, AssetResolver<?>> assetResolversByKey = new HashMap<AssetResolverKey, AssetResolver<?>>(); |
26 | |
|
27 | |
|
28 | 0 | private final Map<Asset, Object> assetCache = new HashMap<Asset, Object>(); |
29 | |
|
30 | |
@Override |
31 | |
public void addAssetValue(Asset asset, Object value) { |
32 | 0 | assetCache.put(asset, value); |
33 | 0 | } |
34 | |
|
35 | |
@Override |
36 | |
public void addAssetResolver(AssetResolver<?> assetResolver) { |
37 | 0 | if (assetResolver == null) throw new IllegalArgumentException("assetResolver is reuqired"); |
38 | 0 | if (assetResolver.getOutput() == null) throw new IllegalArgumentException("assetResolver.getOutput() must not be null"); |
39 | |
|
40 | 0 | List<AssetResolver<?>> assetResolvers = assetResolversByOutput.get(assetResolver.getOutput()); |
41 | 0 | if (assetResolvers == null) { |
42 | 0 | assetResolvers = new LinkedList<AssetResolver<?>>(); |
43 | 0 | assetResolversByOutput.put(assetResolver.getOutput(), assetResolvers); |
44 | |
} |
45 | 0 | assetResolversByKey.put(new AssetResolverKey(assetResolver), assetResolver); |
46 | 0 | assetResolvers.add(assetResolver); |
47 | 0 | } |
48 | |
|
49 | |
@SuppressWarnings("unchecked") |
50 | |
@Override |
51 | |
public <T> T resolveAsset(final Asset asset) throws AssetResolutionException { |
52 | 0 | LOG.debug("+--> resolveAsset(" + asset + ")"); |
53 | 0 | if (assetCache.containsKey(asset)) return (T)assetCache.get(asset); |
54 | |
|
55 | 0 | List<AssetResolverKey> resolutionPlan = buildAssetResolutionPlan(asset); |
56 | |
|
57 | |
|
58 | 0 | LOG.debug("resolutionPlan: " + (resolutionPlan == null ? "null" : StringUtils.join(resolutionPlan.iterator(), ", "))); |
59 | |
|
60 | 0 | if (resolutionPlan != null) { |
61 | 0 | LOG.debug("executing plan"); |
62 | 0 | for (AssetResolverKey resolverKey : resolutionPlan) { |
63 | 0 | AssetResolver<?> resolver = assetResolversByKey.get(resolverKey); |
64 | |
|
65 | |
|
66 | 0 | Map<Asset, Object> resolvedPrereqs = new HashMap<Asset, Object>(); |
67 | |
|
68 | |
|
69 | |
|
70 | 0 | for (Asset prereq : resolver.getPrerequisites()) { |
71 | 0 | Object resolvedPrereq = assetCache.get(prereq); |
72 | 0 | resolvedPrereqs.put(prereq, resolvedPrereq); |
73 | 0 | } |
74 | |
|
75 | 0 | Object resolvedAsset = resolver.resolve(resolvedPrereqs); |
76 | 0 | assetCache.put(resolver.getOutput(), resolvedAsset); |
77 | 0 | } |
78 | |
} else { |
79 | 0 | throw new AssetResolutionException("Unable to plan the resolution of asset " + asset); |
80 | |
} |
81 | 0 | return (T)assetCache.get(asset); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
protected List<AssetResolverKey> buildAssetResolutionPlan(Asset asset) { |
86 | |
|
87 | 0 | List<AssetResolverKey> resolutionPlan = null; |
88 | |
|
89 | |
|
90 | 0 | Map<AssetResolverKey, Visited> visitedByKey = new HashMap<AssetResolverKey, Visited>(); |
91 | |
|
92 | |
|
93 | 0 | PriorityQueue<ToVisit> toVisits = new PriorityQueue<ToVisit>(); |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | 0 | AssetResolver destination = createDestination(asset); |
99 | 0 | AssetResolverKey destinationKey = new AssetResolverKey(destination); |
100 | |
|
101 | 0 | LOG.debug("Beginning resolution tree search for " + asset); |
102 | |
|
103 | |
|
104 | |
|
105 | 0 | toVisits.add(new ToVisit(0, destination, null)); |
106 | |
|
107 | |
|
108 | 0 | boolean plannedToDestination = false; |
109 | |
|
110 | |
|
111 | |
|
112 | 0 | while (!plannedToDestination && toVisits.size() > 0) { |
113 | |
|
114 | 0 | ToVisit visiting = toVisits.poll(); |
115 | |
|
116 | 0 | LOG.debug("visiting " + visiting.getAssetResolverKey()); |
117 | |
|
118 | |
|
119 | 0 | AssetResolver resolver = getResolver(visiting.getAssetResolverKey(), destination, destinationKey); |
120 | 0 | AssetResolver parent = getResolver(visiting.getParentKey(), destination, destinationKey); |
121 | |
|
122 | 0 | if (visitedByKey.containsKey(visiting.getAssetResolverKey())) { |
123 | 0 | continue; |
124 | |
} |
125 | |
|
126 | 0 | Visited parentVisited = visitedByKey.get(visiting.getParentKey()); |
127 | |
|
128 | 0 | if (resolver == null) throw new RuntimeException("Unable to get AssetResolver by its key"); |
129 | 0 | Set<Asset> prereqs = resolver.getPrerequisites(); |
130 | |
|
131 | 0 | List<Asset> metPrereqs = new LinkedList<Asset>(); |
132 | |
|
133 | |
|
134 | 0 | if (prereqs != null) for (Asset prereq : prereqs) { |
135 | 0 | if (!assetCache.containsKey(prereq)) { |
136 | |
|
137 | 0 | List<AssetResolver<?>> prereqResolvers = assetResolversByOutput.get(prereq); |
138 | 0 | if (prereqResolvers != null) for (AssetResolver prereqResolver : prereqResolvers) { |
139 | 0 | toVisits.add(new ToVisit(visiting.getCost() , prereqResolver, resolver)); |
140 | |
} |
141 | 0 | } else { |
142 | 0 | metPrereqs.add(prereq); |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
|
147 | 0 | Visited visited = buildVisited(resolver, parentVisited, metPrereqs); |
148 | 0 | visitedByKey.put(visited.getResolverKey(), visited); |
149 | |
|
150 | 0 | plannedToDestination = isPlannedBackToDestination(visited, destinationKey, visitedByKey); |
151 | 0 | } |
152 | |
|
153 | 0 | if (plannedToDestination) { |
154 | |
|
155 | 0 | resolutionPlan = new LinkedList<AssetResolverKey>(); |
156 | |
|
157 | 0 | assembleLinearResolutionPlan(visitedByKey.get(destinationKey), visitedByKey, resolutionPlan); |
158 | |
} |
159 | 0 | return resolutionPlan; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private Visited buildVisited(AssetResolver resolver, Visited parentVisited, Collection<Asset> metPrereqs) { |
166 | 0 | Visited visited = null; |
167 | |
|
168 | 0 | List<AssetResolverKey> pathTo = new ArrayList<AssetResolverKey>(1 + (parentVisited == null ? 0 : parentVisited.pathTo.size())); |
169 | 0 | if (parentVisited != null && parentVisited.getPathTo() != null) pathTo.addAll(parentVisited.getPathTo()); |
170 | 0 | if (parentVisited != null) pathTo.add(parentVisited.getResolverKey()); |
171 | 0 | AssetResolverKey resolverKey = new AssetResolverKey(resolver); |
172 | |
|
173 | 0 | visited = new Visited(resolverKey, pathTo, resolver.getPrerequisites(), resolver.getCost() + (parentVisited == null ? 0 : parentVisited.getCost())); |
174 | 0 | for (Asset metPrereq : metPrereqs) { visited.addPlannedPrereq(metPrereq); } |
175 | |
|
176 | 0 | return visited; |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
private AssetResolver getResolver(AssetResolverKey resolverKey, |
183 | |
AssetResolver destination, AssetResolverKey destinationKey) { |
184 | |
AssetResolver resolver; |
185 | 0 | if (destinationKey.equals(resolverKey)) { |
186 | 0 | resolver = destination; |
187 | |
} else { |
188 | 0 | resolver = assetResolversByKey.get(resolverKey); |
189 | |
} |
190 | 0 | return resolver; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
private boolean isPlannedBackToDestination(Visited visited, |
201 | |
AssetResolverKey destinationKey, |
202 | |
Map<AssetResolverKey, Visited> visitedByKey) { |
203 | 0 | boolean plannedToDestination = false; |
204 | 0 | if (visited.isFullyPlanned()) { |
205 | 0 | LOG.debug("Leaf! this resolver's prereqs are all avialable."); |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | 0 | if (visited.getPathTo().size() > 0) { |
211 | |
|
212 | 0 | List<AssetResolverKey> reversePathTo = new ArrayList<AssetResolverKey>(visited.getPathTo()); |
213 | 0 | Collections.reverse(reversePathTo); |
214 | |
|
215 | |
|
216 | 0 | Visited previousAncestor = visited; |
217 | |
|
218 | 0 | for (AssetResolverKey ancestorKey : reversePathTo) { |
219 | |
|
220 | 0 | Visited ancestorVisited = visitedByKey.get(ancestorKey); |
221 | 0 | ancestorVisited.addPlannedPrereq(previousAncestor.getResolverKey()); |
222 | |
|
223 | 0 | LOG.debug("checking ancestor " + ancestorKey); |
224 | |
|
225 | 0 | if (ancestorVisited.isFullyPlanned() && ancestorKey.equals(destinationKey)) { |
226 | |
|
227 | 0 | plannedToDestination = true; |
228 | 0 | break; |
229 | 0 | } else if (!ancestorVisited.isFullyPlanned()) { |
230 | 0 | LOG.debug("Still have planning to do."); |
231 | 0 | break; |
232 | |
} |
233 | |
|
234 | 0 | previousAncestor = ancestorVisited; |
235 | 0 | } |
236 | 0 | } else { |
237 | |
|
238 | 0 | LOG.debug("Trivial plan."); |
239 | 0 | plannedToDestination = true; |
240 | |
} |
241 | |
} |
242 | 0 | return plannedToDestination; |
243 | |
} |
244 | |
|
245 | |
private void assembleLinearResolutionPlan(Visited visited, Map<AssetResolverKey, Visited> visitedByKey, List<AssetResolverKey> plan) { |
246 | |
|
247 | 0 | for (AssetResolverKey prereqResolverKey : visited.getPrereqResolvers()) { |
248 | 0 | Visited prereqVisited = visitedByKey.get(prereqResolverKey); |
249 | 0 | assembleLinearResolutionPlan(prereqVisited, visitedByKey, plan); |
250 | 0 | plan.add(prereqResolverKey); |
251 | 0 | } |
252 | 0 | } |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
private AssetResolver<? extends Object> createDestination(final Asset asset) { |
259 | 0 | AssetResolver<?> destination = new AssetResolver<Object>() { |
260 | 0 | final Asset dest = new Asset("", ""); |
261 | |
@Override |
262 | |
public int getCost() { |
263 | 0 | return 0; |
264 | |
} |
265 | |
@Override |
266 | |
public Asset getOutput() { |
267 | 0 | return dest; |
268 | |
} |
269 | |
@Override |
270 | |
public Set<Asset> getPrerequisites() { |
271 | 0 | return Collections.<Asset>singleton(asset); |
272 | |
} |
273 | |
@Override |
274 | |
public Object resolve(Map<Asset, Object> resolvedPrereqs) throws AssetResolutionException { |
275 | 0 | return null; |
276 | |
} |
277 | |
}; |
278 | 0 | return destination; |
279 | |
} |
280 | |
|
281 | |
|
282 | 0 | private static class ToVisit implements Comparable<ToVisit> { |
283 | |
|
284 | |
private final int precost; |
285 | |
private final int addcost; |
286 | |
private final AssetResolverKey resolverKey; |
287 | |
|
288 | |
|
289 | |
private final AssetResolverKey parentKey; |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public ToVisit(int precost, AssetResolver resolver, AssetResolver parent) { |
296 | 0 | super(); |
297 | 0 | this.precost = precost; |
298 | 0 | this.addcost = resolver.getCost(); |
299 | 0 | this.resolverKey = new AssetResolverKey(resolver.getOutput(), resolver.getPrerequisites()); |
300 | |
|
301 | 0 | if (parent != null) { |
302 | 0 | this.parentKey = new AssetResolverKey(parent.getOutput(), parent.getPrerequisites()); |
303 | |
} else { |
304 | 0 | this.parentKey = null; |
305 | |
} |
306 | 0 | } |
307 | |
|
308 | |
public int getCost() { |
309 | 0 | return precost + addcost; |
310 | |
} |
311 | |
|
312 | |
@Override |
313 | |
public boolean equals(Object obj) { |
314 | 0 | if (this == obj) return true; |
315 | 0 | if (obj == null) return false; |
316 | 0 | if (getClass() != obj.getClass()) |
317 | 0 | return false; |
318 | 0 | ToVisit other = (ToVisit)obj; |
319 | 0 | return this.compareTo(other) == 0; |
320 | |
} |
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
@Override |
326 | |
public int hashCode() { |
327 | 0 | final int prime = 31; |
328 | 0 | int result = 1; |
329 | 0 | result = prime * result + getCost(); |
330 | 0 | result = prime * result + ((resolverKey == null) ? 0 : resolverKey.hashCode()); |
331 | 0 | return result; |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
@Override |
338 | |
public int compareTo(ToVisit o) { |
339 | 0 | if (o == null) return 1; |
340 | 0 | if (getCost() > o.getCost()) return 1; |
341 | 0 | if (getCost() < o.getCost()) return -1; |
342 | 0 | return resolverKey.compareTo(o.resolverKey); |
343 | |
} |
344 | |
|
345 | |
public AssetResolverKey getAssetResolverKey() { |
346 | 0 | return resolverKey; |
347 | |
} |
348 | |
|
349 | |
public AssetResolverKey getParentKey() { |
350 | 0 | return parentKey; |
351 | |
} |
352 | |
|
353 | |
@Override |
354 | |
public String toString() { |
355 | 0 | return getClass().getSimpleName()+"("+getAssetResolverKey()+")"; |
356 | |
} |
357 | |
} |
358 | |
|
359 | 0 | protected static class AssetResolverKey implements Comparable<AssetResolverKey> { |
360 | |
private final List<Asset> data; |
361 | |
|
362 | |
|
363 | 0 | private static final Asset[] TYPER = new Asset[0]; |
364 | |
|
365 | |
public AssetResolverKey(AssetResolver resolver) { |
366 | 0 | this(resolver.getOutput(), resolver.getPrerequisites()); |
367 | 0 | } |
368 | |
|
369 | 0 | private AssetResolverKey(Asset dest, Set<Asset> prereqs) { |
370 | 0 | if (dest == null) throw new IllegalArgumentException("dest parameter must not be null"); |
371 | 0 | data = new ArrayList<Asset>(1 + ((prereqs == null) ? 0 : prereqs.size())); |
372 | 0 | data.add(dest); |
373 | 0 | if (prereqs != null) { |
374 | |
|
375 | 0 | Asset [] prereqsArray = prereqs.toArray(TYPER); |
376 | 0 | Arrays.sort(prereqsArray); |
377 | 0 | for (Asset prereq : prereqsArray) { |
378 | 0 | data.add(prereq); |
379 | |
} |
380 | |
} |
381 | 0 | } |
382 | |
|
383 | |
public Asset getOutput() { |
384 | 0 | return data.get(0); |
385 | |
} |
386 | |
|
387 | 0 | private String comparatorHelperMemo = null; |
388 | |
|
389 | |
private String getComparatorHelper() { |
390 | 0 | if (comparatorHelperMemo == null) { |
391 | 0 | StringBuilder sb = new StringBuilder(); |
392 | 0 | for (int i=0; i<data.size(); i++) { |
393 | 0 | Asset asset = data.get(i); |
394 | 0 | if (i == 1) { |
395 | 0 | sb.append(" <- "); |
396 | 0 | } else if (i > 1) { |
397 | 0 | sb.append(","); |
398 | |
} |
399 | 0 | if (asset != null) { |
400 | 0 | sb.append(asset.getComparatorHelper()); |
401 | |
} |
402 | |
} |
403 | 0 | comparatorHelperMemo = sb.toString(); |
404 | |
} |
405 | 0 | return comparatorHelperMemo; |
406 | |
} |
407 | |
|
408 | |
@Override |
409 | |
public int compareTo(AssetResolverKey o) { |
410 | 0 | if (o == null) return 1; |
411 | 0 | return getComparatorHelper().compareTo(o.getComparatorHelper()); |
412 | |
} |
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
@Override |
418 | |
public int hashCode() { |
419 | 0 | return data.hashCode(); |
420 | |
} |
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
@Override |
426 | |
public boolean equals(Object obj) { |
427 | 0 | if (this == obj) |
428 | 0 | return true; |
429 | 0 | if (obj == null) |
430 | 0 | return false; |
431 | 0 | if (getClass() != obj.getClass()) |
432 | 0 | return false; |
433 | 0 | AssetResolverKey other = (AssetResolverKey) obj; |
434 | 0 | return this.compareTo(other) == 0; |
435 | |
} |
436 | |
|
437 | |
@Override |
438 | |
public String toString() { |
439 | 0 | return getClass().getSimpleName()+"("+getComparatorHelper()+")"; |
440 | |
} |
441 | |
} |
442 | |
|
443 | |
|
444 | 0 | private static class Visited { |
445 | |
private AssetResolverKey resolverKey; |
446 | |
private List<AssetResolverKey> pathTo; |
447 | |
private Set<Asset> remainingPrereqs; |
448 | |
private Map<Asset, AssetResolverKey> prereqResolvers; |
449 | |
private int cost; |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
public Visited(AssetResolverKey resolverKey, List<AssetResolverKey> pathTo, Set<Asset> prereqs, int cost) { |
458 | 0 | super(); |
459 | 0 | this.resolverKey = resolverKey; |
460 | 0 | this.pathTo = pathTo; |
461 | 0 | this.remainingPrereqs = new HashSet<Asset>(prereqs); |
462 | 0 | this.prereqResolvers = new HashMap<Asset, AssetResolverKey>(); |
463 | 0 | this.cost = cost; |
464 | 0 | } |
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
public List<AssetResolverKey> getPathTo() { |
470 | 0 | return pathTo; |
471 | |
} |
472 | |
|
473 | |
public AssetResolverKey getResolverKey() { |
474 | 0 | return resolverKey; |
475 | |
} |
476 | |
|
477 | |
public Collection<AssetResolverKey> getPrereqResolvers() { |
478 | 0 | return prereqResolvers.values(); |
479 | |
} |
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
public boolean isFullyPlanned() { |
485 | 0 | return remainingPrereqs.isEmpty(); |
486 | |
} |
487 | |
|
488 | |
public int getCost() { |
489 | 0 | return cost; |
490 | |
} |
491 | |
|
492 | |
public void addPlannedPrereq(AssetResolverKey assetResolverKey) { |
493 | 0 | remainingPrereqs.remove(assetResolverKey.getOutput()); |
494 | 0 | prereqResolvers.put(assetResolverKey.getOutput(), assetResolverKey); |
495 | 0 | } |
496 | |
|
497 | |
public void addPlannedPrereq(Asset asset) { |
498 | 0 | remainingPrereqs.remove(asset); |
499 | 0 | } |
500 | |
} |
501 | |
|
502 | 0 | private static class InvalidResolutionPathException extends Exception { |
503 | |
private static final long serialVersionUID = 1L; |
504 | |
|
505 | 0 | public InvalidResolutionPathException() { |
506 | 0 | } |
507 | |
} |
508 | |
|
509 | |
} |
510 | |
|
511 | |
|