`
stenlylee
  • 浏览: 260605 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

第14章、Seam 与 JBoss Rules(Drools)

阅读更多

Seam 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 of org.drools.RuleBase available 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 via components.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 .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.

 

<drools:rule-base name="policyPricingRules">
    <drools:rule-files>
        <value>policyPricingRules.drl</value>
    </drools:rule-files>
</drools:rule-base>

 

该组件能够自动编译一组后缀名为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 in components.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=insuranceconfig

 

It 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 of org.drools.WorkingMemory available to each conversation. (Each WorkingMemory accumulates 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 the policyPricingWorkingMemory a reference back to our rule base via the ruleBase configuration property.

 

注意一下,通过设置rule-base属性,我们给这个policyPricingWorkingMemory设置了一个关联到前面定义好的规则库的参数。

14.2. 在Seam component中使用规则库

 

We can now inject our WorkingMemory into 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();
}<!---->

14.3. 在jBPM中使用

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 the WorkingMemory.

 

<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 jBPM Assignable, as assignable and a Seam Decision object, as decision. Rules which handle decisions should call decision.setOutcome("result") to determine the result of the decision. Rules which perform assignments should set the actor id using the Assignable.

 

相应的对象会被当作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");
end

package 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 at http://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版本。
分享到:
评论

相关推荐

    深入浅出JBoss Seam

    例如,Seam与jBPM、JBoss Rules(Drools)、JBoss Portal和JBoss Microcontainer等框架的整合,既保持了各框架的原有功能,又提升了整体的效能。 Seam还特别关注ORM的使用。由于大多数Web框架并未为ORM设计,所以在...

    JBoss Seam

    JBoss Seam JBoss Seam JBoss Seam

    Eclipse3.7集成JbossTools+Drools

    2. **JBossTools**:JBossTools是Red Hat开发的一套Eclipse插件集,包含了针对JBoss相关技术如JSF、Hibernate、Seam、jBPM等的开发工具。它提供了对这些技术的集成开发、调试和部署支持,帮助开发者更高效地进行企业...

    jboss seam 教程

    ### JBoss Seam 教程知识点概述 #### 一、Seam简介与教程概览 - **Seam**:一个基于Java EE平台的应用框架,旨在简化企业级应用开发。 - **JBoss Seam**:由JBoss提供的Seam实现,提供了一系列功能强大的工具和...

    深入浅出JBoss Seam.pdf

    除了整合EJB 3.0和JSF之外,Seam还能够扩展其他一系列开源框架,例如jBPM、JBoss Rules (Drools)、JBoss Portal 和 JBoss Microcontainer等。这不仅使得Seam能够成为一个高度集成的平台,还能够利用这些框架的功能来...

    JBOSS_SEAM配置

    JBOSS_SEAM配置

    Jboss Seam中文版

    #### 第四章:配置Seam组件 最后一章重点介绍了如何通过配置文件和注解等方式来定制Seam组件的行为。 1. **通过属性设置配置组件**:说明了如何使用属性来调整组件的默认行为。 2. **后续内容预告**:预览了接下来...

    JBOSS SEAM组件中文手册

    **JBoss Seam组件中文手册** **一、Seam框架概述** Seam是一个开源的企业级Java框架,由JBoss公司开发,旨在简化Java EE应用程序的开发。它将多种技术如JavaServer Faces (JSF),Java Persistence API (JPA),EJB 3...

    JBoss Seam入门介绍

    标题:JBoss Seam入门介绍 描述:本文将详细介绍JBoss Seam框架的核心概念、关键特性以及如何构建基于Seam的应用程序。Seam作为一个企业级Java Web应用框架,它将Java EE和JSF无缝集成,旨在填补Java EE 5.0中缺失...

    jboss seam 中文文档集合

    **JBoss Seam 中文文档集合概述** JBoss Seam 是一个开源的应用框架,它结合了JavaServer Faces (JSF)、Java Persistence API (JPA)、Enterprise JavaBeans (EJB) 3.0 和其他Java EE组件,旨在简化企业级开发。这个...

    [JBoss] JSF & Facelets & JBoss Seam 核心技术 (英文版)

    [TipTec Development] JSF & Facelets & JBoss Seam 核心技术 (英文版) [TipTec Development] Essential JSF, Facelets & JBoss Seam (E-Book) ☆ 出版信息:☆ [作者信息] Kent Ka Iok Tong [出版机构] TipTec ...

    jboss-seam-selectitems

    java jboss seam jboss-seam-selectitems

    jboss seam

    这是中文手册,Seam为持久化集成了JPA和Hibernate 3,为轻量化的异步性集成了EJB Timer Service和Quartz,为工作流集成了jBPM,为业务规则集成了JBoss规则,为电子邮件集成了Meldware Mail,为完整的文本搜索集成了...

    Jboss seam3 实战

    标题中的“Jboss seam3 实战”表明,本文将重点介绍JBoss Seam框架的第三个版本的实际应用。JBoss Seam是一个开源的Java EE框架,它通过依赖注入和会话模型,简化了基于Java EE的企业级应用开发。Seam框架为开发者...

    jboss教程及深入浅出JBoss+Seam

    【JBoss教程及深入浅出JBoss+Seam】是一个针对企业级Java应用服务器JBoss的深度学习资源,其中包含了两个主要部分:JBoss教程和对JBoss与Seam框架结合使用的详细介绍。这两个主题都是Java开发人员在部署和管理企业...

    jboss-seam2.0文档

    【JBoss Seam 2.0文档详解】 JBoss Seam 是一个开源的企业级开发框架,它旨在简化Java EE应用的开发过程,特别是在Web和富互联网应用程序(Rich Internet Applications, RIA)领域。Seam 2.0是其重要的版本,提供了...

    Beginning_JSF _trade _2_APIs_and_JBoss_reg_Seam-4382 源码

    JBoss seam源码 JBoss seam源码 JBoss seam源码 JBoss seam源码 JBoss seam源码 JBoss seam源码 JBoss seam源码 JBoss seam源码

Global site tag (gtag.js) - Google Analytics