View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.framework;
17  
18  import static junit.framework.Assert.assertNotNull;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.kuali.rice.krms.api.engine.EngineResults;
30  import org.kuali.rice.krms.api.engine.ExecutionOptions;
31  import org.kuali.rice.krms.api.engine.ExecutionFlag;
32  import org.kuali.rice.krms.api.engine.Facts;
33  import org.kuali.rice.krms.api.engine.SelectionCriteria;
34  import org.kuali.rice.krms.api.engine.Term;
35  import org.kuali.rice.krms.api.engine.TermResolver;
36  import org.kuali.rice.krms.api.repository.LogicalOperator;
37  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
38  import org.kuali.rice.krms.framework.engine.Agenda;
39  import org.kuali.rice.krms.framework.engine.BasicAgenda;
40  import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
41  import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
42  import org.kuali.rice.krms.framework.engine.BasicContext;
43  import org.kuali.rice.krms.framework.engine.BasicRule;
44  import org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition;
45  import org.kuali.rice.krms.framework.engine.CompoundProposition;
46  import org.kuali.rice.krms.framework.engine.Context;
47  import org.kuali.rice.krms.framework.engine.ContextProvider;
48  import org.kuali.rice.krms.framework.engine.Proposition;
49  import org.kuali.rice.krms.framework.engine.ProviderBasedEngine;
50  import org.kuali.rice.krms.framework.engine.ResultLogger;
51  import org.kuali.rice.krms.framework.engine.Rule;
52  import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
53  import org.kuali.rice.krms.framework.engine.expression.ComparisonOperatorServiceImpl;
54  
55  public class KRMSTest {
56  	private static final ResultLogger LOG = ResultLogger.getInstance();
57  
58      private ComparisonOperator operatorGreaterThan;
59      private ComparisonOperator operatorLessThan;
60  
61      @Before
62      public void setUp() {
63          operatorGreaterThan = ComparisonOperator.GREATER_THAN;
64          operatorGreaterThan.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
65          operatorLessThan = ComparisonOperator.LESS_THAN;
66          operatorLessThan.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
67      }
68  
69  	@Test
70  	public void compoundPropositionTest() {
71  
72  		Proposition prop1 = new ComparableTermBasedProposition(operatorGreaterThan, totalCostTerm, Integer.valueOf(1));
73  		Proposition prop2 = new ComparableTermBasedProposition(operatorLessThan, totalCostTerm, Integer.valueOf(1000));
74  		Proposition prop3 = new ComparableTermBasedProposition(operatorGreaterThan, totalCostTerm, Integer.valueOf(1000));
75  		CompoundProposition compoundProp1 = new CompoundProposition(LogicalOperator.AND, Arrays.asList(prop1, prop2, prop3));
76  		
77  		Rule rule = new BasicRule("InBetween",compoundProp1, null);
78  		
79  		BasicAgendaTree agendaTree = new BasicAgendaTree(new BasicAgendaTreeEntry(rule)); 
80  		Agenda agenda = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree);
81  		
82  		Map<String, String> contextQualifiers = new HashMap<String, String>();
83  		contextQualifiers.put("docTypeName", "Proposal");
84  		
85  		List<TermResolver<?>> testResolvers = new ArrayList<TermResolver<?>>();
86  		testResolvers.add(testResolver);
87  		
88  		Context context = new BasicContext(Arrays.asList(agenda), testResolvers);
89  		ContextProvider contextProvider = new ManualContextProvider(context);
90  		
91  		SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers,
92                  Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"));
93  		
94  		ProviderBasedEngine engine = new ProviderBasedEngine();
95  		engine.setContextProvider(contextProvider);
96  		
97  		// Set execution options to log execution
98  		ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true);
99  		
100 		EngineResults results = engine.execute(selectionCriteria, Facts.EMPTY_FACTS, executionOptions);
101 		assertNotNull(results);
102 	}
103 	
104 	
105 	private static final Term totalCostTerm = new Term("totalCost");
106 
107 	private static final TermResolver<Integer> testResolver = new TermResolverMock<Integer>(totalCostTerm.getName(), 1);
108 }