当前位置:网站首页>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
边栏推荐
- lombok常用注解
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- Maximum profit of jz63 shares
- (C language) octal conversion decimal
- String palindrome hash template question o (1) judge whether the string is palindrome
- Multiply LCA (nearest common ancestor)
- This article takes you to understand the operation of vim
- HR wonderful dividing line
- Heap (priority queue)
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
猜你喜欢
YYGH-BUG-04
drools动态增加、修改、删除规则
From scratch, develop a web office suite (3): mouse events
甜心教主:王心凌
深入理解PyTorch中的nn.Embedding
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Natural language processing series (II) -- building character level language model using RNN
Depth filter of SvO2 series
YYGH-BUG-05
随机推荐
Sort---
刷题---二叉树--2
LeetCode—剑指 Offer 37、38
jenkins 凭证管理
Intel 内部指令 --- AVX和AVX2学习笔记
lombok常用注解
CDA数据分析——AARRR增长模型的介绍、使用
Multiply LCA (nearest common ancestor)
Full link voltage measurement
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
FastDateFormat为什么线程安全
浅谈sklearn中的数据预处理
JZ63 股票的最大利润
CDA data analysis -- Introduction and use of aarrr growth model
Map和Set
Codeforces 771 div2 B (no one FST, refers to himself)
Yygh-9-make an appointment to place an order
On data preprocessing in sklearn
(C语言)八进制转换十进制
Mish shake the new successor of the deep learning relu activation function