当前位置:网站首页>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
边栏推荐
猜你喜欢
MSI announced that its motherboard products will cancel all paper accessories
(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?
CDA数据分析——AARRR增长模型的介绍、使用
[C language] convert decimal numbers to binary numbers
Power Spectral Density Estimates Using FFT---MATLAB
GGPlot Examples Best Reference
YYGH-BUG-04
How to Visualize Missing Data in R using a Heatmap
Yygh-9-make an appointment to place an order
Seriation in R: How to Optimally Order Objects in a Data Matrice
随机推荐
How to Easily Create Barplots with Error Bars in R
Orb-slam2 data sharing and transmission between different threads
Leetcode922 按奇偶排序数组 II
Jenkins voucher management
mysql索引和事务
From scratch, develop a web office suite (3): mouse events
Mish-撼动深度学习ReLU激活函数的新继任者
【2022 ACTF-wp】
Leetcode122 买卖股票的最佳时机 II
ORB-SLAM2不同线程间的数据共享与传递
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
The most understandable f-string tutorial in history, collecting this one is enough
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
【C语言】杨辉三角,自定义三角的行数
甜心教主:王心凌
mysql数据库基础
Tas (file d'attente prioritaire)
Leetcode209 subarray with the smallest length
史上最易懂的f-string教程,收藏這一篇就够了
ES集群中节点与分片的区别