当前位置:网站首页>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 .

边栏推荐
- Manifest merger failed with multiple errors, see logs
- npm 命令
- What are the ways to open the JDBC log of Youxuan database
- Agile and tidy way
- JIT中的IR工具与JITWatch的下载,编译及使用
- 20220725 convolution in automatic control principle
- one hundred and twenty-three million one hundred and twenty-three thousand one hundred and twenty-three
- MySQL execution plan
- Kubernetes scheduling concept and workflow
- How to realize digital production mode in garment industry
猜你喜欢

【硬十宝典】——7.2【动态RAM】DDR4与DDR3区别解析

在第一次使用德国小鸡要注意的地方

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

Drools(2):Drools快速入门

File server fastdfs

浅谈eval与assert一句话木马执行区别

XSS-labs(1-10)闯关详解

28. Implement strstr() implement strstr()

问题:Can‘t download sh shellcheck. Please install it manually及shell脚本的一些命令使用

28. Implement strStr()实现 strStr()
随机推荐
文件服务器FastDFS
Kernel pwn 入门 (5)
Curve curvature display
Opengauss simple version installation error
Use and analysis of show profile optimized by MySQL
CS5801_ HDMI to EDP advantage replaces lt6711a solution
[QT] detailed explanation of *.pro, *.pri, *.prf, *.prl files
Database performance test (MySQL)
Getting started with kernel PWN (5)
MySQL execution plan
二叉树知识总结
On stock price prediction model (3): are you falling into the trap of machine learning
npm 命令
Log rotation logrotate
Display Chinese characters in uitoolkit
Benefits of the builder model
XSS-labs(1-10)闯关详解
Exclusive lock
Vim中删除^M
C # use log4net plug-in to output logs to files