用好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

Drools Expression 介绍的更多相关文章

  1. drools语法介绍

    这里没有翻译http://docs.jboss.org/drools/release/6.4.0.Final/drools-docs/html_single/index.html上的内容 而是参考了网 ...

  2. 规则引擎 drools

    https://www.jianshu.com/p/725811f420db 深入了解Drools 简单介绍 笔者正在做风控系统,风控系统里边存在非常多的规则(比如:age < 16 || ag ...

  3. drools规则引擎初探

    https://www.cnblogs.com/yuebintse/p/5767996.html 1.drools是什么 Drools是为Java量身定制的基于Charles  Forgy的RETE算 ...

  4. Drools应用实例

    Drools 实例介绍 Drools编译与运行: 在Drools当中,规则的编译与运行要通过Drools提供的各种API来实现,这些API总体来讲可以分为三类:规则编译.规则收集和规则的执行. Kmo ...

  5. Django templates and models

    models templates models and databases models 如何理解models A model is the single, definitive source of ...

  6. C#高级编程9 第11章 Linq

    Linq 1.Linq概述 列表和实体 准备数据: public class Championship { public int Year { get; set; } public string Fi ...

  7. Android 你可能忽略的提高敲代码效率的方式 (转)

    每日推荐 Eyepetizer-in-Kotlin:一款简约的小视频app,带你走进kotlin 作为学习kotlin的一款app,在撸代码的过程中学习kotlin的语法及特性. Eyepetizer ...

  8. linux 抓包 tcpdump 简单应用

    在linuxserver上,常常要定位网络问题,就须要用到抓包. 比如:tcpdump -X -s 0 host 10.17.81.22 and port 9999 -w /home/text.cap ...

  9. 介绍一个可以将Expression表达式树解析成Transact-SQL的项目Expression2Sql

    一.Expression2Sql介绍 Expression2Sql是一个可以将Expression表达式树解析成Transact-SQL的项目.简单易用,几分钟即可上手使用,因为博主在设计Expres ...

随机推荐

  1. synchronized加static区别

    在多线程中,在synchronise方法上加上static 表示的是类锁,锁住的是整个类.而synchroinized 锁住的是当前方法,当前对象.

  2. windows_64下python下载安装Numpy、Scipy、matplotlib模块

    本文应用的python3.6.3及其对应的Numpy.Scipy.matplotlib计算模块的cp36版本,其中Numpy是需要MKL版本的Numpy,这是后续安装Scipy的需要(本机系统win7 ...

  3. 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007

    include "stdafx.h" #include<iostream> #include<vector> #include <algorithm& ...

  4. dede后台title怎么修改的?去掉XXXX-织梦内容管理系统V5.7

    dede后台title怎么修改的? 去掉XXXX-织梦内容管理系统V5.7 打开include/common.inc.php的文件. $cfg_version = 'V57_UTF8_SP1';(这是 ...

  5. 题外话:计算密集型 vs IO密集型

    我们把任务分为计算密集型和IO密集型,erlang作为IO密集型的语言,适合网关等相关的场景,而对计算达到某一量级后,可能处理效率下降的很明显. erlang不适合数值计算.erlang是解释型的,虽 ...

  6. Idea中优化Markdown Support显示效果

    转自:https://www.jianshu.com/p/d093c42a8c29 因为工作中为提高工作效率,我一般习惯于直接在`idea`中使用`markdow support`插件来进行相关文档的 ...

  7. SecureCRT 设置字体跟颜色

    SecureCRT 绝佳配色方案, 保护你的眼睛 分类: Linux 软件使用2013-05-17 08:45 24038人阅读 评论(11) 收藏 举报 SecureCRT 绝佳配色方案, 保护你的 ...

  8. No breeds found in the signature, a signature update is recommended

    cobbler 2.6.11 遇到这个问题,需要 >> cobbler signature update >> and cobblerd restart 转自: https:/ ...

  9. Keepalived 集群在Linux下的搭建

    [概述]:Keepalived 是一个免费开源的,用C编写.主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务 ...

  10. 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数

    typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...