1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.framework.engine;
17
18 import java.util.Map;
19
20 import org.joda.time.DateTime;
21 import org.kuali.rice.krms.api.engine.Engine;
22 import org.kuali.rice.krms.api.engine.EngineResults;
23 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
24 import org.kuali.rice.krms.api.engine.ExecutionOptions;
25 import org.kuali.rice.krms.api.engine.Facts;
26 import org.kuali.rice.krms.api.engine.ResultEvent;
27 import org.kuali.rice.krms.api.engine.SelectionCriteria;
28 import org.kuali.rice.krms.api.engine.Term;
29 import org.kuali.rice.krms.framework.engine.result.TimingResult;
30
31
32
33
34
35 public class ProviderBasedEngine implements Engine {
36
37 private static final Term effectiveExecutionTimeTerm = new Term("effectiveExecutionTime", null);
38
39 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProviderBasedEngine.class);
40 private static final ResultLogger KLog = ResultLogger.getInstance();
41
42 private ContextProvider contextProvider;
43
44 @Override
45 public EngineResults execute(SelectionCriteria selectionCriteria, Map<String, Object> facts,
46 ExecutionOptions executionOptions) {
47 return execute(selectionCriteria,
48 Facts.Builder.create().addFactsByName(facts).build(),
49 executionOptions);
50 }
51
52 @Override
53 public EngineResults execute(SelectionCriteria selectionCriteria, Facts facts, ExecutionOptions executionOptions) {
54 DateTime start, end;
55 start = new DateTime();
56 ExecutionEnvironment environment = establishExecutionEnvironment(selectionCriteria, facts.getFactMap(), executionOptions);
57
58
59 Long effectiveExecutionTime = environment.getSelectionCriteria().getEffectiveExecutionTime();
60 if (effectiveExecutionTime == null) { effectiveExecutionTime = System.currentTimeMillis(); }
61 environment.publishFact(effectiveExecutionTimeTerm, effectiveExecutionTime);
62
63 Context context = selectContext(selectionCriteria, facts.getFactMap(), executionOptions);
64 if (context == null) {
65 LOG.info("Failed to locate a Context for the given qualifiers, skipping rule engine execution: " + selectionCriteria.getContextQualifiers());
66 return null;
67 }
68 context.execute(environment);
69 end = new DateTime();
70 if (KLog.isEnabled(environment)){
71 KLog.logResult(new TimingResult(ResultEvent.TIMING_EVENT, this, environment, start, end));
72 }
73 return environment.getEngineResults();
74 }
75
76
77
78
79
80
81
82
83 protected ExecutionEnvironment establishExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
84 return new BasicExecutionEnvironment(selectionCriteria, facts, executionOptions, new TermResolutionEngineImpl());
85 }
86
87
88
89
90
91
92
93
94
95
96 protected Context selectContext(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
97 if (contextProvider == null) {
98 throw new IllegalStateException("No ContextProvider was configured.");
99 }
100 return contextProvider.loadContext(selectionCriteria, facts, executionOptions);
101 }
102
103
104
105
106
107 public void setContextProvider(ContextProvider contextProvider) {
108 this.contextProvider = contextProvider;
109 }
110
111 }