当前位置:网站首页>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
边栏推荐
- MSI announced that its motherboard products will cancel all paper accessories
- Natural language processing series (III) -- LSTM
- Mish shake the new successor of the deep learning relu activation function
- Orb-slam2 data sharing and transmission between different threads
- 浅谈sklearn中的数据预处理
- 自然语言处理系列(一)——RNN基础
- conda常用命令汇总
- mysql索引和事务
- Power Spectral Density Estimates Using FFT---MATLAB
- Leetcode122 the best time to buy and sell stocks II
猜你喜欢

(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
![[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)](/img/18/f0c9ef6250a717f8e66c95da4de08c.jpg)
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)

GGPlot Examples Best Reference

BEAUTIFUL GGPLOT VENN DIAGRAM WITH R

SVO2系列之深度濾波DepthFilter

(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

MSI announced that its motherboard products will cancel all paper accessories

YYGH-BUG-04

File operation (detailed!)

PyTorch nn. Full analysis of RNN parameters
随机推荐
自然语言处理系列(一)——RNN基础
Pytorch builds LSTM to realize clothing classification (fashionmnist)
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
Codeforces 771-div2 C (trouble, permutation is not very good)
drools执行String规则或执行某个规则文件
Leetcode209 subarray with the smallest length
排序---
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
Uniapp uni list item @click, uniapp uni list item jump with parameters
【2022 ACTF-wp】
File operation (detailed!)
Larvel modify table fields
Natural language processing series (II) -- building character level language model using RNN
YYGH-BUG-05
Mish shake the new successor of the deep learning relu activation function
CDA数据分析——AARRR增长模型的介绍、使用
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
Leetcode122 买卖股票的最佳时机 II
Leetcode922 sort array by parity II
使用Sqoop把ADS层数据导出到MySQL