在lhs和rhs中流口水不同的变量值 [英] Drools different value of variable in lhs and rhs

查看:100
本文介绍了在lhs和rhs中流口水不同的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个规则

rule "test1"
    when
        $cr: MyContext(flag1 == 1)
    then
        System.out.println("<---- Test1 start ---->");
        $cr.setFlag1(10);
        System.out.println("<---- Test1 end ---->");
    end

rule "test2" salience -1
    when
        $cr: MyContext($flag1: flag1 == 1)
    then
        System.out.println("<---- Test2 start ---->");
        System.out.println($flag1);
        System.out.println("<---- Test2 end ---->");
    end

我为rule1,我正在检查变量MyContext.flag1是否为1.因为是真的,所以我将其更改为10.我Rule2,我正在检查变量MyContext.flag1是否为1.我没想到该规则会在flag1为10时执行.

I rule1, I am checking if the variable MyContext.flag1 is 1. Since it's true I am changing it to 10. I rule2, I am checking if the variable MyContext.flag1 is 1. I didn't expect this rule to execute as flag1 is 10.

但是令人惊讶的是,它执行并将flag1打印为10.

But surprisingly, it executes and prints flag1 as 10.

在规则2的LHS中以及在RHS打印标志1中如何将flag1 == 1评估为true?

How is it evaluating flag1 == 1 as true in the LHS of rule2 and in the RHS printing flag1 as 10?

推荐答案

触发流口水的规则时,发生的情况是框架将规则输入输入工作内存并确定哪些规则匹配.然后对这些规则匹配进行排序(以显着性或自然顺序排序).

When you fire drools rules, what happens is that the framework takes your rule inputs in working memory and determines which rules match. These rule matches are then ordered (either by salience or the natural order) and then fired sequentially.

因此,当您触发规则时,"test1"和"test2"被确定为匹配项.然后,Drools继续触发"test1"首先,然后是"test2".

So when you fire your rules, "test1" and "test2" are determined to be matches. Then Drools goes on to fire "test1" first, followed by "test2".

如果您希望Drools重新评估规则匹配项,则需要告诉"规则匹配项.您对工作内存进行了更改的框架.为此,您具有以下内置方法:

If you want Drools to reevaluate the rule matches, you need to "tell" the framework that you have made changes to working memory. To do this, you have the following built-in methods:

<身体>
方法说明
插入 将新事实放入工作记忆中.
删除缩回 从工作内存中删除一些信息(对象).
update 更新/替换工作内存中的事实.
修改 更改工作内存中事实内部的字段.
Method Explanation
insert Put a new fact into working memory.
delete or retract Removes some information (object/s) from working memory.
update Update/replace a fact in working memory.
modify Change fields inside of a fact in working memory.

还有一些逻辑上的"逻辑.您可以在 Drools文档.

There are also some "logical" versions of these methods which you can read more about in the Drools documentation.

如果不要在进行更改时或更改后使用这些内置框架方法,则将继续评估初始规则匹配集.那些更改实际上对于Drools是不可见的,因为您没有告诉Drools嘿,我们已经进行了更改".每个规则的左侧已针对原始数据集进行了评估;右侧将根据匹配顺序进行评估.

If you don't use these built-in framework methods when or after making your change, you're going to keep evaluation the initial set of rule matches. Those changes are effectively invisible to drools, because you haven't told Drools "hey we've made changes." The left hand side of each rule has been evaluated against the original set of data; the right hand side is evaluated sequentially based on matches.

因此,在您的情况下,您想要做的是使用 modify 函数告诉Drools您正在对工作内存中的某条信息应用更改.

So in your case, what you want to do is to use the modify function to tell Drools that you're applying changes to a piece of information in working memory.

rule "test1"
when
  $cr: MyContext(flag1 == 1)
then
  modify( $cr ) {
    setFlag1(10)
  }
end

要注意的一件事是如何评估规则匹配.当您调用 insert 将新数据放入工作内存时,Drools将对规则匹配项进行 partial 重新评估.已经匹配并执行的规则将不会再次运行;仅重新评估随后的规则以找出它们是否现在匹配.

One thing to pay attention to is how the rule matches are evaluated. When you call insert to put a new piece of data into working memory, Drools will do a partial re-evaluation of rule matches. Rule that have already been matched and executed will not be run again; only subsequent rules are reevaluated to figure out if they now match.

相反, update 将对所有规则进行全面评估.这等效于停止当前规则执行并使用工作内存中的新数据调用 fireAllRules .

In contrast, update will do a full evaluation of all rules. It's the functional equivalent of stopping the current rule execution and calling fireAllRules with the new data in working memory.

如果您的规则具有外部副作用或不是幂等的,则调用 update 可能会有严重的副作用,因为随着数据的更改,您可能会多次评估规则的RHS.

If you have rules that have external side effects or are not idempotent, calling update may have serious side effects because you will potentially be evaluating the RHS of a rule multiple times as the data changes.

这篇关于在lhs和rhs中流口水不同的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆