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 public class ProviderBasedEngine implements Engine {
32
33 private static final Term effectiveExecutionTimeTerm = new Term("effectiveExecutionTime", null);
34
35 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProviderBasedEngine.class);
36 private static final ResultLogger KLog = ResultLogger.getInstance();
37
38 private ContextProvider contextProvider;
39
40 @Override
41 public EngineResults execute(SelectionCriteria selectionCriteria, Map<String, Object> facts,
42 ExecutionOptions executionOptions) {
43 return execute(selectionCriteria,
44 Facts.Builder.create().addFactsByName(facts).build(),
45 executionOptions);
46 }
47
48 @Override
49 public EngineResults execute(SelectionCriteria selectionCriteria, Facts facts, ExecutionOptions executionOptions) {
50 DateTime start, end;
51 start = new DateTime();
52 ExecutionEnvironment environment = establishExecutionEnvironment(selectionCriteria, facts.getFactMap(), executionOptions);
53
54
55 Long effectiveExecutionTime = environment.getSelectionCriteria().getEffectiveExecutionTime();
56 if (effectiveExecutionTime == null) { effectiveExecutionTime = System.currentTimeMillis(); }
57 environment.publishFact(effectiveExecutionTimeTerm, effectiveExecutionTime);
58
59 Context context = selectContext(selectionCriteria, facts.getFactMap(), executionOptions);
60 if (context == null) {
61 LOG.info("Failed to locate a Context for the given qualifiers, skipping rule engine execution: " + selectionCriteria.getContextQualifiers());
62 return null;
63 }
64 context.execute(environment);
65 end = new DateTime();
66 if (KLog.isEnabled(environment)){
67 KLog.logResult(new TimingResult(ResultEvent.TimingEvent, this, environment, start, end));
68 }
69 return environment.getEngineResults();
70 }
71
72 protected ExecutionEnvironment establishExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
73 return new BasicExecutionEnvironment(selectionCriteria, facts, executionOptions, new TermResolutionEngineImpl());
74 }
75
76 protected Context selectContext(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
77 if (contextProvider == null) {
78 throw new IllegalStateException("No ContextProvider was configured.");
79 }
80 return contextProvider.loadContext(selectionCriteria, facts, executionOptions);
81 }
82
83
84 public void setContextProvider(ContextProvider contextProvider) {
85 this.contextProvider = contextProvider;
86 }
87
88 }