Seam makes it easy to call JBoss Rules (Drools) rulebases from Seam components or jBPM process definitions. This is an experimental and unsupported feature of Seam 1.0.
The first step is to make an instance of org.drools.RuleBase available in a Seam context variable. In most rules-driven applications, rules need to be dynamically deployable, so you will need to implement some solution that allows you to deploy rules and make them available to Seam (a future release of Drools will provide a Rule Server that solves this problem). For testing purposes, Seam provides a built-in component that compiles a static set of rules from the classpath. You can install this component via components.xml:
<component name="policyPricingRules" class="org.jboss.seam.drools.RuleBase"> <property name="rulesFiles">policyPricingRules.drl</property> </component>
This component compiles rules from a set of .drl files and caches an instance of org.drools.RuleBase in the Seam APPLICATION context. Note that it is quite likely that you will need to install multiple rule bases in a rule-driven application.
Next, we need to make an instance of org.drools.WorkingMemory available to each conversation. (Each WorkingMemory accumulates facts relating to the current conversation.)
<component name="policyPricingWorkingMemory" class="org.jboss.seam.drools.ManagedWorkingMemory"> <property name="ruleBaseName">policyPricingRules</property> </component>
Notice that we gave the policyPricingWorkingMemory a reference back to our rule base via the ruleBaseName configuration property.
We can now inject our WorkingMemory into any Seam component, assert facts, and fire rules:
@In(create=true) WorkingMemory policyPricingWorkingMemory; @In Policy policy; @In Customer customer; public void pricePolicy() throws FactException { policyPricingWorkingMemory.assertObject(policy); policyPricingWorkingMemory.assertObject(customer); policyPricingWorkingMemory.fireAllRules(); }
You can even allow a rule base to act as a jBPM action handler, decision handler, or assignment handler.
<decision name="approval"> <handler class="org.jboss.seam.drools.DroolsDecisionHandler"> <assertObjects> <element>customer</element> <element>order</element> </assertObjects> <workingMemoryName>orderApprovalRulesWorkingMemory</workingMemoryName> </handler> <transition name="approved" to="ship"> <action class="org.jboss.seam.drools.DroolsActionHandler"> <assertObjects> <element>customer</element> <element>order</element> </assertObjects> <workingMemoryName>shippingRulesWorkingMemory</workingMemoryName> </action> </transition> <transition name="rejected" to="cancelled"/> </decision>
The <assertObjects> element specifies names of components in the Seam contexts to be asserted as facts into the WorkingMemory.
Certain objects are available to the rules as Drools globals, namely the jBPM ContextInstance, as contextInstance and a Seam Decision object, as decision. Rules which handle decisions should call decision.setOutcome("result") to determine the result of the decision.
package org.jboss.seam.examples.shop import org.jboss.seam.examples.shop.Customer import org.jboss.seam.examples.shop.Order import org.jboss.seam.drools.Decision global Decision decision rule "Approve Order For Loyal Customer" when Customer( loyaltyStatus = "GOLD" ) Order( totalAmount <= 10 000 ) then decision.setOutcome("approved") end
There is also support for using Drools for jBPM task assignments. TODO!