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