Drools规则引擎 系列教程(二)Drools规则语法 & LHS 条件

4,583 阅读1分钟

Drools规则引擎 系列教程(一)SpringBoot整合 & 快速集成上手

Drools规则引擎 系列教程(三)Drools规则语法 & RHS动作 & header详解

Drools规则引擎 系列教程(四)Drools 主要API & 决策表

教程代码已提交到ytooo-drools,欢迎star

文中代码方法均基于上一篇教程

1. .drl文件结构

  • package 包充当每组规则的唯一名称空间。一个规则库可以包含多个程序包。 通常,将包的所有规则与包声明存储在同一文件中,以便包是独立的。但是, 也可以在规则中使用从其他的包中导入的对象
  • imports 与Java中的import语句类似,用来标识在规则中使用的任何对象的标准路径和类型名称
  • factions 函数代码块如:
function String hello(String applicantName) {
    return "Hello " + applicantName + "!";
}
  • queries 在Drools引擎的工作内存中搜索与DRL文件中的规则相关的事实
query "people under the age of 21"
    $person : Person( age < 21 )
end
  • global 为规则提供数据或服务
  • rules 规则

1.1 import引入java方法 以及 function

   import & function 在下一节中详细描述 带我飞过去

1.2 glable 全局变量

   全局变量在下一节中详细描述 带我飞过去

1.3 querys 查询

   querys查询在下一节中详细描述 带我飞过去

1.4 declare 自定义fact对象

   declare在下一节中详细描述 带我飞过去

2.rules 规则

2.1 drl属性

属性描述
salience定义规则优先级的整数,数值越大,优先级越高
enabled规则启用开关
date-effective包含日期和时间定义的字符串。仅当当前日期和时间在date-effective属性之后时,才能激活该规则。
date-expires如果当前日期和时间在date-expires属性之后,则无法激活该规则。
no-loop选择该选项后,如果规则的结果重新触发了先前满足的条件,则无法重新激活(循环)规则。如果未选择条件,则在这些情况下可以循环规则。
agenda-group标识要向其分配规则的议程组
activation-group激活组,在激活组中,只能激活一个规则。触发的第一个规则将取消激活组中所有规则的所有未决激活。
duration定义了如果仍满足规则条件,则可以激活规则的持续时间(以毫秒为单位)。
timercron定时表达式
calendar时钟
auto-focus仅适用于议程组中的规则。选择该选项后,下次激活该规则时,将自动将焦点分配给分配了该规则的议程组。
lock-on-activeno-loop属性的更强版
ruleflow-group标识规则流组的字符串
dialect用于标识规则中的代码表达式JAVA或MVEL将其用作语言

2.2 匹配模式

2.2.1 没有约束的匹配模式

实事不需要满足任何条件,若类型相同,则触发该规则,如:

package com.ytooo.bean
import com.ytooo.bean.People
dialect  "java"

rule "girl"
    when
        People()
    then
        System.out.println("girl规则执行");
end
2.2.2 有条件约束的匹配模式

实事类型相同,且满足条件,则触发该规则,如:

package com.ytooo.bean
import com.ytooo.bean.People
dialect  "java"

rule "girl"
    when
        People(sex == 0 && drlType == "people")
    then
        System.out.println("girl规则执行");
end
2.2.3 匹配并绑定属性以及实事

实事类型相同,且满足条件,则触发该规则,并绑定数据,如:

package com.ytooo.bean
import com.ytooo.bean.People
dialect  "java"

rule "girl"
    when
         $p:People(sex == 0,$sex : sex && drlType == "people")
    then
        System.out.println("girl规则执行");
        System.out.println($p);
        System.out.println($sex);
end

2.3 条件

and,or 等结合规则条件的多个模式,没有定义关键字连词,默认是and:

package com.ytooo.bean
import com.ytooo.bean.People
import com.ytooo.bean.Cat
dialect  "java"

rule "girl"
    when
        People(sex == 0) and
        Cat(sex == 0)
    then
        System.out.println("girl规则执行");
end

2.4 约束

  标准Java运算符优先级适用于DRL中的约束运算符,而drl运算符除==和!=运算符外均遵循标准Java语义。

  在drl中 Person( firstName != "John" )类似于 !java.util.Objects.equals(person.getFirstName(), "John")

约束描述
!.使用此运算符可以以空安全的方式取消引用属性。!.运算符左侧的值不能为null(解释为!= null)
[]按List索引访问值或Map按键访问值
<,<=,>,>=在具有自然顺序的属性上使用这些运算符
==, !=在约束中使用这些运算符作为equals()和!equals()方法
&&,||组合关系条件
matches,not matches使用这些运算符可以指示字段与指定的Java正则表达式匹配或不匹配
contains,not contains使用这些运算符可以验证Array或字段是否包含或不包含指定值
memberOf,not memberOf使用这些运算符可以验证字段是否为定义为变量Array的成员
soundslike使用英语发音来验证单词是否具有与给定值几乎相同的声音(类似于该matches运算符)
in,notin使用这些运算符可以指定一个以上的可能值来匹配约束(复合值限制)
2.4.1 约束demo

  来自官方文档

  • matches, not matches
Person( country matches "(USA)?\\S*UK" )
Person( country not matches "(USA)?\\S*UK" )
  • contains, not contains
FamilyTree(countries contains "UK" )
Person( fullName not contains "Jr" )
FamilyTree(countries contains $var)
Person( fullName not contains $var )
  • memberOf, not memberOf
FamilyTree( person memberOf $europeanDescendants )
FamilyTree( person not memberOf $europeanDescendants )

2.5 集合

2.5.1 from 取集合中的元素
新建bean Animal
package com.ytooo.bean;
import lombok.Data;
import java.util.List;

/**
 * Created by Youdmeng on 2020/1/7 0007.
 */
@Data
public class Animal {

    private Integer level;

    private List<People> peoples;

}
新建 from.drl
package com.ytooo.frm
dialect "java"
import com.ytooo.bean.People
import com.ytooo.bean.Animal

rule "from"
when
    $an : Animal()
    $p : People(sex != 3 && drlType == "from") from $an.peoples
    then
        System.out.println($p);
    end
新建测试方法
@Test
public void from() {
    People p1 = new People(1,"达","from");
    People p2 = new People(0,"秋","from");
    People p3 = new People(3,"金","from");
    Animal animal = new Animal();
    animal.setPeoples(new ArrayList<>());
    animal.getPeoples().add(p1);
    animal.getPeoples().add(p2);
    animal.getPeoples().add(p3);
    session.insert(animal);//插入
    session.fireAllRules();//执行规则
}
执行测试,在三组数据中,2个people满足条件,执行两次
People(sex=0, name=秋, drlType=from)
People(sex=1, name=达, drlType=from)
2.5.2 collect

  从指定来源或从Drools引擎的工作内存中获取集合,可以使用Java集合(例如List,LinkedList和HashSet)

新建 collect.drl
package com.ytooo.collt
dialect "java"
import com.ytooo.bean.People
import java.util.List

rule "collect"
 when
    $alarms : List( size >= 3 ) from collect(People(sex != 3 && drlType == "collect"))
    then
        System.out.println("collect执行成功,匹配结果为:"+$alarms);
    end
新建测试方法
@Test
public void collect() {
   session.insert(new People(1, "达","collect"));
   session.insert(new People(0, "秋","collect"));
   session.insert(new People(0, "春","collect"));
   session.insert(new People(1, "夏","collect"));
   session.insert(new People(0, "冬","collect"));
   session.insert(new People(3, "金","collect"));

    session.fireAllRules();//执行规则
}
执行测试,正确打印出匹配的实事,其中 sex=3的“金” 没有匹配到结果中
collect执行成功,匹配结果为:
[People(sex=0, name=冬, drlType=collect),
 People(sex=1, name=夏, drlType=collect), 
 People(sex=0, name=春, drlType=collect), 
 People(sex=0, name=秋, drlType=collect), 
 People(sex=1, name=达, drlType=collect)]

  当改变参数,入参只留下两个 sex != 3 的数据,则没有任何打印结果

2.5.3 accumulate 迭代器

  用于遍历数据集对数据项执行自定义或预设动作并返回结果。

2.5.3.1 accumulate 函数
  • average
  • min
  • max
  • count
  • sum
  • collectList 获取列表
  • collectSet 获取集合
average样例 新建 accumulate.drl
package com.ytooo.collt
dialect "java"
import com.ytooo.bean.Sensor
import java.util.List

rule "accumulate"
  when
   $avg : Number() from accumulate(Sensor(temp >= 5 && $temp : temp),average($temp))
  then
    System.out.println("accumulate成功执行,平均温度为:" + $avg);
end
新建测试方法
 @Test
public void accumulate() {
    session.insert(new Sensor("达", 8.26));
    session.insert(new Sensor("秋", 7.12));
    session.insert(new Sensor("春", 3.24));
    session.insert(new Sensor("夏", 6.32));
    session.insert(new Sensor("冬", 12.23));
    session.insert(new Sensor("金", 10.8));

    session.fireAllRules();//执行规则
}
执行测试,触发规则并打印平均值
accumulate成功执行,平均温度为:8.946
2.5.4 自定义 accunmulate
  • init 初始化变量
  • action 每次遍历执行的动作
  • reverse (可选)反转动作,用于优化
  • result 返回的执行结果
@Test
public void diyaccumulate() {
    session.insert(new People(1, "达",26,"diyaccumulate"));
    session.insert(new People(0, "秋",18,"diyaccumulate"));
    session.insert(new People(0, "春",38,"diyaccumulate"));
    session.insert(new People(1, "夏",90,"diyaccumulate"));
    session.insert(new People(0, "冬",55,"diyaccumulate"));
    session.insert(new People(3, "金",12,"diyaccumulate"));

    session.fireAllRules();//执行规则
}
rule "diyaccumulate"
    when
        People(drlType == "diyaccumulate")
        $avg: Number() from accumulate(People($age: age,drlType == "diyaccumulate"),
        init(int $total = 0, $count = 0;),
        action($total += $age; $count++;),
        result($total/$count))

    then
        System.out.println("Avg: " + $avg);
end
输出结果
Avg: 39



教程代码已提交到ytooo-drools,欢迎star

Drools规则引擎 系列教程(一)SpringBoot整合 & 快速集成上手

Drools规则引擎 系列教程(三)Drools规则语法 & RHS动作 & header详解




更多好玩好看的内容,欢迎到我的博客交流,共同进步        WaterMin

喜欢听相声的朋友,也可以来我的 YouTube,来听郭老师的相声    秋酿