java Drools 规则中的全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40910953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global variable in Drools rule
提问by rishi
Is there any way I can get back the Integer value updated in Drools rule. 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 规则中更新的整数值。我正在我的规则中传递字符串。我可以看到我的规则正在运行,但我没有得到更新的全局变量的值。这是我的 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); but no luck :(
我也试过这个: kcontext.getKieRuntime().setGlobal("delayInSeconds" , 10); 但没有运气:(
I know I can pass this variable by setting in POJO. So just wanted to confirm if there is any way by we can get updated value using global Integer. Please suggest.
我知道我可以通过在 POJO 中设置来传递这个变量。所以只是想确认是否有任何方法可以使用全局整数获取更新的值。请建议。
回答by laune
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.
您不能对全局使用 update - 这不是事实。此外,规则不会对全局的变化做出反应。如果需要,请使用事实。
回答by Shivansh Gaur
For me this worked
对我来说这有效
drools.getKnowledgeRuntime().setGlobal(String, Object);
Eg.
例如。
drools.getKnowledgeRuntime().setGlobal("delayInSeconds", Integer.valueOf(10));
drools.getKnowledgeRuntime().setGlobal("delayInSeconds", Integer.valueOf(10));
回答by Pranith Baggan
You can use.
您可以使用。
then
System.out.println("Running delay rule.....");
delayInSeconds = "whatever calculations";
drools.getKnowledgeRuntime().setGlobal("delayInSeconds", delayInSeconds);
end