| 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 | |
|
| 8 | |
import org.apache.commons.lang.ObjectUtils; |
| 9 | |
import org.kuali.rice.krms.api.engine.EngineResults; |
| 10 | |
import org.kuali.rice.krms.api.engine.ExecutionEnvironment; |
| 11 | |
import org.kuali.rice.krms.api.engine.ExecutionOptions; |
| 12 | |
import org.kuali.rice.krms.api.engine.SelectionCriteria; |
| 13 | |
import org.kuali.rice.krms.api.engine.Term; |
| 14 | |
import org.kuali.rice.krms.api.engine.TermResolutionEngine; |
| 15 | |
import org.kuali.rice.krms.api.engine.TermResolutionException; |
| 16 | |
import org.kuali.rice.krms.api.engine.TermResolver; |
| 17 | |
|
| 18 | |
public final class BasicExecutionEnvironment implements ExecutionEnvironment { |
| 19 | |
|
| 20 | |
private final SelectionCriteria selectionCriteria; |
| 21 | |
private final Map<Term, Object> facts; |
| 22 | |
private final ExecutionOptions executionOptions; |
| 23 | |
private final EngineResults engineResults; |
| 24 | |
private final TermResolutionEngine termResolutionEngine; |
| 25 | |
|
| 26 | 29 | public BasicExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions, TermResolutionEngine termResolutionEngine) { |
| 27 | 29 | if (selectionCriteria == null) { |
| 28 | 0 | throw new IllegalArgumentException("Selection criteria must not be null."); |
| 29 | |
} |
| 30 | 29 | if (facts == null) { |
| 31 | 0 | throw new IllegalArgumentException("Facts must not be null."); |
| 32 | |
} |
| 33 | 29 | this.selectionCriteria = selectionCriteria; |
| 34 | 29 | this.executionOptions = new ExecutionOptions(executionOptions); |
| 35 | 29 | this.engineResults = new EngineResultsImpl(); |
| 36 | |
|
| 37 | 29 | this.termResolutionEngine = new TermResolutionEngineImpl(); |
| 38 | |
|
| 39 | |
|
| 40 | 29 | this.facts = new HashMap<Term, Object>(facts.size()); |
| 41 | 29 | this.facts.putAll(facts); |
| 42 | |
|
| 43 | 29 | for (Entry<Term, Object> factsEntry : facts.entrySet()) { |
| 44 | 0 | this.termResolutionEngine.addTermValue(factsEntry.getKey(), factsEntry.getValue()); |
| 45 | |
} |
| 46 | 29 | } |
| 47 | |
|
| 48 | |
@Override |
| 49 | |
public SelectionCriteria getSelectionCriteria() { |
| 50 | 58 | return this.selectionCriteria; |
| 51 | |
} |
| 52 | |
|
| 53 | |
@Override |
| 54 | |
public Map<Term, Object> getFacts() { |
| 55 | 0 | return Collections.unmodifiableMap(facts); |
| 56 | |
} |
| 57 | |
|
| 58 | |
@Override |
| 59 | |
public void addTermResolver(TermResolver<?> termResolver) { |
| 60 | 29 | termResolutionEngine.addTermResolver(termResolver); |
| 61 | 29 | } |
| 62 | |
|
| 63 | |
@Override |
| 64 | |
public <T> T resolveTerm(Term term) throws TermResolutionException { |
| 65 | |
T value; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 36 | value = termResolutionEngine.<T>resolveTerm(term); |
| 70 | |
|
| 71 | 36 | publishFact(term, value); |
| 72 | 36 | return value; |
| 73 | |
} |
| 74 | |
|
| 75 | |
@Override |
| 76 | |
public boolean publishFact(Term factName, Object factValue) { |
| 77 | 65 | if (facts.containsKey(factName) && ObjectUtils.equals(facts.get(factName), factValue)) { |
| 78 | 7 | return false; |
| 79 | |
} |
| 80 | 58 | facts.put(factName, factValue); |
| 81 | 58 | termResolutionEngine.addTermValue(factName, factValue); |
| 82 | 58 | return true; |
| 83 | |
} |
| 84 | |
|
| 85 | |
@Override |
| 86 | |
public ExecutionOptions getExecutionOptions() { |
| 87 | 410 | return executionOptions; |
| 88 | |
} |
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public EngineResults getEngineResults() { |
| 92 | 1418 | return engineResults; |
| 93 | |
} |
| 94 | |
|
| 95 | |
} |