1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.util.krms.proposition;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
20 import org.kuali.rice.krms.api.engine.ResultEvent;
21 import org.kuali.rice.krms.api.engine.Term;
22 import org.kuali.rice.krms.framework.engine.PropositionResult;
23 import org.kuali.rice.krms.framework.engine.result.BasicResult;
24 import org.kuali.student.common.util.krms.RulesExecutionConstants;
25
26 import java.util.Collection;
27 import java.util.Collections;
28
29
30
31
32 public class MaxCourseCompletionProposition extends AbstractLeafProposition {
33
34 private final String courseSetId;
35
36 private final String singleCourseId;
37
38 private final Integer maxCoursesCompleted;
39
40 public MaxCourseCompletionProposition(String singleCourseId) {
41 this.courseSetId = null;
42 this.singleCourseId = singleCourseId;
43 this.maxCoursesCompleted = 0;
44 }
45
46 public MaxCourseCompletionProposition(String courseSetId, Integer maxCourses) {
47 this.courseSetId = courseSetId;
48 this.singleCourseId = null;
49 this.maxCoursesCompleted = maxCourses;
50 }
51
52 @Override
53 public PropositionResult evaluate(ExecutionEnvironment environment) {
54 Collection<String> completedCourses = environment.resolveTerm(RulesExecutionConstants.completedCourseIdsTerm, this);
55 Collection<String> coursesToCheck;
56
57 if(singleCourseId == null) {
58 Term term = new Term(RulesExecutionConstants.COURSE_SET_TERM_NAME, Collections.singletonMap(RulesExecutionConstants.COURSE_SET_ID_TERM_PROPERTY, courseSetId));
59 coursesToCheck = environment.resolveTerm(term, this);
60 }
61 else {
62 coursesToCheck = Collections.singleton(singleCourseId);
63 }
64
65
66 PropositionResult result = new PropositionResult(CollectionUtils.intersection(completedCourses, coursesToCheck).size() <= maxCoursesCompleted);
67
68 environment.getEngineResults().addResult(new BasicResult(ResultEvent.PROPOSITION_EVALUATED, this, environment, result.getResult()));
69
70 return result;
71 }
72 }