当前位置:网站首页>Drools (5): drools basic syntax (3)
Drools (5): drools basic syntax (3)
2022-07-27 07:14:00 【Undead bird. Alexander. Wolf cub】
The rule syntax of the attribute part
Drools The attributes provided in are shown in the following table :
| Property name | explain |
salience | Specify rule execution ⾏ priority |
dialect | Specify rules to make ⽤ The language of ⾔ type , The value is java and mve |
enabled | Specify whether the rule is enabled ⽤ |
date-effective | Specify the rules ⽣ Effective time |
date-expires | Specify the rule expiration time |
activation-group | Activate grouping , Rules with the same group name can only have ⼀ Rules trigger |
agenda-group | Agenda groups , Only the rules in the focus group can trigger |
timer | Timer , Specify when the rule is triggered |
auto-focus | ⾃ Move to get focus ,⼀ Like a combination agenda-group⼀ Start to make ⽤ |
no-loop | prevent ⽌ Dead cycle |
1 salience
The function is to set rule execution ⾏ The priority of the ,salience The value of the property is ⼀ A digital , The more numbers ⼤ Of board ⾏ The higher the priority ⾼. Default love Condition , Regular salience The default value is 0. If not set salience attribute , Enforcement of rule body ⾏ The order is from top to bottom .
Example :
rule "attributes_rule1"
salience 1
when
eval(true)
then
System.out.println("rule1....");
end
rule "attributes_rule2"
salience 2
when
eval(true)
then
System.out.println("rule2....");
endtest :
@Test
public void test7() {
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();
}Execution results :

2 no-loop
The function is to control the execution ⾏ The rules of the past are full again ⾜ Whether to execute again ⾏.no-loop The value of the property is ⼀ A Boolean , By default, the rules are no-loop The value of the property is false, If no-loop The property value is true, Then it means that the rule will only be Engine check ⼀ Time , If full ⾜ Conditions are enforced ⾏ Regular RHS part , If the engine inside because of Fact The update causes the engine to start the check rule again , that It will ignore all no-loop Property is set to true The rules of .
Example :
// test no-loop
rule "attributes_rule3"
salience 1
no-loop true
when
$customer:Customer(name==" Zhang San ")
then
update($customer);
System.out.println("customer name:"+$customer.getName());
endtest :
@Test
public void test8() {
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(" Zhang San ");
kieSession.insert(customer);
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules();
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules();
// Close session
kieSession.dispose();
}test result :
Add the no-loop Set to false

You will find that you have been executing an endless loop

Add the no-loop Set to true

The rule only executes once and ends

3 date-effective
do ⽤ yes ⽤ To control that the rule will only be triggered after arrival , In the rule of transportation ⾏ when , The engine will be ⾃ When taking the current operating system, it is the same as date-effective Set the time value into ⾏⽐ Yes , Only when the system time >=date-effective Set the time value of , Rules will trigger execution ⾏, Otherwise, hold on to ⾏ Will not hold ⾏. Without setting this property , Rules can be triggered at any time , There is no such limitation .
date-effective The value of is ⼀ individual ⽇ Period type string , By default ,date-effective Acceptable ⽇ The format of the period is “dd- MMM-yyyy” In the process of actual use , If you don't want to ⽤ This time format , Then you can adjust ⽤ Of Java Code by making ⽤ System.setProperty(String key,String value)⽅ Method to modify the default time format stay java⽂ Add this command to the file : System.setProperty("drools.dateformat","yyyy-MM-dd");
Example :
rule "attributes_rule4"
date-effective "2022-7-20"
// At present ⽇ Period not ⼩ On 2022-7-20 You can hold ⾏
when
eval(true);
then
System.out.println("attributes_rule4 is execution!");
endtest :
@Test
public void test8() {
// Set and modify the default time format
System.setProperty("drools.dateformat", "yyyy-MM-dd");
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();
}test result :
Test date is 2022 year 7 month 26 Japan , Greater than the set 2022 year 7 month 20 Japan

4 date-expires
do ⽤ Is with the date-effective Attributes are just the opposite ,date-expires Works of ⽤ yes ⽤ To set the validity period of the rule , The engine is running ⾏ When the rules are , Will check if the rules have date-expires attribute , If any , Then the value of this attribute will be compared with the current system Time goes in ⾏⽐ Yes , If ⼤ At system time , Then the rules will be enforced ⾏, Otherwise, don't cling ⾏.
5 enabled
do ⽤ yes ⽤ To define ⼀ Whether a rule can ⽤ Of . The value of this property is ⼀ A Boolean value , The default value of this attribute is true, Indicates that the rule is ⽤ Of . Set up enabled The property value is false, Then the engine will not execute ⾏ This rule
6 dialect
do ⽤ yes ⽤ To define the rules that make ⽤ The language of ⾔ type ,⽬ front ⽀ Hold two types of language ⾔:mvel and java, By default , without ⼿⼯ Set the of rules dialect, Then make ⽤ Of java language ⾔
7 activation-group
do ⽤ Is will if ⼲ The rules are divided into ⼀ A set of ,⽤⼀ String to name this group , In this way, I am clinging to ⾏ When , Have the same activation-group As long as there is ⼀ One will be held ⾏, Other rules will no longer be enforced ⾏.
in other words , stay ⼀ Groups have the same activation-group In the rules of attributes , Only ⼀ Rules will be enforced ⾏, Other rules will not be enforced ⾏. Of course, for people with the same activation-group Which of the rules of attributes ⼀ One meeting first ⾏, Then you can ⽤ similar salience And so on .
Example :
// test activation-group
rule "attributes_rule5"
activation-group "test"
when
eval(true)
then
System.out.println("attributes_rule5 execute...");
end
rule "attributes_rule6"
activation-group "test"
when
eval(true)
then
System.out.println("attributes_rule6 execute...");
endtest :
@Test
public void test9() {
// Set and modify the default time format
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();
}test result :

8 agenda-group
do ⽤ yes agenda-group The value of the property is also ⼀ A string , Through this string , Rules can be divided into if ⼲ individual Agenda Group, By default , The engine is in tune ⽤ These settings agenda-group Attribute rules need to be displayed to specify a Agenda Group obtain Focus( The focus of ), This is where Agenda Group The rules in it will trigger execution ⾏, Otherwise, it will not be executed .
Example :
// test agenda-group
rule "attributes_rule7"
agenda-group "001"
when
eval(true)
then
System.out.println("attributes_rule7 execute");
end
rule "attributes_rule8"
agenda-group "002"
when
eval(true)
then
System.out.println("attributes_rule8 execute...");
endtest :
@Test
public void test10() {
// Set and modify the default time format
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
// Conversation object ,⽤ Interact with the rule engine
KieSession kieSession = kieContainer.newKieSession();
kieSession.getAgenda().getAgendaGroup("002").setFocus();
// Get hold ⾏ The focus of
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules();
// Close session
kieSession.dispose();
}test result :

9 auto-focus
do ⽤ yes ⽤ Come on, it has been set agenda-group Set whether the rule can ⾃ Move alone Focus, If this property is set to true, Then in the engine execution ⾏ when , You don't need to display something Agenda Group Set up Focus, Otherwise, we need .
Example :
rule "attributes_rule7"
agenda-group "001"
auto-focus
when
eval(true)
then
System.out.println("attributes_rule7 execute");
end
rule "attributes_rule8"
agenda-group "002"
when
eval(true)
then
System.out.println("attributes_rule8 execute...");
endtest :
@Test
public void test12() {
// Set and modify the default time format
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
// Conversation object ,⽤ Interact with the rule engine
KieSession kieSession = kieContainer.newKieSession();
// Get hold ⾏ The focus of
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules();
// Close session
kieSession.dispose();
}test result :

10 timer
timer Attributes can be set through the ⽅ Specify rule execution ⾏ Time for , send ⽤⽅ There are two kinds of :
⽅ type ⼀:timer (int: ?)
This kind of ⽅ Follow java.util.Timer The cause of the object ⽤⽅ type , The first ⼀ The parameters represent ⼏ Seconds later ⾏, The first ⼆ Parameters represent every ⼏ Second hold ⾏⼀ Time , The first ⼆ Parameters are optional .
⽅ type ⼆:timer(cron: )
This kind of ⽅ Shi Shi ⽤ The standard unix cron The use of expressions ⽤⽅ Formula to define the time of rule execution .
Example :
边栏推荐
- String类的用法
- MySQL limit paging query optimization practice
- 泛型 -- 学会它,好处多多
- VScode连接远程服务器开发
- DNA modified near infrared two region GaAs quantum dots | GaAs DNA QDs | DNA modified GaAs quantum dots
- Watermelon book learning notes - Chapter 4 decision tree
- Gbase 8C - SQL reference 6 SQL syntax (11)
- Basic statement of MySQL (1) - add, delete, modify and query
- Pytorch uses data_ Prefetcher improves data reading speed
- Gbase 8C product introduction
猜你喜欢

把Excel转换成CSV/CSV UTF-8

(转帖)eureka、consul、nacos的对比2

Digital image processing - Chapter 6 color image processing

OpenGL development with QT (I) drawing plane graphics

李沐动手学深度学习V2-transformer和代码实现

Watermelon book chapter 3 - linear model learning notes

Derivative, partial derivative and gradient

String类的用法

网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...

ZnS-DNA QDs近红外硫化锌ZnS量子点改性脱氧核糖核酸DNA|DNA修饰ZnS量子点
随机推荐
OpenGL development with QT (I) drawing plane graphics
PNA polypeptide PNA TPP | GLT ala ala Pro Leu PNA | suc ala Pro PNA | suc AAPL PNA | suc AAPM PNA
Student status management system based on SSM
MySQL index failure and solution practice
Interpretation of deepsort source code (VII)
ZnS-DNA QDs近红外硫化锌ZnS量子点改性脱氧核糖核酸DNA|DNA修饰ZnS量子点
肽核酸PNA-多肽PNA-TPP|Glt-Ala-Ala-Pro-Leu-pNA|Suc-Ala-Pro-pNA|Suc-AAPL-pNA|Suc-AAPM-pNA
Codeforces Round #804 (Div. 2)(5/5)
Why can cross entropy loss be used to characterize loss
Drools(5):Drools基础语法(3)
Livox SLAM(带LIO+闭环检测优化)
Day012 application of one-dimensional array
Express receive request parameters
Details of cross entropy loss function in pytorch
DNA research experiment application | cyclodextrin modified nucleic acid cd-rna/dna | cyclodextrin nucleic acid probe / quantum dot nucleic acid probe
Analysis on the current situation and optimization strategy of customer experience management in banking industry
Derivative, partial derivative and gradient
Analysis of online and offline integration mode of o2o E-commerce
Codeforces Round #787 (Div. 3)(7/7)
[latex format] there are subtitles side by side on the left and right of double columns and double pictures, and subtitles are side by side up and down