001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krms.framework; 017 018 import static junit.framework.Assert.assertNotNull; 019 020 import java.util.ArrayList; 021 import java.util.Arrays; 022 import java.util.Collections; 023 import java.util.HashMap; 024 import java.util.List; 025 import java.util.Map; 026 import java.util.Set; 027 028 import org.junit.Test; 029 import org.kuali.rice.krms.api.engine.EngineResults; 030 import org.kuali.rice.krms.api.engine.ExecutionOptions; 031 import org.kuali.rice.krms.api.engine.ExecutionFlag; 032 import org.kuali.rice.krms.api.engine.Facts; 033 import org.kuali.rice.krms.api.engine.SelectionCriteria; 034 import org.kuali.rice.krms.api.engine.Term; 035 import org.kuali.rice.krms.api.engine.TermResolver; 036 import org.kuali.rice.krms.api.repository.LogicalOperator; 037 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition; 038 import org.kuali.rice.krms.framework.engine.Action; 039 import org.kuali.rice.krms.framework.engine.Agenda; 040 import org.kuali.rice.krms.framework.engine.BasicAgenda; 041 import org.kuali.rice.krms.framework.engine.BasicAgendaTree; 042 import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry; 043 import org.kuali.rice.krms.framework.engine.BasicContext; 044 import org.kuali.rice.krms.framework.engine.BasicRule; 045 import org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition; 046 import org.kuali.rice.krms.framework.engine.CompoundProposition; 047 import org.kuali.rice.krms.framework.engine.Context; 048 import org.kuali.rice.krms.framework.engine.ContextProvider; 049 import org.kuali.rice.krms.framework.engine.Proposition; 050 import org.kuali.rice.krms.framework.engine.ProviderBasedEngine; 051 import org.kuali.rice.krms.framework.engine.ResultLogger; 052 import org.kuali.rice.krms.framework.engine.Rule; 053 import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator; 054 import org.kuali.rice.krms.framework.engine.expression.ComparisonOperatorServiceImpl; 055 056 public class ResultLoggingTest { 057 private static final ResultLogger LOG = ResultLogger.getInstance(); 058 059 @Test 060 public void integrationTest() { 061 062 // build a simple rule 063 ComparisonOperator greaterThan = ComparisonOperator.GREATER_THAN; 064 greaterThan.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance()); 065 ComparisonOperator lessThan = ComparisonOperator.LESS_THAN; 066 lessThan.setComparisonOperatorService(ComparisonOperatorServiceImpl.getInstance()); 067 068 Proposition prop1 = new ComparableTermBasedProposition(greaterThan, totalCostTerm, Integer.valueOf(1)); 069 Proposition prop2 = new ComparableTermBasedProposition(lessThan, totalCostTerm, Integer.valueOf(1000)); 070 CompoundProposition compoundProp1 = new CompoundProposition(LogicalOperator.AND, Arrays.asList(prop1, prop2)); 071 072 Action action1 = new SayHelloAction(); 073 Rule rule = new BasicRule("InBetween",compoundProp1, Arrays.asList(action1)); 074 075 BasicAgendaTree agendaTree = new BasicAgendaTree(new BasicAgendaTreeEntry(rule)); 076 Agenda agenda = new BasicAgenda(Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test"), agendaTree); 077 078 Map<String, String> contextQualifiers = new HashMap<String, String>(); 079 contextQualifiers.put("docTypeName", "Proposal"); 080 081 List<TermResolver<?>> testResolvers = new ArrayList<TermResolver<?>>(); 082 testResolvers.add(testResolver); 083 084 Context context = new BasicContext(Arrays.asList(agenda), testResolvers); 085 ContextProvider contextProvider = new ManualContextProvider(context); 086 087 SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers, 088 Collections.singletonMap(AgendaDefinition.Constants.EVENT, "test")); 089 090 ProviderBasedEngine engine = new ProviderBasedEngine(); 091 engine.setContextProvider(contextProvider); 092 093 // Set execution options to log execution 094 ExecutionOptions executionOptions = new ExecutionOptions().setFlag(ExecutionFlag.LOG_EXECUTION, true); 095 096 EngineResults results = engine.execute(selectionCriteria, Facts.EMPTY_FACTS, executionOptions); 097 assertNotNull(results); 098 099 } 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 }