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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值