1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.test;
17
18 import org.joda.time.DateTime;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.kuali.rice.kew.util.PerformanceLogger;
22 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
23 import org.kuali.rice.krms.api.engine.EngineResults;
24 import org.kuali.rice.krms.api.engine.ExecutionFlag;
25 import org.kuali.rice.krms.api.engine.ExecutionOptions;
26 import org.kuali.rice.krms.api.engine.Facts;
27 import org.kuali.rice.krms.api.engine.ResultEvent;
28 import org.kuali.rice.krms.api.engine.SelectionCriteria;
29 import org.kuali.rice.krms.api.repository.LogicalOperator;
30 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
31 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
32 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
33 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
34 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
35 import org.kuali.rice.krms.api.repository.function.FunctionParameterDefinition;
36 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
37 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
38 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
39 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
40 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
41 import org.kuali.rice.krms.api.repository.term.TermDefinition;
42 import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
43 import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
44 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
45 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
46 import org.kuali.rice.krms.impl.repository.ActionBoService;
47 import org.kuali.rice.krms.impl.repository.AgendaBoService;
48 import org.kuali.rice.krms.impl.repository.FunctionBoServiceImpl;
49 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
50 import org.kuali.rice.krms.impl.repository.RuleBoService;
51 import org.kuali.rice.krms.impl.repository.TermBo;
52 import org.kuali.rice.krms.impl.repository.TermBoService;
53 import org.kuali.rice.test.BaselineTestCase.BaselineMode;
54 import org.kuali.rice.test.BaselineTestCase.Mode;
55 import org.springframework.transaction.annotation.Transactional;
56
57 import java.util.ArrayList;
58 import java.util.Collections;
59 import java.util.HashMap;
60 import java.util.List;
61 import java.util.Map;
62
63 import static org.junit.Assert.*;
64
65 @BaselineMode(Mode.CLEAR_DB)
66 public class RepositoryCreateAndExecuteIntegrationTest extends AbstractBoTest {
67
68 public static final String CAMPUS_CODE_TERM_NAME = "campusCodeTermSpec";
69
70 static final String NAMESPACE1 = "KRMS_TEST_1";
71 static final String NAMESPACE2 = "KRMS_TEST_2";
72 static final String TSUNAMI_EVENT = "Tsunami";
73 static final String EARTHQUAKE_EVENT = "Earthquake";
74 static final String CONTEXT1 = "Context1";
75 static final String CONTEXT2 = "Context2";
76 static final String NAME = "name";
77 static final String CONTEXT1_QUALIFIER = "Context1Qualifier";
78 static final String CONTEXT1_QUALIFIER_VALUE = "BLAH1";
79 static final String CONTEXT2_QUALIFIER = "Context2Qualifier";
80 static final String CONTEXT2_QUALIFIER_VALUE = "BLAH2";
81 static final String AGENDA1 = "TestAgenda1";
82 static final String AGENDA2 = "Agenda2";
83 static final String AGENDA3 = "Agenda3";
84 static final String AGENDA4 = "Agenda4";
85 static final String PREREQ_TERM_NAME = "prereqTermSpec";
86 static final String PREREQ_TERM_VALUE = "prereqValue";
87 static final String NAMESPACE_CODE = "namespaceCode";
88 static final String BOOL1 = "bool1";
89 static final String BOOL2 = "bool2";
90
91
92 private TermBoService termBoService;
93
94 private RuleBoService ruleBoService;
95 private AgendaBoService agendaBoService;
96 private ActionBoService actionBoService;
97 private FunctionBoServiceImpl functionBoService;
98
99
100
101
102
103
104
105 protected List<String> getPerTestTablesNotToClear() {
106 List<String> tablesNotToClear = super.getPerTestTablesNotToClear();
107
108 tablesNotToClear.add("KRMS_.*");
109
110 return tablesNotToClear;
111 }
112
113
114
115 @Before
116 public void setup() {
117
118 TestActionTypeService.resetActionsFired();
119
120 termBoService = KrmsRepositoryServiceLocator.getTermBoService();
121 contextRepository = KrmsRepositoryServiceLocator.getContextBoService();
122 krmsTypeRepository = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
123
124 ruleBoService = KrmsRepositoryServiceLocator.getRuleBoService();
125 agendaBoService = KrmsRepositoryServiceLocator.getAgendaBoService();
126 actionBoService = KrmsRepositoryServiceLocator.getBean("actionBoService");
127 functionBoService = KrmsRepositoryServiceLocator.getBean("functionRepositoryService");
128 krmsAttributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
129
130 ContextDefinition contextDefintion1 = contextRepository.getContextByNameAndNamespace(CONTEXT1, NAMESPACE1);
131
132
133 if (contextDefintion1 == null) {
134 PerformanceLogger perfLog = new PerformanceLogger();
135 perfLog.log("starting agenda creation");
136
137 contextDefintion1 = createContextDefinition(NAMESPACE1, CONTEXT1, Collections.singletonMap(
138 CONTEXT1_QUALIFIER, CONTEXT1_QUALIFIER_VALUE));
139 createAgendaDefinition(AGENDA1, contextDefintion1, TSUNAMI_EVENT, NAMESPACE1);
140
141 ContextDefinition contextDefinition2 = createContextDefinition(NAMESPACE2, CONTEXT2, Collections.singletonMap(
142 CONTEXT2_QUALIFIER, CONTEXT2_QUALIFIER_VALUE));
143
144
145 createAgendaDefinition(AGENDA2, contextDefinition2, EARTHQUAKE_EVENT, NAMESPACE2);
146 createAgendaDefinition(AGENDA3, contextDefinition2, EARTHQUAKE_EVENT, NAMESPACE2);
147 createAgendaDefinition(AGENDA4, contextDefinition2, TSUNAMI_EVENT, NAMESPACE2);
148
149 perfLog.log("finished agenda creation", true);
150 }
151 }
152
153
154 @Transactional
155 @Test
156 public void testSelectAgendaByAttributeAndName() {
157
158 Map<String,String> contextQualifiers = new HashMap<String,String>();
159 contextQualifiers.put(NAMESPACE_CODE, NAMESPACE1);
160 contextQualifiers.put(NAME, CONTEXT1);
161 contextQualifiers.put(CONTEXT1_QUALIFIER, CONTEXT1_QUALIFIER_VALUE);
162
163 Map<String,String> agendaQualifiers = new HashMap<String,String>();
164 agendaQualifiers.put(AgendaDefinition.Constants.EVENT, TSUNAMI_EVENT);
165 agendaQualifiers.put(NAME, AGENDA1);
166
167 DateTime now = new DateTime();
168
169 SelectionCriteria sc1 = SelectionCriteria.createCriteria(now, contextQualifiers,
170 Collections.singletonMap(AgendaDefinition.Constants.EVENT, TSUNAMI_EVENT));
171
172 Facts.Builder factsBuilder1 = Facts.Builder.create();
173 factsBuilder1.addFact(CAMPUS_CODE_TERM_NAME, "BL");
174 factsBuilder1.addFact(BOOL1, "true");
175 factsBuilder1.addFact(BOOL2, Boolean.TRUE);
176 factsBuilder1.addFact(PREREQ_TERM_NAME, PREREQ_TERM_VALUE);
177
178 ExecutionOptions xOptions1 = new ExecutionOptions();
179 xOptions1.setFlag(ExecutionFlag.LOG_EXECUTION, true);
180
181 PerformanceLogger perfLog = new PerformanceLogger();
182 perfLog.log("starting rule execution");
183 EngineResults eResults1 = KrmsApiServiceLocator.getEngine().execute(sc1, factsBuilder1.build(), xOptions1);
184 perfLog.log("finished rule execution", true);
185 List<ResultEvent> rEvents1 = eResults1.getAllResults();
186
187 List<ResultEvent> ruleEvaluationResults1 = eResults1.getResultsOfType(ResultEvent.RULE_EVALUATED.toString());
188
189 assertEquals("4 rules should have been evaluated", 4, ruleEvaluationResults1.size());
190
191 assertTrue("rule 0 should have evaluated to true", ruleEvaluationResults1.get(0).getResult());
192 assertFalse("rule 1 should have evaluated to false", ruleEvaluationResults1.get(1).getResult());
193 assertTrue("rule 2 should have evaluated to true", ruleEvaluationResults1.get(2).getResult());
194
195
196 assertTrue(TestActionTypeService.actionFired("TestAgenda1::Rule1::TestAction"));
197 assertFalse(TestActionTypeService.actionFired("TestAgenda1::Rule2::TestAction"));
198 assertTrue(TestActionTypeService.actionFired("TestAgenda1::Rule3::TestAction"));
199
200 assertAgendaDidNotExecute(AGENDA2);
201 assertAgendaDidNotExecute(AGENDA3);
202 assertAgendaDidNotExecute(AGENDA4);
203 }
204
205 @Transactional
206 @Test
207 public void testSelectAgendaByName() {
208 Map<String,String> contextQualifiers = new HashMap<String,String>();
209 contextQualifiers.put(NAMESPACE_CODE, NAMESPACE2);
210 contextQualifiers.put(NAME, CONTEXT2);
211 contextQualifiers.put(CONTEXT2_QUALIFIER, CONTEXT2_QUALIFIER_VALUE);
212 Map<String,String> agendaQualifiers = new HashMap<String,String>();
213
214
215
216
217
218
219
220 agendaQualifiers.put(NAME, AGENDA3);
221 DateTime now = new DateTime();
222
223 SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(now, contextQualifiers, agendaQualifiers);
224
225 Facts.Builder factsBuilder2 = Facts.Builder.create();
226 factsBuilder2.addFact(BOOL1, "true");
227 factsBuilder2.addFact(BOOL2, Boolean.TRUE);
228 factsBuilder2.addFact(CAMPUS_CODE_TERM_NAME, "BL");
229 factsBuilder2.addFact(PREREQ_TERM_NAME, PREREQ_TERM_VALUE);
230
231 ExecutionOptions xOptions2 = new ExecutionOptions();
232 xOptions2.setFlag(ExecutionFlag.LOG_EXECUTION, true);
233
234
235 PerformanceLogger perfLog = new PerformanceLogger();
236 perfLog.log("starting rule execution 1");
237 EngineResults eResults1 = KrmsApiServiceLocator.getEngine().execute(selectionCriteria, factsBuilder2.build(), xOptions2);
238 perfLog.log("finished rule execution 1");
239 List<ResultEvent> rEvents1 = eResults1.getAllResults();
240
241 List<ResultEvent> ruleEvaluationResults1 = eResults1.getResultsOfType(ResultEvent.RULE_EVALUATED.toString());
242
243 selectionCriteria = SelectionCriteria.createCriteria(now, contextQualifiers, agendaQualifiers);
244
245 assertEquals("4 rules should have been evaluated", 4, ruleEvaluationResults1.size());
246
247 assertAgendaDidNotExecute(AGENDA1);
248 assertAgendaDidNotExecute(AGENDA2);
249
250
251 assertTrue(TestActionTypeService.actionFired("Agenda3::Rule1::TestAction"));
252 assertFalse(TestActionTypeService.actionFired("Agenda3::Rule2::TestAction"));
253 assertTrue(TestActionTypeService.actionFired("Agenda3::Rule3::TestAction"));
254
255 assertAgendaDidNotExecute(AGENDA4);
256 }
257
258
259 @Transactional
260 @Test
261 public void testSelectMultipleAgendasByAttribute() {
262 Map<String,String> contextQualifiers = new HashMap<String,String>();
263 contextQualifiers.put(NAMESPACE_CODE, NAMESPACE2);
264 contextQualifiers.put(NAME, CONTEXT2);
265 contextQualifiers.put(CONTEXT2_QUALIFIER, CONTEXT2_QUALIFIER_VALUE);
266
267 Map<String,String> agendaQualifiers = new HashMap<String,String>();
268 agendaQualifiers.put(AgendaDefinition.Constants.EVENT, EARTHQUAKE_EVENT);
269
270 DateTime now = new DateTime();
271
272 SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(now, contextQualifiers, agendaQualifiers);
273
274 Facts.Builder factsBuilder2 = Facts.Builder.create();
275 factsBuilder2.addFact(BOOL1, "true");
276 factsBuilder2.addFact(BOOL2, Boolean.TRUE);
277 factsBuilder2.addFact(CAMPUS_CODE_TERM_NAME, "BL");
278 factsBuilder2.addFact(PREREQ_TERM_NAME, PREREQ_TERM_VALUE);
279
280 ExecutionOptions xOptions2 = new ExecutionOptions();
281 xOptions2.setFlag(ExecutionFlag.LOG_EXECUTION, true);
282
283
284 PerformanceLogger perfLog = new PerformanceLogger();
285 perfLog.log("starting rule execution 1");
286 EngineResults eResults1 = KrmsApiServiceLocator.getEngine().execute(selectionCriteria, factsBuilder2.build(), xOptions2);
287 perfLog.log("finished rule execution 1");
288 List<ResultEvent> rEvents1 = eResults1.getAllResults();
289
290 List<ResultEvent> ruleEvaluationResults1 = eResults1.getResultsOfType(ResultEvent.RULE_EVALUATED.toString());
291
292 selectionCriteria = SelectionCriteria.createCriteria(now, contextQualifiers, agendaQualifiers);
293
294 assertEquals("8 rules should have been evaluated", 8, ruleEvaluationResults1.size());
295
296 assertAgendaDidNotExecute(AGENDA1);
297
298
299
300 assertTrue(TestActionTypeService.actionFired("Agenda2::Rule1::TestAction"));
301 assertFalse(TestActionTypeService.actionFired("Agenda2::Rule2::TestAction"));
302 assertTrue(TestActionTypeService.actionFired("Agenda2::Rule3::TestAction"));
303
304 assertTrue(TestActionTypeService.actionFired("Agenda3::Rule1::TestAction"));
305 assertFalse(TestActionTypeService.actionFired("Agenda3::Rule2::TestAction"));
306 assertTrue(TestActionTypeService.actionFired("Agenda3::Rule3::TestAction"));
307
308 assertAgendaDidNotExecute(AGENDA4);
309 }
310
311 private void assertAgendaDidNotExecute(String agendaName) {
312 assertFalse(TestActionTypeService.actionFired(agendaName+"::Rule1::TestAction"));
313 assertFalse(TestActionTypeService.actionFired(agendaName+"::Rule2::TestAction"));
314 assertFalse(TestActionTypeService.actionFired(agendaName+"::Rule3::TestAction"));
315 }
316
317 private void createAgendaDefinition(String agendaName, ContextDefinition contextDefinition, String eventName, String nameSpace ) {
318 KrmsTypeDefinition krmsGenericTypeDefinition = createKrmsGenericTypeDefinition(nameSpace, "testAgendaTypeService", "event name", "Event");
319
320 AgendaDefinition agendaDef =
321 AgendaDefinition.Builder.create(null, agendaName, krmsGenericTypeDefinition.getId(), contextDefinition.getId()).build();
322 agendaDef = agendaBoService.createAgenda(agendaDef);
323
324 AgendaItemDefinition.Builder agendaItemBuilder1 = AgendaItemDefinition.Builder.create(null, agendaDef.getId());
325 agendaItemBuilder1.setRuleId(createRuleDefinition1(contextDefinition, agendaName, nameSpace).getId());
326
327 AgendaItemDefinition.Builder agendaItemBuilder2 = AgendaItemDefinition.Builder.create(null, agendaDef.getId());
328 agendaItemBuilder1.setAlways(agendaItemBuilder2);
329 agendaItemBuilder2.setRuleId(createRuleDefinition2(contextDefinition, agendaName, nameSpace).getId());
330
331 AgendaItemDefinition.Builder agendaItemBuilder3 = AgendaItemDefinition.Builder.create(null, agendaDef.getId());
332 agendaItemBuilder2.setAlways(agendaItemBuilder3);
333 agendaItemBuilder3.setRuleId(createRuleDefinition3(contextDefinition, agendaName, nameSpace).getId());
334
335 AgendaItemDefinition.Builder agendaItemBuilder4 = AgendaItemDefinition.Builder.create(null, agendaDef.getId());
336 agendaItemBuilder3.setAlways(agendaItemBuilder4);
337 agendaItemBuilder4.setRuleId(createRuleDefinition4(contextDefinition, agendaName, nameSpace).getId());
338
339
340 AgendaItemDefinition agendaItem4 = agendaBoService.createAgendaItem(agendaItemBuilder4.build());
341 agendaItemBuilder3.setAlwaysId(agendaItem4.getId());
342 AgendaItemDefinition agendaItem3 = agendaBoService.createAgendaItem(agendaItemBuilder3.build());
343 agendaItemBuilder2.setAlwaysId(agendaItem3.getId());
344 AgendaItemDefinition agendaItem2 = agendaBoService.createAgendaItem(agendaItemBuilder2.build());
345 agendaItemBuilder1.setAlwaysId(agendaItem2.getId());
346 AgendaItemDefinition agendaItem1 = agendaBoService.createAgendaItem(agendaItemBuilder1.build());
347
348 AgendaDefinition.Builder agendaDefBuilder1 = AgendaDefinition.Builder.create(agendaDef);
349 agendaDefBuilder1.setAttributes(Collections.singletonMap("Event", eventName));
350 agendaDefBuilder1.setFirstItemId(agendaItem1.getId());
351 agendaDef = agendaDefBuilder1.build();
352
353 agendaBoService.updateAgenda(agendaDef);
354 }
355
356 private KrmsTypeDefinition createKrmsActionTypeDefinition(String nameSpace) {
357 String ACTION_TYPE_NAME = "KrmsActionResolverType";
358 KrmsTypeDefinition krmsActionTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, ACTION_TYPE_NAME);
359
360 if (krmsActionTypeDefinition == null) {
361 KrmsTypeDefinition.Builder krmsActionTypeDefnBuilder = KrmsTypeDefinition.Builder.create(ACTION_TYPE_NAME, nameSpace);
362 krmsActionTypeDefnBuilder.setServiceName("testActionTypeService");
363 krmsActionTypeDefinition = krmsTypeRepository.createKrmsType(krmsActionTypeDefnBuilder.build());
364 }
365
366 return krmsActionTypeDefinition;
367 }
368
369
370 private RuleDefinition createRuleDefinition(String nameSpace, String ruleName, ContextDefinition contextDefinition,
371 LogicalOperator operator, PropositionParametersBuilder ... pbs) {
372 RuleDefinition.Builder ruleDefBuilder =
373 RuleDefinition.Builder.create(null, ruleName, nameSpace, null, null);
374 RuleDefinition ruleDef1 = ruleBoService.createRule(ruleDefBuilder.build());
375
376 PropositionDefinition.Builder parentProposition =
377 PropositionDefinition.Builder.create(null, PropositionType.COMPOUND.getCode(), ruleDef1.getId(), null, null);
378 parentProposition.setCompoundComponents(new ArrayList<PropositionDefinition.Builder>());
379
380 if (operator != null ) { parentProposition.setCompoundOpCode(operator.getCode()); }
381
382 ruleDefBuilder = RuleDefinition.Builder.create(ruleDef1);
383
384 for (PropositionParametersBuilder params : pbs) {
385
386 StringBuilder propositionNameBuilder = new StringBuilder(ruleName);
387
388 propositionNameBuilder.append("::");
389 for (Object[] param : params.params) {
390 propositionNameBuilder.append(param[0].toString());
391 propositionNameBuilder.append("--");
392 }
393
394 PropositionDefinition.Builder propositionBuilder =
395 createPropositionDefinition(propositionNameBuilder.toString(), params, ruleDef1);
396
397 if (pbs.length > 1) {
398
399 parentProposition.getCompoundComponents().add(propositionBuilder);
400 } else {
401
402 parentProposition = propositionBuilder;
403 }
404 }
405
406 ruleDefBuilder.setProposition(parentProposition);
407 ruleDef1 = ruleDefBuilder.build();
408 ruleBoService.updateRule(ruleDef1);
409
410
411 ActionDefinition.Builder actionDefBuilder1 = ActionDefinition.Builder.create(null, ruleName + "::TestAction", nameSpace, createKrmsActionTypeDefinition(nameSpace).getId(), ruleDef1.getId(), 1);
412 ActionDefinition actionDef1 = actionBoService.createAction(actionDefBuilder1.build());
413
414 return ruleDef1;
415 }
416
417
418
419 private RuleDefinition createRuleDefinition1(ContextDefinition contextDefinition, String agendaName, String nameSpace) {
420
421 PropositionParametersBuilder params1 = new PropositionParametersBuilder();
422 params1.add(createTermDefinition(CAMPUS_CODE_TERM_NAME, String.class, contextDefinition).getId(), PropositionParameterType.TERM);
423 params1.add("QC", PropositionParameterType.CONSTANT);
424 params1.add("=", PropositionParameterType.OPERATOR);
425
426 PropositionParametersBuilder params2 = new PropositionParametersBuilder();
427 params2.add(createTermDefinition(CAMPUS_CODE_TERM_NAME, String.class, contextDefinition).getId(), PropositionParameterType.TERM);
428 params2.add("BL", PropositionParameterType.CONSTANT);
429 params2.add("=", PropositionParameterType.OPERATOR);
430
431 return createRuleDefinition(nameSpace, agendaName+"::Rule1", contextDefinition, LogicalOperator.OR, params1, params2);
432 }
433
434
435 private PropositionDefinition.Builder createPropositionDefinition(String propDescription, PropositionParametersBuilder params, RuleDefinition parentRule) {
436
437 PropositionDefinition.Builder propositionDefBuilder1 =
438 PropositionDefinition.Builder.create(null, PropositionType.SIMPLE.getCode(), parentRule.getId(),
439 null
440 propositionDefBuilder1.setDescription(propDescription);
441
442
443 List<PropositionParameter.Builder> propositionParams1 = params.build();
444
445
446
447 for (PropositionParameter.Builder propositionParamBuilder : propositionParams1) {
448 propositionParamBuilder.setProposition(propositionDefBuilder1);
449 }
450
451 propositionDefBuilder1.setParameters(propositionParams1);
452
453 return propositionDefBuilder1;
454 }
455
456
457
458 private TermDefinition createTermDefinition(String termName, Class termValueType, ContextDefinition contextDefinition) {
459
460
461 Map<String, String> queryArgs = new HashMap<String, String>();
462 queryArgs.put("specification.namespace", contextDefinition.getNamespace());
463 queryArgs.put("specification.name", termName);
464 TermBo termBo = getBoService().findByPrimaryKey(TermBo.class, queryArgs);
465 if (termBo != null) {
466 return TermBo.to(termBo);
467 }
468
469
470 TermSpecificationDefinition termSpec =
471 TermSpecificationDefinition.Builder.create(null, termName, contextDefinition.getNamespace(),
472 termValueType.getCanonicalName()).build();
473
474 termSpec = termBoService.createTermSpecification(termSpec);
475
476
477 TermDefinition termDefinition =
478 TermDefinition.Builder.create(null, TermSpecificationDefinition.Builder.create(termSpec), null).build();
479 termDefinition = termBoService.createTermDefinition(termDefinition);
480
481 return termDefinition;
482 }
483
484
485 private RuleDefinition createRuleDefinition2(ContextDefinition contextDefinition, String agendaName, String nameSpace) {
486
487 PropositionParametersBuilder params1 = new PropositionParametersBuilder();
488 params1.add(createTermDefinition2(contextDefinition, nameSpace).getId(), PropositionParameterType.TERM);
489 params1.add("RESULT1", PropositionParameterType.CONSTANT);
490 params1.add("=", PropositionParameterType.OPERATOR);
491
492 PropositionParametersBuilder params2 = new PropositionParametersBuilder();
493 params2.add(createTermDefinition2(contextDefinition, nameSpace).getId(), PropositionParameterType.TERM);
494 params2.add("NotGonnaBeEqual", PropositionParameterType.CONSTANT);
495 params2.add("=", PropositionParameterType.OPERATOR);
496
497 return createRuleDefinition(nameSpace, agendaName+"::Rule2", contextDefinition, LogicalOperator.AND, params1, params2);
498 }
499
500 private RuleDefinition createRuleDefinition3(ContextDefinition contextDefinition, String agendaName, String nameSpace) {
501
502 FunctionDefinition gcdFunction = functionBoService.getFunctionByNameAndNamespace("gcd", contextDefinition.getNamespace());
503
504 if (null == gcdFunction) {
505
506
507 KrmsTypeDefinition.Builder krmsFunctionTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestFunctionType", nameSpace);
508 krmsFunctionTypeDefnBuilder.setServiceName("testFunctionTypeService");
509 KrmsTypeDefinition krmsFunctionTypeDefinition = krmsTypeRepository.createKrmsType(krmsFunctionTypeDefnBuilder.build());
510
511 FunctionDefinition.Builder functionBuilder =
512 FunctionDefinition.Builder.create(contextDefinition.getNamespace(), "gcd", Integer.class.getName(), krmsFunctionTypeDefinition.getId());
513
514 functionBuilder.getParameters().add(FunctionParameterDefinition.Builder.create("arg0", Integer.class.getName(), 0));
515 functionBuilder.getParameters().add(FunctionParameterDefinition.Builder.create("arg1", Integer.class.getName(), 1));
516 functionBuilder.setReturnType(Integer.class.getName());
517
518 gcdFunction = functionBoService.createFunction(functionBuilder.build());
519 }
520
521 PropositionParametersBuilder params = new PropositionParametersBuilder();
522
523
524 params.add("1024", PropositionParameterType.CONSTANT);
525 params.add("768", PropositionParameterType.CONSTANT);
526 params.add(gcdFunction.getId(), PropositionParameterType.FUNCTION);
527 params.add("256", PropositionParameterType.CONSTANT);
528 params.add("=", PropositionParameterType.OPERATOR);
529
530 return createRuleDefinition(nameSpace, agendaName+"::Rule3", contextDefinition, null, params);
531 }
532
533
534 private RuleDefinition createRuleDefinition4(ContextDefinition contextDefinition, String agendaName, String nameSpace) {
535
536 PropositionParametersBuilder params1 = new PropositionParametersBuilder();
537 params1.add(createTermDefinition(BOOL1, Boolean.class, contextDefinition).getId(), PropositionParameterType.TERM);
538 params1.add(createTermDefinition(BOOL2, Boolean.class, contextDefinition).getId(), PropositionParameterType.TERM);
539 params1.add("=", PropositionParameterType.OPERATOR);
540
541 PropositionParametersBuilder params2 = new PropositionParametersBuilder();
542 params2.add(createTermDefinition(BOOL2, Boolean.class, contextDefinition).getId(), PropositionParameterType.TERM);
543 params2.add(createTermDefinition(BOOL1, Boolean.class, contextDefinition).getId(), PropositionParameterType.TERM);
544 params2.add("=", PropositionParameterType.OPERATOR);
545
546 return createRuleDefinition(nameSpace, agendaName+"::Rule4", contextDefinition, LogicalOperator.AND, params1, params2);
547 }
548
549
550 private TermDefinition createTermDefinition2(ContextDefinition contextDefinition, String nameSpace) {
551
552 Map<String, String> queryArgs = new HashMap<String, String>();
553 queryArgs.put("specification.namespace", contextDefinition.getNamespace());
554 queryArgs.put("specification.name", "outputTermSpec");
555 TermBo result = getBoService().findByPrimaryKey(TermBo.class, queryArgs);
556 if (result != null) return TermBo.to(result);
557
558
559 TermSpecificationDefinition outputTermSpec =
560 TermSpecificationDefinition.Builder.create(null, "outputTermSpec", contextDefinition.getNamespace(),
561 "java.lang.String").build();
562 outputTermSpec = termBoService.createTermSpecification(outputTermSpec);
563
564
565 TermSpecificationDefinition prereqTermSpec =
566 TermSpecificationDefinition.Builder.create(null, PREREQ_TERM_NAME, contextDefinition.getNamespace(),
567 "java.lang.String").build();
568 prereqTermSpec = termBoService.createTermSpecification(prereqTermSpec);
569
570
571 TermParameterDefinition.Builder termParamBuilder2 =
572 TermParameterDefinition.Builder.create(null, null, "testParamName", "testParamValue");
573
574
575 TermDefinition termDefinition2 =
576 TermDefinition.Builder.create(null, TermSpecificationDefinition.Builder.create(outputTermSpec), Collections.singletonList(termParamBuilder2)).build();
577 termDefinition2 = termBoService.createTermDefinition(termDefinition2);
578
579
580 KrmsTypeDefinition.Builder krmsTermResolverTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestResolverType", nameSpace);
581 krmsTermResolverTypeDefnBuilder.setServiceName("testTermResolverTypeService");
582
583 KrmsTypeDefinition krmsTermResolverTypeDefinition = krmsTypeRepository.createKrmsType(krmsTermResolverTypeDefnBuilder.build());
584
585
586 TermResolverDefinition termResolverDef =
587 TermResolverDefinition.Builder.create(null, contextDefinition.getNamespace(), "testResolver1", krmsTermResolverTypeDefinition.getId(),
588 TermSpecificationDefinition.Builder.create(outputTermSpec),
589 Collections.singleton(TermSpecificationDefinition.Builder.create(prereqTermSpec)),
590 null,
591 Collections.singleton("testParamName")).build();
592 termResolverDef = termBoService.createTermResolver(termResolverDef);
593
594 return termDefinition2;
595 }
596
597 private static class PropositionParametersBuilder {
598
599
600 private List<Object[]> params = new ArrayList<Object[]>();
601
602 public PropositionParametersBuilder add(String value, PropositionParameterType type) {
603 if (type == null) throw new IllegalArgumentException("type must not be null");
604 params.add(new Object[]{value, type});
605 return this;
606 }
607
608 public List<PropositionParameter.Builder> build() {
609 int seqCounter = 0;
610
611 List<PropositionParameter.Builder> results = new ArrayList<PropositionParameter.Builder>();
612
613 for (Object[] param : params) {
614 results.add(PropositionParameter.Builder.create(null, null, (String)param[0], ((PropositionParameterType)param[1]).getCode(), seqCounter++));
615 }
616
617 return results;
618 }
619 }
620
621 }