SpringBoot整合 drools规则引擎 系列教程(一)快速集成上手java
教程代码已提交到ytooo-drools,欢迎stargit
一般,将包的全部规则与包声明存储在同一文件中,以便包是独立的。可是,
也能够在规则中使用从其余的包中导入的对象github
function String hello(String applicantName) { return "Hello " + applicantName + "!"; }
query "people under the age of 21" $person : Person( age < 21 ) end
属性 | 描述 |
---|---|
salience | 定义规则优先级的整数 |
enabled | 规则启用开关 |
date-effective | 包含日期和时间定义的字符串。仅当当前日期和时间在date-effective属性以后时,才能激活该规则。 |
date-expires | 若是当前日期和时间在date-expires属性以后,则没法激活该规则。 |
no-loop | 选择该选项后,若是规则的结果从新触发了先前知足的条件,则没法从新激活(循环)规则。若是未选择条件,则在这些状况下能够循环规则。 |
agenda-group | 标识要向其分配规则的议程组 |
activation-group | 激活组,在激活组中,只能激活一个规则。触发的第一个规则将取消激活组中全部规则的全部未决激活。 |
duration | 定义了若是仍知足规则条件,则能够激活规则的持续时间(以毫秒为单位)。 |
timer | cron定时表达式 |
calendar | 时钟 |
auto-focus | 仅适用于议程组中的规则。选择该选项后,下次激活该规则时,将自动将焦点分配给分配了该规则的议程组。 |
lock-on-active | no-loop属性的更强版 |
ruleflow-group | 标识规则流组的字符串 |
dialect | 用于标识规则中的代码表达式JAVA或MVEL将其用做语言 |
实事不须要知足任何条件,若类型相同,则触发该规则,如:正则表达式
package com.ytooo.bean import com.ytooo.bean.People dialect "java" rule "girl" when People() then System.out.println("girl规则执行"); end
实事类型相同,且知足条件,则触发该规则,如:segmentfault
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
实事类型相同,且知足条件,则触发该规则,并绑定数据,如:安全
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
and,or 等结合规则条件的多个模式,没有定义关键字连词,默认是and:session
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
标准Java运算符优先级适用于DRL中的约束运算符,而drl运算符除==和!=运算符外均遵循标准Java语义。app
在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 | 使用这些运算符能够指定一个以上的可能值来匹配约束(复合值限制) |
来自官方文档oop
Person( country matches "(USA)?\\S*UK" ) Person( country not matches "(USA)?\\S*UK" )
FamilyTree(countries contains "UK" ) Person( fullName not contains "Jr" ) FamilyTree(countries contains $var) Person( fullName not contains $var )
FamilyTree( person memberOf $europeanDescendants ) FamilyTree( person not memberOf $europeanDescendants )
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; }
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();//执行规则 }
People(sex=0, name=秋, drlType=from) People(sex=1, name=达, drlType=from)
从指定来源或从Drools引擎的工做内存中获取集合,可使用Java集合(例如List,LinkedList和HashSet)
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();//执行规则 }
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 的数据,则没有任何打印结果
用于遍历数据集对数据项执行自定义或预设动做并返回结果。
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
@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 $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