You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
土豆兄弟 dfc0b84ba7 [新增功能](master): 更新了Drools和EasyExcel相关的内容 2 years ago
..
drools-quickstart [新增功能](master): 更新了Drools和EasyExcel相关的内容 2 years ago
drools-springboot [新增功能](master): 更新了Drools和EasyExcel相关的内容 2 years ago
drools-springboot-stater [新增功能](master): 更新了Drools和EasyExcel相关的内容 2 years ago
.gitignore [新增功能](master): 更新了Drools和EasyExcel相关的内容 2 years ago
README.md [新增功能](master): 更新了Drools和EasyExcel相关的内容 2 years ago
TODO-list.md [新增功能](master): 更新了Drools和EasyExcel相关的内容 2 years ago
pom.xml [新增功能](master): 更新了Drools和EasyExcel相关的内容 2 years ago

README.md

规则引擎

1. 准备

1.1 制定业务边界

进行制定不符合条件的规则

1.2 制定业务内容规则

根据不同的条件指定不同的规则

1.3 实现方式

传统实现方式的缺点:

  • 硬编码让业务规则难以维护
  • 硬编码实现业务规则难以应对变化
  • 业务规则改变,需要重启服务

使用规则引擎进行设计

2. 概述

2.1 概念知识点

规则引擎,全称为业务规则管理系统(BRMS Business Rule Management System)
常见的规则引擎有: Drools, VisualRules, iLog等

2.2 规则引擎的优势

  • 业务规则与系统代码分离, 实现业务规则的集中管理
  • 在不重启服务的情况下可随机对业务规则进行扩展和维护
  • 可以动态修改业务规则, 从而快速响应需求变更
  • 规则引擎是相对独立的, 只关心业务规则, 使得业务分析人员也可以参与编辑, 维护系统的业务规则
  • 减少了硬编码业务规则的成本和风险
  • 使用规则引擎提供的规则编辑工具, 使复杂的业务规则实现变得简单

2.3 规则引擎的使用场景

- 风控系统
- 验证平台
- 营销/促销系统
- 决策平台

2.4 Drools 介绍

- Drools 基于Java语言编写,规则是脚本配置
- 可以单独使用也可以结合Spring进行使用
- 开发步骤: 获取KieServices -》 获取KieContainer -》 KieSession -》 Insert fact(Bean) -》 触发规则 -》 关闭KieSession

3. 入门

查看模块 drools-quickstart

4. 小结

drools规则引擎由以下三部分构成

  • Working Memory工作内存
  • Rule Base规则库
  • Inference Engine推理引擎

其中Inference Engine推理引擎又包括

  • Pattern Matcher匹配器
  • Agenda(议程)
  • Execution Engine执行引擎

相关概念:

Working Memory工作内存drools规则引擎会从Working Memory中获取数据并和规则文件中定义的规则进行模式匹配所以我们开发的应用程序只需要将我们的数据插入到Working Memory中即可例如本案例中我们调用kieSession.insert(order)就是将order对象插入到了工作内存中。

Fact事实是指在drools 规则应用当中将一个普通的JavaBean插入到Working Memory后的对象就是Fact对象例如本案例中的Order对象就属于Fact对象。Fact对象是我们的应用和规则引擎进行数据交互的桥梁或通道。

Rule Base:规则库,我们在规则文件中定义的规则都会被加载到规则库中。

Pattern Matcher匹配器将Rule Base中的所有规则与Working Memory中的Fact对象进行模式匹配匹配成功的规则将被激活并放入Agenda中。

Agenda:议程,用于存放通过匹配器进行模式匹配后被激活的规则。

Execution Engine执行引擎执行Agenda中被激活的规则。

2. 基本语法

2.1 规则文件

关键字 描述
package 包名,只限于逻辑上的管理,同一个包名下的查询或者函数可以直接调用
import 用于导入类或者静态方法
global 全局变量
function 自定义函数
query 查询
rule end 规则体

2.2 规则提语法结构

rule "ruleName"
    attributes
    when
        LHS
    then
        RHS
end

rule:关键字,表示规则开始,参数为规则的唯一名称。

attributes规则属性是rule与when之间的参数为可选项。

when:关键字,后面跟规则的条件部分。

LHS(Left Hand Side)是规则的条件部分的通用名称。它由零个或多个条件元素组成。如果LHS为空则它将被视为始终为true的条件元素。

then:关键字,后面跟规则的结果部分。

RHS(Right Hand Side):是规则的后果或行动部分的通用名称。

end:关键字,表示一个规则结束。

2.3 注释

在drl形式的规则文件中使用注释和Java类中使用注释一致分为单行注释和多行注释。

单行注释用"//"进行标记,多行注释以"/*"开始,以"*/"结束。如下示例:

//规则rule1的注释这是一个单行注释
rule "rule1"
    when
    then
    	System.out.println("rule1触发");
end

/*
规则rule2的注释
这是一个多行注释
*/
rule "rule2"
    when
    then
    	System.out.println("rule2触发");
end

2.4 Pattern模式匹配

前面我们已经知道了Drools中的匹配器可以将Rule Base中的所有规则与Working Memory中的Fact对象进行模式匹配那么我们就需要在规则体的LHS部分定义规则并进行模式匹配。LHS部分由一个或者多个条件组成条件又称为pattern。

pattern的语法结构为绑定变量名:Object(Field约束)

其中绑定变量名可以省略,通常绑定变量名的命名一般建议以$开始。如果定义了绑定变量名就可以在规则体的RHS部分使用此绑定变量名来操作相应的Fact对象。Field约束部分是需要返回true或者false的0个或多个表达式。

//规则二所购图书总价在100到200元的优惠20元
rule "book_discount_2"
    when
    	//Order为类型约束originalPrice为属性约束
        $order:Order(originalPrice < 200 && originalPrice >= 100)
    then
        $order.setRealPrice($order.getOriginalPrice() - 20);
        System.out.println("成功匹配到规则二所购图书总价在100到200元的优惠20元");
end

通过上面的例子我们可以知道,匹配的条件为:

1、工作内存中必须存在Order这种类型的Fact对象-----类型约束

2、Fact对象的originalPrice属性值必须小于200------属性约束

3、Fact对象的originalPrice属性值必须大于等于100------属性约束

以上条件必须同时满足当前规则才有可能被激活。

绑定变量既可以用在对象上,也可以用在对象的属性上。例如上面的例子可以改为:

//规则二所购图书总价在100到200元的优惠20元
rule "book_discount_2"
    when
        $order:Order($op:originalPrice < 200 && originalPrice >= 100)
    then
        System.out.println("$op=" + $op);
        $order.setRealPrice($order.getOriginalPrice() - 20);
        System.out.println("成功匹配到规则二所购图书总价在100到200元的优惠20元");
end

LHS部分还可以定义多个pattern多个pattern之间可以使用and或者or进行连接也可以不写默认连接为and。

//规则二所购图书总价在100到200元的优惠20元
rule "book_discount_2"
    when
        $order:Order($op:originalPrice < 200 && originalPrice >= 100) and
        $customer:Customer(age > 20 && gender=='male')
    then
        System.out.println("$op=" + $op);
        $order.setRealPrice($order.getOriginalPrice() - 20);
        System.out.println("成功匹配到规则二所购图书总价在100到200元的优惠20元");
end

2.5 比较操作符

Drools提供的比较操作符如下表

符号 说明
> 大于
< 小于
>= 大于等于
<= 小于等于
== 等于
!= 不等于
contains 检查一个Fact对象的某个属性值是否包含一个指定的对象值
not contains 检查一个Fact对象的某个属性值是否不包含一个指定的对象值
memberOf 判断一个Fact对象的某个属性是否在一个或多个集合中
not memberOf 判断一个Fact对象的某个属性是否不在一个或多个集合中
matches 判断一个Fact对象的属性是否与提供的标准的Java正则表达式进行匹配
not matches 判断一个Fact对象的属性是否不与提供的标准的Java正则表达式进行匹配

  • contains | not contains语法结构

    Object(Field[Collection/Array] contains value)

    Object(Field[Collection/Array] not contains value)

  • memberOf | not memberOf语法结构

    Object(field memberOf value[Collection/Array])

    Object(field not memberOf value[Collection/Array])

  • matches | not matches语法结构

    Object(field matches "正则表达式")

    Object(field not matches "正则表达式")

第一步:创建实体类,用于测试比较操作符

package com.itheima.drools.entity;
import java.util.List;

/**
 * 实体类
 * 用于测试比较操作符
 */
public class ComparisonOperatorEntity {
    private String names;
    private List<String> list;

    public String getNames() {
        return names;
    }

    public void setNames(String names) {
        this.names = names;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }
}

第二步:在/resources/rules下创建规则文件comparisonOperator.drl

package comparisonOperator
import com.itheima.drools.entity.ComparisonOperatorEntity
/*
 当前规则文件用于测试Drools提供的比较操作符
*/

//测试比较操作符contains
rule "rule_comparison_contains"
    when
        ComparisonOperatorEntity(names contains "张三")
        ComparisonOperatorEntity(list contains names)
    then
        System.out.println("规则rule_comparison_contains触发");
end

//测试比较操作符not contains
rule "rule_comparison_notContains"
    when
        ComparisonOperatorEntity(names not contains "张三")
        ComparisonOperatorEntity(list not contains names)
    then
        System.out.println("规则rule_comparison_notContains触发");
end

//测试比较操作符memberOf
rule "rule_comparison_memberOf"
    when
        ComparisonOperatorEntity(names memberOf list)
    then
        System.out.println("规则rule_comparison_memberOf触发");
end

//测试比较操作符not memberOf
rule "rule_comparison_notMemberOf"
    when
        ComparisonOperatorEntity(names not memberOf list)
    then
        System.out.println("规则rule_comparison_notMemberOf触发");
end

//测试比较操作符matches
rule "rule_comparison_matches"
    when
        ComparisonOperatorEntity(names matches "张.*")
    then
        System.out.println("规则rule_comparison_matches触发");
end

//测试比较操作符not matches
rule "rule_comparison_notMatches"
    when
        ComparisonOperatorEntity(names not matches "张.*")
    then
        System.out.println("规则rule_comparison_notMatches触发");
end

第三步:编写单元测试

//测试比较操作符
@Test
public void test3(){
    KieServices kieServices = KieServices.Factory.get();
    KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
    KieSession kieSession = kieClasspathContainer.newKieSession();

    ComparisonOperatorEntity comparisonOperatorEntity = new ComparisonOperatorEntity();
    comparisonOperatorEntity.setNames("张三");
    List<String> list = new ArrayList<String>();
    list.add("张三");
    list.add("李四");
    comparisonOperatorEntity.setList(list);

    //将数据提供给规则引擎,规则引擎会根据提供的数据进行规则匹配,如果规则匹配成功则执行规则
    kieSession.insert(comparisonOperatorEntity);

    kieSession.fireAllRules();
    kieSession.dispose();
}

2.6 执行指定规则

通过前面的案例可以看到,我们在调用规则代码时,满足条件的规则都会被执行。那么如果我们只想执行其中的某个规则如何实现呢?

Drools给我们提供的方式是通过规则过滤器来实现执行指定规则。对于规则文件不用做任何修改只需要修改Java代码即可如下

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

ComparisonOperatorEntity comparisonOperatorEntity = new ComparisonOperatorEntity();
comparisonOperatorEntity.setNames("张三");
List<String> list = new ArrayList<String>();
list.add("张三");
list.add("李四");
comparisonOperatorEntity.setList(list);
kieSession.insert(comparisonOperatorEntity);

//通过规则过滤器实现只执行指定规则
kieSession.fireAllRules(new RuleNameEqualsAgendaFilter("rule_comparison_memberOf"));

kieSession.dispose();

2.7 关键字

Drools的关键字分为硬关键字(Hard keywords)和软关键字(Soft keywords)。

硬关键字是我们在规则文件中定义包名或者规则名时明确不能使用的,否则程序会报错。软关键字虽然可以使用,但是不建议使用。

硬关键字包括true false null

软关键字包括: lock-on-active date-effective date-expires no-loop auto-focus activation-group agenda-group ruleflow-group entry-point duration package import dialect salience enabled attributes rule extend when then template query declare function global eval not in or and exists forall accumulate collect from action reverse result end over init

2.8 Drools内置方法

规则文件的RHS部分的主要作用是通过插入删除或修改工作内存中的Fact数据来达到控制规则引擎执行的目的。Drools提供了一些方法可以用来操作工作内存中的数据操作完成后规则引擎会重新进行相关规则的匹配原来没有匹配成功的规则在我们修改数据完成后有可能就会匹配成功了。

创建如下实体类:

package com.itheima.drools.entity;

import java.util.List;

/**
 * 学生
 */
public class Student {
    private int id;
    private String name;
    private int age;
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

4.8.1 update方法

update方法的作用是更新工作内存中的数据并让相关的规则重新匹配。

第一步:编写规则文件/resources/rules/student.drl文件内容如下

package student
import com.itheima.drools.entity.Student

/*
 当前规则文件用于测试Drools提供的内置方法
*/

rule "rule_student_age小于10岁"
    when
        $s:Student(age < 10)
    then
        $s.setAge(15);
        update($s);//更新数据,导致相关的规则会重新匹配
        System.out.println("规则rule_student_age小于10岁触发");
end

rule "rule_student_age小于20岁同时大于10岁"
    when
        $s:Student(age < 20 && age > 10)
    then
        $s.setAge(25);
        update($s);//更新数据,导致相关的规则会重新匹配
        System.out.println("规则rule_student_age小于20岁同时大于10岁触发");
end

rule "rule_student_age大于20岁"
    when
        $s:Student(age > 20)
    then
        System.out.println("规则rule_student_age大于20岁触发");
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

Student student = new Student();
student.setAge(5);

//将数据提供给规则引擎,规则引擎会根据提供的数据进行规则匹配,如果规则匹配成功则执行规则
kieSession.insert(student);

kieSession.fireAllRules();
kieSession.dispose();

通过控制台的输出可以看到规则文件中定义的三个规则都触发了。

在更新数据时需要注意防止发生死循环。

4.8.2 insert方法

insert方法的作用是向工作内存中插入数据并让相关的规则重新匹配。

第一步修改student.drl文件内容如下

package student
import com.itheima.drools.entity.Student

/*
 当前规则文件用于测试Drools提供的内置方法
*/

rule "rule_student_age等于10岁"
    when
        $s:Student(age == 10)
    then
        Student student = new Student();
        student.setAge(5);
        insert(student);//插入数据,导致相关的规则会重新匹配
        System.out.println("规则rule_student_age等于10岁触发");
end

rule "rule_student_age小于10岁"
    when
        $s:Student(age < 10)
    then
        $s.setAge(15);
        update($s);
        System.out.println("规则rule_student_age小于10岁触发");
end

rule "rule_student_age小于20岁同时大于10岁"
    when
        $s:Student(age < 20 && age > 10)
    then
        $s.setAge(25);
        update($s);
        System.out.println("规则rule_student_age小于20岁同时大于10岁触发");
end

rule "rule_student_age大于20岁"
    when
        $s:Student(age > 20)
    then
        System.out.println("规则rule_student_age大于20岁触发");
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

Student student = new Student();
student.setAge(10);

//将数据提供给规则引擎,规则引擎会根据提供的数据进行规则匹配,如果规则匹配成功则执行规则
kieSession.insert(student);

kieSession.fireAllRules();
kieSession.dispose();

通过控制台输出可以发现,四个规则都触发了,这是因为首先进行规则匹配时只有第一个规则可以匹配成功,但是在第一个规则中向工作内存中插入了一个数据导致重新进行规则匹配,此时第二个规则可以匹配成功。在第二个规则中进行了数据修改导致第三个规则也可以匹配成功,以此类推最终四个规则都匹配成功并执行了。

4.8.3 retract方法

retract方法的作用是删除工作内存中的数据并让相关的规则重新匹配。

第一步修改student.drl文件内容如下

package student
import com.itheima.drools.entity.Student

/*
 当前规则文件用于测试Drools提供的内置方法
*/

rule "rule_student_age等于10岁时删除数据"
    /*
    salience设置当前规则的执行优先级数值越大越优先执行默认值为0.
    因为当前规则的匹配条件和下面规则的匹配条件相同,为了保证先执行当前规则,需要设置优先级
    */
    salience 100 
    when
        $s:Student(age == 10)
    then
        retract($s);//retract方法的作用是删除工作内存中的数据并让相关的规则重新匹配。
        System.out.println("规则rule_student_age等于10岁时删除数据触发");
end

rule "rule_student_age等于10岁"
    when
        $s:Student(age == 10)
    then
        Student student = new Student();
        student.setAge(5);
        insert(student);
        System.out.println("规则rule_student_age等于10岁触发");
end

rule "rule_student_age小于10岁"
    when
        $s:Student(age < 10)
    then
        $s.setAge(15);
        update($s);
        System.out.println("规则rule_student_age小于10岁触发");
end

rule "rule_student_age小于20岁同时大于10岁"
    when
        $s:Student(age < 20 && age > 10)
    then
        $s.setAge(25);
        update($s);
        System.out.println("规则rule_student_age小于20岁同时大于10岁触发");
end

rule "rule_student_age大于20岁"
    when
        $s:Student(age > 20)
    then
        System.out.println("规则rule_student_age大于20岁触发");
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

Student student = new Student();
student.setAge(10);

//将数据提供给规则引擎,规则引擎会根据提供的数据进行规则匹配,如果规则匹配成功则执行规则
kieSession.insert(student);

kieSession.fireAllRules();
kieSession.dispose();

通过控制台输出可以发现,只有第一个规则触发了,因为在第一个规则中将工作内存中的数据删除了导致第二个规则并没有匹配成功。

5. 规则属性

前面我们已经知道了规则体的构成如下:

rule "ruleName"
    attributes
    when
        LHS
    then
        RHS
end

本章节就是针对规则体的attributes属性部分进行讲解。Drools中提供的属性如下表(部分属性)

属性名 说明
salience 指定规则执行优先级
dialect 指定规则使用的语言类型取值为java和mvel
enabled 指定规则是否启用
date-effective 指定规则生效时间
date-expires 指定规则失效时间
activation-group 激活分组,具有相同分组名称的规则只能有一个规则触发
agenda-group 议程分组,只有获取焦点的组中的规则才有可能触发
timer 定时器,指定规则触发的时间
auto-focus 自动获取焦点一般结合agenda-group一起使用
no-loop 防止死循环

5.1 enabled属性

enabled属性对应的取值为true和false默认值为true。

用于指定当前规则是否启用如果设置的值为false则当前规则无论是否匹配成功都不会触发。

rule "rule_comparison_notMemberOf"
    //指定当前规则不可用,当前规则无论是否匹配成功都不会执行
    enabled false
    when
        ComparisonOperatorEntity(names not memberOf list)
    then
        System.out.println("规则rule_comparison_notMemberOf触发");
end

5.2 dialect属性

dialect属性用于指定当前规则使用的语言类型取值为java和mvel默认值为java。

mvel是一种基于java语法的表达式语言。

mvel像正则表达式一样有直接支持集合、数组和字符串匹配的操作符。

mvel还提供了用来配置和构造字符串的模板语言。

mvel表达式内容包括属性表达式布尔表达式方法调用变量赋值函数定义等。

5.3 salience属性

salience属性用于指定规则的执行优先级取值类型为Integer。数值越大越优先执行。每个规则都有一个默认的执行顺序如果不设置salience属性规则体的执行顺序为由上到下。

可以通过创建规则文件salience.drl来测试salience属性内容如下

package test.salience

rule "rule_1"
    when
        eval(true)
    then
        System.out.println("规则rule_1触发");
end
    
rule "rule_2"
    when
        eval(true)
    then
        System.out.println("规则rule_2触发");
end

rule "rule_3"
    when
        eval(true)
    then
        System.out.println("规则rule_3触发");
end

通过控制台可以看到由于以上三个规则没有设置salience属性所以执行的顺序是按照规则文件中规则的顺序由上到下执行的。接下来我们修改一下文件内容

package testsalience

rule "rule_1"
    salience 9
    when
        eval(true)
    then
        System.out.println("规则rule_1触发");
end

rule "rule_2"
    salience 10
    when
        eval(true)
    then
        System.out.println("规则rule_2触发");
end

rule "rule_3"
    salience 8
    when
        eval(true)
    then
        System.out.println("规则rule_3触发");
end

通过控制台可以看到规则文件执行的顺序是按照我们设置的salience值由大到小顺序执行的。

建议在编写规则时使用salience属性明确指定执行优先级。

5.4 no-loop属性

no-loop属性用于防止死循环当规则通过update之类的函数修改了Fact对象时可能使当前规则再次被激活从而导致死循环。取值类型为Boolean默认值为false。测试步骤如下

第一步:编写规则文件/resource/rules/noloop.drl

package testnoloop
import com.itheima.drools.entity.Student
/*
    此规则文件用于测试no-loop属性
*/
rule "rule_noloop"
    when
    	// no-loop true
        $student:Student(age == 25)
    then
        update($student);//注意此处执行update会导致当前规则重新被激活
        System.out.println("规则rule_noloop触发");
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

Student student = new Student();
student.setAge(25);

//将数据提供给规则引擎,规则引擎会根据提供的数据进行规则匹配,如果规则匹配成功则执行规则
kieSession.insert(student);

kieSession.fireAllRules();
kieSession.dispose();

通过控制台可以看到由于我们没有设置no-loop属性的值所以发生了死循环。接下来设置no-loop的值为true再次测试则不会发生死循环。

5.5 activation-group属性

activation-group属性是指激活分组取值为String类型。具有相同分组名称的规则只能有一个规则被触发。

第一步:编写规则文件/resources/rules/activationgroup.drl

package testactivationgroup
/*
    此规则文件用于测试activation-group属性
*/
    
rule "rule_activationgroup_1"
    activation-group "mygroup"
    when
    then
        System.out.println("规则rule_activationgroup_1触发");
end

rule "rule_activationgroup_2"
    activation-group "mygroup"
    when
    then
        System.out.println("规则rule_activationgroup_2触发");
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();
kieSession.fireAllRules();
kieSession.dispose();

通过控制台可以发现上面的两个规则因为属于同一个分组所以只有一个触发了。同一个分组中的多个规则如果都能够匹配成功具体哪一个最终能够被触发可以通过salience属性确定。

5.6 agenda-group属性

agenda-group属性为议程分组属于另一种可控的规则执行方式。用户可以通过设置agenda-group来控制规则的执行只有获取焦点的组中的规则才会被触发。

第一步:创建规则文件/resources/rules/agendagroup.drl

package testagendagroup
/*
	此规则文件用于测试agenda-group属性
*/
rule "rule_agendagroup_1"
    agenda-group "myagendagroup_1"
    when
    then
        System.out.println("规则rule_agendagroup_1触发");
end

rule "rule_agendagroup_2"
    agenda-group "myagendagroup_1"
    when
    then
        System.out.println("规则rule_agendagroup_2触发");
end
//========================================================
rule "rule_agendagroup_3"
    agenda-group "myagendagroup_2"
    when
    then
        System.out.println("规则rule_agendagroup_3触发");
end

rule "rule_agendagroup_4"
    agenda-group "myagendagroup_2"
    when
    then
        System.out.println("规则rule_agendagroup_4触发");
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

//设置焦点对应agenda-group分组中的规则才可能被触发
kieSession.getAgenda().getAgendaGroup("myagendagroup_1").setFocus();

kieSession.fireAllRules();
kieSession.dispose();

通过控制台可以看到只有获取焦点的分组中的规则才会触发。与activation-group不同的是activation-group定义的分组中只能够有一个规则可以被触发而agenda-group分组中的多个规则都可以被触发。

5.7 auto-focus属性

auto-focus属性为自动获取焦点取值类型为Boolean默认值为false。一般结合agenda-group属性使用当一个议程分组未获取焦点时可以设置auto-focus属性来控制。

第一步:修改/resources/rules/agendagroup.drl文件内容如下

package testagendagroup

rule "rule_agendagroup_1"
    agenda-group "myagendagroup_1"
    when
    then
        System.out.println("规则rule_agendagroup_1触发");
end

rule "rule_agendagroup_2"
    agenda-group "myagendagroup_1"
    when
    then
        System.out.println("规则rule_agendagroup_2触发");
end
//========================================================
rule "rule_agendagroup_3"
    agenda-group "myagendagroup_2"
    auto-focus true //自动获取焦点
    when
    then
        System.out.println("规则rule_agendagroup_3触发");
end

rule "rule_agendagroup_4"
    agenda-group "myagendagroup_2"
    auto-focus true //自动获取焦点
    when
    then
        System.out.println("规则rule_agendagroup_4触发");
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();
kieSession.fireAllRules();
kieSession.dispose();

通过控制台可以看到设置auto-focus属性为true的规则都触发了。

5.8 timer属性

timer属性可以通过定时器的方式指定规则执行的时间使用方式有两种

方式一timer (int: <initial delay> <repeat interval>?)

此种方式遵循java.util.Timer对象的使用方式第一个参数表示几秒后执行第二个参数表示每隔几秒执行一次第二个参数为可选。

方式二timer(cron: <cron expression>)

此种方式使用标准的unix cron表达式的使用方式来定义规则执行的时间。

第一步:创建规则文件/resources/rules/timer.drl

package testtimer
import java.text.SimpleDateFormat
import java.util.Date
/*
    此规则文件用于测试timer属性
*/

rule "rule_timer_1"
    timer (5s 2s) //含义5秒后触发然后每隔2秒触发一次
    when
    then
        System.out.println("规则rule_timer_1触发触发时间为" + 
                         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
end

rule "rule_timer_2"
    timer (cron:0/1 * * * * ?) //含义每隔1秒触发一次
    when
    then
        System.out.println("规则rule_timer_2触发触发时间为" + 
                         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
final KieSession kieSession = kieClasspathContainer.newKieSession();

new Thread(new Runnable() {
    public void run() {
        //启动规则引擎进行规则匹配直到调用halt方法才结束规则引擎
        kieSession.fireUntilHalt();
    }
}).start();

Thread.sleep(10000);
//结束规则引擎
kieSession.halt();
kieSession.dispose();

注意单元测试的代码和以前的有所不同因为我们规则文件中使用到了timer进行定时执行需要程序能够持续一段时间才能够看到定时器触发的效果。

5.9 date-effective属性

date-effective属性用于指定规则的生效时间即只有当前系统时间大于等于设置的时间或者日期规则才有可能触发。默认日期格式为dd-MMM-yyyy。用户也可以自定义日期格式。

第一步:编写规则文件/resources/rules/dateeffective.drl

package testdateeffective
/*
    此规则文件用于测试date-effective属性
*/
rule "rule_dateeffective_1"
    date-effective "2020-10-01 10:00"
    when
    then
        System.out.println("规则rule_dateeffective_1触发");
end

第二步:编写单元测试

//设置日期格式
System.setProperty("drools.dateformat","yyyy-MM-dd HH:mm");
KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();
kieSession.fireAllRules();
kieSession.dispose();

注意:上面的代码需要设置日期格式,否则我们在规则文件中写的日期格式和默认的日期格式不匹配程序会报错。

5.10 date-expires属性

date-expires属性用于指定规则的失效时间即只有当前系统时间小于设置的时间或者日期规则才有可能触发。默认日期格式为dd-MMM-yyyy。用户也可以自定义日期格式。

第一步:编写规则文件/resource/rules/dateexpires.drl

package testdateexpires
/*
    此规则文件用于测试date-expires属性
*/

rule "rule_dateexpires_1"
    date-expires "2019-10-01 10:00"
    when
    then
        System.out.println("规则rule_dateexpires_1触发");
end

第二步:编写单元测试

//设置日期格式
System.setProperty("drools.dateformat","yyyy-MM-dd HH:mm");
KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();
kieSession.fireAllRules();
kieSession.dispose();

注意:上面的代码需要设置日期格式,否则我们在规则文件中写的日期格式和默认的日期格式不匹配程序会报错。

6. Drools高级语法

前面章节我们已经知道了一套完整的规则文件内容构成如下:

关键字 描述
package 包名,只限于逻辑上的管理,同一个包名下的查询或者函数可以直接调用
import 用于导入类或者静态方法
global 全局变量
function 自定义函数
query 查询
rule end 规则体

本章节我们就来学习其中的几个关键字。

6.1 global全局变量

global关键字用于在规则文件中定义全局变量它可以让应用程序的对象在规则文件中能够被访问。可以用来为规则文件提供数据或服务。

语法结构为:global 对象类型 对象名称

在使用global定义的全局变量时有两点需要注意

1、如果对象类型为包装类型时在一个规则中改变了global的值那么只针对当前规则有效对其他规则中的global不会有影响。可以理解为它是当前规则代码中的global副本规则内部修改不会影响全局的使用。

2、如果对象类型为集合类型或JavaBean时在一个规则中改变了global的值对java代码和所有规则都有效。

下面我们通过代码进行验证:

第一步创建UserService类

package com.itheima.drools.service;

public class UserService {
    public void save(){
        System.out.println("UserService.save()...");
    }
}

第二步:编写规则文件/resources/rules/global.drl

package testglobal
/*
    此规则文件用于测试global全局变量
*/

global java.lang.Integer count //定义一个包装类型的全局变量
global com.itheima.drools.service.UserService userService //定义一个JavaBean类型的全局变量
global java.util.List gList //定义一个集合类型的全局变量

rule "rule_global_1"
    when
    then
        count += 10; //全局变量计算,只对当前规则有效,其他规则不受影响
        userService.save();//调用全局变量的方法
        gList.add("itcast");//向集合类型的全局变量中添加元素Java代码和所有规则都受影响
        gList.add("itheima");
        System.out.println("count=" + count);
        System.out.println("gList.size=" + gList.size());
end

rule "rule_global_2"
    when
    then
        userService.save();
        System.out.println("count=" + count);
        System.out.println("gList.size=" + gList.size());
end

第三步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

//设置全局变量,名称和类型必须和规则文件中定义的全局变量名称对应
kieSession.setGlobal("userService",new UserService());
kieSession.setGlobal("count",5);
List list = new ArrayList();//size为0
kieSession.setGlobal("gList",list);

kieSession.fireAllRules();
kieSession.dispose();

//因为在规则中为全局变量添加了两个元素所以现在的size为2
System.out.println(list.size());

6.2 query查询

query查询提供了一种查询working memory中符合约束条件的Fact对象的简单方法。它仅包含规则文件中的LHS部分不用指定“when”和“then”部分并且以end结束。具体语法结构如下

query 查询的名称(可选参数)
	LHS
end

具体操作步骤:

第一步:编写规则文件/resources/rules/query.drl

package testquery
import com.itheima.drools.entity.Student
/*
    此规则文件用于测试query查询
*/

//不带参数的查询
//当前query用于查询Working Memory中age>10的Student对象
query "query_1"
    $student:Student(age > 10)
end

//带有参数的查询
//当前query用于查询Working Memory中age>10同时name需要和传递的参数name相同的Student对象
query "query_2"(String sname)
    $student:Student(age > 20 && name == sname)
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

Student student1 = new Student();
student1.setName("张三");
student1.setAge(12);

Student student2 = new Student();
student2.setName("李四");
student2.setAge(8);

Student student3 = new Student();
student3.setName("王五");
student3.setAge(22);

//将对象插入Working Memory中
kieSession.insert(student1);
kieSession.insert(student2);
kieSession.insert(student3);

//调用规则文件中的查询
QueryResults results1 = kieSession.getQueryResults("query_1");
int size = results1.size();
System.out.println("size=" + size);
for (QueryResultsRow row : results1) {
    Student student = (Student) row.get("$student");
    System.out.println(student);
}

//调用规则文件中的查询
QueryResults results2 = kieSession.getQueryResults("query_2","王五");
size = results2.size();
System.out.println("size=" + size);
for (QueryResultsRow row : results2) {
    Student student = (Student) row.get("$student");
    System.out.println(student);
}
//kieSession.fireAllRules();
kieSession.dispose();

6.3 function函数

function关键字用于在规则文件中定义函数就相当于java类中的方法一样。可以在规则体中调用定义的函数。使用函数的好处是可以将业务逻辑集中放置在一个地方根据需要可以对函数进行修改。

函数定义的语法结构如下:

function 返回值类型 函数名(可选参数){
	//逻辑代码
}

具体操作步骤:

第一步:编写规则文件/resources/rules/function.drl

package testfunction
import com.itheima.drools.entity.Student
/*
    此规则文件用于测试function函数
*/

//定义一个函数
function String sayHello(String name){
    return "hello " + name;
}

rule "rule_function_1"
    when
        $student:Student(name != null)
    then
        //调用上面定义的函数
        String ret = sayHello($student.getName());
        System.out.println(ret);
end

第二步:编写单元测试

KieServices kieServices = KieServices.Factory.get();
KieContainer kieClasspathContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieClasspathContainer.newKieSession();

Student student = new Student();
student.setName("小明");

kieSession.insert(student);

kieSession.fireAllRules();
kieSession.dispose();

6.4 LHS加强

前面我们已经知道了在规则体中的LHS部分是介于when和then之间的部分主要用于模式匹配只有匹配结果为true时才会触发RHS部分的执行。本章节我们会针对LHS部分学习几个新的用法。

6.4.1 复合值限制in/not in

复合值限制是指超过一种匹配值的限制条件类似于SQL语句中的in关键字。Drools规则体中的LHS部分可以使用in或者not in进行复合值的匹配。具体语法结构如下

Object(field in (比较值1,比较值2...))

举例:

$s:Student(name in ("张三","李四","王五"))
$s:Student(name not in ("张三","李四","王五"))
6.4.2 条件元素eval

eval用于规则体的LHS部分并返回一个Boolean类型的值。语法结构如下

eval(表达式)

举例:

eval(true)
eval(false)
eval(1 == 1)
6.4.3 条件元素not

not用于判断Working Memory中是否存在某个Fact对象如果不存在则返回true如果存在则返回false。语法结构如下

not Object(可选属性约束)

举例:

not Student()
not Student(age < 10)
6.4.4 条件元素exists

exists的作用与not相反用于判断Working Memory中是否存在某个Fact对象如果存在则返回true不存在则返回false。语法结构如下

exists Object(可选属性约束)

举例:

exists Student()
exists Student(age < 10 && name != null)

可能有人会有疑问我们前面在LHS部分进行条件编写时并没有使用exists也可以达到判断Working Memory中是否存在某个符合条件的Fact元素的目的那么我们使用exists还有什么意义

两者的区别当向Working Memory中加入多个满足条件的Fact对象时使用了exists的规则执行一次不使用exists的规则会执行多次。

例如:

规则文件(只有规则体)

rule "使用exists的规则"
    when
    	exists Student()
    then
    	System.out.println("规则使用exists的规则触发");
end

rule "没有使用exists的规则"
    when
    	Student()
    then
    	System.out.println("规则没有使用exists的规则触发");
end

Java代码

kieSession.insert(new Student());
kieSession.insert(new Student());
kieSession.fireAllRules();

上面第一个规则只会执行一次因为Working Memory中存在两个满足条件的Fact对象第二个规则会执行两次。

6.4.5 规则继承

规则之间可以使用extends关键字进行规则条件部分的继承类似于java类之间的继承。

例如:

rule "rule_1"
    when
    	Student(age > 10)
    then
    	System.out.println("规则rule_1触发");
end

rule "rule_2" extends "rule_1" //继承上面的规则
    when
    	/*
    	此处的条件虽然只写了一个,但是从上面的规则继承了一个条件,
    	所以当前规则存在两个条件即Student(age < 20)和Student(age > 10)
    	*/
    	Student(age < 20) 
    then
    	System.out.println("规则rule_2触发");
end

6.5 RHS加强

RHS部分是规则体的重要组成部分当LHS部分的条件匹配成功后对应的RHS部分就会触发执行。一般在RHS部分中需要进行业务处理。

在RHS部分Drools为我们提供了一个内置对象名称就是drools。本小节我们来介绍几个drools对象提供的方法。

6.5.1 halt

halt方法的作用是立即终止后面所有规则的执行。

package testhalt
rule "rule_halt_1"
    when
    then
    	System.out.println("规则rule_halt_1触发");
		drools.halt();//立即终止后面所有规则执行
end

//当前规则并不会触发因为上面的规则调用了halt方法导致后面所有规则都不会执行
rule "rule_halt_2"
    when
    then
    	System.out.println("规则rule_halt_2触发");
end
6.5.2 getWorkingMemory

getWorkingMemory方法的作用是返回工作内存对象。

package testgetWorkingMemory
rule "rule_getWorkingMemory"
    when
    then
    	System.out.println(drools.getWorkingMemory());
end
6.5.3 getRule

getRule方法的作用是返回规则对象。

package testgetRule
rule "rule_getRule"
    when
    then
    	System.out.println(drools.getRule());
end

6.6 规则文件编码规范

我们在进行drl类型的规则文件编写时尽量遵循如下规范

  • 所有的规则文件(.drl)应统一放在一个规定的文件夹中,如:/rules文件夹
  • 书写的每个规则应尽量加上注释。注释要清晰明了,言简意赅
  • 同一类型的对象尽量放在一个规则文件中如所有Student类型的对象尽量放在一个规则文件中
  • 规则结果部分(RHS)尽量不要有条件语句如if(...),尽量不要有复杂的逻辑和深层次的嵌套语句
  • 每个规则最好都加上salience属性明确执行顺序
  • Drools默认dialect为"Java"尽量避免使用dialect "mvel"

7. SpringBoot 整合 Drools