Coverage Report - org.kuali.rice.krms.framework.engine.ProviderBasedEngine
 
Classes in this File Line Coverage Branch Coverage Complexity
ProviderBasedEngine
84%
21/25
50%
4/8
2.2
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.engine;
 17  
 
 18  
 import java.util.Map;
 19  
 
 20  
 import org.joda.time.DateTime;
 21  
 import org.kuali.rice.krms.api.engine.Engine;
 22  
 import org.kuali.rice.krms.api.engine.EngineResults;
 23  
 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
 24  
 import org.kuali.rice.krms.api.engine.ExecutionOptions;
 25  
 import org.kuali.rice.krms.api.engine.Facts;
 26  
 import org.kuali.rice.krms.api.engine.ResultEvent;
 27  
 import org.kuali.rice.krms.api.engine.SelectionCriteria;
 28  
 import org.kuali.rice.krms.api.engine.Term;
 29  
 import org.kuali.rice.krms.framework.engine.result.TimingResult;
 30  
 
 31  29
 public class ProviderBasedEngine implements Engine {
 32  
 
 33  1
         private static final Term effectiveExecutionTimeTerm = new Term("effectiveExecutionTime", null);
 34  
         
 35  1
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProviderBasedEngine.class);
 36  1
         private static final ResultLogger KLog = ResultLogger.getInstance();
 37  
 
 38  
         private ContextProvider contextProvider;
 39  
 
 40  
     @Override
 41  
     public EngineResults execute(SelectionCriteria selectionCriteria, Map<String, Object> facts,
 42  
             ExecutionOptions executionOptions) {
 43  0
         return execute(selectionCriteria,
 44  
                 Facts.Builder.create().addFactsByName(facts).build(),
 45  
                 executionOptions);
 46  
     }
 47  
 
 48  
     @Override
 49  
         public EngineResults execute(SelectionCriteria selectionCriteria, Facts facts, ExecutionOptions executionOptions) {
 50  
                 DateTime start, end;
 51  29
                 start = new DateTime();
 52  29
                 ExecutionEnvironment environment = establishExecutionEnvironment(selectionCriteria, facts.getFactMap(), executionOptions);
 53  
                 
 54  
                 // set execution time
 55  29
                 Long effectiveExecutionTime = environment.getSelectionCriteria().getEffectiveExecutionTime();
 56  29
                 if (effectiveExecutionTime == null) { effectiveExecutionTime = System.currentTimeMillis(); }
 57  29
                 environment.publishFact(effectiveExecutionTimeTerm, effectiveExecutionTime);
 58  
 
 59  29
                 Context context = selectContext(selectionCriteria, facts.getFactMap(), executionOptions);
 60  29
                 if (context == null) {
 61  0
                         LOG.info("Failed to locate a Context for the given qualifiers, skipping rule engine execution: " + selectionCriteria.getContextQualifiers());
 62  0
                         return null;
 63  
                 }
 64  29
                 context.execute(environment);
 65  29
                 end = new DateTime();
 66  29
                 if (KLog.isEnabled(environment)){
 67  29
                         KLog.logResult(new TimingResult(ResultEvent.TimingEvent, this, environment, start, end));
 68  
                 }
 69  29
                 return environment.getEngineResults();
 70  
         }
 71  
         
 72  
         protected ExecutionEnvironment establishExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
 73  29
                 return new BasicExecutionEnvironment(selectionCriteria, facts, executionOptions, new TermResolutionEngineImpl());
 74  
         }
 75  
         
 76  
         protected Context selectContext(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
 77  29
                 if (contextProvider == null) {
 78  0
                         throw new IllegalStateException("No ContextProvider was configured.");
 79  
                 }
 80  29
                 return contextProvider.loadContext(selectionCriteria, facts, executionOptions);
 81  
         }
 82  
         
 83  
         
 84  
         public void setContextProvider(ContextProvider contextProvider) {
 85  29
                 this.contextProvider = contextProvider;
 86  29
         }
 87  
         
 88  
 }