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