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