【问题标题】:Global variable in Drools ruleDrools 规则中的全局变量
【发布时间】:2017-04-16 02:37:48
【问题描述】:

有什么方法可以取回 Drools 规则中更新的整数值。我在我的规则中传递字符串。我可以看到我的规则正在运行,但我没有得到更新的全局变量的值。这是我的 Drools 规则文件:

import com.MessageType;

global java.lang.Integer delayInSeconds;

rule "Delay for Update"
when 
String(this == MessageType.UPDATE.getType())
then
System.out.println("Running delay rule.....");
delayInSeconds = 10;
update(delayInSeconds); // This gives me runtime error. If I remove it I dont get error but dont get updated value.
end

我也试过这个: kcontext.getKieRuntime().setGlobal("delayInSeconds" , 10);但没有运气:(

我知道我可以通过在 POJO 中设置来传递这个变量。所以只是想确认是否有任何方法可以使用全局整数获取更新值。请提出建议。

【问题讨论】:

    标签: java drools jbpm


    【解决方案1】:

    全局变量是不同的种类。你可以阅读它们,你几乎可以像普通的旧变量一样使用它们(!),但是:

    全局变量的任何更新都必须通过 API 方法 KieRuntime.setGlobal() 完成。

    在你的规则中,你可以使用

    drools.setGlobal( delayInSeconds, Integer.valueOf(10) );
    

    您不能将更新与全局一起使用 - 这不是事实。此外,规则不会对全局的变化做出反应。如果需要,请使用事实。

    【讨论】:

    • 感谢您的回答。我刚刚使用了这个 kcontext.getKnowledgeRuntime().setGlobal("delay , 10");我直接访问变量。我不得不使用 kSession.getGlobal("delay")。
    【解决方案2】:

    对我来说这很有效

    drools.getKnowledgeRuntime().setGlobal(String, Object);
    

    例如。

    drools.getKnowledgeRuntime().setGlobal("delayInSeconds", Integer.valueOf(10));

    【讨论】:

    • 或者,更准确地说,当在规则的 RHS 中使用上述内容时:String 是全局变量的字符串表示形式,而 Object 是它的新值。
      例如:
      drools.getKnowledgeRuntime().setGlobal("loopCounter", Integer.valueOf(loopCounter + 1));
    【解决方案3】:

    你可以使用。

    then
       System.out.println("Running delay rule.....");  
       delayInSeconds = "whatever calculations";
       drools.getKnowledgeRuntime().setGlobal("delayInSeconds", delayInSeconds);
    end
    

    【讨论】:

      猜你喜欢
      • 2014-03-11
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多