zoukankan      html  css  js  c++  java
  • 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

     
  • 相关阅读:
    100个经典的动态规划方程
    poj 2105 IP Address
    codeforces 299 A. Ksusha and Array
    codeforces 305 C. Ivan and Powers of Two
    性能测试实战 | 修改 JMeter 源码,定制化聚合压测报告
    Pytest 测试框架训练营,测试大咖带你搞定自动化+接口测试实战 | 限时免费报名
    MTSC 测试开开发大会(深圳站),报名立减 100 元!
    测试人面试 BAT 大厂之前,需要做好哪些准备?
    iOS 自动化测试踩坑(二):Appium 架构原理、环境命令、定位方式
    【北京】美团打车招聘职位
  • 原文地址:https://www.cnblogs.com/qiyexue/p/7823080.html
Copyright © 2011-2022 走看看