drl 文件中的 Drool 全局变量初始化
Drool Global Variable Initialization in drl file
package com.example.drools;
global Integer count; // I want to initialize this count variable with some
default value.
rule "Initialize"
when
then
count= 1; // Locally it's possible but want it to set globally which can
be use in any other rules just simply by calling it.
System.out.println("count="+count);
end
rule "Drools Introduction"
when
then
System.out.println("count="+count); // Here output is coming null which in
want some default value set for
global value.
end
所以只想在 drl 文件中初始化 Count 变量?
从规则内部更新全局变量的方法是使用自动变量 kcontext
:
global Integer count;
rule "Initialize"
salience 100
when
then
kcontext.getKieRuntime().setGlobal("count", 1);
end
一些注意事项:
- 您应该在您的规则中使用高显着性,以便它在任何其他也使用全局的规则之前执行。
- 如果您在规则的 LHS 中使用全局变量,则此方法将不起作用。如果是这种情况,我建议使用事实而不是全局。
希望对您有所帮助,
package com.example.drools;
global Integer count; // I want to initialize this count variable with some
default value.
rule "Initialize"
when
then
count= 1; // Locally it's possible but want it to set globally which can
be use in any other rules just simply by calling it.
System.out.println("count="+count);
end
rule "Drools Introduction"
when
then
System.out.println("count="+count); // Here output is coming null which in
want some default value set for
global value.
end
所以只想在 drl 文件中初始化 Count 变量?
从规则内部更新全局变量的方法是使用自动变量 kcontext
:
global Integer count;
rule "Initialize"
salience 100
when
then
kcontext.getKieRuntime().setGlobal("count", 1);
end
一些注意事项:
- 您应该在您的规则中使用高显着性,以便它在任何其他也使用全局的规则之前执行。
- 如果您在规则的 LHS 中使用全局变量,则此方法将不起作用。如果是这种情况,我建议使用事实而不是全局。
希望对您有所帮助,