[seam-commits] Seam SVN: r10633 - in modules/trunk/drools: src/main/java and 4 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Fri Apr 24 19:25:44 EDT 2009
Author: shane.bryzak at jboss.com
Date: 2009-04-24 19:25:43 -0400 (Fri, 24 Apr 2009)
New Revision: 10633
Added:
modules/trunk/drools/pom.xml
modules/trunk/drools/src/main/java/org/
modules/trunk/drools/src/main/java/org/jboss/
modules/trunk/drools/src/main/java/org/jboss/seam/
modules/trunk/drools/src/main/java/org/jboss/seam/drools/
modules/trunk/drools/src/main/java/org/jboss/seam/drools/Decision.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsActionHandler.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsAssignmentHandler.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsDecisionHandler.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsHandler.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/ManagedWorkingMemory.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/RuleAgent.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/RuleBase.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/SeamGlobalResolver.java
modules/trunk/drools/src/main/java/org/jboss/seam/drools/package-info.java
Modified:
modules/trunk/drools/
Log:
initial import of drools module
Property changes on: modules/trunk/drools
___________________________________________________________________
Name: svn:ignore
+ target
.classpath
.project
.settings
Added: modules/trunk/drools/pom.xml
===================================================================
--- modules/trunk/drools/pom.xml (rev 0)
+++ modules/trunk/drools/pom.xml 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,33 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <artifactId>seam-parent</artifactId>
+ <groupId>org.jboss.seam</groupId>
+ <version>3.0.0-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.jboss.seam</groupId>
+ <artifactId>seam-drools</artifactId>
+ <packaging>jar</packaging>
+ <version>3.0.0-SNAPSHOT</version>
+ <name>Seam Drools</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.jboss.webbeans</groupId>
+ <artifactId>jsr299-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.webbeans</groupId>
+ <artifactId>webbeans-logging</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-core</artifactId>
+ </dependency>
+
+ </dependencies>
+
+</project>
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/Decision.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/Decision.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/Decision.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,21 @@
+package org.jboss.seam.drools;
+
+/**
+ * API for setting the result of a decision from rules in
+ * a Drools decision handler.
+ *
+ */
+public class Decision
+{
+ private String outcome;
+
+ public String getOutcome()
+ {
+ return outcome;
+ }
+
+ public void setOutcome(String outcome)
+ {
+ this.outcome = outcome;
+ }
+}
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsActionHandler.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsActionHandler.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsActionHandler.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,28 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+
+import org.jbpm.graph.def.ActionHandler;
+import org.jbpm.graph.exe.ExecutionContext;
+
+/**
+ * A jBPM ActionHandler that delegates to a Drools WorkingMemory
+ * held in a Seam context variable.
+ *
+ * @author Jeff Delong
+ * @author Gavin King
+ *
+ */
+public class DroolsActionHandler extends DroolsHandler implements ActionHandler
+{
+ private static final long serialVersionUID = 7752070876220597913L;
+
+ public List<String> assertObjects;
+ public String workingMemoryName;
+
+ public void execute(ExecutionContext executionContext) throws Exception
+ {
+ getWorkingMemory(workingMemoryName, assertObjects, executionContext).fireAllRules();
+ }
+
+}
\ No newline at end of file
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsAssignmentHandler.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsAssignmentHandler.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsAssignmentHandler.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,32 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+
+import org.drools.WorkingMemory;
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.taskmgmt.def.AssignmentHandler;
+import org.jbpm.taskmgmt.exe.Assignable;
+
+/**
+ * A jBPM AssignmentHandler that delegates to a Drools WorkingMemory
+ * held in a Seam context variable.
+ *
+ * @author Jeff Delong
+ * @author Gavin King
+ *
+ */
+public class DroolsAssignmentHandler extends DroolsHandler implements AssignmentHandler
+{
+ private static final long serialVersionUID = -7114640047036854546L;
+
+ public String workingMemoryName;
+ public List<String> assertObjects;
+
+ public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception
+ {
+ WorkingMemory workingMemory = getWorkingMemory(workingMemoryName, assertObjects, executionContext);
+ workingMemory.setGlobal( "assignable", assignable );
+ workingMemory.fireAllRules();
+ }
+
+}
\ No newline at end of file
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsDecisionHandler.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsDecisionHandler.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsDecisionHandler.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,32 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+
+import org.drools.WorkingMemory;
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.graph.node.DecisionHandler;
+
+/**
+ * A jBPM DecisionHandler that delegates to a Drools WorkingMemory
+ * held in a Seam context variable. The decision outcome is returned
+ * by setting the outcome attribute of the global named "decision".
+ *
+ * @author Gavin King
+ *
+ */
+public class DroolsDecisionHandler extends DroolsHandler implements DecisionHandler
+{
+ private static final long serialVersionUID = -8900810376838166513L;
+
+ public List<String> assertObjects;
+ public String workingMemoryName;
+
+ public String decide(ExecutionContext executionContext) throws Exception
+ {
+ WorkingMemory workingMemory = getWorkingMemory(workingMemoryName, assertObjects, executionContext);
+ workingMemory.setGlobal( "decision", new Decision() );
+ workingMemory.fireAllRules();
+ return ( (Decision) workingMemory.getGlobal("decision") ).getOutcome();
+ }
+
+}
\ No newline at end of file
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsHandler.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsHandler.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/DroolsHandler.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,63 @@
+package org.jboss.seam.drools;
+
+import java.util.List;
+
+import org.drools.FactHandle;
+import org.drools.WorkingMemory;
+import org.jboss.seam.Component;
+import org.jboss.seam.bpm.Actor;
+import org.jboss.seam.core.Expressions;
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.jpdl.el.ELException;
+
+/**
+ * Common functionality for jBPM handlers for Drools.
+ *
+ * @author Jeff Delong
+ * @author Gavin King
+ *
+ */
+public class DroolsHandler
+{
+ protected WorkingMemory getWorkingMemory(String workingMemoryName, List<String> expressions, ExecutionContext executionContext)
+ throws ELException
+ {
+ WorkingMemory workingMemory = (WorkingMemory) Component.getInstance(workingMemoryName, true);
+
+ for (String objectName: expressions)
+ {
+ Object object = Expressions.instance().createValueExpression(objectName).getValue();
+ //Object object = new SeamVariableResolver().resolveVariable(objectName);
+ // assert the object into the rules engine
+ if (object instanceof Iterable)
+ {
+ for (Object element: (Iterable) object)
+ {
+ assertObject(workingMemory, element);
+ }
+ }
+ else
+ {
+ assertObject(workingMemory, object);
+ }
+ }
+
+ //workingMemory.setGlobal( "contextInstance", executionContext.getContextInstance() );
+ workingMemory.insert(Actor.instance());
+
+ return workingMemory;
+ }
+
+ private void assertObject(WorkingMemory workingMemory, Object element)
+ {
+ FactHandle fact = workingMemory.getFactHandle(element);
+ if (fact==null)
+ {
+ workingMemory.insert(element);
+ }
+ else
+ {
+ workingMemory.update(fact, element);
+ }
+ }
+}
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/ManagedWorkingMemory.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/ManagedWorkingMemory.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/ManagedWorkingMemory.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,118 @@
+package org.jboss.seam.drools;
+
+import java.io.Serializable;
+
+import org.drools.RuleBase;
+import org.drools.StatefulSession;
+import org.drools.spi.GlobalResolver;
+import org.jboss.seam.Component;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Destroy;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.core.Mutable;
+import org.jboss.seam.core.Expressions.ValueExpression;
+
+/**
+ * A conversation-scoped Drools WorkingMemory for a named RuleBase
+ *
+ * @author Gavin King
+ *
+ */
+ at Scope(ScopeType.CONVERSATION)
+ at BypassInterceptors
+public class ManagedWorkingMemory implements Mutable, Serializable
+{
+ private static final long serialVersionUID = -1746942080571374743L;
+
+ private String ruleBaseName;
+ private StatefulSession statefulSession;
+ private ValueExpression<RuleBase> ruleBase;
+
+ public boolean clearDirty()
+ {
+ return true;
+ }
+
+ /**
+ * The name of a Seam context variable holding an
+ * instance of org.drools.RuleBase
+ *
+ * @return a context variable name
+ * @deprecated
+ */
+ public String getRuleBaseName()
+ {
+ return ruleBaseName;
+ }
+
+ /**
+ * The name of a Seam context variable holding an
+ * instance of org.drools.RuleBase
+ *
+ * @param ruleBaseName a context variable name
+ * @deprecated
+ */
+ public void setRuleBaseName(String ruleBaseName)
+ {
+ this.ruleBaseName = ruleBaseName;
+ }
+
+ @Unwrap
+ public StatefulSession getStatefulSession()
+ {
+ if (statefulSession==null)
+ {
+ statefulSession = getRuleBaseFromValueBinding().newStatefulSession();
+ statefulSession.setGlobalResolver( createGlobalResolver( statefulSession.getGlobalResolver() ) );
+ }
+ return statefulSession;
+ }
+
+ protected RuleBase getRuleBaseFromValueBinding()
+ {
+ RuleBase ruleBase;
+ if (this.ruleBase!=null)
+ {
+ ruleBase = this.ruleBase.getValue();
+ }
+ else if (ruleBaseName!=null)
+ {
+ //deprecated stuff
+ ruleBase = (RuleBase) Component.getInstance(ruleBaseName, true);
+ }
+ else
+ {
+ throw new IllegalStateException("No RuleBase");
+ }
+
+ if (ruleBase==null)
+ {
+ throw new IllegalStateException("RuleBase not found: " + ruleBaseName);
+ }
+ return ruleBase;
+ }
+
+ protected GlobalResolver createGlobalResolver(GlobalResolver delegate)
+ {
+ return new SeamGlobalResolver(delegate);
+ }
+
+ @Destroy
+ public void destroy()
+ {
+ statefulSession.dispose();
+ }
+
+ public ValueExpression<RuleBase> getRuleBase()
+ {
+ return ruleBase;
+ }
+
+ public void setRuleBase(ValueExpression<RuleBase> ruleBase)
+ {
+ this.ruleBase = ruleBase;
+ }
+
+}
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/RuleAgent.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/RuleAgent.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/RuleAgent.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,161 @@
+package org.jboss.seam.drools;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.util.Resources;
+
+/**
+ * Manager component for a rule base loaded from a drools RulesAgent
+ */
+ at Scope(ScopeType.APPLICATION)
+ at BypassInterceptors
+public class RuleAgent
+{
+ private static final LogProvider log = Logging.getLogProvider(RuleAgent.class);
+
+ private org.drools.agent.RuleAgent agent;
+ private String configurationFile;
+
+ private String newInstance;
+ private String files;
+ private String url;
+ private String localCacheDir;
+ private String poll;
+ private String configName;
+
+ @Create
+ public void createAgent() throws Exception
+ {
+ Properties properties = new Properties();
+
+ loadFromPath(properties, configurationFile);
+ setLocalProperties(properties);
+
+ agent = org.drools.agent.RuleAgent.newRuleAgent(properties);
+ log.debug("Creating new rules agent");
+ }
+
+ protected void setLocalProperties(Properties properties)
+ {
+ if (newInstance != null) {
+ properties.setProperty(org.drools.agent.RuleAgent.NEW_INSTANCE, newInstance);
+ }
+ if (files != null) {
+ properties.setProperty(org.drools.agent.RuleAgent.FILES, files);
+ }
+ if (url != null) {
+ properties.setProperty(org.drools.agent.RuleAgent.URLS, url);
+ }
+ if (localCacheDir != null) {
+ properties.setProperty(org.drools.agent.RuleAgent.LOCAL_URL_CACHE, localCacheDir);
+ }
+ if (poll != null) {
+ properties.setProperty(org.drools.agent.RuleAgent.POLL_INTERVAL, poll);
+ }
+ if (configName != null) {
+ properties.setProperty(org.drools.agent.RuleAgent.CONFIG_NAME, configName);
+ }
+
+ }
+
+ protected void loadFromPath(Properties properties, String configurationFile)
+ throws IOException
+ {
+ if (configurationFile != null) {
+ InputStream inputStream = Resources.getResourceAsStream(configurationFile, null);
+ if (inputStream != null) {
+ try {
+ properties.load(inputStream);
+ } finally {
+ inputStream.close();
+ }
+ }
+ }
+ }
+
+ @Unwrap
+ public org.drools.RuleBase getRuleBase()
+ {
+ return agent.getRuleBase();
+ }
+
+ public String getNewInstance()
+ {
+ return newInstance;
+ }
+
+ public void setNewInstance(String newInstance)
+ {
+ this.newInstance = newInstance;
+ }
+
+ public String getFiles()
+ {
+ return files;
+ }
+
+ public void setFiles(String files)
+ {
+ this.files = files;
+ }
+
+ public String getUrl()
+ {
+ return url;
+ }
+
+ public void setUrl(String url)
+ {
+ this.url = url;
+ }
+
+ public String getLocalCacheDir()
+ {
+ return localCacheDir;
+ }
+
+ public void setLocalCacheDir(String localCacheDir)
+ {
+ this.localCacheDir = localCacheDir;
+ }
+
+ public String getPoll()
+ {
+ return poll;
+ }
+
+ public void setPoll(String poll)
+ {
+ this.poll = poll;
+ }
+
+ public String getConfigName()
+ {
+ return configName;
+ }
+
+ public void setConfigName(String name)
+ {
+ this.configName = name;
+ }
+
+ public String getConfigurationFile()
+ {
+ return configurationFile;
+ }
+
+ public void setConfigurationFile(String brmsConfig)
+ {
+ this.configurationFile = brmsConfig;
+ }
+
+}
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/RuleBase.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/RuleBase.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/RuleBase.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,116 @@
+package org.jboss.seam.drools;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.drools.RuleBaseFactory;
+import org.drools.compiler.DroolsError;
+import org.drools.compiler.PackageBuilder;
+import org.drools.compiler.PackageBuilderConfiguration;
+import org.drools.compiler.RuleError;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Unwrap;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.core.ResourceLoader;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
+/**
+ * Manager component for a Drools RuleBase
+ *
+ * @author Gavin King
+ *
+ */
+ at Scope(ScopeType.APPLICATION)
+ at BypassInterceptors
+public class RuleBase
+{
+ private static final LogProvider log = Logging.getLogProvider(RuleBase.class);
+
+ private String[] ruleFiles;
+ private String dslFile;
+ private org.drools.RuleBase ruleBase;
+
+ @Create
+ public void compileRuleBase() throws Exception
+ {
+ PackageBuilderConfiguration conf = new PackageBuilderConfiguration();
+ PackageBuilder builder = new PackageBuilder(conf);
+
+ if (ruleFiles!=null)
+ {
+ for (String ruleFile: ruleFiles)
+ {
+ log.info("parsing rules: " + ruleFile);
+ InputStream stream = ResourceLoader.instance().getResourceAsStream(ruleFile);
+ if (stream==null)
+ {
+ throw new IllegalStateException("could not locate rule file: " + ruleFile);
+ }
+ // read in the source
+ Reader drlReader = new InputStreamReader(stream);
+
+ if (dslFile==null)
+ {
+ builder.addPackageFromDrl(drlReader);
+ }
+ else
+ {
+ Reader dslReader = new InputStreamReader( ResourceLoader.instance().getResourceAsStream(dslFile) );
+ builder.addPackageFromDrl(drlReader, dslReader);
+ }
+
+ if ( builder.hasErrors() )
+ {
+ log.error("errors parsing rules in: " + ruleFile);
+ for ( DroolsError error: builder.getErrors().getErrors() )
+ {
+ if (error instanceof RuleError)
+ {
+ RuleError ruleError = (RuleError) error;
+ log.error( ruleError.getMessage() + " (" + ruleFile + ':' + ruleError.getLine() + ')' );
+ }
+ else
+ {
+ log.error( error.getMessage() + " (" + ruleFile + ')' );
+ }
+ }
+ }
+ }
+ }
+
+ // add the package to a rulebase
+ ruleBase = RuleBaseFactory.newRuleBase();
+ ruleBase.addPackage( builder.getPackage() );
+ }
+
+ @Unwrap
+ public org.drools.RuleBase getRuleBase()
+ {
+ return ruleBase;
+ }
+
+ public String[] getRuleFiles()
+ {
+ return ruleFiles;
+ }
+
+ public void setRuleFiles(String[] ruleFiles)
+ {
+ this.ruleFiles = ruleFiles;
+ }
+
+ public String getDslFile()
+ {
+ return dslFile;
+ }
+
+ public void setDslFile(String dslFile)
+ {
+ this.dslFile = dslFile;
+ }
+
+}
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/SeamGlobalResolver.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/SeamGlobalResolver.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/SeamGlobalResolver.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,52 @@
+package org.jboss.seam.drools;
+
+import org.drools.spi.GlobalResolver;
+import org.jboss.seam.Component;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.core.Init;
+
+/**
+ * Resolves Seam context variables as Drools globals
+ *
+ * @author Gavin King
+ *
+ */
+public class SeamGlobalResolver implements GlobalResolver
+{
+ private GlobalResolver delegate;
+
+ public SeamGlobalResolver(GlobalResolver delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ public void setGlobal(String name, Object value)
+ {
+ //TODO: is this the right thing to do??
+ //or: Contexts.getConversationContext().set(name, value);
+ delegate.setGlobal(name, value);
+ }
+
+ public Object resolveGlobal(String name)
+ {
+ if ( !Contexts.isApplicationContextActive() )
+ {
+ return delegate.resolveGlobal(name);
+ }
+ else
+ {
+ Object instance = Component.getInstance(name);
+ if (instance==null)
+ {
+ instance = delegate.resolveGlobal(name);
+ return instance==null ?
+ Init.instance().getRootNamespace().getChild(name) :
+ instance;
+ }
+ else
+ {
+ return instance;
+ }
+ }
+ }
+}
\ No newline at end of file
Added: modules/trunk/drools/src/main/java/org/jboss/seam/drools/package-info.java
===================================================================
--- modules/trunk/drools/src/main/java/org/jboss/seam/drools/package-info.java (rev 0)
+++ modules/trunk/drools/src/main/java/org/jboss/seam/drools/package-info.java 2009-04-24 23:25:43 UTC (rev 10633)
@@ -0,0 +1,7 @@
+/**
+ * Seam components for integrating Drools.
+ */
+ at Namespace(value="http://jboss.com/products/seam/drools", prefix="org.jboss.seam.drools")
+package org.jboss.seam.drools;
+
+import org.jboss.seam.annotations.Namespace;
More information about the seam-commits
mailing list