1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.framework;
17
18 import static junit.framework.Assert.assertFalse;
19 import static junit.framework.Assert.assertNotNull;
20 import static junit.framework.Assert.assertTrue;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.kuali.rice.krms.api.engine.EngineResults;
32 import org.kuali.rice.krms.api.engine.ExecutionOptions;
33 import org.kuali.rice.krms.api.engine.ExecutionFlag;
34 import org.kuali.rice.krms.api.engine.Facts;
35 import org.kuali.rice.krms.api.engine.SelectionCriteria;
36 import org.kuali.rice.krms.api.engine.Term;
37 import org.kuali.rice.krms.api.engine.TermResolver;
38 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
39 import org.kuali.rice.krms.framework.engine.Action;
40 import org.kuali.rice.krms.framework.engine.Agenda;
41 import org.kuali.rice.krms.framework.engine.AgendaTreeEntry;
42 import org.kuali.rice.krms.framework.engine.BasicAgenda;
43 import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
44 import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
45 import org.kuali.rice.krms.framework.engine.BasicContext;
46 import org.kuali.rice.krms.framework.engine.BasicRule;
47 import org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition;
48 import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
49 import org.kuali.rice.krms.framework.engine.Context;
50 import org.kuali.rice.krms.framework.engine.ContextProvider;
51 import org.kuali.rice.krms.framework.engine.Proposition;
52 import org.kuali.rice.krms.framework.engine.ProviderBasedEngine;
53 import org.kuali.rice.krms.framework.engine.ResultLogger;
54 import org.kuali.rice.krms.framework.engine.Rule;
55
56 public class AgendaTest {
57 private static final ResultLogger LOG = ResultLogger.getInstance();
58
59 @Before
60 public void setUp() {
61 ActionMock.resetActionsFired();
62 }
63
64
65 private Proposition trueProp = new ComparableTermBasedProposition(ComparisonOperator.GREATER_THAN, totalCostTerm, Integer.valueOf(1));
66 private Proposition falseProp = new ComparableTermBasedProposition(ComparisonOperator.GREATER_THAN, totalCostTerm, Integer.valueOf(1000));
67
68 @Test
69 public void testAllRulesAgenda() {
70
71 Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
72 Rule rule2 = new BasicRule("r2", falseProp, Collections.<Action>singletonList(new ActionMock("a2")));
73 Rule rule3 = new BasicRule("r3", trueProp, Collections.<Action>singletonList(new ActionMock("a3")));
74
75 AgendaTreeEntry entry1 = new BasicAgendaTreeEntry(rule1);
76 AgendaTreeEntry entry2 = new BasicAgendaTreeEntry(rule2);
77 AgendaTreeEntry entry3 = new BasicAgendaTreeEntry(rule3);
78 BasicAgendaTree agendaTree = new BasicAgendaTree(entry1, entry2, entry3);
79 Agenda agenda = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree);
80
81 execute(agenda);
82
83 assertTrue(ActionMock.actionFired("a1"));
84 assertFalse(ActionMock.actionFired("a2"));
85 assertTrue(ActionMock.actionFired("a3"));
86 }
87
88 @Test
89 public void testIfTrueSubAgenda() {
90
91 Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
92 Rule rule2 = new BasicRule("r2", falseProp, Collections.<Action>singletonList(new ActionMock("a2")));
93 Rule subRule1 = new BasicRule("r1s1", trueProp, Collections.<Action>singletonList(new ActionMock("a3")));
94
95 BasicAgendaTree subAgendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(subRule1));
96 BasicAgendaTree agendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule1, subAgendaTree1, null));
97 Agenda agenda1 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree1);
98
99 execute(agenda1);
100
101 assertTrue(ActionMock.actionFired("a1"));
102 assertTrue(ActionMock.actionFired("a3"));
103
104
105 ActionMock.resetActionsFired();
106
107 BasicAgendaTree subAgendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(subRule1));
108 BasicAgendaTree agendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule2, subAgendaTree2, null));
109 Agenda agenda2 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree2);
110
111 execute(agenda2);
112
113 assertFalse(ActionMock.actionFired("a2"));
114 assertFalse(ActionMock.actionFired("a3"));
115 }
116
117 @Test
118 public void testIfFalseSubAgenda() {
119
120 Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
121 Rule rule2 = new BasicRule("r2", falseProp, Collections.<Action>singletonList(new ActionMock("a2")));
122 Rule subRule1 = new BasicRule("r1s1", trueProp, Collections.<Action>singletonList(new ActionMock("a3")));
123
124 BasicAgendaTree subAgendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(subRule1));
125 BasicAgendaTree agendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule1, null, subAgendaTree1));
126 Agenda agenda1 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree1);
127
128 execute(agenda1);
129
130 assertTrue(ActionMock.actionFired("a1"));
131 assertFalse(ActionMock.actionFired("a3"));
132
133
134 ActionMock.resetActionsFired();
135
136 BasicAgendaTree subAgendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(subRule1));
137 BasicAgendaTree agendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule2, null, subAgendaTree2));
138 Agenda agenda2 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree2);
139
140 execute(agenda2);
141
142 assertFalse(ActionMock.actionFired("a2"));
143 assertTrue(ActionMock.actionFired("a3"));
144 }
145
146 @Test
147 public void testAfterAgenda() {
148
149 Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
150 Rule rule2 = new BasicRule("r2", falseProp, Collections.<Action>singletonList(new ActionMock("a2")));
151 Rule subRule1 = new BasicRule("r1s1", trueProp, Collections.<Action>singletonList(new ActionMock("a3")));
152
153 BasicAgendaTree agendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule1), new BasicAgendaTreeEntry(subRule1));
154 Agenda agenda1 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree1);
155
156 execute(agenda1);
157
158 assertTrue(ActionMock.actionFired("a1"));
159 assertTrue(ActionMock.actionFired("a3"));
160
161
162 ActionMock.resetActionsFired();
163
164 BasicAgendaTree agendaTree2 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule2), new BasicAgendaTreeEntry(subRule1));
165 Agenda agenda2 = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree2);
166
167 execute(agenda2);
168
169 assertFalse(ActionMock.actionFired("a2"));
170 assertTrue(ActionMock.actionFired("a3"));
171 }
172
173
174
175
176
177 public void testQualifierMissingFromAgenda() {
178
179 ActionMock.resetActionsFired();
180
181 Rule rule1 = new BasicRule("r1", trueProp, Collections.<Action>singletonList(new ActionMock("a1")));
182
183 BasicAgendaTree agendaTree1 = new BasicAgendaTree(new BasicAgendaTreeEntry(rule1));
184 Agenda agenda1 = new BasicAgenda(Collections.<String, String>emptyMap(), agendaTree1);
185
186
187 execute(agenda1, Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"));
188
189
190 assertFalse("the agenda should not have been selected and executed", ActionMock.actionFired("a1"));
191 }
192
193
194
195
196
197
198 private void execute(Agenda agenda) {
199 execute(agenda, Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"));
200 }
201
202
203
204
205
206
207
208 private void execute(Agenda agenda, Map<String, String> agendaQualifiers) {
209 Map<String, String> contextQualifiers = new HashMap<String, String>();
210 contextQualifiers.put("docTypeName", "Proposal");
211
212 List<TermResolver<?>> testResolvers = new ArrayList<TermResolver<?>>();
213 testResolvers.add(testResolver);
214
215 Context context = new BasicContext(Arrays.asList(agenda), testResolvers);
216 ContextProvider contextProvider = new ManualContextProvider(context);
217
218 SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers,
219 agendaQualifiers);
220
221 ProviderBasedEngine engine = new ProviderBasedEngine();
222 engine.setContextProvider(contextProvider);
223
224
225 ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true);
226
227 EngineResults results = engine.execute(selectionCriteria, Facts.EMPTY_FACTS, executionOptions);
228 assertNotNull(results);
229 }
230
231 private static final Term totalCostTerm = new Term("totalCost");
232
233 private static final TermResolver<Integer> testResolver = new TermResolverMock<Integer>(totalCostTerm.getName(), 10);
234 }