| 1 | |
package org.kuali.rice.krms.framework.engine; |
| 2 | |
|
| 3 | |
import java.util.Collections; |
| 4 | |
import java.util.HashMap; |
| 5 | |
import java.util.Map; |
| 6 | |
import java.util.Map.Entry; |
| 7 | |
import java.util.Set; |
| 8 | |
import java.util.HashSet; |
| 9 | |
|
| 10 | |
import org.apache.commons.lang.ObjectUtils; |
| 11 | |
import org.kuali.rice.krms.api.engine.EngineResults; |
| 12 | |
import org.kuali.rice.krms.api.engine.ExecutionEnvironment; |
| 13 | |
import org.kuali.rice.krms.api.engine.ExecutionOptions; |
| 14 | |
import org.kuali.rice.krms.api.engine.SelectionCriteria; |
| 15 | |
import org.kuali.rice.krms.api.engine.Term; |
| 16 | |
import org.kuali.rice.krms.api.engine.TermResolutionEngine; |
| 17 | |
import org.kuali.rice.krms.api.engine.TermResolutionException; |
| 18 | |
import org.kuali.rice.krms.api.engine.TermResolver; |
| 19 | |
import org.kuali.rice.krms.api.engine.ResultEvent; |
| 20 | |
|
| 21 | |
|
| 22 | |
public final class BasicExecutionEnvironment implements ExecutionEnvironment { |
| 23 | |
|
| 24 | |
private final SelectionCriteria selectionCriteria; |
| 25 | |
private final Map<Term, Object> facts; |
| 26 | |
private final ExecutionOptions executionOptions; |
| 27 | |
private final EngineResults engineResults; |
| 28 | |
private final TermResolutionEngine termResolutionEngine; |
| 29 | |
private Map<Object, Set<Term>> termPropositionMap; |
| 30 | |
|
| 31 | 29 | public BasicExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions, TermResolutionEngine termResolutionEngine) { |
| 32 | 29 | if (selectionCriteria == null) { |
| 33 | 0 | throw new IllegalArgumentException("Selection criteria must not be null."); |
| 34 | |
} |
| 35 | 29 | if (facts == null) { |
| 36 | 0 | throw new IllegalArgumentException("Facts must not be null."); |
| 37 | |
} |
| 38 | 29 | this.selectionCriteria = selectionCriteria; |
| 39 | 29 | this.executionOptions = new ExecutionOptions(executionOptions); |
| 40 | 29 | this.engineResults = new EngineResultsImpl(); |
| 41 | |
|
| 42 | 29 | this.termResolutionEngine = new TermResolutionEngineImpl(); |
| 43 | |
|
| 44 | |
|
| 45 | 29 | this.facts = new HashMap<Term, Object>(facts.size()); |
| 46 | 29 | this.facts.putAll(facts); |
| 47 | |
|
| 48 | 29 | for (Entry<Term, Object> factsEntry : facts.entrySet()) { |
| 49 | 0 | this.termResolutionEngine.addTermValue(factsEntry.getKey(), factsEntry.getValue()); |
| 50 | |
} |
| 51 | 29 | } |
| 52 | |
|
| 53 | |
@Override |
| 54 | |
public SelectionCriteria getSelectionCriteria() { |
| 55 | 58 | return this.selectionCriteria; |
| 56 | |
} |
| 57 | |
|
| 58 | |
@Override |
| 59 | |
public Map<Term, Object> getFacts() { |
| 60 | 0 | return Collections.unmodifiableMap(facts); |
| 61 | |
} |
| 62 | |
|
| 63 | |
@Override |
| 64 | |
public void addTermResolver(TermResolver<?> termResolver) { |
| 65 | 29 | termResolutionEngine.addTermResolver(termResolver); |
| 66 | 29 | } |
| 67 | |
|
| 68 | |
@Override |
| 69 | |
public <T> T resolveTerm(Term term, Object caller) throws TermResolutionException { |
| 70 | |
T value; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | 36 | value = termResolutionEngine.<T>resolveTerm(term); |
| 75 | |
|
| 76 | 36 | if (caller != null) { |
| 77 | 36 | if(termPropositionMap == null) { |
| 78 | 29 | termPropositionMap = new HashMap<Object, Set<Term>>(); |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | 36 | if(termPropositionMap.containsKey(caller)) { |
| 83 | 3 | termPropositionMap.get(caller).add(term); |
| 84 | |
} else { |
| 85 | 33 | termPropositionMap.put(caller, new HashSet<Term>()); |
| 86 | 33 | termPropositionMap.get(caller).add(term); |
| 87 | |
} |
| 88 | |
} |
| 89 | |
|
| 90 | 36 | publishFact(term, value); |
| 91 | |
|
| 92 | 36 | return value; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public Set<Term> getTermsForCaller(Object caller) { |
| 96 | 0 | return termPropositionMap.get(caller); |
| 97 | |
} |
| 98 | |
|
| 99 | |
@Override |
| 100 | |
public boolean publishFact(Term factName, Object factValue) { |
| 101 | 65 | if (facts.containsKey(factName) && ObjectUtils.equals(facts.get(factName), factValue)) { |
| 102 | 7 | return false; |
| 103 | |
} |
| 104 | 58 | facts.put(factName, factValue); |
| 105 | 58 | termResolutionEngine.addTermValue(factName, factValue); |
| 106 | 58 | return true; |
| 107 | |
} |
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public ExecutionOptions getExecutionOptions() { |
| 111 | 418 | return executionOptions; |
| 112 | |
} |
| 113 | |
|
| 114 | |
@Override |
| 115 | |
public EngineResults getEngineResults() { |
| 116 | 1437 | return engineResults; |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
} |