| 1 | |
package org.kuali.rice.krms.framework.engine; |
| 2 | |
|
| 3 | |
import java.util.Date; |
| 4 | |
import java.util.Map; |
| 5 | |
|
| 6 | |
import org.kuali.rice.krms.api.Asset; |
| 7 | |
import org.kuali.rice.krms.api.Context; |
| 8 | |
import org.kuali.rice.krms.api.ContextProvider; |
| 9 | |
import org.kuali.rice.krms.api.Engine; |
| 10 | |
import org.kuali.rice.krms.api.EngineResults; |
| 11 | |
import org.kuali.rice.krms.api.ExecutionEnvironment; |
| 12 | |
import org.kuali.rice.krms.api.ResultEvent; |
| 13 | |
import org.kuali.rice.krms.api.SelectionCriteria; |
| 14 | |
import org.kuali.rice.krms.framework.engine.result.TimingResult; |
| 15 | |
|
| 16 | 0 | public class ProviderBasedEngine implements Engine { |
| 17 | |
|
| 18 | 0 | private static final Asset effectiveExecutionTimeAsset = new Asset("effectiveExecutionTime", "java.lang.Long"); |
| 19 | |
|
| 20 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProviderBasedEngine.class); |
| 21 | 0 | private static final ResultLogger KLog = ResultLogger.getInstance(); |
| 22 | |
|
| 23 | |
private ContextProvider contextProvider; |
| 24 | |
|
| 25 | |
@Override |
| 26 | |
public EngineResults execute(SelectionCriteria selectionCriteria, Map<Asset, Object> facts, Map<String, String> executionOptions) { |
| 27 | |
Date start, end; |
| 28 | 0 | start = new Date(); |
| 29 | 0 | ExecutionEnvironment environment = establishExecutionEnvironment(selectionCriteria, facts, executionOptions); |
| 30 | |
|
| 31 | |
|
| 32 | 0 | Long effectiveExecutionTime = environment.getSelectionCriteria().getEffectiveExecutionTime(); |
| 33 | 0 | if (effectiveExecutionTime == null) { effectiveExecutionTime = System.currentTimeMillis(); } |
| 34 | 0 | environment.publishFact(effectiveExecutionTimeAsset, effectiveExecutionTime); |
| 35 | |
|
| 36 | 0 | Context context = selectContext(selectionCriteria, facts, executionOptions); |
| 37 | 0 | if (context == null) { |
| 38 | 0 | LOG.info("Failed to locate a Context for the given qualifiers, skipping rule engine execution: " + selectionCriteria.getContextQualifiers()); |
| 39 | 0 | return null; |
| 40 | |
} |
| 41 | 0 | context.execute(environment); |
| 42 | 0 | end = new Date(); |
| 43 | 0 | if (KLog.isEnabled(environment)){ |
| 44 | 0 | KLog.logResult(new TimingResult(ResultEvent.TimingEvent, this, environment, start, end)); |
| 45 | |
} |
| 46 | 0 | return environment.getEngineResults(); |
| 47 | |
} |
| 48 | |
|
| 49 | |
protected ExecutionEnvironment establishExecutionEnvironment(SelectionCriteria selectionCriteria, Map<Asset, Object> facts, Map<String, String> executionOptions) { |
| 50 | 0 | return new BasicExecutionEnvironment(selectionCriteria, facts, executionOptions); |
| 51 | |
} |
| 52 | |
|
| 53 | |
protected Context selectContext(SelectionCriteria selectionCriteria, Map<Asset, Object> facts, Map<String, String> executionOptions) { |
| 54 | 0 | if (contextProvider == null) { |
| 55 | 0 | throw new IllegalStateException("No ContextProvider was configured."); |
| 56 | |
} |
| 57 | 0 | return contextProvider.loadContext(selectionCriteria, facts, executionOptions); |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | |
public void setContextProvider(ContextProvider contextProvider) { |
| 62 | 0 | this.contextProvider = contextProvider; |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
} |