001/**
002 * Copyright 2005-2015 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 */
016package org.kuali.rice.krms.framework;
017
018import static junit.framework.Assert.assertNotNull;
019
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import org.junit.Test;
029import org.kuali.rice.krms.api.engine.EngineResults;
030import org.kuali.rice.krms.api.engine.ExecutionOptions;
031import org.kuali.rice.krms.api.engine.ExecutionFlag;
032import org.kuali.rice.krms.api.engine.Facts;
033import org.kuali.rice.krms.api.engine.SelectionCriteria;
034import org.kuali.rice.krms.api.engine.Term;
035import org.kuali.rice.krms.api.engine.TermResolver;
036import org.kuali.rice.krms.api.repository.LogicalOperator;
037import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
038import org.kuali.rice.krms.framework.engine.Action;
039import org.kuali.rice.krms.framework.engine.Agenda;
040import org.kuali.rice.krms.framework.engine.BasicAgenda;
041import org.kuali.rice.krms.framework.engine.BasicAgendaTree;
042import org.kuali.rice.krms.framework.engine.BasicAgendaTreeEntry;
043import org.kuali.rice.krms.framework.engine.BasicContext;
044import org.kuali.rice.krms.framework.engine.BasicRule;
045import org.kuali.rice.krms.framework.engine.ComparableTermBasedProposition;
046import org.kuali.rice.krms.framework.engine.CompoundProposition;
047import org.kuali.rice.krms.framework.engine.Context;
048import org.kuali.rice.krms.framework.engine.ContextProvider;
049import org.kuali.rice.krms.framework.engine.Proposition;
050import org.kuali.rice.krms.framework.engine.ProviderBasedEngine;
051import org.kuali.rice.krms.framework.engine.ResultLogger;
052import org.kuali.rice.krms.framework.engine.Rule;
053import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
054import org.kuali.rice.krms.framework.engine.expression.ComparisonOperatorServiceImpl;
055
056public 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}