1 package org.kuali.rice.krms.framework;
2
3 import static junit.framework.Assert.assertNotNull;
4
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import org.junit.Test;
13 import org.kuali.rice.krms.api.engine.EngineResults;
14 import org.kuali.rice.krms.api.engine.ExecutionOptions;
15 import org.kuali.rice.krms.api.engine.ExecutionFlag;
16 import org.kuali.rice.krms.api.engine.SelectionCriteria;
17 import org.kuali.rice.krms.api.engine.Term;
18 import org.kuali.rice.krms.api.engine.TermResolver;
19 import org.kuali.rice.krms.api.engine.TermSpecification;
20 import org.kuali.rice.krms.api.repository.LogicalOperator;
21 import org.kuali.rice.krms.framework.engine.Agenda;
22 import org.kuali.rice.krms.framework.engine.BasicAgenda;
23 import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
24 import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
25 import org.kuali.rice.krms.framework.engine.BasicContext;
26 import org.kuali.rice.krms.framework.engine.BasicRule;
27 import org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition;
28 import org.kuali.rice.krms.framework.engine.ComparisonOperator;
29 import org.kuali.rice.krms.framework.engine.CompoundProposition;
30 import org.kuali.rice.krms.framework.engine.Context;
31 import org.kuali.rice.krms.framework.engine.ContextProvider;
32 import org.kuali.rice.krms.framework.engine.Proposition;
33 import org.kuali.rice.krms.framework.engine.ProviderBasedEngine;
34 import org.kuali.rice.krms.framework.engine.ResultLogger;
35 import org.kuali.rice.krms.framework.engine.Rule;
36
37 public class KRMSTest {
38 private static final ResultLogger LOG = ResultLogger.getInstance();
39
40 @Test
41 public void compoundPropositionTest() {
42
43 Proposition prop1 = new ComparableTermBasedProposition(ComparisonOperator.GREATER_THAN, totalCostTerm, Integer.valueOf(1));
44 Proposition prop2 = new ComparableTermBasedProposition(ComparisonOperator.LESS_THAN, totalCostTerm, Integer.valueOf(1000));
45 Proposition prop3 = new ComparableTermBasedProposition(ComparisonOperator.GREATER_THAN, totalCostTerm, Integer.valueOf(1000));
46 CompoundProposition compoundProp1 = new CompoundProposition(LogicalOperator.AND, Arrays.asList(prop1, prop2, prop3));
47
48 Rule rule = new BasicRule("InBetween",compoundProp1, null);
49
50 BasicAgendaTree agendaTree = new BasicAgendaTree(new BasicAgendaTreeEntry(rule));
51 Agenda agenda = new BasicAgenda("test", new HashMap<String, String>(), agendaTree);
52
53 Map<String, String> contextQualifiers = new HashMap<String, String>();
54 contextQualifiers.put("docTypeName", "Proposal");
55
56 List<TermResolver<?>> testResolvers = new ArrayList<TermResolver<?>>();
57 testResolvers.add(testResolver);
58
59 Context context = new BasicContext(Arrays.asList(agenda), testResolvers);
60 ContextProvider contextProvider = new ManualContextProvider(context);
61
62 SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria("test", null, contextQualifiers, Collections.EMPTY_MAP);
63
64 ProviderBasedEngine engine = new ProviderBasedEngine();
65 engine.setContextProvider(contextProvider);
66
67
68 ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true);
69
70 LOG.init();
71 EngineResults results = engine.execute(selectionCriteria, new HashMap<Term, Object>(), executionOptions);
72 assertNotNull(results);
73 }
74
75
76 private static final Term totalCostTerm = new Term(new TermSpecification("totalCost","Integer"));
77
78 private static final TermResolver<Integer> testResolver = new TermResolverMock<Integer>(totalCostTerm.getSpecification(), 1);
79 }