当前位置:网站首页>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
边栏推荐
- [geek challenge 2019] upload
- XSS labs master shooting range environment construction and 1-6 problem solving ideas
- Some problems encountered in introducing lvgl into esp32 Arduino
- CDA data analysis -- Introduction and use of aarrr growth model
- drools决策表的简单使用
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- YYGH-10-微信支付
- Leetcode209 subarray with the smallest length
- Uniapp uni list item @click, uniapp uni list item jump with parameters
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
猜你喜欢

Test shift left and right

基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)

HOW TO ADD P-VALUES TO GGPLOT FACETS

Some problems encountered in introducing lvgl into esp32 Arduino

Natural language processing series (II) -- building character level language model using RNN

Pytorch builds LSTM to realize clothing classification (fashionmnist)

mysql索引和事务

From scratch, develop a web office suite (3): mouse events

堆(優先級隊列)

甜心教主:王心凌
随机推荐
机械臂速成小指南(七):机械臂位姿的描述方法
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
Uniapp uni list item @click, uniapp uni list item jump with parameters
Deep understanding of P-R curve, ROC and AUC
Leetcode14 longest public prefix
Yygh-9-make an appointment to place an order
Natural language processing series (III) -- LSTM
PyTorch搭建LSTM实现服装分类(FashionMNIST)
How to Easily Create Barplots with Error Bars in R
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Jenkins用户权限管理
B high and beautiful code snippet sharing image generation
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Log4j2
【工控老马】西门子PLC Siemens PLC TCP协议详解
conda常用命令汇总
Leetcode739 daily temperature
MSI announced that its motherboard products will cancel all paper accessories