当前位置:网站首页>drools执行指定的规则
drools执行指定的规则
2022-07-02 09:43:00 【huan_1993】
1、背景
在我们编写drools规则文件的过程中,可能会编写好多规则。drools引擎在模式匹配的时候,可能一下子激活了好多规则,但是我只想执行某个指定的规则,那么这个时候该怎么操作呢?
2、方案
2.1 通过AgendaFilter来实现
我们知道,在drools模式匹配的时候,会将所有的规则进行匹配,匹配成功的规则会放入到Agenda(议程)中,而fireAllRules(AgendaFilter)方法,可以传递一个AgendaFilter对Agenda中的激活的规则进行过滤。
2.2 通过entry-point来实现
使用entry-point可以定义模式的数据源对应的入口点或事件流。这个不能完全实现,只是提供一个思路。
举个例子:
Api(api == "/users/info" ) from entry-point "first-entry-point"
Api api = new Api("/users/info", 100);
EntryPoint entryPoint = kieSession.getEntryPoint("second-entry-point");
entryPoint.insert(api);
kieSession.fireAllRules();
上面这个例子,虽然工作内存中存在Api这个对象,但是由于 规则中的 entry-point和java代码中的entry-point不一致,规则是没有匹配成功的。
3、实现
此处我们通过 AgendaFilter来实现具体调用某个具体的规则。
3.1 需求
我们存在一个Api(api,invokedCnt)对象
规则一:工作内存中存在Api对象,且属性api=="/users/info"。
规则二:工作内存中存在Api对象,且属性invokedCnt > 10。
我们向工作内存中插入一个 Api("/users/info",100),此时规则一和规则二都会匹配到,但是我只想执行规则二。
3.2 drl 文件编写
package rules
import com.huan.drools.Api
rule "rule_agenda_filter_01"
when
$api: Api(api == "/users/info" )
then
System.out.println("当前执行的规则是: " + drools.getRule().getName());
end
rule "rule_agenda_filter_02"
when
$api: Api(invokedCnt > 10)
then
System.out.println("当前执行的规则是: " + drools.getRule().getName());
end
3.3 部分java代码
Api api = new Api("/users/info", 100);
kieSession.insert(api);
// 所有模式匹配成功后的规则回进入到agenda中,然后通过AgendaFilter过滤出需要执行的规则
kieSession.fireAllRules(new AgendaFilter() {
@Override
public boolean accept(Match match) {
String ruleName = match.getRule().getName();
return Objects.equals(ruleName, "rule_agenda_filter_02");
}
});
可以看到此处通过AgendaFilter进行了规则的过滤,只执行rule_agenda_filter_02规则。
注意: 此处虽然是一个过滤,但是并不意味着只有rule_agenda_filter_02规则是激活的,而是所有满足条件的规则都是激活的。
3.4 运行结果
当前执行的规则是: rule_agenda_filter_02
可以看到实现了我们想要的结果。
4、完整代码
https://gitee.com/huan1993/spring-cloud-parent/tree/master/drools/drools-invoked-specify-rule
边栏推荐
- Leetcode topic [array] -540- single element in an ordered array
- Log4j2
- Input a three digit number and output its single digit, ten digit and hundred digit.
- lombok常用注解
- 浅谈sklearn中的数据预处理
- CDA data analysis -- Introduction and use of aarrr growth model
- File operation (detailed!)
- Leetcode122 买卖股票的最佳时机 II
- 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?
猜你喜欢

【工控老马】西门子PLC Siemens PLC TCP协议详解

File operation (detailed!)

CDA数据分析——AARRR增长模型的介绍、使用

WSL 2 will not be installed yet? It's enough to read this article
![[C language] convert decimal numbers to binary numbers](/img/9b/1848b68b95d98389ed985c83f2e856.png)
[C language] convert decimal numbers to binary numbers

YYGH-BUG-05

【2022 ACTF-wp】

CONDA common command summary

Read the Flink source code and join Alibaba cloud Flink group..

drools决策表的简单使用
随机推荐
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
GGPlot Examples Best Reference
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
post请求体内容无法重复获取
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Leetcode209 subarray with the smallest length
Le tutoriel F - String le plus facile à comprendre de l'histoire.
CONDA common command summary
(C语言)八进制转换十进制
Input a three digit number and output its single digit, ten digit and hundred digit.
SSH automatically disconnects (pretends to be dead) after a period of no operation
PyTorch nn. Full analysis of RNN parameters
【C语言】十进制数转换成二进制数
自然语言处理系列(一)——RNN基础
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
jenkins 凭证管理
mysql索引和事务
YYGH-BUG-04
ES集群中节点与分片的区别
mysql表的增删改查(进阶)