View Javadoc

1   package org.kuali.rice.krms.framework;
2   
3   import static junit.framework.Assert.assertFalse;
4   import static junit.framework.Assert.assertNotNull;
5   import static junit.framework.Assert.assertTrue;
6   
7   import java.util.Arrays;
8   import java.util.Collection;
9   import java.util.Collections;
10  import java.util.HashMap;
11  import java.util.List;
12  import java.util.Map;
13  
14  import org.junit.Before;
15  import org.junit.Test;
16  import org.kuali.rice.krms.api.engine.EngineResults;
17  import org.kuali.rice.krms.api.engine.ExecutionOptions;
18  import org.kuali.rice.krms.api.engine.ExecutionFlag;
19  import org.kuali.rice.krms.api.engine.SelectionCriteria;
20  import org.kuali.rice.krms.api.engine.Term;
21  import org.kuali.rice.krms.api.engine.TermResolver;
22  import org.kuali.rice.krms.api.engine.TermSpecification;
23  import org.kuali.rice.krms.framework.engine.Action;
24  import org.kuali.rice.krms.framework.engine.Agenda;
25  import org.kuali.rice.krms.framework.engine.BasicAgenda;
26  import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
27  import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
28  import org.kuali.rice.krms.framework.engine.BasicContext;
29  import org.kuali.rice.krms.framework.engine.BasicRule;
30  import org.kuali.rice.krms.framework.engine.CollectionOfComparablesTermBasedProposition;
31  import org.kuali.rice.krms.framework.engine.CollectionOperator;
32  import org.kuali.rice.krms.framework.engine.ComparisonOperator;
33  import org.kuali.rice.krms.framework.engine.Context;
34  import org.kuali.rice.krms.framework.engine.ContextProvider;
35  import org.kuali.rice.krms.framework.engine.Proposition;
36  import org.kuali.rice.krms.framework.engine.ProviderBasedEngine;
37  import org.kuali.rice.krms.framework.engine.ResultLogger;
38  import org.kuali.rice.krms.framework.engine.Rule;
39  import org.kuali.rice.krms.framework.engine.TermResolutionEngineImpl;
40  
41  public class CollectionOfComparablesTermBasedPropositionTest {
42  	private static final ResultLogger LOG = ResultLogger.getInstance();
43  	
44  	
45  	@Before
46  	public void setUp() {
47  		ActionMock.resetActionsFired();
48  	}
49  
50  	@Test
51  	public void allPropositionTest() {
52  
53  		// True cases:
54  		
55  		// this can be read as "for the collection (100, 1000), all elements are greater than 1"
56  		assertRuleTrue(Arrays.asList(100f, 1000f), CollectionOperator.ALL, ComparisonOperator.GREATER_THAN, 1f);
57  		
58  		assertRuleTrue(Arrays.asList(100f), CollectionOperator.ALL, ComparisonOperator.GREATER_THAN, 1f);
59  		// This can bend the intuition a bit, but this behavior is correct -- see Wikipedia's entry on the empty set
60  		assertRuleTrue(Arrays.<Float>asList(), CollectionOperator.ALL, ComparisonOperator.GREATER_THAN, 1f);
61  		
62  		// False cases:
63  		assertRuleFalse(Arrays.asList(100f, 1000f), CollectionOperator.ALL, ComparisonOperator.GREATER_THAN, 10000f);
64  		assertRuleFalse(Arrays.asList(100f, 1000f), CollectionOperator.ALL, ComparisonOperator.GREATER_THAN, 500f);
65  		assertRuleFalse(Arrays.asList(1000f, 100f), CollectionOperator.ALL, ComparisonOperator.GREATER_THAN, 500f);
66  		assertRuleFalse(Arrays.asList(100f), CollectionOperator.ALL, ComparisonOperator.GREATER_THAN, 10000f);
67  
68  	}
69  	
70  	@Test
71  	public void oneOrMorePropositionTest() {
72  
73  		// True cases:
74  		
75  		// this can be read as "for the collection (100, 1000), one or more elements are greater than 500"
76  		assertRuleTrue(Arrays.asList(100f, 1000f), CollectionOperator.ONE_OR_MORE, ComparisonOperator.GREATER_THAN, 500f);
77  		assertRuleTrue(Arrays.asList(1000f, 100f), CollectionOperator.ONE_OR_MORE, ComparisonOperator.GREATER_THAN, 500f);
78  		assertRuleTrue(Arrays.asList(1000f), CollectionOperator.ONE_OR_MORE, ComparisonOperator.GREATER_THAN, 500f);
79  
80  		// False cases:
81  		assertRuleFalse(Arrays.asList(1000f, 2000f), CollectionOperator.ONE_OR_MORE, ComparisonOperator.GREATER_THAN, 5000f);
82  		assertRuleFalse(Arrays.asList(2000f, 1000f), CollectionOperator.ONE_OR_MORE, ComparisonOperator.GREATER_THAN, 5000f);
83  		assertRuleFalse(Arrays.asList(1000f), CollectionOperator.ONE_OR_MORE, ComparisonOperator.GREATER_THAN, 5000f);
84  		assertRuleFalse(Arrays.<Float>asList(), CollectionOperator.ONE_OR_MORE, ComparisonOperator.GREATER_THAN, 5000f);
85  
86  	}
87  	
88  	@Test
89  	public void nonePropositionTest() {
90  		
91  		// True cases:
92  		
93  		// this can be read as "for the collection (100, 1000), none of the elements are greater than 5000"
94  		assertRuleTrue(Arrays.asList(100f, 1000f), CollectionOperator.NONE, ComparisonOperator.GREATER_THAN, 5000f);
95  		assertRuleTrue(Arrays.asList(1000f), CollectionOperator.NONE, ComparisonOperator.GREATER_THAN, 5000f);
96  		assertRuleTrue(Arrays.<Float>asList(), CollectionOperator.NONE, ComparisonOperator.GREATER_THAN, 5000f);
97  		
98  		// False cases:
99  		assertRuleFalse(Arrays.asList(1000f, 7000f), CollectionOperator.NONE, ComparisonOperator.GREATER_THAN, 5000f);
100 		assertRuleFalse(Arrays.asList(7000f, 1000f), CollectionOperator.NONE, ComparisonOperator.GREATER_THAN, 5000f);
101 		assertRuleFalse(Arrays.asList(7000f), CollectionOperator.NONE, ComparisonOperator.GREATER_THAN, 5000f);
102 
103 	}
104 	
105 	/**
106 	 * @param agenda
107 	 */
108 	private void execute(Agenda agenda, TermResolver ... termResolvers) {
109 		Map<String, String> contextQualifiers = new HashMap<String, String>();
110 		contextQualifiers.put("docTypeName", "Proposal");
111 		
112 		List<TermResolver<?>> testResolvers = Arrays.<TermResolver<?>>asList(termResolvers);
113 		
114 		Context context = new BasicContext(Arrays.asList(agenda), testResolvers);
115 		ContextProvider contextProvider = new ManualContextProvider(context);
116 		
117 		SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria("test", null, contextQualifiers, Collections.EMPTY_MAP);
118 		
119 		ProviderBasedEngine engine = new ProviderBasedEngine();
120 		engine.setContextProvider(contextProvider);
121 		engine.setTermResolutionEngine(new TermResolutionEngineImpl());
122 		
123 		// Set execution options to log execution
124 		ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true);
125 		
126 		LOG.init();
127 		EngineResults results = engine.execute(selectionCriteria, new HashMap<Term, Object>(), executionOptions);
128 		assertNotNull(results);
129 	}
130 	
131 	private void assertRuleTrue(List<Float> termValue,
132 			CollectionOperator collectionOper,
133 			ComparisonOperator comparisonOper, Float compareValue) {
134 		assertRule(termValue, collectionOper, comparisonOper, compareValue, true);
135 	}
136 	
137 	private void assertRuleFalse(List<Float> termValue,
138 			CollectionOperator collectionOper,
139 			ComparisonOperator comparisonOper, Float compareValue) {
140 		assertRule(termValue, collectionOper, comparisonOper, compareValue, false);
141 	}
142 	
143 	private void assertRule(List<Float> termValue,
144 			CollectionOperator collectionOper,
145 			ComparisonOperator comparisonOper, Float compareValue, boolean assertTrue) {
146 		
147 		boolean actionFired = executeTestAgenda(termValue, collectionOper, comparisonOper, compareValue).actionFired();
148 		
149 		if (assertTrue) { 
150 			assertTrue(actionFired); 
151 		} else { 
152 			assertFalse(actionFired); 
153 		}
154 		
155 		ActionMock.resetActionsFired();
156 	}
157 
158 	/**
159 	 * This method ...
160 	 * 
161 	 * @param termValue
162 	 * @param collectionOper
163 	 * @param comparisonOper
164 	 * @param compareValue
165 	 * @return
166 	 */
167 	private ActionMock executeTestAgenda(List<Float> termValue,
168 			CollectionOperator collectionOper,
169 			ComparisonOperator comparisonOper, Float compareValue) {
170 		Term expensesTerm = new Term(new TermSpecification("expenses","Collection<Float>"));
171 		TermResolver<Collection<Float>> expensesResolver = new TermResolverMock<Collection<Float>>(expensesTerm.getSpecification(), termValue); 
172 		
173 		Proposition prop1 = new CollectionOfComparablesTermBasedProposition(collectionOper, comparisonOper, expensesTerm, compareValue);
174 		ActionMock prop1Action = new ActionMock("prop1Action");
175 		
176 		Rule rule = new BasicRule("ALL", prop1, Collections.<Action>singletonList(prop1Action));
177 		
178 		BasicAgendaTree agendaTree = new BasicAgendaTree(new BasicAgendaTreeEntry(rule)); 
179 		Agenda agenda = new BasicAgenda("test", new HashMap<String, String>(), agendaTree);
180 		
181 		execute(agenda, expensesResolver);
182 		return prop1Action;
183 	}
184 
185 }