View Javadoc

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  	public BasicExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions, TermResolutionEngine termResolutionEngine) {
27  		if (selectionCriteria == null) {
28  			throw new IllegalArgumentException("Selection criteria must not be null.");
29  		}
30  		if (facts == null) {
31  			throw new IllegalArgumentException("Facts must not be null.");
32  		}
33  		this.selectionCriteria = selectionCriteria;
34  		this.executionOptions = new ExecutionOptions(executionOptions);
35  		this.engineResults = new EngineResultsImpl();
36  		// TODO: inject this (will have to make it non-final)
37  		this.termResolutionEngine = new TermResolutionEngineImpl();
38  		
39  		// Add facts
40  		this.facts = new HashMap<Term, Object>(facts.size());
41  		this.facts.putAll(facts);
42  		
43  		for (Entry<Term, Object> factsEntry : facts.entrySet()) {
44  			this.termResolutionEngine.addTermValue(factsEntry.getKey(), factsEntry.getValue());
45  		}
46  	}
47  	
48  	@Override
49  	public SelectionCriteria getSelectionCriteria() {
50  		return this.selectionCriteria;
51  	}
52  	
53  	@Override
54  	public Map<Term, Object> getFacts() {
55  		return Collections.unmodifiableMap(facts);
56  	}
57  	
58  	@Override
59  	public void addTermResolver(TermResolver<?> termResolver) {
60  		termResolutionEngine.addTermResolver(termResolver);
61  	}
62  	
63  	@Override
64  	public <T> T resolveTerm(Term term) throws TermResolutionException {
65  		T value;
66  		
67  		// This looks funny, but works around a javac bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954
68  		// Specifically, using <T> below works around it.
69  		value = termResolutionEngine.<T>resolveTerm(term);
70  		
71  		publishFact(term, value);
72  		return value;
73  	}
74  
75  	@Override
76  	public boolean publishFact(Term factName, Object factValue) {
77  		if (facts.containsKey(factName) && ObjectUtils.equals(facts.get(factName), factValue)) {
78  			return false;
79  		}
80  		facts.put(factName, factValue);
81  		termResolutionEngine.addTermValue(factName, factValue);
82  		return true;
83  	}
84  
85  	@Override
86  	public ExecutionOptions getExecutionOptions() {
87  		return executionOptions;
88  	}
89  	
90  	@Override
91  	public EngineResults getEngineResults() {
92  		return engineResults;
93  	}
94  
95  }