1 | |
package org.kuali.student.r2.core.statement.util; |
2 | |
|
3 | |
import org.joda.time.DateTime; |
4 | |
import org.kuali.rice.krms.api.engine.Engine; |
5 | |
import org.kuali.rice.krms.api.engine.EngineResults; |
6 | |
import org.kuali.rice.krms.api.engine.ExecutionFlag; |
7 | |
import org.kuali.rice.krms.api.engine.ExecutionOptions; |
8 | |
import org.kuali.rice.krms.api.engine.ResultEvent; |
9 | |
import org.kuali.rice.krms.api.engine.SelectionCriteria; |
10 | |
import org.kuali.rice.krms.api.engine.TermResolver; |
11 | |
import org.kuali.rice.krms.framework.engine.Agenda; |
12 | |
import org.kuali.rice.krms.framework.engine.BasicContext; |
13 | |
import org.kuali.rice.krms.framework.engine.Context; |
14 | |
import org.kuali.rice.krms.framework.engine.ContextProvider; |
15 | |
import org.kuali.rice.krms.framework.engine.Proposition; |
16 | |
import org.kuali.rice.krms.framework.engine.ProviderBasedEngine; |
17 | |
import org.kuali.student.common.util.krms.ManualContextProvider; |
18 | |
import org.kuali.student.r2.core.statement.dto.ReqComponentInfo; |
19 | |
|
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Arrays; |
22 | |
import java.util.Collections; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.List; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class RulesEvaluationUtil { |
34 | |
|
35 | |
private List<TermResolver<?>> termResolvers; |
36 | |
private ExecutionOptions executionOptions; |
37 | |
private Map<String, String> contextQualifiers; |
38 | |
private SelectionCriteria selectionCriteria; |
39 | |
|
40 | |
public List<TermResolver<?>> getTermResolvers() { |
41 | 0 | return termResolvers; |
42 | |
} |
43 | |
|
44 | |
public void setTermResolvers(List<TermResolver<?>> termResolvers) { |
45 | 0 | this.termResolvers = termResolvers; |
46 | 0 | } |
47 | |
|
48 | |
public ExecutionOptions getExecutionOptions() { |
49 | 0 | return executionOptions; |
50 | |
} |
51 | |
|
52 | |
public void setExecutionOptions(ExecutionOptions executionOptions) { |
53 | 0 | this.executionOptions = executionOptions; |
54 | 0 | } |
55 | |
|
56 | |
public Map<String, String> getContextQualifiers() { |
57 | 0 | return contextQualifiers; |
58 | |
} |
59 | |
|
60 | |
public void setContextQualifiers(Map<String, String> contextQualifiers) { |
61 | 0 | this.contextQualifiers = contextQualifiers; |
62 | 0 | } |
63 | |
|
64 | |
public SelectionCriteria getSelectionCriteria() { |
65 | 0 | return selectionCriteria; |
66 | |
} |
67 | |
|
68 | |
public void setSelectionCriteria(SelectionCriteria selectionCriteria) { |
69 | 0 | this.selectionCriteria = selectionCriteria; |
70 | 0 | } |
71 | |
|
72 | 0 | public RulesEvaluationUtil() { |
73 | 0 | executionOptions = new ExecutionOptions(); |
74 | 0 | executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true); |
75 | 0 | executionOptions.setFlag(ExecutionFlag.EVALUATE_ALL_PROPOSITIONS, true); |
76 | |
|
77 | 0 | contextQualifiers = new HashMap<String, String>(); |
78 | |
|
79 | 0 | contextQualifiers.put("docTypeName", "Course.PreRequisities"); |
80 | |
|
81 | 0 | Map<String, String> empty = Collections.emptyMap(); |
82 | 0 | selectionCriteria = SelectionCriteria.createCriteria(new DateTime(), contextQualifiers, empty); |
83 | 0 | } |
84 | |
|
85 | |
private ProviderBasedEngine buildEngine(Agenda agenda) { |
86 | 0 | Context context = new BasicContext(Arrays.asList(agenda), termResolvers); |
87 | 0 | ContextProvider contextProvider = new ManualContextProvider(context); |
88 | |
|
89 | 0 | ProviderBasedEngine engine = new ProviderBasedEngine(); |
90 | 0 | engine.setContextProvider(contextProvider); |
91 | |
|
92 | 0 | return engine; |
93 | |
} |
94 | |
|
95 | |
public EngineResults executeAgenda(Agenda agenda, Map<String, Object> executionFacts) { |
96 | |
|
97 | 0 | Engine engine = buildEngine(agenda); |
98 | 0 | EngineResults results = engine.execute(selectionCriteria, executionFacts, executionOptions); |
99 | |
|
100 | 0 | return results; |
101 | |
} |
102 | |
|
103 | |
public List<ReqComponentInfo> getFailedRequirementsFromEngineResults(EngineResults results, Map<Proposition, ReqComponentInfo> reqComponentPropositionMap) { |
104 | 0 | List<ReqComponentInfo> failedRequirements = new ArrayList<ReqComponentInfo>(); |
105 | |
|
106 | 0 | List<ResultEvent> events = results.getResultsOfType(ResultEvent.PROPOSITION_EVALUATED); |
107 | 0 | for (ResultEvent e : events) { |
108 | 0 | if (!e.getResult()) { |
109 | 0 | Proposition prop = (Proposition) e.getSource(); |
110 | 0 | failedRequirements.add(reqComponentPropositionMap.get(prop)); |
111 | 0 | } |
112 | |
} |
113 | |
|
114 | 0 | return failedRequirements; |
115 | |
} |
116 | |
} |