当前位置:网站首页>drools执行完某个规则后终止别的规则执行
drools执行完某个规则后终止别的规则执行
2022-07-02 09:42:00 【huan_1993】
1、背景
在我们开发的过程中,可能一个场景需要写多个规则,但是某些规则和别的规则又是互斥的关系。即某个规则成立时,不管其余的规则是否成立,其余的规则都不在执行。
那么这种场景该如何处理呢?
2、需求
假设我们有三个规则 rule-01
、rule-02
和rule-03
,当rule-02
执行后,那么rule-03
必须终止执行。
3、实现方案
1、通过Fact判断
即当rule-02
规则执行后,修改Fact
中的某个属性,设置规则已执行,后续规则发现是已执行则不在执行规则。
2、通过全局变量判断
在drools中使用全局变量进行判断。
3、通过halt方法
当我们在drl
文件中调用drools.halt()
方法后,它会立即终止它后面的规则,即后面的规则都不在执行。
此处我们通过halt
方法来实现。
4、实现上述需求
4.1 drl 文件编写
package rules
/** 需求:在 "rule-01" 和 "rule-02" 规则执行完之后,终止规则 "rule-03" 的执行 */
rule "rule-01"
salience 3
when
then
System.out.println(drools.getRule().getName());
end
rule "rule-02"
salience 2
when
then
System.out.println(drools.getRule().getName());
// 终止下方的 "rule-03" 规则的执行
drools.halt();
end
rule "rule-03"
salience 1
when
then
System.out.println(drools.getRule().getName());
end
上述的规则比较简单,通过salience
设置规则的优先级,值越大优先级越高。when
后面没有条件,说明规则都会通过模式匹配,即都可能会执行。但是rule-02
规则的then
部分有drools.halt()
,说明,该规则执行完之后,它后方激活的规则需要终止执行。
4.2 运行结果
rule-01
rule-02
可以看到rule-03
没有输出出来,说明这个规则被终止执行了。
5、完整代码
https://gitee.com/huan1993/spring-cloud-parent/tree/master/drools/drools-termination-rule
边栏推荐
- post请求体内容无法重复获取
- [C language] convert decimal numbers to binary numbers
- (C语言)八进制转换十进制
- Leetcode209 长度最小的子数组
- Deep understanding of NN in pytorch Embedding
- Data analysis - Matplotlib sample code
- XSS labs master shooting range environment construction and 1-6 problem solving ideas
- Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
- 多文件程序X32dbg动态调试
- 自然语言处理系列(一)——RNN基础
猜你喜欢
随机推荐
YYGH-10-微信支付
XSS labs master shooting range environment construction and 1-6 problem solving ideas
Industry analysis
[untitled] how to mount a hard disk in armbian
drools执行String规则或执行某个规则文件
ThreadLocal的简单理解
Leetcode209 长度最小的子数组
Gaode map test case
Fabric. JS 3 APIs to set canvas width and height
SCM power supply
conda常用命令汇总
Jenkins用户权限管理
On data preprocessing in sklearn
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
JZ63 股票的最大利润
File operation (detailed!)
倍增 LCA(最近公共祖先)
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Jenkins voucher management
Pytorch builds LSTM to realize clothing classification (fashionmnist)