Drools Expression 介绍

用好Drools 中的表达式是你的规则引擎能否强大的必要条件

http://docs.jboss.org/drools/release/6.1.0.Final/drools-docs/html_single/index.html

下面列出了几个个人认为比较重要的点:

AND

Drools 默认的并列表达式就是 前置的AND

(and Cheese( cheeseType : type )
    Person( favouriteCheese == cheeseType ) )

when     Cheese( cheeseType : type )
        Person( favouriteCheese == cheeseType )
 then     ...    

OR

OR 实际上并不是按照||的短路逻辑进行处理的, 而是将一个规则写为两个子规则, 分别匹配和执行

(or Person( sex == "f", age > 60 )     Person( sex == "m", age > 65 )
pensioner : ( Person( sex == "f", age > 60 ) or Person( sex == "m", age > 65 ) )

(or pensioner : Person( sex == "f", age > 60 )      pensioner : Person( sex == "m", age > 65 ) )

NOT

there must be none of...

相当于work memory必须没有所示表达式的对象, 建议写()

// Brackets are optional:
not Bus(color == "red")
// Brackets are optional:
not ( Bus(color == "red", number == 42) )
// "not" with nested infix and - two patterns,
// brackets are requires:
not ( Bus(color == "red") and
      Bus(color == "blue") ) 

EXIST 

there is at least one..

相当于work memory至少有一个所示的对象

exists Bus(color == "red")
// brackets are optional:
exists ( Bus(color == "red", number == 42) )
// "exists" with nested infix and,
// brackets are required:
exists ( Bus(color == "red") and
         Bus(color == "blue") )

FORALL

WorkMemory中所有facts都需要符合表达式, 可以支持多项匹配

单项匹配

rule "All Buses are Red"
when
    forall( Bus( color == ‘red‘ ) )
then
    // all Bus facts are red
end

多项顺序匹配, 所有英文的bus都是红色的

rule "All English buses are red"
when
    forall( $bus : Bus( type == ‘english‘)
                   Bus( this == $bus, color = ‘red‘ ) )
then
    // all English buses are red
end

更复杂的情况, 所有employee都有health和dental care

rule "all employees have health and dental care programs"
when
    forall( $emp : Employee()
            HealthCare( employee == $emp )
            DentalCare( employee == $emp )
          )
then
    // all employees have health and dental care
end

FROM

从fact中拿出匹配表达式的field, 支持 基本类型 和 集合类, 如果是集合类且有多个符合条件的field, rule会fire多次

rule "apply 10% discount to all items over US$ 100,00 in an order"
when
    $order : Order()
    $item  : OrderItem( value > 100 ) from $order.items
then
    // apply discount to $item
end

只要value>100就会被fire一次, 相当于   遍历items, 判断并执行   for item in items: if value > 100 then ....

rule "Assign people in North Carolina (NC) to sales region 1"
ruleflow-group "test"
lock-on-active true
when
    $p : Person( )
    $a : Address( state == "NC") from $p.address
then
    modify ($p) {} // Assign person to sales region 1 in a modify block
end

rule "Apply a discount to people in the city of Raleigh"
ruleflow-group "test"
lock-on-active true
when
    $p : Person( )
    $a : Address( city == "Raleigh") from $p.address
then
    modify ($p) {} // Apply discount to person in a modify block
end

如果person在Raleign, NC生活, 应该两个rule都被Fire, 实际上, 只有第二个rule被fire, 原因是lock-on-active 当一系列fact变化时,  阻止rule进行新的activition, 所以第一个rule没有被再次active, 如果没有fact发生变化, 则一切正常

注意点:

  • Avoid the use of from when you can assert all facts into working memory or use nested object references in your constraint expressions (shown below).
  • Place the variable assigned used in the modify block as the last sentence in your condition (LHS).
  • Avoid the use of lock-on-active when you can explicitly manage how rules within the same rule-flow group place activations on one another (explained below).

Accumulate

将WorkMemory中的每个fact中的值进行累加操作, 内嵌支持min max avg count sum collectList collectSet, 也可以自己实现org.drools.core.runtime.rule.TypedAccumulateFunction接口, 从而写出自己的accumulate 函数

rule "Raise alarm"
when
    $s : Sensor()
    accumulate( Reading( sensor == $s, $temp : temperature );
                $min : min( $temp ),
                $max : max( $temp ),
                $avg : average( $temp );
                $min < 20, $avg > 70 )
then
    // raise the alarm
end

Eval

用于接受boolean值

其他注意点:

ruleflow-group

default value: N/A

type: String

Ruleflow is a Drools feature that lets you exercise control over the firing of rules. Rules that are assembled by the same ruleflow-group identifier fire only when their group is active.

规则流是一个Drools的功能,让你在规则的fire进行控制。即用相同的规则流组只有当他们的group active时才会被fire。

agenda-group
default value: MAIN

type: String

Agenda groups allow the user to partition the Agenda providing more execution control. Only rules in the agenda group that has acquired the focus are allowed to fire.

Agenda 允许用户进行分区并提供更多的执行控制。只有在 agenda group 被focus的时候才可以被fire

auto-focus
default value: false

type: Boolean

When a rule is activated where the auto-focus value is true and the rule‘s agenda group does not have focus yet, then it is given focus, allowing the rule to potentially fire.

可以让没有被focus的 agenda group获得 focus, 允许rule去执行

activation-group
default value: N/A

type: String

Rules that belong to the same activation-group, identified by this attribute‘s string value, will only fire exclusively. More precisely, the first rule in an activation-group to fire will cancel all pending activations of all rules in the group, i.e., stop them from firing.

Note: This used to be called Xor group, but technically it‘s not quite an Xor. You may still hear people mention Xor group; just swap that term in your mind with activation-group.

属于activation-group的rules将进行 独占式的触发, 更准确地说,一个已被fire的规则可以阻止所有的未执行规则

date-effective

default value: N/A

type: String, containing a date and time definition

A rule can only activate if the current date and time is after date-effective attribute.

一个rule只能在当前时间或者当前时间之后被触发

date-expires
default value: N/A

type: String, containing a date and time definition

A rule cannot activate if the current date and time is after the date-expires attribute.

一个rule不能在当前时间或者当前时间之后被触发

duration

default value: no default value

type: long

The duration dictates that the rule will fire after a specified duration, if it is still true.

rule将在duration时间之后被fire

时间: 2024-10-06 00:10:53

Drools Expression 介绍的相关文章

drools 决策引擎介绍、开发

1. 背景介绍 1.1  何为规则引擎 很多企业的IT业务系统中,经常会有大量的业务规则配置,而且随着企业管理者的决策变化,这些业务规则也会随之发生更改,为了适应这样的需求,IT业务系统应该能够快速且低成本的更新,通常做法是将业务规则的配置单独拿出来,使之与业务系统保持低耦合,实现这样功能的程序,叫做规则引擎. 接受数据输入,解释业务规则,并根据业务规则作出业务决策,从而实现了将业务决策从应用程序中分离出来. 1.2  一个实际的例子 银行贷款业务中,每种贷款类型都有不同的业务规则,并且这些规则

Drools应用实例

Drools 实例介绍 Drools编译与运行: 在Drools当中,规则的编译与运行要通过Drools提供的各种API来实现,这些API总体来讲可以分为三类:规则编译.规则收集和规则的执行. Kmodule.xml的编写 kmodule.xml文件放到 src/main/resources/META-INF/文件夹下 代码的实现(具体内容) <?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns

drools规则引擎初探

1.drools是什么 Drools是为Java量身定制的基于Charles  Forgy的RETE算法的规则引擎的实现.具有了OO接口的RETE,使得商业规则有了更自然的表达. Rule是什么呢? 一条规则是对商业知识的编码.一条规则有 attributes ,一个 Left Hand Side ( LHS )和一个 Right Hand Side ( RHS ).Drools 允许下列几种 attributes : salience , agenda-group , no-loop , au

linux 抓包 tcpdump 简单应用

在linux服务器上,经常要定位网络问题,就需要用到抓包. 例如:tcpdump -X -s 0 host 10.17.81.22 and port 9999 -w /home/text.cap -i eth4 上面的意思是抓取和 10.17.81.22 服务器 端口9999进行通讯的所有(-X)不限制大小(-s 0)的网络包,并输出到文件 text.cap ,抓取网卡eth4. tcpdump采用命令行方式,它的命令格式为: tcpdump [ -adeflnNOpqStvx ] [ -c 数

Drools介绍

http://www.oschina.net/p/drools Drools 是用 Java 语言编写的开放源码规则引擎,使用 Rete 算法对所编写的规则求值.Drools 允许使用声明方式表达业务逻辑.可以使用非 XML 的本地语言编写规则,从而便于学习和理解.并且,还可以将 Java 代码直接嵌入到规则文件中,这令 Drools 的学习更加吸引人. Drools 还具有其他优点: 非常活跃的社区支持 易用 快速的执行速度 在 Java 开发人员中流行 与 Java Rule Engine

Drools文档(八) 规则语言参考

规则语言参考 概述 Drools有一个"本地"的规则语言.这种格式在标点符号上非常轻,并且通过"扩展器"支持自然语言和领域特定的语言,使语言能够变形到您的问题领域.本章主要与本机规则格式一致.用于表示语法的图表被称为"铁路"图表,它们基本上是语言术语的流程图.技术上非常热衷的也可以参考DRL.g这是规则语言的Antlr3语法.如果您使用Rule Workbench,则可以通过内容帮助为您完成许多规则结构,例如,输入"ru"并按

Python正则表达式Regular Expression基本用法

资料来源:http://blog.csdn.net/whycadi/article/details/2011046   直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表达式re模块的基本函数. (1)findall函数的用法 findall(rule,target[,flag])是在目标字符串中找到符合规则的字符串.参数说明:rule表示规则,target表示目标字符串,[,flag]表示的是规则选项.返回的结果是一个列表.若没找到符合的,是一个空列表. 如: 因

kie-api介绍和使用

参考:KIE kie在drools jbpm uberfire里广泛被使用,下面对kie-api中的几个重要组件做下简单介绍 maven依赖 <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>6.0.2.Final</version> </dependency> 几个组件 KieServices:k

MongoDB的数据类型介绍

参考MongoDB官网:https://docs.mongodb.com/manual/reference/bson-types/ MongoDB文档存储是使用BSON类型,BSON(BSON short for Bin-ary JSON, is a bin-ary-en-coded seri-al-iz-a-tion of JSON-like doc-u-ments)是二进制序列化的形式.类如JSON,同样支持内嵌各种类型. Type Number Alias Notes Double 1 "