Drools学习笔记4—Consequence/RHS
- Right Hand Side,当LHS所有条件满足才会执行
- 可以使用LHS部分定义的绑定变量、全局变量、或者直接编写JAVA代码。
- 提供宏函数操作working memory fact对象,如insert/update/retract,使用会触发规则重新匹配
- 宏对象drools操作working memory,kcontext操作KnowledgeRuntime
modify块
- Modify表达式块,修改完后自动更新到workmemory中
- 语法
modify(fact-expression){
<修改Fact 属性的表达式>[,<修改Fact 属性的表达式>*]
}
package com.sample
import com.bean.Customer;
import com.bean.Account;
rule "modify"
when
$customer : Customer(name=="七夜雪" )
then
System.out.println( "modify Rule success and Customer is : " + $customer.getName() );
modify($customer){ //modify块,修改之后会重新触发规则执行
setName("碧落"); //直接使用修改fact属性的表达式
}
end
rule "modify 1"
when
$customer : Customer(name=="碧落" )
then
System.out.println( "modify Rule success and Customer is : " + $customer.getName() );
end
/**
* modify块
* @throws Exception
*/
@Test
public void testModify() throws Exception {
KnowledgeBase kbase = readKnowledgeBase("modify.drl");
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
Customer customer = new Customer();
customer.setName("七夜雪");
ksession.insert(customer);
ksession.fireAllRules();
logger.close();
}
执行结果:
属性
用来控制规则执行:
- activation-group:具有相同的activation-group的规则,只有一个会被执行,可配合salience确定哪个规则被执行
- agenda-group:在执行fireAllRules之前,所有的规则以及fact都存在agenda中,agenda-group对agenda进行分组
- auto-focus:
- date-effective:规则生效时间,系统时间>=date-effective时触发,默认格式“dd-MMM-yyyy”,可通过System.setProperty("drools.dateformat", "yyyy-MM-dd");设置时间格式
- date-expires:规则失效时间,与date-effective相反,格式一致
- dialect:两种方言,java和mvel,默认为java
- duration:规则将在指定时间之后的另一个线程触发,时间单位毫秒
- enabled:规则是否可用,默认为true
- lock-on-active:增强版的no-loop,主要在使用ruleflow-group或agenda-group时使用,默认为false
- no-loop:控制已经执行的规则在条件满足的情况下,是否会再次执行,默认false,为true则只能执行一次
- ruleflow-group
- salience:规则优先级,值越大优先级越高,默认为0
date-effective、date-expires
Drools学习笔记4—Consequence/RHS
- Right Hand Side,当LHS所有条件满足才会执行
- 可以使用LHS部分定义的绑定变量、全局变量、或者直接编写JAVA代码。
- 提供宏函数操作working memory fact对象,如insert/update/retract,使用会触发规则重新匹配
- 宏对象drools操作working memory,kcontext操作KnowledgeRuntime
modify块
- Modify表达式块,修改完后自动更新到workmemory中
- 语法
modify(fact-expression){
<修改Fact 属性的表达式>[,<修改Fact 属性的表达式>*]
}
package com.sample
import com.bean.Customer;
import com.bean.Account;
rule "modify"
when
$customer : Customer(name=="七夜雪" )
then
System.out.println( "modify Rule success and Customer is : " + $customer.getName() );
modify($customer){ //modify块,修改之后会重新触发规则执行
setName("碧落"); //直接使用修改fact属性的表达式
}
end
rule "modify 1"
when
$customer : Customer(name=="碧落" )
then
System.out.println( "modify Rule success and Customer is : " + $customer.getName() );
end
/**
* modify块
* @throws Exception
*/
@Test
public void testModify() throws Exception {
KnowledgeBase kbase = readKnowledgeBase("modify.drl");
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
Customer customer = new Customer();
customer.setName("七夜雪");
ksession.insert(customer);
ksession.fireAllRules();
logger.close();
}
执行结果:
属性
用来控制规则执行:
- activation-group:具有相同的activation-group的规则,只有一个会被执行,可配合salience确定哪个规则被执行
- agenda-group:在执行fireAllRules之前,所有的规则以及fact都存在agenda中,agenda-group对agenda进行分组
- auto-focus:
- date-effective:规则生效时间,系统时间>=date-effective时触发,默认格式“dd-MMM-yyyy”,可通过System.setProperty("drools.dateformat", "yyyy-MM-dd");设置时间格式
- date-expires:规则失效时间,与date-effective相反,格式一致
- dialect:两种方言,java和mvel,默认为java
- duration:规则将在指定时间之后的另一个线程触发,时间单位毫秒
- enabled:规则是否可用,默认为true
- lock-on-active:增强版的no-loop,主要在使用ruleflow-group或agenda-group时使用,默认为false
- no-loop:控制已经执行的规则在条件满足的情况下,是否会再次执行,默认false,为true则只能执行一次
- ruleflow-group
- salience:规则优先级,值越大优先级越高,默认为0
date-effective、date-expires