简体   繁体   English

Drools规则中的全局变量

[英]Global variable in Drools rule

Is there any way I can get back the Integer value updated in Drools rule. 有没有办法可以取回Drools规则中更新的Integer值。 I am passing the string in my rule. 我在我的规则中传递字符串。 I can see my rule running but I am not getting the updated global variable's value. 我可以看到我的规则正在运行,但我没有得到更新的全局变量的值。 Here is my Drools rule file : 这是我的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

I have also tried this : kcontext.getKieRuntime().setGlobal("delayInSeconds" , 10); 我也试过这个:kcontext.getKieRuntime()。setGlobal(“delayInSeconds”,10); but no luck :( 但没有运气:(

I know I can pass this variable by setting in POJO. 我知道我可以通过在POJO中设置来传递这个变量。 So just wanted to confirm if there is any way by we can get updated value using global Integer. 因此,我们想确认是否有任何方法可以使用全局Integer获取更新值。 Please suggest. 请建议。

Global variables are a different species. 全局变量是一个不同的物种。 You can read them, you can use them almost (!) like plain old variables, but: 你可以阅读它们,你可以像普通的旧变量一样使用它们(!),但是:

Any update of a global variable must be done via the API method KieRuntime.setGlobal() . 必须通过API方法KieRuntime.setGlobal()完成全局变量的任何更新。

In your rule, you could use 在你的规则中,你可以使用

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

You cannot use update with a global - it is not a fact. 您不能将更新与全局一起使用 - 这不是事实。 Also, rules will not react to a change of a global. 此外,规则不会对全球变化作出反应。 Use a fact if you need this. 如果需要,请使用一个事实。

For me this worked 对我来说这很有效

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

Eg. 例如。

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

You can use. 您可以使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM