当前位置:网站首页>Drools (4): drools basic syntax (2)
Drools (4): drools basic syntax (2)
2022-07-26 07:02:00 【Undead bird. Alexander. Wolf cub】
The rule syntax of the result part
stay Drools among , stay RHS Inside , Provides some information on the current Working Memory Macro macro function or object to realize fast operation , such as insert/insertLogical、 update and retract You can achieve the current Working Memory Medium Fact Object into ⾏ newly added 、 Delete or modify
1 insert
function insert The role of Java Call in class StatefulKnowledgeSession Object's insert⽅ The work of law ⽤ identical , It's all used to ⼀ individual Fact Object interpolation ⼊ To the present Working Memory among
Attention should be paid to :⼀ Dan tone ⽤ insert Macro functions , that Drools It will re match with all the rules ⼀ Time , For no settings no-loop The attribute is true The rules of , If the conditions are satisfied ⾜, Whether or not it has been carried out before ⾏ I will cling to it again after I have passed ⾏⼀ Time , This feature does not exist only in insert On macro function , after ⾯ To introduce the update、retract Macro functions also have this feature , So in some cases, due to improper consideration ⽤ insert、update or retract Prone to life and death cycle
Example :
//Drools Built in ⽅ Law insert
rule "rule5"
when
eval(true);
// Default to ⽴
then
Customer cus=new Customer();
cus.setName(" Zhang San ");
insert(cus);
System.out.println(" test Drools Built in ⽅ Law insert Trigger ...");
end
rule "rule6"
when
$customer:Customer(name ==" Zhang San ");
then
System.out.println(" test Drools Built in ⽅ Law insert touch Hair ..."+$customer.getName());
endtest :
@Test
public void test4() {
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
// Conversation object ,⽤ Interact with the rule engine
KieSession kieSession = kieContainer.newKieSession();
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules();
// Close session
kieSession.dispose();
}The results are as follows :

insertLogical
insertLogical The functions and insert similar , Its work ⽤ Will be ⼀ individual Fact Object interpolation ⼊ To the present Working Memroy among
2 update
update Function meaning and its name ⼀ sample ,⽤ To realize the current Working Memory In the middle of Fact Into the ⾏ to update , Used to tell the current Working Memory The Fact The object has changed .
Example :
rule "rule7"
//no-loop true
when
$customer:Customer(name ==" Li Si ");
then
$customer.setName(" Zhang San ");
update($customer);
System.out.println(" test Drools Built in ⽅ Law update Trigger ...");
end
rule "rule8"
when
$customer:Customer(name ==" Zhang San ");
then
System.out.println(" test Drools Built in ⽅ Law update touch Hair ..."+$customer.getName());
endtest :
@Test
public void test5() {
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
// Conversation object ,⽤ Interact with the rule engine
KieSession kieSession = kieContainer.newKieSession();
Customer customer = new Customer();
customer.setName(" Li Si ");
kieSession.insert(customer);
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules();
// Close session
kieSession.dispose();
}The results are as follows :

3 retract
retract⽤ to Working Memory One of them Fact Objects from Working Memory Delete
//Drools Built in ⽅ Law retract
rule "rule9"
when
$customer:Customer(name ==" Li Si ");
then
retract($customer);
System.out.println(" test Drools Built in ⽅ Law retract Trigger ...");
end
rule "rule10"
when
$customer:Customer();
then
System.out.println(" test Drools Built in ⽅ Law retract touch Hair ..."+$customer.getName());
endtest :
@Test
public void test6() {
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
// Conversation object ,⽤ Interact with the rule engine
KieSession kieSession = kieContainer.newKieSession();
Customer customer = new Customer();
customer.setName(" Li Si ");
kieSession.insert(customer);
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules();
// Implement execution only through rule filters ⾏ Specify the rules
// kieSession.fireAllRules(new RuleNameEqualsAgendaFilter("rule5"));
// Close session
kieSession.dispose();
}The results are as follows :
rule9 The data of Li Si has been deleted , therefore rule10 No implementation .

If you will retract Comment out and then execute , give the result as follows : 
Rule not implemented retract, therefore rule10 The rules take effect .

边栏推荐
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- 如何删除语句审计日志?
- Is the passenger flow always low? There is something wrong with the location of your store!
- 强网杯2021 pwn 赛题解析——baby_diary
- Is there any online account opening process of Huatai Securities? Is online account opening safe
- 曲线曲率展示
- one hundred and twenty-three million one hundred and twenty-three thousand one hundred and twenty-three
- 【Star项目】小帽飞机大战(四)
- Can you learn fast and well with dual stream network? Harbin Institute of Technology & Microsoft proposed a distillation dual encoder model for visual language understanding, which can achieve fast an
- openssl: error while loading shared libraries: libssl.so.1.1
猜你喜欢

Analysis of strong tennis cup 2021 PWN competition -- baby_ diary

buuReserve(4)

"Niuke | daily question" inverse Polish expression

String and memory functions

在第一次使用德国小鸡要注意的地方
![[graduation season _ advanced technology Er] farewell to yourself who has been confused for the past two years. Regroup, junior I'm coming](/img/04/3121514fcd8fcf1c939cbca7f4c67a.jpg)
[graduation season _ advanced technology Er] farewell to yourself who has been confused for the past two years. Regroup, junior I'm coming

Queue assistant | product update log in June 2022

Realize the full link grayscale based on Apache APIs IX through MSE
![Rust语言- Slice(切片)类型(&[u8])](/img/d1/68c73c8b34b848212083c08df3137f.png)
Rust语言- Slice(切片)类型(&[u8])

强网杯2021 pwn 赛题解析——baby_diary
随机推荐
Manifest merger failed with multiple errors, see logs
Log rotation logrotate
【Star项目】小帽飞机大战(四)
"Niuke | daily question" inverse Polish expression
【无标题】转载
Rust语言- Slice(切片)类型(&[u8])
Shell programming
C # use log4net plug-in to output logs to files
The results of the soft test can be checked, and the entry to query the results of the soft test has been opened in the first half of 2022
Press in and pop-up sequence of "Niuke | daily question" stack
one hundred and twenty-three million one hundred and twenty-three thousand one hundred and twenty-three
IR tool in JIT and download, compile and use of jitwatch
你了解MySQL都包含哪些“零件“吗?
浅谈eval与assert一句话木马执行区别
On stock price prediction model (3): are you falling into the trap of machine learning
On the difference between Eval and assert
QT监听socket事件,使用QSocketNotifier类
强网杯2021 pwn 赛题解析——baby_diary
Do you want to restart the database to replace the license?
如何删除语句审计日志?