1 | |
package org.kuali.rice.krms.framework.engine; |
2 | |
|
3 | |
import java.util.Map; |
4 | |
|
5 | |
import org.joda.time.DateTime; |
6 | |
import org.kuali.rice.krms.api.engine.Engine; |
7 | |
import org.kuali.rice.krms.api.engine.EngineResults; |
8 | |
import org.kuali.rice.krms.api.engine.ExecutionEnvironment; |
9 | |
import org.kuali.rice.krms.api.engine.ExecutionOptions; |
10 | |
import org.kuali.rice.krms.api.engine.ResultEvent; |
11 | |
import org.kuali.rice.krms.api.engine.SelectionCriteria; |
12 | |
import org.kuali.rice.krms.api.engine.Term; |
13 | |
import org.kuali.rice.krms.api.engine.TermResolutionEngine; |
14 | |
import org.kuali.rice.krms.api.engine.TermSpecification; |
15 | |
import org.kuali.rice.krms.framework.engine.result.TimingResult; |
16 | |
|
17 | 29 | public class ProviderBasedEngine implements Engine { |
18 | |
|
19 | 1 | private static final Term effectiveExecutionTimeTerm = new Term(new TermSpecification("effectiveExecutionTime", "java.lang.Long"), null); |
20 | |
|
21 | 1 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProviderBasedEngine.class); |
22 | 1 | private static final ResultLogger KLog = ResultLogger.getInstance(); |
23 | |
|
24 | |
private ContextProvider contextProvider; |
25 | |
private TermResolutionEngine termResolutionEngine; |
26 | |
|
27 | |
@Override |
28 | |
public EngineResults execute(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) { |
29 | |
DateTime start, end; |
30 | 29 | start = new DateTime(); |
31 | 29 | ExecutionEnvironment environment = establishExecutionEnvironment(selectionCriteria, facts, executionOptions); |
32 | |
|
33 | |
|
34 | 29 | Long effectiveExecutionTime = environment.getSelectionCriteria().getEffectiveExecutionTime(); |
35 | 29 | if (effectiveExecutionTime == null) { effectiveExecutionTime = System.currentTimeMillis(); } |
36 | 29 | environment.publishFact(effectiveExecutionTimeTerm, effectiveExecutionTime); |
37 | |
|
38 | 29 | Context context = selectContext(selectionCriteria, facts, executionOptions); |
39 | 29 | if (context == null) { |
40 | 0 | LOG.info("Failed to locate a Context for the given qualifiers, skipping rule engine execution: " + selectionCriteria.getContextQualifiers()); |
41 | 0 | return null; |
42 | |
} |
43 | 29 | context.execute(environment); |
44 | 29 | end = new DateTime(); |
45 | 29 | if (KLog.isEnabled(environment)){ |
46 | 29 | KLog.logResult(new TimingResult(ResultEvent.TimingEvent, this, environment, start, end)); |
47 | |
} |
48 | 29 | return environment.getEngineResults(); |
49 | |
} |
50 | |
|
51 | |
protected ExecutionEnvironment establishExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) { |
52 | 29 | return new BasicExecutionEnvironment(selectionCriteria, facts, executionOptions, termResolutionEngine); |
53 | |
} |
54 | |
|
55 | |
protected Context selectContext(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) { |
56 | 29 | if (contextProvider == null) { |
57 | 0 | throw new IllegalStateException("No ContextProvider was configured."); |
58 | |
} |
59 | 29 | return contextProvider.loadContext(selectionCriteria, facts, executionOptions); |
60 | |
} |
61 | |
|
62 | |
|
63 | |
public void setContextProvider(ContextProvider contextProvider) { |
64 | 29 | this.contextProvider = contextProvider; |
65 | 29 | } |
66 | |
|
67 | |
public void setTermResolutionEngine(TermResolutionEngine termResolutionEngine) { |
68 | 27 | this.termResolutionEngine = termResolutionEngine; |
69 | 27 | } |
70 | |
|
71 | |
} |