zoukankan      html  css  js  c++  java
  • 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.2 lock-on-active

    转载至:https://blog.csdn.net/wo541075754/article/details/75208955

    lock-on-active

    当在规则上使用ruleflow-group属性或agenda-group属性的时候,将lock-on-active 属性的值设置为true,可避免因某些Fact对象被修改而使已经执行过的规则再次被激活执行。可以看出该属性与no-loop属性有相似之处,no-loop属性是为了避免Fact被修改或调用了insert、retract、update之类的方法而导致规则再次激活执行,这里的lock-on-active 属性起同样的作用,lock-on-active是no-loop的增强版属性,它主要作用在使用ruleflow-group属性或agenda-group属性的时候。lock-on-active属性默认值为false。与no-loop不同的是lock-on-active可以避免其他规则修改FACT对象导致规则的重新执行。

    因FACT对象修改导致其他规则被重复执行示例:

    package com.rules
    
    import com.secbro.drools.model.Product;
    
    rule rule1
        no-loop true
        when
            obj : Product(discount > 0);
        then
            obj.setDiscount(obj.getDiscount() + 1);
            System.out.println("新折扣为:" + obj.getDiscount());
            update(obj);
        end
    
    rule rule2
        when
            productObj : Product(discount > 1);
        then
            System.out.println("其他规则被触发了" + productObj.getDiscount());
        end
    View Code

    执行之后打印结果为:

    新折扣为:2
    其他规则被触发了2
    第一次执行命中了2条规则!
    View Code

    其他规则(rule2)因FACT对象的改变而被出发了。

    通过lock-on-active属性来避免被其他规则更新导致自身规则被重复执行示例:

    package com.rules
    
    import com.secbro.drools.model.Product;
    
    rule rule1
        no-loop true
        when
            obj : Product(discount > 0);
        then
            obj.setDiscount(obj.getDiscount() + 1);
            System.out.println("新折扣为:" + obj.getDiscount());
            update(obj);
        end
    
    rule rule2
        lock-on-active true
        when
            productObj : Product(discount > 1);
        then
            System.out.println("其他规则被触发了" + productObj.getDiscount());
        end
    View Code

    很明显在rule2的属性部分新增了lock-on-active true。执行结果为:

    新折扣为:2
    第一次执行命中了1条规则!
    View Code

    标注了lock-on-active true的规则不再被触发。

  • 相关阅读:
    perl 监控mysql数据库
    17.3Replication Solutions
    java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
    java.sql.SQLException: Can not issue empty query.
    [2015-06-10 20:53:50
    mysqldump --flush-logs
    Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'L
    Error Code: 1414. OUT or INOUT argument 2 for routine company.new_procedure is not a variable or NEW
    Deadlock found when trying to get lock; try restarting transaction
    java.text.ParseException: Unparseable date: "2015-06-09 hh:56:19"
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/9438431.html
Copyright © 2011-2022 走看看