当前位置:网站首页>Writing method of then part in drools
Writing method of then part in drools
2022-07-02 12:13:00 【huan_ one thousand nine hundred and ninety-three】
List of articles
- 1、 background
- 2、 Supported methods
- 3、drools Simple use of variables
- 4、 Inheritance of rules
- 5、 Use do[...] Syntax rewrite the example inherited above
- 6、 Realization if else if The effect of
- 7、 Complete code
- 8、 Reference documents
1、 background
Here is an introduction to drools
in then
Part of it , And some built-in methods , such as insert/delete/modify
wait . Also introduce rule
Inheritance , And in when
To realize if else if
Wait for the operation .
2、 Supported methods
drools Some built-in methods are provided , These methods will modify drools Of The working memory
in Fact
The value of the object . This will result in pattern matching again .
2.1 insert Insert object into working memory
insert
Is to insert objects into working memory , At the same time, it will lead to the re pattern matching of rules . At the same time, when the rules are not satisfied , Will not automatically delete .
2.1.1 demand
When there is a fire Fire
when , Insert a... Into the working memory Alarm
object , After the alarm occurs , Delete... In working memory Fire
object , And then test Alarm
Whether the object still exists .
2.1.2 drl Documentation
package rules
import com.huan.drools.insertmethod.Fire
import com.huan.drools.insertmethod.Alarm
rule "insert_ When there is a fire , Insert the alarm object into the working memory "
when
$fire: Fire()
then
System.out.println("1、 When there is a fire , Insert the alarm object into the working memory ");
insert(new Alarm($fire));
end
rule "insert_ When there is an alarm object in the rule memory , Alarm , Then delete the fire object "
when
$fire: Fire()
$alarm: Alarm( fire == $fire )
then
System.out.println("2、 Alarm , Then delete the corresponding fire object ");
end
rule "insert_ Check whether the alarm object still exists -01"
when
Alarm()
then
System.out.println("3、insert The inserted alarm object still exists ");
// Delete... In working memory Fire object
delete($fire);
end
rule "insert_ Detect that the alarm object does not exist "
when
not Alarm()
then
System.out.println("3、insert The inserted alarm object does not exist ");
end
What is used here is insert
Insert
2.1.3 part java Code writing
// Insert the fire object into the working memory
kieSession.insert(new Fire());
// Only trigger the rule name with insert_ Opening rule
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("insert_"));
2.1.4 Running results
1、 When there is a fire , Insert the alarm object into the working memory
2、 Alarm , Then delete the corresponding fire object
3、insert The inserted alarm object still exists
2.1.5 Conclusion
insert
You can insert... Into working memoryFact
object .insert
After method call , This will result in pattern re matching , Lead to rules that will not be executed before , Re execution .insert
Method to insert an object into working memory , When the rules don't hold , Will not automatically delete , Manual deletion required , Pay attention to andinsertLogical
The difference between
2.2 insertLogical Insert object into working memory
insert
Is to insert objects into working memory , At the same time, it will lead to the re pattern matching of rules . At the same time, when the rules are not satisfied , It will be deleted automatically .
2.2.1 demand
When there is a fire Fire
when , Insert a... Into the working memory Alarm
object , After the alarm occurs , Delete... In working memory Fire
object , And then test Alarm
Whether the object still exists .
2.2.2 drl Documentation
package rules
import com.huan.drools.Fire
import com.huan.drools.Alarm
rule "insertLogical_ When there is a fire , Insert the alarm object into the working memory "
when
$fire: Fire()
then
System.out.println("1、 When there is a fire , Insert the alarm object into the working memory ");
insertLogical(new Alarm($fire));
end
rule "insertLogical_ When there is an alarm object in the rule memory , Alarm , Then delete the fire object "
when
$fire: Fire()
$alarm: Alarm( fire == $fire )
then
System.out.println("2、 Alarm , Then delete the corresponding fire object ");
delete($fire);
end
rule "insertLogical_ Check whether the alarm object still exists -01"
when
Alarm()
then
System.out.println("3、insertLogical The inserted alarm object still exists ");
end
rule "insertLogical_ Detect that the alarm object does not exist "
when
not Alarm()
then
System.out.println("3、insertLogical The inserted alarm object does not exist ");
end
What is used here is insertLogical
Insert
2.2.3 part java Code writing
kieSession.insert(new Fire());
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("insertLogical_"));
2.2.4 Running results
1、 When there is a fire , Insert the alarm object into the working memory
2、 Alarm , Then delete the corresponding fire object
3、insertLogical The inserted alarm object does not exist
2.2.5 Conclusion
insertLogical
You can insert... Into working memoryFact
object .insertLogical
After method call , This will result in pattern re matching , Lead to rules that will not be executed before , Re execution .insertLogical
Method to insert an object into working memory , When the rules don't hold , Will automatically delete attention andinsert
The difference between
2.3、update Update objects in working memory
update:
Use it to specify the fields to be updated and the entire related fields Fact, And notify... Of the change Drools engine . Fact After the change , You must call before changing another fact that may be affected by the updated value update. To avoid this, add steps , Please switch to modify Method .
2.3.1 demand
Rule one : When there is a fire object in the working memory Fire
, And the name name Trigger rule when null , At the same time, set the name of the fire as a great fire
.
Rule 2 : When a fire exists , Output fire name
2.3.2 drl Documentation
package rules
import com.huan.drools.Fire
import com.huan.drools.Alarm
rule "update_ When there is a fire object , Set a name for the fire "
when
$fire: Fire(name == null)
then
System.out.println("1、 Set fire name ");
$fire.setName(" a great fire ");
update($fire)
end
rule "update_ Trigger when the name of the fire object exists "
when
$fire: Fire(name != null)
then
System.out.println("2、 The name of the fire object is : " + $fire.getName());
end
2.3.3 part java Documentation
kieSession.insert(new Fire());
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("update_"));
2.3.4 Running results
1、 Set fire name
2、 The name of the fire object is : a great fire
2.3.4 Conclusion
update
This will result in pattern re matching .update
Will modify the value in the working object memory .
2.4、modify Update objects in working memory
modify:
Use it to specify to be Fact Object and notify you of the changes Drools engine . This method provides a structured fact update method . It will update the operation with setter Call to change object fields .
2.4.1 demand
Rule one : When there is a fire object in the working memory Fire
, And the name name Trigger rule when null , At the same time, set the name of the fire as a great fire
.
Rule 2 : When a fire exists , Output fire name
2.4.2 drl Documentation
package rules
import com.huan.drools.Fire
import com.huan.drools.Alarm
rule "modify_ When there is a fire object , Set a name for the fire "
when
$fire: Fire(name == null)
then
System.out.println("1、 Set fire name ");
modify($fire){
setName(" a great fire ")
}
end
rule "modify_ Trigger when the name of the fire object exists "
when
$fire: Fire(name != null)
then
System.out.println("2、 The name of the fire object is : " + $fire.getName());
end
2.4.3 part java Documentation
kieSession.insert(new Fire());
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("modify_"));
2.4.4 Running results
1、 Set fire name
2、 The name of the fire object is : a great fire
2.4.5 Conclusion
modify
This will result in pattern re matching .modify
Will modify the value in the working object memory .- In general use
modify
, Do not useupdate
.
2.5 delete Delete objects in working memory
usage :delete(<object>)
retract
Also and delete
Same effect , But it is recommended delete
.
3、drools Simple use of variables
package rules
rule "drools_ Use of variables "
when
eval(true)
then
System.out.println("Match Active current trigger rule : " + drools.getMatch());
System.out.println(" The name of the current trigger rule : " + drools.getRule().getName());
// System.out.println(" Terminate rule execution fireUntilHalt(): " + drools.getKieRuntime().halt());
// System.out.println(" Activate AgendaGroup Group : " + drools.getKieRuntime().getAgenda().getAgendaGroup( "CleanUp" ).setFocus());
System.out.println(" Get all global variables : " + drools.getKieRuntime().getGlobals());
// System.out.println(" Set global variables :" + drools.getKieRuntime().setGlobal("username","huan"); );
// System.out.println(" Get query results :" + drools.getKieRuntime().getQueryResults());
end
4、 Inheritance of rules
4.1 demand
Rule one : If the user (customer
) In the age of (age
) Greater than 60 year , Then beat 0.9
fold .
Rule 2 : On the basis of rule one , If the user has a car (car
), You can park for free (freeParking
).
4.2 drl Documentation
package rules
import com.huan.drools.Customer
import com.huan.drools.Car
rule "rule_extends_ Rule one "
when
$c: Customer(age > 60)
then
modify($c){
setDiscount(0.9)
}
System.out.println(" Trigger rule 1 : User age >60 year , hit 0.9 fold ");
end
// Rule two inherits the condition of rule one
rule "rule_extends_ Rule 2 " extends "rule_extends_ Rule one "
when
$car: Car()
then
modify($car){
setFreeParking(true)
}
System.out.println(" Trigger rule 2 : The user has a car , Free parking ");
end
here rule_extends_ Rule 2
Inherited rule_extends_ Rule one
, So the conditions of rule 1 also inherit .
4.3 part java Code
Car car = new Car();
Customer customer = new Customer();
customer.setAge(65);
kieSession.insert(customer);
kieSession.insert(car);
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("rule_extends_"));
The customer has a car , And the age is 65 year , Satisfy rules 1 and 2 above
4.4 Running results
Trigger rule 1 : User age >60 year , hit 0.9 fold
Trigger rule 2 : The user has a car , Free parking
4.5 Conclusion
You can see in the rule
Upper use extends
keyword , You can implement rule inheritance .
5、 Use do[…] Syntax rewrite the example inherited above
5.1 demand
As long as the user is greater than 60 year , Direct hit 0.9 fold , If there's a car , You can park for free .
5.2 drl Documentation
package rules
import com.huan.drools.Customer
import com.huan.drools.Car
rule " Name the result _rule"
when
$c: Customer(age > 60)
do[giveDiscount] // When the above condition holds, execute then [giveDiscount]
$car: Car() // When this condition holds , Execute the default then
then
modify($car){
setFreeParking(true)
};
System.out.println(" The user has a car , Free parking ");
then [giveDiscount]
modify($c){
setDiscount(0.9)
};
System.out.println(" User age >60 year , hit 0.9 fold ");
end
explain :
See the rule file above
5.3 part java Code writing
Car car = new Car();
Customer customer = new Customer();
customer.setAge(65);
kieSession.insert(customer);
kieSession.insert(car);
kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter(" Name the result _"));
5.4 Running results
User age >60 year , hit 0.9 fold
The user has a car , Free parking
It also realizes the requirements
5.5 Conclusion
adopt when
Use in do[ name ]
then then
then name
It can also be realized .
6、 Realization if else if The effect of
6.1 demand
Finish something like if else if else
effect . See various execution results below .
6.2 Rules file
rule "if else-if"
when
$customer: Customer(age > 60) // Rule memory exists Customer object , also age>60
if($customer.getLevel() == 1) do[level1] // User level is 1, perform then[level1], Then continue to execute the following conditions
else if ($customer.getLevel() == 2) break[level2] // User level is 2, perform then[level2], Do not execute the following conditions
else do[levelOther] // Other level Level , perform then[levelOther], Then execute the following conditions
Car()
then
System.out.println(" I did ");
then[level1]
System.out.println("level1");
then[level2]
System.out.println("level2");
then[levelOther]
System.out.println("levelOther");
end
6.3 Execution result diagram
6.4 Various execution results - That's the explanation above
1、Customer
Of age
Less than 60
. Output :
No output .
2、Customer
Of age
Greater than 60
also level=1
, No, Car
. Output :
level1
3、Customer
Of age
Greater than 60
also level=1
, Yes Car
. Output :
level1 I did
4、Customer
Of age
Greater than 60
also level=2
, No, Car
. Output :
level2
5、Customer
Of age
Greater than 60
also level=2
, Yes Car
. Output :
level2
6、Customer
Of age
Greater than 60
also level=3
, No, Car
. Output :
levelOther
7、Customer
Of age
Greater than 60
also level=3
, Yes Car
. Output :
levelOther I did
6.5 do and break What's the difference?
do
: After execution , We will continue to judge the following execution conditions . ( That is, the following Car Judge , According to whether there is Car Get different results
)break
: After execution , Will not judge the execution conditions behind .( That is, the following Car Judge ,rule performed
)
7、 Complete code
https://gitee.com/huan1993/spring-cloud-parent/tree/master/drools/drools-drl-then
8、 Reference documents
1、https://docs.drools.org/7.69.0.Final/drools-docs/html_single/index.html#drl-rules-THEN-con_drl-rules
边栏推荐
- Time format display
- Sparkcontext: error initializing sparkcontext solution
- CDH6之Sqoop添加数据库驱动
- Uniapp uni list item @click, uniapp uni list item jump with parameters
- Applet link generation
- uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
- XSS labs master shooting range environment construction and 1-6 problem solving ideas
- Test shift left and right
- ORB-SLAM2不同线程间的数据共享与传递
猜你喜欢
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
Natural language processing series (I) -- RNN Foundation
Differences between nodes and sharding in ES cluster
mysql数据库基础
CDA数据分析——AARRR增长模型的介绍、使用
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
jenkins 凭证管理
HR wonderful dividing line
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
【C语言】十进制数转换成二进制数
随机推荐
Natural language processing series (I) -- RNN Foundation
YYGH-BUG-05
刷题---二叉树--2
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
CDA data analysis -- Introduction and use of aarrr growth model
Yygh-9-make an appointment to place an order
Differences between nodes and sharding in ES cluster
Tas (file d'attente prioritaire)
XSS labs master shooting range environment construction and 1-6 problem solving ideas
WSL 2 will not be installed yet? It's enough to read this article
CDA data analysis -- common knowledge points induction of Excel data processing
自然语言处理系列(一)——RNN基础
Time format display
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
Jenkins user rights management
ThreadLocal的简单理解
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
寻找二叉树中任意两个数的公共祖先
Fastdateformat why thread safe
ES集群中节点与分片的区别