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.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  public class ProviderBasedEngine implements Engine {
32  
33  	private static final Term effectiveExecutionTimeTerm = new Term("effectiveExecutionTime", null);
34  	
35  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProviderBasedEngine.class);
36  	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          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  		start = new DateTime();
52  		ExecutionEnvironment environment = establishExecutionEnvironment(selectionCriteria, facts.getFactMap(), executionOptions);
53  		
54  		// set execution time
55  		Long effectiveExecutionTime = environment.getSelectionCriteria().getEffectiveExecutionTime();
56  		if (effectiveExecutionTime == null) { effectiveExecutionTime = System.currentTimeMillis(); }
57  		environment.publishFact(effectiveExecutionTimeTerm, effectiveExecutionTime);
58  
59  		Context context = selectContext(selectionCriteria, facts.getFactMap(), executionOptions);
60  		if (context == null) {
61  			LOG.info("Failed to locate a Context for the given qualifiers, skipping rule engine execution: " + selectionCriteria.getContextQualifiers());
62  			return null;
63  		}
64  		context.execute(environment);
65  		end = new DateTime();
66  		if (KLog.isEnabled(environment)){
67  			KLog.logResult(new TimingResult(ResultEvent.TimingEvent, this, environment, start, end));
68  		}
69  		return environment.getEngineResults();
70  	}
71  	
72  	protected ExecutionEnvironment establishExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
73  		return new BasicExecutionEnvironment(selectionCriteria, facts, executionOptions, new TermResolutionEngineImpl());
74  	}
75  	
76  	protected Context selectContext(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
77  		if (contextProvider == null) {
78  			throw new IllegalStateException("No ContextProvider was configured.");
79  		}
80  		return contextProvider.loadContext(selectionCriteria, facts, executionOptions);
81  	}
82  	
83  	
84  	public void setContextProvider(ContextProvider contextProvider) {
85  		this.contextProvider = contextProvider;
86  	}
87  	
88  }