View Javadoc

1   /**
2    * Copyright 2005-2014 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  import java.util.Set;
27  
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.Action;
39  import org.kuali.rice.krms.framework.engine.Agenda;
40  import org.kuali.rice.krms.framework.engine.BasicAgenda;
41  import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
42  import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
43  import org.kuali.rice.krms.framework.engine.BasicContext;
44  import org.kuali.rice.krms.framework.engine.BasicRule;
45  import org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition;
46  import org.kuali.rice.krms.framework.engine.CompoundProposition;
47  import org.kuali.rice.krms.framework.engine.Context;
48  import org.kuali.rice.krms.framework.engine.ContextProvider;
49  import org.kuali.rice.krms.framework.engine.Proposition;
50  import org.kuali.rice.krms.framework.engine.ProviderBasedEngine;
51  import org.kuali.rice.krms.framework.engine.ResultLogger;
52  import org.kuali.rice.krms.framework.engine.Rule;
53  import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
54  import org.kuali.rice.krms.framework.engine.expression.ComparisonOperatorServiceImpl;
55  
56  public class ResultLoggingTest {
57  	private static final ResultLogger LOG = ResultLogger.getInstance();
58  
59  	@Test
60  	public void integrationTest() {
61  
62  		// build a simple rule
63          ComparisonOperator greaterThan = ComparisonOperator.GREATER_THAN;
64          greaterThan.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
65          ComparisonOperator lessThan = ComparisonOperator.LESS_THAN;
66          lessThan.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance());
67  
68  		Proposition prop1 = new ComparableTermBasedProposition(greaterThan, totalCostTerm, Integer.valueOf(1));
69  		Proposition prop2 = new ComparableTermBasedProposition(lessThan, totalCostTerm, Integer.valueOf(1000));
70  		CompoundProposition compoundProp1 = new CompoundProposition(LogicalOperator.AND, Arrays.asList(prop1, prop2));
71  		
72  		Action action1 = new SayHelloAction();
73  		Rule rule = new BasicRule("InBetween",compoundProp1, Arrays.asList(action1));
74  		
75  		BasicAgendaTree agendaTree = new BasicAgendaTree(new BasicAgendaTreeEntry(rule)); 
76  		Agenda agenda = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree);
77  		
78  		Map<String, String> contextQualifiers = new HashMap<String, String>();
79  		contextQualifiers.put("docTypeName", "Proposal");
80  		
81  		List<TermResolver<?>> testResolvers = new ArrayList<TermResolver<?>>();
82  		testResolvers.add(testResolver);
83  		
84  		Context context = new BasicContext(Arrays.asList(agenda), testResolvers);
85  		ContextProvider contextProvider = new ManualContextProvider(context);
86  		
87  		SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers,
88                  Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"));
89  		
90  		ProviderBasedEngine engine = new ProviderBasedEngine();
91  		engine.setContextProvider(contextProvider);
92  		
93  		// Set execution options to log execution
94  		ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true);
95  		
96  		EngineResults results = engine.execute(selectionCriteria, Facts.EMPTY_FACTS, executionOptions);
97  		assertNotNull(results);
98  		
99  	}
100 	
101 	private static final Term totalCostTerm = new Term("totalCost");
102 	
103 	private static final TermResolver<Integer> testResolver = new TermResolver<Integer>(){
104 		
105 		@Override
106 		public int getCost() { return 1; }
107 		
108 		@Override
109 		public String getOutput() { return totalCostTerm.getName(); }
110 		
111 		@Override
112 		public Set<String> getPrerequisites() { return Collections.emptySet(); }
113 		
114 		@Override
115 		public Set<String> getParameterNames() {
116 			return Collections.emptySet();
117 		}
118 		
119 		@Override
120 		public Integer resolve(Map<String, Object> resolvedPrereqs, Map<String, String> parameters) {
121 			return 5;
122 		}
123 	};
124 
125 }