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

边栏推荐
- 日志轮转logrotate
- What to pay attention to when using German chicks for the first time
- 敏捷整洁之道
- Depth cloning and reflection of typescript class objects
- 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快速入门
- MySQL table read lock
- 从Architecture带你认识JVM
- Manifest merger failed with multiple errors, see logs
- Drools(4):Drools基础语法(2)
猜你喜欢
![[749. Isolate virus]](/img/12/b8c3cdb664f4415d20c2fc5c697a41.png)
[749. Isolate virus]

基于C51实现led流水灯

"Niuke | daily question" inverse Polish expression

Opengauss simple version installation error

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

2万字带你从0到1搭建一套企业级微服务安全框架

Kernel pwn 入门 (5)

Drools(4):Drools基础语法(2)

【数据库】CTE(Common Table Expression(公共表表达式))
![[hardware ten treasures] - 7.1 [dynamic RAM] key points of DDR hardware design](/img/ba/87cd3b1600bcb6f2839e7bb093ff62.png)
[hardware ten treasures] - 7.1 [dynamic RAM] key points of DDR hardware design
随机推荐
针对前面文章的整改思路
Database performance test (MySQL)
Opengauss simple version installation error
Intention lock
28. Implement strStr()实现 strStr()
Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !
"Final review" 16/32-bit microprocessor (8086) basic register
文件服务器FastDFS
解决 Chrome 浏览器被毒霸篡改问题
<二> objectARX开发:创建和编辑基本图形对象
Is there any online account opening process of Huatai Securities? Is online account opening safe
“蔚来杯“2022牛客暑期多校训练营1补题记录(ACDGIJ)
MySQL read / write lock
【数据库】CTE(Common Table Expression(公共表表达式))
MySQL table read lock
Do you know what "parts" MySQL contains?
Manifest merger failed with multiple errors, see logs
SQL shell (PSQL) tool under PostgreSQL
替换license是否要重启数据库?
[Star Project] small hat aircraft War (III)