当前位置:网站首页>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
边栏推荐
- 记录一下MySql update会锁定哪些范围的数据
- Natural language processing series (III) -- LSTM
- Deep understanding of P-R curve, ROC and AUC
- (C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
- 堆(優先級隊列)
- 初始JDBC 编程
- Read the Flink source code and join Alibaba cloud Flink group..
- Leetcode922 sort array by parity II
- Yygh-10-wechat payment
- Leetcode topic [array] -540- single element in an ordered array
猜你喜欢
自然语言处理系列(二)——使用RNN搭建字符级语言模型
CDH6之Sqoop添加数据库驱动
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Mish shake the new successor of the deep learning relu activation function
Dynamic memory (advanced 4)
CONDA common command summary
PyTorch nn. Full analysis of RNN parameters
MSI announced that its motherboard products will cancel all paper accessories
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
[geek challenge 2019] upload
随机推荐
Tas (file d'attente prioritaire)
Full link voltage measurement
Uniapp uni list item @click, uniapp uni list item jump with parameters
Test shift left and right
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
From scratch, develop a web office suite (3): mouse events
Le tutoriel F - String le plus facile à comprendre de l'histoire.
甜心教主:王心凌
HR wonderful dividing line
Leetcode922 按奇偶排序数组 II
Industry analysis
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
PyTorch nn. Full analysis of RNN parameters
mysql表的增删改查(进阶)
to_ Bytes and from_ Bytes simple example
Those logs in MySQL
xss-labs-master靶场环境搭建与1-6关解题思路
高德地图测试用例
lombok常用注解