[jboss-svn-commits] JBL Code SVN: r20434 - in labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules: lib/ext and 11 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jun 12 02:37:31 EDT 2008
Author: beve
Date: 2008-06-12 02:37:30 -0400 (Thu, 12 Jun 2008)
New Revision: 20434
Added:
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-compiler-4.0.3.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-core-4.0.3.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-decisiontables-4.0.3.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-jsr94-4.0.3.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/mvel14-1.2.10.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/stax-api-1.0.1.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/xstream-1.2.2.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleBaseHelper.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleService.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceBuilderException.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceException.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceFactory.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/ListenerTagNames.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/RuleService.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/rules/
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleBaseHelperUnitTest.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/RuleBaseHelper.drl
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/RuleBaseHelper.xls
Removed:
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-compiler-4.0.2.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-core-4.0.2.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-decisiontables-4.0.2.jar
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-jsr94-4.0.2.jar
Modified:
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/build.xml
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBRulesCounter.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/BusinessProcessorRoutingUnitTest.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/BusinessProcessorUnitTest.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouterUnitTest.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java
labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/JBossESBRules.drl
Log:
Suggestion for RuleService based on Jeffs work. Checking in to share and discuss this with Kevin
Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/build.xml
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/build.xml 2008-06-12 06:00:37 UTC (rev 20433)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/build.xml 2008-06-12 06:37:30 UTC (rev 20434)
@@ -4,5 +4,13 @@
<!-- Import the base Ant build script... -->
<import file="${services.base.build}"/>
- <target name="test" depends="base.test"/>
+ <target name="test">
+ <copy todir="${basedir}/build/tests/classes" overwrite="true">
+ <fileset dir="${basedir}/src/test/resources">
+ <include name="*.xls"/>
+ <include name="*.drl"/>
+ </fileset>
+ </copy>
+ <antcall target="base.test"/>
+ </target>
</project>
Deleted: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-compiler-4.0.2.jar
===================================================================
(Binary files differ)
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-compiler-4.0.3.jar
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-compiler-4.0.3.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Deleted: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-core-4.0.2.jar
===================================================================
(Binary files differ)
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-core-4.0.3.jar
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-core-4.0.3.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Deleted: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-decisiontables-4.0.2.jar
===================================================================
(Binary files differ)
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-decisiontables-4.0.3.jar
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-decisiontables-4.0.3.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Deleted: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-jsr94-4.0.2.jar
===================================================================
(Binary files differ)
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-jsr94-4.0.3.jar
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/drools-jsr94-4.0.3.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/mvel14-1.2.10.jar
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/mvel14-1.2.10.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/stax-api-1.0.1.jar
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/stax-api-1.0.1.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/xstream-1.2.2.jar
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/lib/ext/xstream-1.2.2.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBRulesCounter.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBRulesCounter.java 2008-06-12 06:00:37 UTC (rev 20433)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBRulesCounter.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -256,7 +256,7 @@
/**
* Register this MBean with JBoss.
*/
- protected void registerMBean() {
+ public void registerMBean() {
MBeanServer mbeanServer = null;
try {
mbeanServer = MBeanServerLocator.locateJBoss();
Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java 2008-06-12 06:00:37 UTC (rev 20433)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -21,69 +21,59 @@
*/
package org.jboss.internal.soa.esb.services.routing.cbr;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;
-import org.apache.log4j.Level;
-import org.drools.RuleBase;
-import org.drools.RuleBaseFactory;
-import org.drools.StatefulSession;
-import org.drools.compiler.DroolsParserException;
-import org.drools.compiler.PackageBuilder;
-import org.drools.compiler.PackageBuilderConfiguration;
-import org.drools.rule.Package;
-import org.jboss.internal.soa.esb.services.routing.cbr.JBRulesCounter;
+import org.jboss.internal.soa.esb.services.rules.DroolsRuleService;
+import org.jboss.internal.soa.esb.services.rules.RuleServiceException;
+import org.jboss.internal.soa.esb.services.rules.RuleServiceFactory;
import org.jboss.soa.esb.helpers.ConfigTree;
-import org.jboss.soa.esb.lifecycle.LifecyclePriorities;
-import org.jboss.soa.esb.lifecycle.LifecycleResource;
-import org.jboss.soa.esb.lifecycle.LifecycleResourceException;
-import org.jboss.soa.esb.lifecycle.LifecycleResourceFactory;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.services.routing.MessageRouterException;
-import org.jboss.soa.esb.services.routing.cbr.CBRException;
import org.jboss.soa.esb.services.routing.cbr.ContentBasedRouter;
-import org.jboss.soa.esb.util.ClassUtil;
+import org.jboss.soa.esb.services.rules.RuleService;
/**
- * The Implementation of a rule based Content Router. Here we use JBossRules. We
- * keep a HashMap of so called working memories for performance reasons.
+ * The Implementation of a rule based Content Router. Here we use JBossRules.
+ * <p/>
+ * We keep a HashMap of so called working memories for performance reasons.
+ * <br><br>
+ * Usage:
+ * <pre>{@code
*
+ * <action class="org.jboss.soa.esb.actions.ContentBasedRouter" name="ContentBasedRouter">
+ * <property name="ruleSet" value="FunCBRRules-XPath.drl"/>
+ * <property name="ruleLanguage" value="XPathLanguage.dsl"/>
+ * <property name="ruleReload" value="true"/>
+ * <property name="destinations">
+ * <route-to destination-name="blue" service-category="BlueTeam" service-name="GoBlue" />
+ * <route-to destination-name="red" service-category="RedTeam" service-name="GoRed" />
+ * <route-to destination-name="green" service-category="GreenTeam" service-name="GoGreen" />
+ * </property>
+ * </action>
+ *
+ * }</pre>
+ *
+ * Optional properties
+ * <pre>{@code
+ * <property name="stateful" value="true"/>
+ * <property name="dispose" value="true"/>
+ * }</pre>
+ *
+ * Property descriptions:
+ * <lu>
+ * <li>stateful - if true Drools will execute using a StatfulSession. Default is false
+ * <li>dispose - only valid if stateful is true if true Drools will execute using a StatfulSession. Default is false
+ * </lu>
+ * <br>
* @author kstam at redhat.com
*
*/
public class JBossRulesRouter implements ContentBasedRouter
{
- /**
- * The lifecycle resource factory.
- */
- private static final LifecycleResourceFactory<Map<String, RuleBase>> lifecycleRuleBaseFactory = new LifecycleRuleBaseFactory() ;
- /**
- * Lifecycle couriers.
- */
- private static final LifecycleResource<Map<String, RuleBase>> lifecycleRuleBases =
- new LifecycleResource<Map<String,RuleBase>>(lifecycleRuleBaseFactory,
- LifecyclePriorities.RULE_BASE_PRIORITY) ;
- /**
- * The lifecycle resource factory.
- */
- private static final LifecycleResourceFactory<Map<String, String>> lifecycleRuleSetFactory = new LifecycleRuleSetFactory() ;
- /**
- * Lifecycle couriers.
- */
- private static final LifecycleResource<Map<String, String>> lifecycleRuleSets =
- new LifecycleResource<Map<String,String>>(lifecycleRuleSetFactory,
- LifecyclePriorities.RULE_BASE_PRIORITY) ;
-
private static Logger logger = Logger.getLogger(JBossRulesRouter.class);
- private JBRulesCounter rulesCounter = null;
+ private ConfigTree configTree;
/**
* Route the message with a reference to the ruleSets supplied in the message.
@@ -91,8 +81,7 @@
*
* @param message - Message that needs routing.
*/
- public List<String> route (Message message)
- throws MessageRouterException
+ public List<String> route (Message message) throws MessageRouterException
{
logger.error("Not implemented");
return null;
@@ -102,12 +91,25 @@
* Route the message, using the given ruleSet (drl).
*
* @param ruleSet -
- * Filename of the drl that will be used.
+ * String reference to a file which contains a ruleSet.
+ * @param ruleLanguage -
+ * String reference to a file which contains a custom rule language
+ * definition.
* @param message -
* Message that needs routing.
+ * @param objectList -
+ * List of objects that should be extracted from the message
+ * object and which will be inserted into the Drools working
+ * memory.
+ * @return List<String> -
+ * List of destinations
+ *
*/
- public List<String> route (String ruleSet, boolean ruleReload,
- Message message, List<Object> objectList) throws MessageRouterException
+ public List<String> route (
+ final String ruleSet,
+ final boolean ruleReload,
+ final Message message,
+ final List<Object> objectList) throws MessageRouterException
{
return route(ruleSet, null, ruleReload, message, objectList);
}
@@ -118,301 +120,65 @@
* now. However when the need arises, this is where it goes.
*
* @param ruleSet -
- * Filename of the drl that will be used.
+ * String reference to a file which contains a ruleSet.
* @param ruleLanguage -
- * Filename of the dsl that will be used.
+ * String reference to a file which contains a custom rule language
+ * definition.
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading the
+ * ruleSet.
* @param message -
* Message that needs routing.
+ * @param objectList -
+ * a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory.
+ * @return List<String> -
+ * List of Service destinations.
*/
- @SuppressWarnings("unchecked")
- public List<String> route (String ruleSet, String ruleLanguage,
- boolean ruleReload, Message message, List<Object> objectList) throws MessageRouterException
+ public List<String> route (
+ final String ruleSet,
+ final String ruleLanguage,
+ final boolean ruleReload,
+ final Message message,
+ final List<Object> objectList) throws MessageRouterException
{
- List<String> destinations = new ArrayList();
- long startTime = System.nanoTime();
-
- try {
- String newRuleSet = null;
- boolean isRulesChanged = false;
-
- final Map<String, String> ruleSets = lifecycleRuleSets.getLifecycleResource() ;
- if (ruleReload)
+ try
+ {
+ RuleService ruleService = RuleServiceFactory.getRuleService( DroolsRuleService.class.getName() );
+ ruleService.setConfigTree( configTree );
+
+ boolean stateful = Boolean.valueOf( configTree.getAttribute( "stateful" ) );
+ boolean dispose = Boolean.valueOf( configTree.getAttribute( "dispose", "true" ) );
+
+ Message routeMessage;
+ if ( ruleSet.endsWith( ".xls" ) || ruleSet.endsWith( ".csv" ) )
{
- String currentRuleSet = ruleSets.get(ruleSet);
- newRuleSet = getRules(ruleSet, ruleLanguage);
- if (currentRuleSet == null || !currentRuleSet
- .equals(newRuleSet))
- {
- isRulesChanged = true;
- }
+ if ( stateful )
+ routeMessage = ruleService.executeStatefulRulesFromDecisionTable( ruleSet, ruleReload, dispose, message, objectList );
+ else
+ routeMessage = ruleService.executeStatelessRulesFromDecisionTable( ruleSet, ruleReload, message, objectList );
}
- final Map<String, RuleBase> ruleBases = lifecycleRuleBases.getLifecycleResource() ;
- RuleBase ruleBase = ruleBases.get(ruleSet);
- if (ruleBase == null || isRulesChanged)
+ else
{
- logger.log(Level.DEBUG,
- "Reading ruleSet from file=" + ruleSet);
- ruleBase = readRuleBase(ruleSet, ruleLanguage);
- if (ruleBase != null) ruleBases.put(ruleSet, ruleBase);
- if (newRuleSet == null)
- {
- newRuleSet = getRules(ruleSet, ruleLanguage);
- }
- if (ruleSet != null) ruleSets.put(ruleSet, newRuleSet);
+ if ( stateful )
+ routeMessage = ruleService.executeStatefulRules( ruleSet, ruleLanguage, ruleReload, dispose, message, objectList );
+ else
+ routeMessage = ruleService.executeStatelessRules( ruleSet, ruleLanguage, ruleReload, message, objectList );
}
- StatefulSession workingMemory = ruleBase.newStatefulSession();
- logger.log(Level.DEBUG,
- "Obtained message=" + message + " with ruleSet=" + ruleSet);
- workingMemory.setGlobal("destinations", destinations);
- if (objectList!=null) {
- for (Object object : objectList) {
- workingMemory.insert(object);
- }
- }
- workingMemory.insert(message);
- logger.log(Level.DEBUG, "Fire the JBossRules Engine");
- workingMemory.fireAllRules();
- long procTime = System.nanoTime() - startTime;
- if (rulesCounter != null) {
- rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_SUCCEED);
- }
-
- logger.log(Level.DEBUG,
- "Outgoing Destinations: " + destinations);
- workingMemory.dispose();
- return destinations;
- } catch (final LifecycleResourceException lre) {
- if (rulesCounter != null) {
- long procTime = System.nanoTime() - startTime;
- rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_FAILED);
- }
- throw new MessageRouterException("Could not load lifecycle data. " + lre.getMessage(), lre);
- } catch (IOException ioe) {
- if (rulesCounter != null) {
- long procTime = System.nanoTime() - startTime;
- rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_FAILED);
- }
- throw new MessageRouterException("Could not read the rules. " + ioe.getMessage(), ioe);
- } catch (DroolsParserException dpe) {
- if (rulesCounter != null) {
- long procTime = System.nanoTime() - startTime;
- rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_FAILED);
- }
- throw new MessageRouterException("Could not parse the rules. " + dpe.getMessage(), dpe);
- } catch (CBRException cbre) {
- if (rulesCounter != null) {
- long procTime = System.nanoTime() - startTime;
- rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_FAILED);
- }
- throw new MessageRouterException("Could not parse the rules. " + cbre.getMessage(), cbre);
- }
- }
-
- /**
- * Reading the rules and dsl from files. Note that we may want to allow
- * other ways to set the rule, but for now it's just files.
- */
- private static RuleBase readRuleBase (String rulesFile, String ruleLanguage)
- throws DroolsParserException, IOException, CBRException
- {
- // read in the rules
- logger.debug("Going to read the rule: " + rulesFile);
- InputStream inputStreamDrl = ClassUtil.getResourceAsStream(rulesFile.charAt(0) == '/' ? rulesFile : "/" + rulesFile, JBossRulesRouter.class);
- if (inputStreamDrl == null)
+
+ @SuppressWarnings("unchecked")
+ List<String> destinations = (List<String>) routeMessage.getBody().get( "destinations" );
+ return destinations;
+ }
+ catch (final RuleServiceException e)
{
- logger.error("Could not find rulesFile: " + rulesFile);
- return null;
+ throw new MessageRouterException(e.getMessage(), e);
}
- else
- {
- PackageBuilderConfiguration pkgBuilderCfg = new PackageBuilderConfiguration();
- //pkgBuilderCfg.setCompiler(PackageBuilderConfiguration.JANINO);
- PackageBuilder builder = new PackageBuilder(pkgBuilderCfg);
- try
- {
- Reader rules = new InputStreamReader(inputStreamDrl);
- // this wil parse and compile in one step
- if (ruleLanguage == null)
- {
- builder.addPackageFromDrl(rules);
- }
- else
- {
- logger.debug("Going to read the language: " + ruleLanguage);
- InputStream inputStreamDsl = ClassUtil.getResourceAsStream(ruleLanguage.charAt(0) == '/' ? ruleLanguage : "/" + ruleLanguage, JBossRulesRouter.class);
- if (inputStreamDsl == null)
- {
- logger.error("Could not find ruleLanguage: " + rulesFile);
- }
- else
- {
- try
- {
- Reader dsl = new InputStreamReader(inputStreamDsl);
- builder.addPackageFromDrl(rules, dsl);
- }
- finally
- {
- safeClose(inputStreamDsl);
- }
- }
- }
- }
- finally
- {
- safeClose(inputStreamDrl);
- }
- // get the compiled package (which is serializable)
- Package pkg = builder.getPackage();
- // add the package to a rulebase (deploy the rule package).
- try
- {
- RuleBase ruleBase = RuleBaseFactory.newRuleBase();
- ruleBase.addPackage(pkg);
- return ruleBase;
- } catch (Exception ex) {
- throw new CBRException(ex.getMessage(), ex);
- }
- }
}
- /**
- * Reading the rules and dsl from files. Note that we may want to allow
- * other ways to set the rule, but for now it's just files.
- */
- private static String getRules (String rulesFile, String ruleLanguage)
- throws IOException
+ public void setConfigTree(final ConfigTree configTree)
{
- logger.debug("Going to hash the rule: " + rulesFile);
- InputStream inputStreamDrl = ClassUtil.getResourceAsStream(rulesFile.charAt(0) == '/' ? rulesFile : "/" + rulesFile, JBossRulesRouter.class);
- if (inputStreamDrl == null)
- {
- logger.error("Could not find rulesFile: " + rulesFile);
- return null;
- }
- else
- {
- final String rules ;
- try
- {
- rules = getString(inputStreamDrl);
- }
- finally
- {
- safeClose(inputStreamDrl);
- }
- String language = "";
- if (ruleLanguage != null)
- {
- logger.debug("Going to hash the language: " + ruleLanguage);
- InputStream inputStreamDsl = ClassUtil.getResourceAsStream(ruleLanguage.charAt(0) == '/' ? ruleLanguage : "/" + ruleLanguage, JBossRulesRouter.class);
- if (inputStreamDsl == null)
- {
- logger.error("Could not find language: " + ruleLanguage);
- }
- else
- {
- try
- {
- language = getString(inputStreamDsl);
- }
- finally
- {
- safeClose(inputStreamDsl);
- }
- }
- }
- return rules + language;
- }
-
+ this.configTree = configTree;
}
-
- private static void safeClose(final InputStream is)
- {
- try
- {
- is.close() ;
- }
- catch (final Throwable th) {} // ignore
- }
-
- private static String getString (InputStream in) throws IOException
- {
- StringBuffer stringBuffer = new StringBuffer();
- byte[] b = new byte[4096];
- for (int i; (i = in.read(b)) != -1;)
- {
- stringBuffer.append(new String(b, 0, i));
- }
- return stringBuffer.toString();
- }
-
- public void setConfigTree(ConfigTree configTree) {
- rulesCounter = new JBRulesCounter(configTree);
- rulesCounter.registerMBean();
- }
- /**
- * The lifecycle resource factory for rule sets.
- * @author kevin
- */
- public static class LifecycleRuleBaseFactory implements LifecycleResourceFactory<Map<String, RuleBase>>
- {
- /**
- * Create a resource object which will be associated with the specified lifecycle identity.
- * @param lifecycleIdentity The associated lifecycle identity.
- * @return The lifecycle resource
- * @throws LifecycleResourceException for errors during construction.
- */
- public Map<String, RuleBase> createLifecycleResource(final String lifecycleIdentity)
- throws LifecycleResourceException
- {
- return new ConcurrentHashMap<String, RuleBase>() ;
- }
-
- /**
- * Destroy a resource object which is associated with the specified lifecycle identity.
- * @param resource The lifecycle resource.
- * @param lifecycleIdentity The associated lifecycle identity.
- * @return The lifecycle resource.
- * @throws LifecycleResourceException for errors during destroy.
- */
- public void destroyLifecycleResource(final Map<String, RuleBase> resource,
- final String lifecycleIdentity)
- throws LifecycleResourceException
- {
- }
- }
-
- /**
- * The lifecycle resource factory for rule sets.
- * @author kevin
- */
- public static class LifecycleRuleSetFactory implements LifecycleResourceFactory<Map<String, String>>
- {
- /**
- * Create a resource object which will be associated with the specified lifecycle identity.
- * @param lifecycleIdentity The associated lifecycle identity.
- * @return The lifecycle resource
- * @throws LifecycleResourceException for errors during construction.
- */
- public Map<String, String> createLifecycleResource(final String lifecycleIdentity)
- throws LifecycleResourceException
- {
- return new ConcurrentHashMap<String, String>() ;
- }
-
- /**
- * Destroy a resource object which is associated with the specified lifecycle identity.
- * @param resource The lifecycle resource.
- * @param lifecycleIdentity The associated lifecycle identity.
- * @return The lifecycle resource.
- * @throws LifecycleResourceException for errors during destroy.
- */
- public void destroyLifecycleResource(final Map<String, String> resource,
- final String lifecycleIdentity)
- throws LifecycleResourceException
- {
- }
- }
}
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleBaseHelper.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleBaseHelper.java (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleBaseHelper.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,260 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.internal.soa.esb.services.rules;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.apache.log4j.Logger;
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.agent.RuleAgent;
+import org.drools.compiler.DroolsParserException;
+import org.drools.compiler.PackageBuilder;
+import org.drools.compiler.PackageBuilderConfiguration;
+import org.drools.decisiontable.InputType;
+import org.drools.decisiontable.SpreadsheetCompiler;
+import org.drools.rule.Package;
+import org.jboss.soa.esb.util.ClassUtil;
+
+/**
+ * A helper class, it returns rulebases based on various methods of creating them
+ *
+ * @author jdelong at redhat.com
+ *
+ */
+public class DroolsRuleBaseHelper {
+
+ private static Logger logger = Logger.getLogger(DroolsRuleBaseHelper.class);
+
+ /**
+ * Creates a rulebase using rules and dsl from files
+ * <p/>
+ *
+ * @param ruleFile -
+ * file name which contains the Drools rules. Can be a file on the local
+ * filesystem, a file on the classpath or an URL. Must not be null.
+ * @param dsl -
+ * file containing the Drools Domain Specific Language (dsl)
+ * @throws DroolsParserException -
+ * if an exception occurs during Drools package building
+ * @throws IOException -
+ * if an IOException occurs during Drools package building
+ * @throws RuleServiceException -
+ * if the ruleFile cannot be located or if it was not possible to
+ * add the package to the RuleBase.
+ */
+ public RuleBase createRuleBaseFromRuleFiles(String ruleFile, String dsl)
+ throws DroolsParserException, IOException, RuleServiceException {
+ assertNotNull( ruleFile, "ruleFile" );
+
+ logger.debug("Going to create a rulebase from the ruleFile: " + ruleFile);
+ // read in the rules
+ InputStream rulesInputStream = getRulesInputStream(ruleFile);
+
+ Reader ruleReader = getRulesReader(rulesInputStream);
+
+ PackageBuilder builder = new PackageBuilder(new PackageBuilderConfiguration());
+ try {
+ if (dsl == null) {
+ builder.addPackageFromDrl(ruleReader);
+ } else {
+ logger.debug("Going to read the dsl: " + dsl);
+ InputStream dslInputStream = getRulesInputStream(dsl);
+ Reader dslReader = getRulesReader(dslInputStream);
+ if (dslReader == null) {
+ logger.error("Could not find dsl: " + ruleFile);
+ } else {
+ try {
+ builder.addPackageFromDrl(ruleReader, dslReader);
+ } finally {
+ safeClose(dslInputStream);
+ }
+ }
+ }
+ } finally {
+ safeClose(rulesInputStream);
+ }
+
+ // get the compiled package (which is serializable)
+ Package pkg = builder.getPackage();
+ // add the package to a rulebase
+ try {
+ RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ ruleBase.addPackage(pkg);
+ return ruleBase;
+ } catch (final Exception ex) {
+ throw new RuleServiceException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Reads the rules and dsl from files and returning as a string
+ *
+ * @param ruleFile -
+ * file name which contains the Drools rules. Can be a file on the local
+ * filesystem, a file on the classpath or an URL. Must not be null.
+ * @param dsl -
+ * file containing the Drools Domain Specific Language (dsl)
+ * @return String -
+ * String representation of the rules
+ * @throws IOException -
+ * if an IOException occurs during Drools package building
+ * @throws RuleServiceException -
+ * if the ruleFile cannot be located or if it was not possible to
+ * add the package to the RuleBase.
+ * @throws RuleServiceException
+ */
+ public String getRulesAsString(String ruleFile, String dsl) throws IOException, RuleServiceException {
+ assertNotNull( ruleFile, "rulefile" );
+
+ logger.debug("Going to hash the rule: " + ruleFile);
+ InputStream rulesInputStream = getRulesInputStream(ruleFile);
+
+ final String rules;
+ try {
+ rules = getString(rulesInputStream);
+ } finally {
+ safeClose(rulesInputStream);
+ }
+
+ String language = "";
+ if (dsl != null) {
+ logger.debug("Going to hash the language: " + dsl);
+ InputStream dslInputStream = getRulesInputStream(dsl);
+ if (dslInputStream == null) {
+ logger.error("Could not find language: " + dsl);
+ } else {
+ try {
+ language = getString(dslInputStream);
+ } finally {
+ safeClose(dslInputStream);
+ }
+ }
+ }
+ return rules + language;
+ }
+
+
+ /**
+ * Reading the rules from a decision table.
+ * <p/>
+ * @param decisionTable -
+ * file name which contains the Drools decision table. Can be a file on the local
+ * filesystem, a file on the classpath or an URL. Must not be null.
+ * @throws DroolsParserException -
+ *
+ * @throws IOException -
+ *
+ * @throws RuleServiceException -
+ */
+ public RuleBase createRuleBaseFromDecisionTable(String decisionTable)
+ throws DroolsParserException, IOException, RuleServiceException {
+ assertNotNull( decisionTable, "decisionTable" );
+
+ logger.debug("Going to read the decisionTable: " + decisionTable);
+ String drl = getSpreadsheetRules(decisionTable);
+ PackageBuilder builder = new PackageBuilder();
+ builder.addPackageFromDrl(new StringReader(drl));
+ try {
+ //add the package to a rulebase (deploy the rule package).
+ RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ ruleBase.addPackage(builder.getPackage());
+ return ruleBase;
+ } catch (Exception ex) {
+ throw new RuleServiceException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Reading the decision table and creating a drl
+ * </p>
+ * @throws RuleServiceException
+ */
+ public String getSpreadsheetRules(String decisionTable) throws RuleServiceException {
+ logger.debug("Going to read the decisionTable: " + decisionTable);
+ String drl;
+ InputStream inputStreamDT = getRulesInputStream(decisionTable);
+ SpreadsheetCompiler compiler = new SpreadsheetCompiler();
+
+ try {
+ drl = compiler.compile(inputStreamDT, InputType.XLS);
+ logger.debug("Compiled the decsionTable to a drl: " + drl);
+ } finally {
+ safeClose(inputStreamDT);
+ }
+ return drl;
+ }
+
+ /**
+ * This shows how rules are loaded up from a deployed package.
+ */
+ public RuleBase loadRuleBaseFromRuleAgent(String ruleAgentProperties) throws IOException, Exception {
+
+ logger.debug("Going to read the ruleAgentProperties: " + ruleAgentProperties);
+ RuleAgent agent = RuleAgent.newRuleAgent("/" + ruleAgentProperties);
+ RuleBase rb = agent.getRuleBase();
+
+ return rb;
+ }
+
+ // private instance methods
+
+ private Reader getRulesReader(InputStream inputStream) {
+ return new InputStreamReader(inputStream);
+ }
+
+ private InputStream getRulesInputStream(String rulesFile) throws RuleServiceException {
+ InputStream resourceAsStream = ClassUtil.getResourceAsStream( "/" + rulesFile, getClass() );
+ if ( resourceAsStream == null )
+ throw new RuleServiceException("Could not locate file [" + rulesFile + "], neither as a file on the local filesystem, on the classpath nor as a URL.");
+ else
+ return resourceAsStream;
+ }
+
+ // private static methods
+
+ private static void safeClose(final InputStream is) {
+ try {
+ is.close();
+ } catch (final Throwable th) {
+ } // ignore
+ }
+
+ private static String getString(InputStream in) throws IOException {
+ StringBuilder stringBuilder = new StringBuilder();
+ byte[] b = new byte[4096];
+ for (int i; (i = in.read(b)) != -1;) {
+ stringBuilder.append(new String(b, 0, i));
+ }
+ return stringBuilder.toString();
+ }
+
+ private static void assertNotNull( final String argumentValue, final String argumentName ) {
+ if( argumentValue == null )
+ throw new NullPointerException("[" + argumentName + "] argument must not be null");
+ }
+
+}
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleService.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleService.java (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleService.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,828 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.internal.soa.esb.services.rules;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.Level;
+import org.drools.RuleBase;
+import org.drools.StatefulSession;
+import org.drools.StatelessSession;
+import org.drools.WorkingMemory;
+import org.drools.compiler.DroolsParserException;
+import org.drools.rule.Package;
+import org.jboss.internal.soa.esb.services.routing.cbr.JBRulesCounter;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.lifecycle.LifecyclePriorities;
+import org.jboss.soa.esb.lifecycle.LifecycleResource;
+import org.jboss.soa.esb.lifecycle.LifecycleResourceException;
+import org.jboss.soa.esb.lifecycle.LifecycleResourceFactory;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.services.rules.RuleService;
+
+/**
+ * The Implementation of a rule engine interface for rules services. Here we use
+ * JBossRules (aka Drools). We keep a HashMap of stateful session working
+ * memories.
+ *
+ * @author jdelong at redhat.com
+ *
+ */
+public class DroolsRuleService implements RuleService {
+
+ private static final String MESSAGE = "message";
+ private static final String DESTINATIONS = "destinations";
+
+ /**
+ * The lifecycle resource factory.
+ */
+ private static final LifecycleResourceFactory<Map<String, RuleBase>> lifecycleRuleBaseFactory = new LifecycleRuleBaseFactory();
+ /**
+ * Lifecycle couriers.
+ */
+ private static final LifecycleResource<Map<String, RuleBase>> lifecycleRuleBases = new LifecycleResource<Map<String, RuleBase>>(
+ lifecycleRuleBaseFactory, LifecyclePriorities.RULE_BASE_PRIORITY);
+ /**
+ * The lifecycle resource factory.
+ */
+ private static final LifecycleResourceFactory<Map<String, String>> lifecycleRuleSetFactory = new LifecycleRuleSetFactory();
+ /**
+ * Lifecycle couriers.
+ */
+ private static final LifecycleResource<Map<String, String>> lifecycleRuleSets = new LifecycleResource<Map<String, String>>(
+ lifecycleRuleSetFactory, LifecyclePriorities.RULE_BASE_PRIORITY);
+
+ private static Logger logger = Logger.getLogger(DroolsRuleService.class);
+
+ private JBRulesCounter rulesCounter = null;
+
+ /**
+ * Execute rules using a certain ruleSet and domain specific language using
+ * the Stateless rule engine API
+ *
+ * @param ruleSet -
+ * String reference to a file which contains a ruleSet.
+ * @param dsl -
+ * String reference to a file which contains a custom rule
+ * language definition
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading
+ * the ruleSet.
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList -
+ * a list with additional objects (typically pulled from the
+ * message) to be inserted into working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatelessRules(String ruleSet, String dsl,
+ boolean ruleReload, Message message, List<Object> objectList)
+ throws RuleServiceException
+
+ {
+ long startTime = System.nanoTime();
+
+ try {
+ RuleBase ruleBase = getRuleBaseForFileBasedRules(ruleSet, dsl,
+ ruleReload);
+ message = executeStatelessRules(ruleBase, message, objectList);
+
+ long procTime = System.nanoTime() - startTime;
+ if (rulesCounter != null) {
+ rulesCounter.update(procTime, ruleSet,
+ JBRulesCounter.RULES_SUCCEED);
+ }
+
+ return message;
+
+ } catch (RuleServiceException ree) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleSet,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + ree.getMessage(), ree);
+ }
+ }
+
+ /**
+ * Execute rules from a decision table using the Stateless rule engine API
+ *
+ * @param decisionTable -
+ * String reference to a file which contains a spreadsheet of rules
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading
+ * the ruleSet.
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatelessRulesFromDecisionTable(String decisionTable,
+ boolean ruleReload, Message message, List<Object> objectList)
+ throws RuleServiceException {
+ long startTime = System.nanoTime();
+
+ try {
+ RuleBase ruleBase = getRuleBaseForDecisionTable(decisionTable,
+ ruleReload);
+ message = executeStatelessRules(ruleBase, message, objectList);
+
+ long procTime = System.nanoTime() - startTime;
+ if (rulesCounter != null) {
+ rulesCounter.update(procTime, decisionTable,
+ JBRulesCounter.RULES_SUCCEED);
+ }
+
+ return message;
+
+ } catch (RuleServiceException ree) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, decisionTable,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + ree.getMessage(), ree);
+ }
+ }
+
+ /**
+ * Execute rules using a rule package retrieved via the Rule Agent from a URL of local file system
+ * using the Stateless rule engine API
+ *
+ * @param ruleAgentProperties -
+ * String reference to a file which contains properties used by the RuleAgent to locate a rule package.
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatelessRulesFromRuleAgent(
+ String ruleAgentProperties, Message message, List<Object> objectList)
+ throws RuleServiceException {
+ long startTime = System.nanoTime();
+ try {
+ DroolsRuleBaseHelper rbHelper = new DroolsRuleBaseHelper();
+ RuleBase ruleBase = rbHelper
+ .loadRuleBaseFromRuleAgent(ruleAgentProperties);
+ final Map<String, RuleBase> ruleBases = lifecycleRuleBases
+ .getLifecycleResource();
+ if (ruleBase != null) {
+ ruleBases.put(ruleAgentProperties, ruleBase);
+
+ message = executeStatelessRules(ruleBase, message, objectList);
+
+ long procTime = System.nanoTime() - startTime;
+ if (rulesCounter != null) {
+ rulesCounter.update(procTime, ruleAgentProperties,
+ JBRulesCounter.RULES_SUCCEED);
+ }
+ }
+
+ return message;
+
+ } catch (IOException ioe) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleAgentProperties,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException(
+ "Could not read the ruleAgentProperties. "
+ + ioe.getMessage(), ioe);
+ } catch (RuleServiceException ree) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleAgentProperties,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + ree.getMessage(), ree);
+ } catch (Exception e) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleAgentProperties,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException(
+ "RuleAgent could not get the RuleBase. " + e.getMessage(),
+ e);
+ }
+
+ }
+
+ /**
+ * Execute rules using a certain ruleSet and domain specific language using the Stateful rule engine API
+ *
+ * @param ruleSet -
+ * String reference to a file which contains a ruleSet.
+ * @param domainSpecificLanguage -
+ * String reference to a file which contains a custom rule
+ * language definition
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading
+ * the ruleSet.
+ * @param dispose -
+ * if set to true, working memory will be disposed after the rules are fired
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatefulRules(String ruleSet, String dsl,
+ boolean ruleReload, boolean dispose, Message message,
+ List<Object> objectList) throws RuleServiceException {
+ long startTime = System.nanoTime();
+
+ try {
+ RuleBase ruleBase = getRuleBaseForFileBasedRules(ruleSet, dsl,
+ ruleReload);
+ message = executeStatefulRules(ruleBase, dispose, message,
+ objectList);
+
+ long procTime = System.nanoTime() - startTime;
+ if (rulesCounter != null) {
+ rulesCounter.update(procTime, ruleSet,
+ JBRulesCounter.RULES_SUCCEED);
+ }
+
+ return message;
+
+ } catch (RuleServiceException ree) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleSet,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + ree.getMessage(), ree);
+ }
+ }
+
+ /**
+ * Execute rules from a decision table using the Stateful rule engine API
+ *
+ * @param decisionTable -
+ * String reference to a file which contains a spreadsheet of rules
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading
+ * the ruleSet.
+ * @param dispose -
+ * if set to true, working memory will be disposed after the rules are fired
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatefulRulesFromDecisionTable(String decisionTable,
+ boolean ruleReload, boolean dispose, Message message,
+ List<Object> objectList) throws RuleServiceException {
+ long startTime = System.nanoTime();
+
+ try {
+ RuleBase ruleBase = getRuleBaseForDecisionTable(decisionTable, ruleReload);
+ message = executeStatefulRules(ruleBase, dispose, message, objectList);
+
+ long procTime = System.nanoTime() - startTime;
+ if (rulesCounter != null) {
+ rulesCounter.update(procTime, decisionTable,
+ JBRulesCounter.RULES_SUCCEED);
+ }
+
+ return message;
+
+ } catch (RuleServiceException ree) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, decisionTable,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + ree.getMessage(), ree);
+ }
+ }
+
+ /**
+ * Execute rules using a rule package retrieved via the Rule Agent from a URL of local file system
+ * using the Stateful rule engine API
+ *
+ * @param ruleAgentProperties -
+ * String reference to a file which contains properties used by the RuleAgent to locate a rule package.
+ * @param dispose -
+ * if set to true, working memory will be disposed after the rules are fired
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatefulRulesFromRuleAgent(
+ String ruleAgentProperties, boolean dispose, Message message,
+ List<Object> objectList) throws RuleServiceException {
+ long startTime = System.nanoTime();
+ try {
+ DroolsRuleBaseHelper rbHelper = new DroolsRuleBaseHelper();
+ RuleBase ruleBase = rbHelper
+ .loadRuleBaseFromRuleAgent(ruleAgentProperties);
+
+ final Map<String, RuleBase> ruleBases = lifecycleRuleBases
+ .getLifecycleResource();
+ if (ruleBase != null) {
+ ruleBases.put(ruleAgentProperties, ruleBase);
+
+ message = executeStatefulRules(ruleBase, dispose, message,
+ objectList);
+
+ long procTime = System.nanoTime() - startTime;
+ if (rulesCounter != null) {
+ rulesCounter.update(procTime, ruleAgentProperties,
+ JBRulesCounter.RULES_SUCCEED);
+ }
+ }
+
+ return message;
+
+ } catch (IOException ioe) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleAgentProperties,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException(
+ "Could not read the ruleAgentProperties. "
+ + ioe.getMessage(), ioe);
+ } catch (RuleServiceException ree) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleAgentProperties,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + ree.getMessage(), ree);
+ } catch (Exception e) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleAgentProperties,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException(
+ "RuleAgent could not get the RuleBase. " + e.getMessage(),
+ e);
+ }
+
+ }
+
+ /**
+ * Continue executing rules where the working memory already exists.
+ *
+ * @param rules -
+ * String reference to a file, either the drl file, the decision table,
+ * or the ruleAgentProperties. This is used to find the ruleBase.
+ * @param dispose -
+ * if set to true, working memory will be disposed after the rules are fired
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message continueStatefulRulesExecution(String rules,
+ boolean dispose, Message message, List<Object> objectList)
+ throws RuleServiceException {
+ long startTime = System.nanoTime();
+ try {
+ final Map<String, RuleBase> ruleBases = lifecycleRuleBases
+ .getLifecycleResource();
+
+ RuleBase ruleBase = ruleBases.get(rules);
+ StatefulSession[] workingMemories = ruleBase.getStatefulSessions();
+ // there should only be one created from this ruleBase?
+ StatefulSession workingMemory = workingMemories[0];
+ logger
+ .log(Level.DEBUG, "Executing rules with RuleBase="
+ + ruleBase);
+
+ workingMemory.setGlobal(MESSAGE, message);
+
+ if (objectList != null) {
+ for (Object object : objectList) {
+ workingMemory.insert(object);
+ }
+ }
+
+ // workingMemory.insert(message);
+ logger.log(Level.DEBUG, "Fire the JBossRules Engine");
+ workingMemory.fireAllRules();
+
+ logger.log(Level.DEBUG, "Updated message: " + message);
+
+ if (dispose) {
+ workingMemory.dispose();
+ }
+
+ long procTime = System.nanoTime() - startTime;
+ if (rulesCounter != null) {
+ rulesCounter.update(procTime, rules,
+ JBRulesCounter.RULES_SUCCEED);
+ }
+
+ return message;
+
+ } catch (Exception e) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, rules,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException(
+ "Could not continue rule execution. " + e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Determine if file based rules need reloading and return the rulebase
+ *
+ * @param ruleSet -
+ * String reference to a file which contains a ruleSet.
+ * @param dsl -
+ * String reference to a file which contains a custom rule language
+ * definition
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading the
+ * ruleSet.
+ *
+ * @return Message with updated objects.
+ */
+ protected RuleBase getRuleBaseForFileBasedRules(String ruleSet, String dsl,
+ boolean ruleReload) throws RuleServiceException {
+ long startTime = System.nanoTime();
+ try {
+ DroolsRuleBaseHelper rbHelper = new DroolsRuleBaseHelper();
+
+ String newRuleSet = null;
+ boolean isRulesChanged = false;
+
+ final Map<String, String> ruleSets = lifecycleRuleSets
+ .getLifecycleResource();
+ if (ruleReload) {
+ String currentRuleSet = ruleSets.get(ruleSet);
+ newRuleSet = rbHelper.getRulesAsString(ruleSet, dsl);
+ if (currentRuleSet == null
+ || !currentRuleSet.equals(newRuleSet)) {
+ isRulesChanged = true;
+ }
+
+ }
+ final Map<String, RuleBase> ruleBases = lifecycleRuleBases
+ .getLifecycleResource();
+ RuleBase ruleBase = ruleBases.get(ruleSet);
+ if (ruleBase == null || isRulesChanged) {
+ logger.log(Level.DEBUG, "Reading ruleSet from file=" + ruleSet);
+ ruleBase = rbHelper.createRuleBaseFromRuleFiles(ruleSet, dsl);
+ if (ruleBase != null)
+ ruleBases.put(ruleSet, ruleBase);
+ if (newRuleSet == null) {
+ newRuleSet = rbHelper.getRulesAsString(ruleSet, dsl);
+ }
+ if (ruleSet != null)
+ ruleSets.put(ruleSet, newRuleSet);
+ }
+
+ // this should really be a RULES_COMPILED status, and processing time should be compilationTime
+ // long procTime = System.nanoTime() - startTime;
+ // if (rulesCounter != null) {
+ // rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_SUCCEED);
+ // }
+
+ return ruleBase;
+
+ } catch (final LifecycleResourceException lre) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleSet,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not load lifecycle data. "
+ + lre.getMessage(), lre);
+ } catch (IOException ioe) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleSet,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not read the rules. "
+ + ioe.getMessage(), ioe);
+ } catch (DroolsParserException dpe) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleSet,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + dpe.getMessage(), dpe);
+ } catch (RuleServiceException ree) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, ruleSet,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + ree.getMessage(), ree);
+ }
+
+ }
+
+ /**
+ * Determine if decision table need reloading and return the rulebase
+ *
+ * @param ruleSet -
+ * String reference to a file which contains a ruleSet.
+ * @param dsl -
+ * String reference to a file which contains a custom rule language
+ * definition
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading the
+ * ruleSet.
+ *
+ * @return Message with updated objects.
+ */
+ protected RuleBase getRuleBaseForDecisionTable(String decisionTable,
+ boolean ruleReload) throws RuleServiceException {
+ long startTime = System.nanoTime();
+ try {
+ DroolsRuleBaseHelper rbHelper = new DroolsRuleBaseHelper();
+ RuleBase ruleBase;
+ if (ruleReload) {
+ String drl = rbHelper.getSpreadsheetRules(decisionTable);
+ ruleBase = getRuleBaseForFileBasedRules(drl, null, ruleReload);
+ } else {
+ ruleBase = rbHelper
+ .createRuleBaseFromDecisionTable(decisionTable);
+ }
+ // this should really be a RULES_COMPILED status, and processing time should be compilationTime
+ // long procTime = System.nanoTime() - startTime;
+ // if (rulesCounter != null) {
+ // rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_SUCCEED);
+ // }
+ return ruleBase;
+
+ } catch (IOException ioe) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, decisionTable,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not read the rules. "
+ + ioe.getMessage(), ioe);
+ } catch (DroolsParserException dpe) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, decisionTable,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + dpe.getMessage(), dpe);
+ } catch (RuleServiceException ree) {
+ if (rulesCounter != null) {
+ long procTime = System.nanoTime() - startTime;
+ rulesCounter.update(procTime, decisionTable,
+ JBRulesCounter.RULES_FAILED);
+ }
+ throw new RuleServiceException("Could not parse the rules. "
+ + ree.getMessage(), ree);
+ }
+ }
+
+ /**
+ * Execute rules using using the Stateless API
+ *
+ * @param rulebase -
+ * the rulebase to use
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList -
+ * a list with additional objects (typically pulled from the message)
+ * to be inserted into working memory
+ *
+ * @return Message -
+ * with updated objects.
+ */
+ @SuppressWarnings("unchecked")
+ protected Message executeStatelessRules(
+ final RuleBase ruleBase,
+ final Message message,
+ final List<Object> objectList) {
+
+ StatelessSession workingMemory = ruleBase.newStatelessSession();
+
+ List<Object> facts = objectList != null ? new ArrayList(objectList) : new ArrayList();
+
+ // always add the message to the facts
+ facts.add(message);
+
+ workingMemory.setGlobal(MESSAGE, message);
+
+ List<String> destinations = new ArrayList<String>();
+ workingMemory.setGlobal(DESTINATIONS, destinations);
+
+ workingMemory.execute(facts);
+
+ message.getBody().add( DESTINATIONS, destinations );
+
+ return message;
+ }
+
+
+
+ /**
+ * Execute rules using using the Stateful API
+ *
+ * @param rulebase -
+ * the rulebase to use
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList -
+ * a list with additional objects (typically pulled from the message)
+ * to be inserted into working memory
+ *
+ * @return Message with updated objects.
+ */
+ protected Message executeStatefulRules(
+ final RuleBase ruleBase,
+ final boolean dispose,
+ final Message message,
+ final List<Object> objectList) {
+
+ StatefulSession statefulSession = ruleBase.newStatefulSession();
+
+ Map<String,Class> globals = getAllGlobals( ruleBase );
+ if ( globals.containsKey( MESSAGE ) )
+ statefulSession.setGlobal(MESSAGE, message);
+
+ statefulSession.insert( message );
+
+ List<String> destinations = new ArrayList<String>();
+ boolean useDestinations = globals.containsKey( DESTINATIONS );
+ if ( useDestinations )
+ statefulSession.setGlobal( DESTINATIONS, destinations );
+
+ insertObjectsIntoWorkingMemory( objectList, statefulSession );
+
+ statefulSession.fireAllRules();
+
+ if ( useDestinations )
+ message.getBody().add( DESTINATIONS, destinations );
+
+ if ( dispose ) {
+ statefulSession.dispose();
+ }
+
+ return message;
+ }
+
+ public void setConfigTree(ConfigTree configTree) {
+ rulesCounter = new JBRulesCounter(configTree);
+ rulesCounter.registerMBean();
+ }
+
+ /**
+ * The lifecycle resource factory for rule sets.
+ *
+ * @author kevin
+ */
+ public static class LifecycleRuleBaseFactory implements
+ LifecycleResourceFactory<Map<String, RuleBase>> {
+ /**
+ * Create a resource object which will be associated with the
+ * specified lifecycle identity.
+ *
+ * @param lifecycleIdentity
+ * The associated lifecycle identity.
+ * @return The lifecycle resource
+ * @throws LifecycleResourceException
+ * for errors during construction.
+ */
+ public Map<String, RuleBase> createLifecycleResource(
+ final String lifecycleIdentity)
+ throws LifecycleResourceException {
+ return new ConcurrentHashMap<String, RuleBase>();
+ }
+
+ /**
+ * Destroy a resource object which is associated with the specified
+ * lifecycle identity.
+ *
+ * @param resource
+ * The lifecycle resource.
+ * @param lifecycleIdentity
+ * The associated lifecycle identity.
+ * @return The lifecycle resource.
+ * @throws LifecycleResourceException
+ * for errors during destroy.
+ */
+ public void destroyLifecycleResource(
+ final Map<String, RuleBase> resource,
+ final String lifecycleIdentity)
+ throws LifecycleResourceException {
+ }
+ }
+
+ /**
+ * The lifecycle resource factory for rule sets.
+ *
+ * @author kevin
+ */
+ public static class LifecycleRuleSetFactory implements
+ LifecycleResourceFactory<Map<String, String>> {
+ /**
+ * Create a resource object which will be associated with the
+ * specified lifecycle identity.
+ *
+ * @param lifecycleIdentity
+ * The associated lifecycle identity.
+ * @return The lifecycle resource
+ * @throws LifecycleResourceException
+ * for errors during construction.
+ */
+ public Map<String, String> createLifecycleResource(
+ final String lifecycleIdentity)
+ throws LifecycleResourceException {
+ return new ConcurrentHashMap<String, String>();
+ }
+
+ /**
+ * Destroy a resource object which is associated with the specified
+ * lifecycle identity.
+ *
+ * @param resource
+ * The lifecycle resource.
+ * @param lifecycleIdentity
+ * The associated lifecycle identity.
+ * @return The lifecycle resource.
+ * @throws LifecycleResourceException
+ * for errors during destroy.
+ */
+ public void destroyLifecycleResource(
+ final Map<String, String> resource,
+ final String lifecycleIdentity)
+ throws LifecycleResourceException {
+ }
+ }
+
+ private Map<String,Class> getAllGlobals( final RuleBase ruleBase )
+ {
+ Map<String,Class> globals = new HashMap<String,Class>();
+ for (Package pack : ruleBase.getPackages() )
+ {
+ globals.putAll( pack.getGlobals() );
+ }
+ return globals;
+ }
+
+ private void insertObjectsIntoWorkingMemory(final List<Object> objectList, final WorkingMemory workingMemory)
+ {
+ if (objectList != null) {
+ for (Object object : objectList) {
+ workingMemory.insert(object);
+ }
+ }
+ }
+}
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceBuilderException.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceBuilderException.java (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceBuilderException.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,74 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2006, JBoss Inc., and
+ * individual contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of individual
+ * contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.internal.soa.esb.services.rules;
+
+import org.drools.compiler.PackageBuilderErrors;
+
+/**
+ *
+ * @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
+ *
+ */
+public class RuleServiceBuilderException extends RuleServiceException
+{
+ private static final long serialVersionUID = 1L;
+
+ private PackageBuilderErrors builderErrors;
+
+ public RuleServiceBuilderException()
+ {
+ super();
+ }
+
+ public RuleServiceBuilderException(String msg, Throwable cause)
+ {
+ super( msg, cause );
+ }
+
+ public RuleServiceBuilderException(String msg)
+ {
+ super( msg );
+ }
+
+ public RuleServiceBuilderException(Throwable cause)
+ {
+ super( cause );
+ }
+
+ public RuleServiceBuilderException(String msg, PackageBuilderErrors builderErrors)
+ {
+ super( msg );
+ this.builderErrors = builderErrors;
+ }
+
+ public RuleServiceBuilderException(String msg, Throwable cause, PackageBuilderErrors builderErrors)
+ {
+ super( msg, cause );
+ this.builderErrors = builderErrors;
+ }
+
+ public PackageBuilderErrors getBuilderErrors()
+ {
+ return builderErrors;
+ }
+
+}
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceException.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceException.java (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceException.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.internal.soa.esb.services.rules;
+
+/**
+ * @author jdelong at redhat.com
+ *
+ */
+public class RuleServiceException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public RuleServiceException() {
+ super();
+ }
+
+ public RuleServiceException(String msg) {
+ super(msg);
+ }
+
+ public RuleServiceException(Throwable cause) {
+ super(cause);
+ }
+
+ public RuleServiceException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+}
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceFactory.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceFactory.java (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/rules/RuleServiceFactory.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,62 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.internal.soa.esb.services.rules;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.services.rules.RuleService;
+import org.jboss.soa.esb.util.ClassUtil;
+/**
+ * Returns an instance of the RuleService.
+ *
+ * @author jdelong at redhat.com
+ *
+ */
+public class RuleServiceFactory {
+ private static Logger logger = Logger.getLogger(RuleServiceFactory.class);
+
+ /**
+ * This factory returns a ruleService
+ *
+ * @author jdelong at redhat.com
+ *
+ */
+ public static RuleService getRuleService(String className)
+ throws RuleServiceException {
+ RuleService rs = null;
+ logger.log(Level.DEBUG, "Going to load " + className);
+ try {
+ // instruct class loader to load the RuleService Implementation
+ Class rsClass = ClassUtil.forName(className,
+ RuleServiceFactory.class);
+ // Try to get an instance of the RS
+ rs = (RuleService) rsClass.newInstance();
+ } catch (ClassNotFoundException cnfex) {
+ throw new RuleServiceException("RuleService Implementation="
+ + className + " not found", cnfex);
+ } catch (Exception e) {
+ throw new RuleServiceException(className + " invocation problem. "
+ + e.getLocalizedMessage(), e);
+ }
+ return rs;
+ }
+}
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/ListenerTagNames.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/ListenerTagNames.java (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/ListenerTagNames.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+/**
+ * Contains tag names as they can be used in the configuration.
+ *
+ */
+package org.jboss.soa.esb.services.rules;
+
+/**
+ * Add several tag names for the RuleService.
+ * These could be added to org.jboss.soa.esb.listeners.ListenerTagNames;
+ *
+ * @author jdelong at redhat.com
+ *
+ */
+
+public class ListenerTagNames {
+
+ /** RuleSet reference (Filename) */
+
+ // tag names, need to be moved to ListernTagNames in the org.jboss.soa.esb.listeners.ListenerTagNames;
+ public static final String RS_CLASS = "rsClass";
+ public static final String RULE_SET_TAG = "ruleSet";
+ public static final String RULE_LANGUAGE_TAG = "ruleLanguage";
+ public static final String RULE_RELOAD_TAG = "ruleReload";
+ public static final String DECISION_TABLE_TAG = "decisionTable";
+ public static final String RULE_AGENT_PROPERTIES_TAG = "ruleAgentProperties";
+ public static final String DISPOSE_TAG = "dispose";
+ public static final String CONTINUE_TAG = "continue";
+ public static final String STATEFUL_TAG = "stateful";
+}
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/RuleService.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/RuleService.java (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/main/java/org/jboss/soa/esb/services/rules/RuleService.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,182 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.services.rules;
+
+import java.util.List;
+
+import org.jboss.internal.soa.esb.services.rules.RuleServiceException;
+import org.jboss.soa.esb.helpers.ConfigTree;
+
+import org.jboss.soa.esb.message.Message;
+
+/**
+ * RulesEngine Interface. Separates implementation from the runtime, allowing
+ * the ESB to support different implementations of rule engines.
+ *
+ * @author jdelong at redhat.com
+ *
+ */
+public interface RuleService {
+ public void setConfigTree(ConfigTree configTree);
+
+ /**
+ * Execute rules using a certain ruleSet and domain specific language using the Stateless rule engine API
+ *
+ * @param ruleSet -
+ * String reference to a file which contains a ruleSet.
+ * @param dsl -
+ * String reference to a file which contains a custom rule
+ * language definition
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading
+ * the ruleSet.
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatelessRules(String ruleSet, String dsl,
+ boolean ruleReload, Message message, List<Object> objectList)
+ throws RuleServiceException;
+
+ /**
+ * Execute rules from a decision table using the Stateless rule engine API
+ *
+ * @param decisionTable -
+ * String reference to a file which contains a spreadsheet of rules
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading
+ * the ruleSet.
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatelessRulesFromDecisionTable(String decisionTable,
+ boolean ruleReload, Message message, List<Object> objectList)
+ throws RuleServiceException;
+
+ /**
+ * Execute rules using a rule package retrieved via the Rule Agent from a URL of local file system
+ * using the Stateless rule engine API
+ *
+ * @param ruleAgentProperties -
+ * String reference to a file which contains properties used by the RuleAgent to locate a rule package.
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatelessRulesFromRuleAgent(
+ String ruleAgentProperties, Message message, List<Object> objectList)
+ throws RuleServiceException;
+
+ /**
+ * Execute rules using a certain ruleSet and domain specific language using the Stateful rule engine API
+ *
+ * @param ruleSet -
+ * String reference to a file which contains a ruleSet.
+ * @param dsl -
+ * String reference to a file which contains a custom rule
+ * language definition
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading
+ * the ruleSet.
+ * @param dispose -
+ * if set to true, working memory will be disposed after the rules are fired
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatefulRules(String ruleSet, String dsl,
+ boolean ruleReload, boolean dispose, Message message,
+ List<Object> objectList) throws RuleServiceException;
+
+ /**
+ * Execute rules from a decision table using the Stateful rule engine API
+ *
+ * @param decisionTable -
+ * String reference to a file which contains a spreadsheet of rules
+ * @param ruleReload -
+ * if set to true, a ruleSet update should result in reloading
+ * the ruleSet.
+ * @param dispose -
+ * if set to true, working memory will be disposed after the rules are fired
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatefulRulesFromDecisionTable(String decisionTable,
+ boolean ruleReload, boolean dispose, Message message,
+ List<Object> objectList) throws RuleServiceException;
+
+ /**
+ * Execute rules using a rule package retrieved via the Rule Agent from a URL of local file system
+ * using the Stateful rule engine API
+ *
+ * @param ruleAgentProperties -
+ * String reference to a file which contains properties used by the RuleAgent to locate a rule package.
+ * @param dispose -
+ * if set to true, working memory will be disposed after the rules are fired
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message)t o be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message executeStatefulRulesFromRuleAgent(
+ String ruleAgentProperties, boolean dispose, Message message,
+ List<Object> objectList) throws RuleServiceException;
+
+ /**
+ * Continue executing rules where the working memory already exists.
+ *
+ * @param rules -
+ * String reference to a file, either the drl file, the decision table,
+ * or the ruleAgentProperties. This is used to find the ruleBase.
+ * @param dispose -
+ * if set to true, working memory will be disposed after the rules are fired
+ * @param message -
+ * Message that is updated with the results.
+ * @param objectList - a list with additional objects (typically pulled from the message) to be inserted into
+ * working memory
+ *
+ * @return Message with updated objects.
+ */
+ public Message continueStatefulRulesExecution(String rules,
+ boolean dispose, Message message, List<Object> objectList)
+ throws RuleServiceException;
+
+}
Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/BusinessProcessorRoutingUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/BusinessProcessorRoutingUnitTest.java 2008-06-12 06:00:37 UTC (rev 20433)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/BusinessProcessorRoutingUnitTest.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -30,6 +30,7 @@
import junit.framework.JUnit4TestAdapter;
+import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.jboss.soa.esb.message.format.MessageType;
@@ -61,6 +62,7 @@
List<Object> objectList = mapper.createObjectList(message, messagePathList);
//Now send to the rules engine
JBossRulesRouter jbossRulesRouter = new JBossRulesRouter();
+ jbossRulesRouter.setConfigTree( new ConfigTree("dummy") );
try {
List<String> destinations = jbossRulesRouter.route("JBossESBPricingRoutingRules.drl", false, message, objectList);
assertEquals(order.getDiscount(),10.0);
@@ -92,6 +94,7 @@
List<Object> objectList = mapper.createObjectList(message, messagePathList);
//Now send to the rules engine
JBossRulesRouter jbossRulesRouter = new JBossRulesRouter();
+ jbossRulesRouter.setConfigTree( new ConfigTree("dummy") );
try {
List<String> destinations = jbossRulesRouter.route("JBossESBPricingRoutingRules.drl", false, message, objectList);
assertEquals(order.getDiscount(),0.0);
Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/BusinessProcessorUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/BusinessProcessorUnitTest.java 2008-06-12 06:00:37 UTC (rev 20433)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/BusinessProcessorUnitTest.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -30,6 +30,7 @@
import junit.framework.JUnit4TestAdapter;
+import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.jboss.soa.esb.message.format.MessageType;
@@ -61,6 +62,7 @@
List<Object> objectList = mapper.createObjectList(message, messagePathList);
//Now send to the rules engine
JBossRulesRouter jbossRulesRouter = new JBossRulesRouter();
+ jbossRulesRouter.setConfigTree( new ConfigTree("dummy") );
try {
jbossRulesRouter.route("JBossESBPricingRules.drl", false, message, objectList);
assertEquals(order.getDiscount(),10.0);
Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouterUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouterUnitTest.java 2008-06-12 06:00:37 UTC (rev 20433)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouterUnitTest.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -28,6 +28,7 @@
import junit.framework.JUnit4TestAdapter;
+import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.jboss.soa.esb.message.format.MessageType;
@@ -56,6 +57,7 @@
message.getAttachment().addItem(new String("TEST ATTACHMENT2"));
JBossRulesRouter jbossRulesRouter = new JBossRulesRouter();
+ jbossRulesRouter.setConfigTree( new ConfigTree("dummy") );
try {
List<String> destinationServices = jbossRulesRouter.route("JBossESBRules.drl", false, message, null);
assertEquals(destinationServices.iterator().next(),"serialized-destination");
@@ -82,6 +84,7 @@
message.getAttachment().addItem(new String("TEST ATTACHMENT2"));
JBossRulesRouter jbossRulesRouter = new JBossRulesRouter();
+ jbossRulesRouter.setConfigTree( new ConfigTree("dummy") );
try {
List<String> destinationServices = jbossRulesRouter.route("JBossESBRules.drl", false, message, null);
assertEquals(destinationServices.iterator().next(),"xml-destination");
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleBaseHelperUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleBaseHelperUnitTest.java (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/internal/soa/esb/services/rules/DroolsRuleBaseHelperUnitTest.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,112 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2006, JBoss Inc., and
+ * individual contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of individual
+ * contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.internal.soa.esb.services.rules;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.drools.RuleBase;
+import org.drools.compiler.DroolsParserException;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link DroolsRuleBaseHelper}
+ * <p/>
+ *
+ * @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
+ *
+ */
+public class DroolsRuleBaseHelperUnitTest
+{
+ // instance under test
+ private DroolsRuleBaseHelper helper = new DroolsRuleBaseHelper();
+
+ // rule file shared by multiple tests methods
+ private final static String RULE_FILE = "RuleBaseHelper.drl";
+
+ // decision table file shared by multiple test methods
+ private final static String DECISION_TABLE_FILE = "RuleBaseHelper.xls";
+
+ private final static String NULL_DSL_FILE = null;
+ private final static String NULL_RULE_FILE = null;
+ private final static String NULL_DECISION_TABLE_FILE = null;
+
+ @Test ( expected = NullPointerException.class )
+ public void shouldThrowIfRuleFileIsNull() throws DroolsParserException, IOException, RuleServiceException
+ {
+ helper.createRuleBaseFromRuleFiles( NULL_RULE_FILE, NULL_DSL_FILE );
+ }
+
+ @Test
+ public void createRuleBaseFromRuleFiles() throws DroolsParserException, IOException, RuleServiceException
+ {
+ RuleBase ruleBase = helper.createRuleBaseFromRuleFiles( RULE_FILE, NULL_DSL_FILE );
+
+ assertNotNull( ruleBase );
+ assertTrue( ruleBase.getPackages().length > 0 );
+ assertEquals( "org.jboss.internal.soa.esb.rules" , ruleBase.getPackages()[0].getName());
+ }
+
+ @Test ( expected = NullPointerException.class )
+ public void shouldThrowIfDecisionTableIsNull() throws DroolsParserException, IOException, RuleServiceException
+ {
+ helper.createRuleBaseFromDecisionTable( NULL_DECISION_TABLE_FILE );
+ }
+
+ @Test
+ public void createRuleBaseFromDecisionTable() throws DroolsParserException, IOException, RuleServiceException
+ {
+ RuleBase ruleBase = helper.createRuleBaseFromDecisionTable( DECISION_TABLE_FILE );
+ assertNotNull( ruleBase );
+ assertEquals( "org.jboss.internal.soa.esb.rules", ruleBase.getPackages()[0].getName());
+ }
+
+ @Test ( expected = NullPointerException.class )
+ public void getRuleAsStringShouldThrowIfRuleFileIsNull() throws IOException, RuleServiceException
+ {
+ String rulesAsString = helper.getRulesAsString( NULL_RULE_FILE, NULL_DSL_FILE );
+ assertNotNull( rulesAsString );
+ }
+
+ @Test
+ public void getRuleFileAsString() throws IOException, RuleServiceException
+ {
+ String rulesAsString = helper.getRulesAsString( RULE_FILE, NULL_DSL_FILE );
+ assertNotNull( rulesAsString );
+ }
+
+ @Test
+ public void getSpreadSheetRules() throws IOException, RuleServiceException
+ {
+ String spreadSheetAsString = helper.getSpreadsheetRules( DECISION_TABLE_FILE );
+ assertNotNull( spreadSheetAsString );
+ }
+
+ public static junit.framework.Test suite()
+ {
+ return new JUnit4TestAdapter( DroolsRuleBaseHelperUnitTest.class );
+ }
+
+}
Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java 2008-06-12 06:00:37 UTC (rev 20433)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/java/org/jboss/soa/esb/services/routing/cbr/ContentBasedRoutingUnitTest.java 2008-06-12 06:37:30 UTC (rev 20434)
@@ -22,17 +22,20 @@
package org.jboss.soa.esb.services.routing.cbr;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import junit.framework.JUnit4TestAdapter;
+import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.jboss.soa.esb.message.format.MessageType;
import org.jboss.soa.esb.services.routing.MessageRouterException;
import org.jboss.soa.esb.testutils.TestEnvironmentUtil;
+import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
@@ -42,31 +45,50 @@
*/
public class ContentBasedRoutingUnitTest
{
+ // instance under test
+ private ContentBasedRouter cbr;
+
@Test
- public void routeSerializedMessage()
+ public void routeSerializedMessage() throws MessageRouterException
{
- try {
- //new messages
- Message message = MessageFactory.getInstance().getMessage(MessageType.JAVA_SERIALIZED);
- //set some properties inside the Message
- message.getProperties().setProperty("prop1", "val1");
- message.getProperties().setProperty("prop2", "val2");
- //set the body inside the Message
- message.getBody().add(("TEST BODY").getBytes());
- //set some object attachments inside the Message
- message.getAttachment().addItem(new String("TEST ATTACHMENT1"));
- message.getAttachment().addItem(new String("TEST ATTACHMENT2"));
+ //new messages
+ Message message = createSerializedMessage();
- ContentBasedRouter cbr = ContentBasedRouterFactory.getRouter(org.jboss.soa.esb.actions.ContentBasedRouter.DEFAULT_CBR_CLASS);
- List<String> destinationServices = cbr.route("JBossESBRules.drl",false,message,null);
- assertEquals(destinationServices.iterator().next(),"serialized-destination");
- } catch (MessageRouterException e) {
- e.printStackTrace();
- assertTrue(false);
- }
+ List<String> destinationServices = cbr.route("JBossESBRules.drl",false,message,null);
+ assertNotNull(destinationServices);
+ assertTrue(destinationServices.size()>0);
+ assertEquals(destinationServices.iterator().next(),"serialized-destination");
}
@Test
+ public void routeSerializedMessageDecisionTableStateless() throws MessageRouterException
+ {
+ Message message = createSerializedMessage();
+
+ List<String> destinationServices = cbr.route( "RuleBaseHelper.xls", false, message, null);
+
+ assertNotNull( destinationServices );
+ assertTrue( "One destination should have been added by Drools", destinationServices.size() == 1 );
+ assertEquals( "serialized-destination", destinationServices.get(0).toString() );
+ }
+
+ @Test
+ public void routeSerializedMessageDecisionTableStateful() throws MessageRouterException
+ {
+ Message message = createSerializedMessage();
+
+ ConfigTree config = new ConfigTree("statelessConfig");
+ config.setAttribute( "stateful", "true" );
+ cbr.setConfigTree( config );
+
+ List<String> destinationServices = cbr.route( "RuleBaseHelper.xls", false, message, null);
+
+ assertNotNull( destinationServices );
+ assertTrue( "One destination should have been added by Drools", destinationServices.size() == 1 );
+ assertEquals( "serialized-destination", destinationServices.get(0).toString() );
+ }
+
+ @Test
public void routeXMLMessage()
{
try {
@@ -81,7 +103,6 @@
message.getAttachment().addItem(new String("TEST ATTACHMENT1"));
message.getAttachment().addItem(new String("TEST ATTACHMENT2"));
- ContentBasedRouter cbr = ContentBasedRouterFactory.getRouter(org.jboss.soa.esb.actions.ContentBasedRouter.DEFAULT_CBR_CLASS);
List<String> destinationServices = cbr.route("JBossESBRules.drl",false,message,null);
assertEquals(destinationServices.iterator().next(),"xml-destination");
} catch (MessageRouterException e) {
@@ -104,7 +125,6 @@
message.getAttachment().addItem(new String("TEST ATTACHMENT1"));
message.getAttachment().addItem(new String("TEST ATTACHMENT2"));
- ContentBasedRouter cbr = ContentBasedRouterFactory.getRouter(org.jboss.soa.esb.actions.ContentBasedRouter.DEFAULT_CBR_CLASS);
List<String> destinationServices = cbr.route("JBossESBRules-XPath.drl","XPathLanguage.dsl",false,message,null);
assertEquals(destinationServices.iterator().next(),"XML_XPath_Destination");
} catch (MessageRouterException e) {
@@ -121,7 +141,6 @@
//set the body inside the Message
message.getBody().add(("<Dave>rocks</Dave>").getBytes());
- ContentBasedRouter cbr = ContentBasedRouterFactory.getRouter(org.jboss.soa.esb.actions.ContentBasedRouter.DEFAULT_CBR_CLASS);
List<String> destinationServices = cbr.route("JBossESBRules-XPath.drl","XPathLanguage.dsl",false,message,null);
assertEquals(destinationServices.iterator().next(),"XML_XPath_Dave_Destination");
} catch (MessageRouterException e) {
@@ -138,7 +157,6 @@
//set the body inside the Message
message.getBody().add(("<price>1.55</price>").getBytes());
- ContentBasedRouter cbr = ContentBasedRouterFactory.getRouter(org.jboss.soa.esb.actions.ContentBasedRouter.DEFAULT_CBR_CLASS);
List<String> destinationServices = cbr.route("JBossESBRules-XPath.drl","XPathLanguage.dsl",false,message,null);
assertEquals(destinationServices.iterator().next(),"XML_XPath_GreaterThan_Destination");
} catch (MessageRouterException e) {
@@ -155,7 +173,6 @@
//set the body inside the Message
message.getBody().add(("<price>0.55</price>").getBytes());
- ContentBasedRouter cbr = ContentBasedRouterFactory.getRouter(org.jboss.soa.esb.actions.ContentBasedRouter.DEFAULT_CBR_CLASS);
List<String> destinationServices = cbr.route("JBossESBRules-XPath.drl","XPathLanguage.dsl",false,message,null);
assertEquals(destinationServices.iterator().next(),"XML_XPath_LessThan_Destination");
} catch (MessageRouterException e) {
@@ -163,6 +180,13 @@
}
}
+ @Before
+ public void setup() throws MessageRouterException
+ {
+ cbr = ContentBasedRouterFactory.getRouter(org.jboss.soa.esb.actions.ContentBasedRouter.DEFAULT_CBR_CLASS);
+ cbr.setConfigTree( new ConfigTree("dummy" ));
+ }
+
@BeforeClass
public static void runBeforeAllTests() throws Exception
{
@@ -179,4 +203,19 @@
return new JUnit4TestAdapter(ContentBasedRoutingUnitTest.class);
}
+ private Message createSerializedMessage()
+ {
+ //new messages
+ Message message = MessageFactory.getInstance().getMessage(MessageType.JAVA_SERIALIZED);
+ //set some properties inside the Message
+ message.getProperties().setProperty("prop1", "val1");
+ message.getProperties().setProperty("prop2", "val2");
+ //set the body inside the Message
+ message.getBody().add(("TEST BODY").getBytes());
+ //set some object attachments inside the Message
+ message.getAttachment().addItem(new String("TEST ATTACHMENT1"));
+ message.getAttachment().addItem(new String("TEST ATTACHMENT2"));
+ return message;
+ }
+
}
Modified: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/JBossESBRules.drl
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/JBossESBRules.drl 2008-06-12 06:00:37 UTC (rev 20433)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/JBossESBRules.drl 2008-06-12 06:37:30 UTC (rev 20434)
@@ -7,6 +7,7 @@
#declare any global variables here
global java.util.List destinations;
+global Message message;
rule "Routing Rule - Serialized based message"
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/RuleBaseHelper.drl
===================================================================
--- labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/RuleBaseHelper.drl (rev 0)
+++ labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/RuleBaseHelper.drl 2008-06-12 06:37:30 UTC (rev 20434)
@@ -0,0 +1,29 @@
+#created on: Oct 30, 2006
+package org.jboss.internal.soa.esb.rules
+
+#list any import classes here.
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageType;
+
+#declare any global variables here
+global java.util.List destinations;
+
+
+rule "Routing Rule - Serialized based message"
+
+ when
+ Message( type == MessageType.JAVA_SERIALIZED)
+ then
+ System.out.println("Serialized");
+ destinations.add("serialized-destination");
+
+end
+
+rule "Routing Rule - XML based message"
+
+ when
+ Message( type == MessageType.JBOSS_XML)
+ then
+ System.out.println("JBoss_XML");
+ destinations.add("xml-destination");
+end
\ No newline at end of file
Added: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/RuleBaseHelper.xls
===================================================================
(Binary files differ)
Property changes on: labs/jbossesb/workspace/dbevenius/ruleservice/product/services/jbrules/src/test/resources/RuleBaseHelper.xls
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
More information about the jboss-svn-commits
mailing list