001 package com.hammurapi.reasoning.impl;
002
003 import java.io.File;
004 import java.net.MalformedURLException;
005 import java.net.URL;
006 import java.util.Map;
007
008 import org.eclipse.emf.common.util.URI;
009 import org.eclipse.emf.ecore.EObject;
010
011 import com.hammurapi.config.bootstrap.ConfigurationException;
012 import com.hammurapi.config.runtime.FactoryConfig;
013 import com.hammurapi.config.runtime.ResourceLoader;
014 import com.hammurapi.flow.runtime.FlowCompiler;
015 import com.hammurapi.reasoning.ForwardReasoningSession;
016 import com.hammurapi.reasoning.ForwardReasoningSessionFactory;
017 import com.hammurapi.reasoning.ReasoningException;
018 import com.hammurapi.reasoning.spi.model.RuleSet;
019
020 public class ForwardReasoningSessionFactoryImpl<F> implements ForwardReasoningSessionFactory<F> {
021
022 private com.hammurapi.flow.Flow flowDef;
023 private FactoryConfig factoryConfig;
024
025 private static FactoryConfig cloneConfig(FactoryConfig master, URL contextURL) throws ReasoningException {
026 try {
027 FactoryConfig ret = (FactoryConfig) master.clone();
028 if (ret.getContextUrl()==null) {
029 ret.setContextUrl(contextURL);
030 }
031 return ret;
032 } catch (CloneNotSupportedException e) {
033 throw new ReasoningException(e);
034 }
035 }
036
037 public ForwardReasoningSessionFactoryImpl(File ruleSetFile, FactoryConfig factoryConfig) throws ReasoningException, MalformedURLException {
038 this(URI.createFileURI(ruleSetFile.getAbsolutePath()), cloneConfig(factoryConfig, ruleSetFile.toURI().toURL()));
039 }
040
041 public ForwardReasoningSessionFactoryImpl(String ruleSetUri, FactoryConfig factoryConfig) throws ReasoningException, MalformedURLException {
042 this(URI.createURI(ruleSetUri), cloneConfig(factoryConfig, new URL(ruleSetUri)));
043 }
044
045 public ForwardReasoningSessionFactoryImpl(URI ruleSetUri, FactoryConfig factoryConfig) throws ReasoningException {
046 this(ResourceLoader.load(ruleSetUri).getContents().get(0), factoryConfig);
047 }
048
049 public ForwardReasoningSessionFactoryImpl(EObject def, FactoryConfig factoryConfig) throws ReasoningException {
050 try {
051 this.factoryConfig = (FactoryConfig) factoryConfig.clone();
052 } catch (CloneNotSupportedException e) {
053 throw new ReasoningException(e);
054 }
055
056 if (def instanceof RuleSet) {
057 RuleSetToFlowCompiler ruleSetCompiler = new RuleSetToFlowCompiler();
058 try {
059 flowDef = ruleSetCompiler.compile((RuleSet) def, factoryConfig);
060 } catch (ConfigurationException e) {
061 throw new ReasoningException(e);
062 }
063 } else if (def instanceof com.hammurapi.flow.Flow) {
064 flowDef = (com.hammurapi.flow.Flow) def;
065 }
066
067 if (!FlowCompiler.isCompiled(flowDef)) {
068 FlowCompiler flowCompiler = new FlowCompiler();
069 try {
070 flowDef = flowCompiler.compile(flowDef);
071 } catch (ConfigurationException e) {
072 throw new ReasoningException(e);
073 }
074 }
075 }
076
077 @Override
078 public ForwardReasoningSession<F> createSession(Map<?, ?> properties) throws ReasoningException {
079 try {
080 return new ForwardReasoningSessionImpl<F>(flowDef.create((FactoryConfig) factoryConfig.clone()), properties);
081 } catch (Exception e) {
082 throw new ReasoningException(e);
083 }
084 }
085
086 }
087