当前位置:网站首页>Drools executes the specified rule
Drools executes the specified rule
2022-07-02 12:13:00 【huan_ one thousand nine hundred and ninety-three】
1、 background
Before we write drools In the process of rule file , May write a lot of rules .drools When the engine matches patterns , It may activate many rules at once , But I just want to enforce a specified rule , So how to operate at this time ?
2、 programme
2.1 adopt AgendaFilter To achieve
We know , stay drools When pattern matching , Will match all the rules , The rules that match successfully will be put into Agenda( agenda ) in , and fireAllRules(AgendaFilter) Method , You can pass a AgendaFilter Yes Agenda Filter the activated rules in .
2.2 adopt entry-point To achieve
Use entry-point You can define the entry point or event flow corresponding to the data source of the schema . This cannot be fully realized , It's just a way of thinking .
for instance :
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();
The above example , Although there are... In working memory Api This object , But because of In the rules entry-point and java In code entry-point atypism , Rules are not matched successfully .
3、 Realization
Here we pass AgendaFilter To call a specific rule .
3.1 demand
We have a Api(api,invokedCnt) object
Rule one : There is... In the working memory Api object , And attribute api=="/users/info".
Rule 2 : There is... In the working memory Api object , And attribute invokedCnt > 10.
We insert a... Into the working memory Api("/users/info",100), At this point, both rule 1 and rule 2 will match , But I just want to enforce rule two .
3.2 drl Documentation
package rules
import com.huan.drools.Api
rule "rule_agenda_filter_01"
when
$api: Api(api == "/users/info" )
then
System.out.println(" The current rule is : " + drools.getRule().getName());
end
rule "rule_agenda_filter_02"
when
$api: Api(invokedCnt > 10)
then
System.out.println(" The current rule is : " + drools.getRule().getName());
end
3.3 part java Code
Api api = new Api("/users/info", 100);
kieSession.insert(api);
// After all the patterns are matched successfully, the rules will go back to agenda in , And then through AgendaFilter Filter out the rules to be executed
kieSession.fireAllRules(new AgendaFilter() {
@Override
public boolean accept(Match match) {
String ruleName = match.getRule().getName();
return Objects.equals(ruleName, "rule_agenda_filter_02");
}
});
You can see here through AgendaFilter Filter the rules , Only execute rule_agenda_filter_02 The rules .
Be careful : Although this is a filter , But that doesn't mean only rule_agenda_filter_02 The rule is active , But all the rules that meet the conditions are activated .
3.4 Running results
The current rule is : rule_agenda_filter_02
We can see that we have achieved the results we want .
4、 Complete code
https://gitee.com/huan1993/spring-cloud-parent/tree/master/drools/drools-invoked-specify-rule
边栏推荐
猜你喜欢

堆(优先级队列)

Mish shake the new successor of the deep learning relu activation function

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

ES集群中节点与分片的区别

The blink code based on Arduino and esp8266 runs successfully (including error analysis)

CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节

自然语言处理系列(一)——RNN基础

PyTorch nn.RNN 参数全解析

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement

Pytorch builds LSTM to realize clothing classification (fashionmnist)
随机推荐
PyTorch nn.RNN 参数全解析
Larvel modify table fields
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
CDA data analysis -- Introduction and use of aarrr growth model
Go学习笔记—多线程
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
【C语言】十进制数转换成二进制数
刷题---二叉树--2
From scratch, develop a web office suite (3): mouse events
Differences between nodes and sharding in ES cluster
Leetcode209 长度最小的子数组
B high and beautiful code snippet sharing image generation
Multiply LCA (nearest common ancestor)
From scratch, develop a web office suite (3): mouse events
CDA data analysis -- common knowledge points induction of Excel data processing
子线程获取Request
Test shift left and right
drools执行指定的规则
YYGH-BUG-04