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  /**
32   * An implementation of {@link Engine}
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class ProviderBasedEngine implements Engine {
36  
37  	private static final Term effectiveExecutionTimeTerm = new Term("effectiveExecutionTime", null);
38  	
39  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProviderBasedEngine.class);
40  	private static final ResultLogger KLog = ResultLogger.getInstance();
41  
42  	private ContextProvider contextProvider;
43  
44      @Override
45      public EngineResults execute(SelectionCriteria selectionCriteria, Map<String, Object> facts,
46              ExecutionOptions executionOptions) {
47          return execute(selectionCriteria,
48                  Facts.Builder.create().addFactsByName(facts).build(),
49                  executionOptions);
50      }
51  
52      @Override
53  	public EngineResults execute(SelectionCriteria selectionCriteria, Facts facts, ExecutionOptions executionOptions) {
54  		DateTime start, end;
55  		start = new DateTime();
56  		ExecutionEnvironment environment = establishExecutionEnvironment(selectionCriteria, facts.getFactMap(), executionOptions);
57  		
58  		// set execution time
59  		Long effectiveExecutionTime = environment.getSelectionCriteria().getEffectiveExecutionTime();
60  		if (effectiveExecutionTime == null) { effectiveExecutionTime = System.currentTimeMillis(); }
61  		environment.publishFact(effectiveExecutionTimeTerm, effectiveExecutionTime);
62  
63  		Context context = selectContext(selectionCriteria, facts.getFactMap(), executionOptions);
64  		if (context == null) {
65  			LOG.info("Failed to locate a Context for the given qualifiers, skipping rule engine execution: " + selectionCriteria.getContextQualifiers());
66  			return null;
67  		}
68  		context.execute(environment);
69  		end = new DateTime();
70  		if (KLog.isEnabled(environment)){
71  			KLog.logResult(new TimingResult(ResultEvent.TIMING_EVENT, this, environment, start, end));
72  		}
73  		return environment.getEngineResults();
74  	}
75  
76      /**
77       * Return a {@link BasicExecutionEnvironment} using the given parameters
78       * @param selectionCriteria {@link SelectionCriteria}
79       * @param facts
80       * @param executionOptions {@link ExecutionOptions}
81       * @return {@link ExecutionEnvironment} created with the given parameters
82       */
83  	protected ExecutionEnvironment establishExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
84  		return new BasicExecutionEnvironment(selectionCriteria, facts, executionOptions, new TermResolutionEngineImpl());
85  	}
86  
87      /**
88       * Load a Context from the contextProvider using the given parameters
89       * @see ContextProvider loadContext
90       * @param selectionCriteria
91       * @param facts
92       * @param executionOptions
93       * @return {@link Context}
94       * @throws IllegalStateException if the contextProvider is null;
95       */
96  	protected Context selectContext(SelectionCriteria selectionCriteria, Map<Term, Object> facts, ExecutionOptions executionOptions) {
97  		if (contextProvider == null) {
98  			throw new IllegalStateException("No ContextProvider was configured.");
99  		}
100 		return contextProvider.loadContext(selectionCriteria, facts, executionOptions);
101 	}
102 
103     /**
104      * Set the {@link ContextProvider}
105      * @param contextProvider to loadContext from.
106      */
107 	public void setContextProvider(ContextProvider contextProvider) {
108 		this.contextProvider = contextProvider;
109 	}
110 	
111 }