View Javadoc

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