`

Seam 与 JBoss Rules(Drools)

阅读更多


14.1. 安装14.2. Using rules from a Seam component14.3. Using rules from a jBPM process definitionSeam makes it easy to call JBoss Rules (Drools) rulebases from Seam components or jBPM process definitions.
Seam与JBoss Rules(Drools)规则框架的结合非常简便,通过Seam components与jBPM流程定义中都能很方便地获得相关规则。


The first step is to make an instance oforg.drools.RuleBaseavailable in a Seam context variable. For testing purposes, Seam provides a built-in component that compiles a static set of rules from the classpath. You can install this component viacomponents.xml:
首先,我们需要在Seam的上下文变量中设置一个org.drools.RuleBase的实例。为了方便测试,Seam提供了一个用来编译classpath下静态规则的内置组件。你可以通过在components.xml中编写相关配置来安装该组件:
<drools:rule-base name="policyPricingRules">

    <drools:rule-files>

        <value>policyPricingRules.drl</value>

    </drools:rule-files>

</drools:rule-base>
This component compiles rules from a set of.drlfiles and caches an instance oforg.drools.RuleBasein the SeamAPPLICATIONcontext. Note that it is quite likely that you will need to install multiple rule bases in a rule-driven application.
该组件能够自动编译一组后缀名为drl的规则文件,并且将org.drools.RuleBase的实例缓存在APPLICATION范围下。不过需要注意的是,如果你的程序是完全用规则驱动的,那么你可能需要安装多个规则库。
If you want to use a Drools DSL, you alse need to specify the DSL definition:
如果你想要使用Drools DSL,那么你需要定义一下DSL:
<drools:rule-base name="policyPricingRules" dsl-file="policyPricing.dsl">

    <drools:rule-files>

        <value>policyPricingRules.drl</value>

    </drools:rule-files>

</drools:rule-base>
In most rules-driven applications, rules need to be dynamically deployable, so a production application will want to use a Drools RuleAgent to manage the RuleBase. The RuleAgent can connect to a Drools rule server (BRMS) or hot deploy rules packages from a local file repository. The RulesAgent-managed RuleBase is also configurable incomponents.xml:
在大多数规则驱动的程序中,规则通常需要动态部署。所以一个软件产品可能会需要使用Drools RuleAgent来管理RuleBase。RuleAgent可以连接到Drools规则服务(BRMS)或者从本地文件将规则包热部署到服务器中。这种通过RulesAgent来管理RuleBase的情况也需要在components.xml中进行相关配置:
<drools:rule-agent name="insuranceRules"configurationFile="/WEB-INF/deployedrules.properties" />
The properties file contains properties specific to the RulesAgent. Here is an example configuration file from the Drools example distribution.
properties文件包含了一组定义好的属性,这些属性用来配置RulesAgent。下面是一个配置文件的示例,该例子收录在Drools的示例集中。
newInstance=true
url=http://localhost:8080/drools-jbrms/org.drools.brms.JBRMS/package/org.acme.insurance/fmeyer
localCacheDir=/Users/fernandomeyer/projects/jbossrules/drools-examples/drools-examples-brms/cache
poll=30
name=insuranceconfigIt is also possible to configure the options on the component directly, bypassing the configuration file.
你同样可以不适用配置文件,直接在component上面直接指定相关参数就可以。
<drools:rule-agent name="insuranceRules"

   url="http://localhost:8080/drools-jbrms/org.drools.brms.JBRMS/package/org.acme.insurance/fmeyer"

   local-cache-dir="/Users/fernandomeyer/projects/jbossrules/drools-examples/drools-examples-brms/cache"

   poll="30"

   configuration-name="insuranceconfig" />
Next, we need to make an instance oforg.drools.WorkingMemoryavailable to each conversation. (EachWorkingMemoryaccumulates facts relating to the current conversation.)
下面,我们需要让org.drools.WorkingMemory在每个conversation内都有效。(每个WorkingMemory都能关联到当前回话)
<drools:managed-working-memory name="policyPricingWorkingMemory" auto-create="true" rule-base="#{policyPricingRules}"/>
Notice that we gave thepolicyPricingWorkingMemorya reference back to our rule base via theruleBaseconfiguration property.
注意一下,通过设置rule-base属性,我们给这个policyPricingWorkingMemory设置了一个关联到前面定义好的规则库的参数。



We can now inject ourWorkingMemoryinto any Seam component, assert facts, and fire rules:
我们可以直接注入一个WorkingMemory到任何一个Seam components中,然后我们就可以assert实体、执行规则:
@In 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—in either a pageflow or business process definition.
你甚至可以通过设置,让一个rule base来充当action handler、decision handler或者assignment handler。随便是pageflow还是业务流程定义。
<decision name="approval">
   <handler class="org.jboss.seam.drools.DroolsDecisionHandler">
       <workingMemoryName>orderApprovalRulesWorkingMemory</workingMemoryName>
       <assertObjects>
           <element>#{customer}</element>
           <element>#{order}</element>
           <element>#{order.lineItems}</element>
       </assertObjects>
   </handler>
   <transition name="approved" to="ship">
       <action class="org.jboss.seam.drools.DroolsActionHandler">
           <workingMemoryName>shippingRulesWorkingMemory</workingMemoryName>
           <assertObjects>
               <element>#{customer}</element>
               <element>#{order}</element>
               <element>#{order.lineItems}</element>
           </assertObjects>
       </action>
   </transition>
   <transition name="rejected" to="cancelled"/>
</decision>
The<assertObjects>element specifies EL expressions that return an object or collection of objects to be asserted as facts into theWorkingMemory.
<assertObjects>标签通过一组EL表达式来将一个对象或者对象的集合插入到WorkingMemory中去。
There is also support for using Drools for jBPM task assignments:
同样,Seam也支持使用Drools来进行jBPM任务分配:
<task-node name="review">

    <task name="review" description="Review Order">

        <assignment handler="org.jboss.seam.drools.DroolsAssignmentHandler">

            <workingMemoryName>orderApprovalRulesWorkingMemory</workingMemoryName>

            <assertObjects>

                <element>#{actor}</element>

                <element>#{customer}</element>

                <element>#{order}</element>

                <element>#{order.lineItems}</element>

            </assertObjects>

        </assignment>

    </task>

    <transition name="rejected" to="cancelled"/>

    <transition name="approved" to="approved"/>

</task-node>
Certain objects are available to the rules as Drools globals, namely the jBPMAssignable, asassignableand a SeamDecisionobject, asdecision. Rules which handle decisions should calldecision.setOutcome("result")to determine the result of the decision. Rules which perform assignments should set the actor id using theAssignable.
相应的对象会被当作Drools globals使用在规则中,同时也会被当作Seam决策对象在jBPM中被指定。进行相关决策的规则可以调用decision.setOutcome("result")来判断决策的结果。而充当分配者的规则也使用这个Assignable来设置参与者id。
package org.jboss.seam.examples.shop

import org.jboss.seam.drools.Decision

global Decision decision

rule "Approve Order For Loyal Customer"
  when
    Customer( loyaltyStatus == "GOLD" )
    Order( totalAmount <= 10000 )
  then
    decision.setOutcome("approved");
endpackage org.jboss.seam.examples.shop

import org.jbpm.taskmgmt.exe.Assignable

global Assignable assignable

rule "Assign Review For Small Order"
  when
    Order( totalAmount <= 100 )
  then
    assignable.setPooledActors( new String[] {"reviewers"} );
end注意You can find out more about Drools athttp://www.drools.org
想要了解更多有关Drools的资料,请参考http://www.drools.org

警告Seam comes with enough of Drools' dependencies to implement some simple rules. If you want to add extra capabilities to Drools you should download the full distribution and add in extra dependencies as needed.
Seam仅仅包含了实现一些简单规则的Drools依赖库,如果你想要使用一些较复杂的特性,那么你需要下载完整的版本,并且添加到依赖库中。

提示Drools comes with MVEL compiled for Java 1.4, which is compatible with Java 1.4, Java 5 and Java 6. You may want to change your MVEL jar with one compiled for the version of Java you are using


Drools包含的MVEL是使用Java 1.4编译的,能够适用于Java 1.4、Java 5和Java 6版本的环境。如果你需要改变你的MVEL jar包来符合你使用的Java版本。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics