有没有办法让 drools 变量在规则之间持续存在?
我想做的是将单独存储到全局输出映射中的各种字符串连接起来。但是,我有一些限制。
- 我不允许将任何没有“Output”键的对添加到outputMap 中。
- 除了outputMap 之外,我不允许在 kie 会话中插入/设置任何全局变量。
- 这些以下面字母结尾的子规则需要定义为单独的规则。
因此,我使用 kie 会话未设置的另一个全局变量尝试了以下解决方案。
import java.util.*
global java.util.Map outputMap;
global java.util.List concatList;
rule 101A
salience 3
when
$v1:Map()
$fruit:String() from $v1.get("Strawberries");
then
outputMap.put("Output", $fruit);
if(concatList == null)
{
concatList = new List<String>();
}
concatList.add($fruit);
end
rule 101B
salience 2
when
$v1:Map()
$fruit:String() from $v1.get("Cherries");
then
outputMap.put("Output", $fruit);
if(concatList == null)
{
concatList = new List<String>();
}
concatList.add($fruit);
end
rule 101
salience 1
when
$v1:Map()
$fruit:String() from $v1.get("All");
then
String s = "";
for(int i=0; i<concatList.size(); ++i)
{
s+=concatList.get(i);
}
outputMap.put("Output", s);
end
此方法的问题在于,全局 concatList 的值似乎在每次规则执行后都会重置,因为我无法将其插入 kie 会话,并且由于我有第一个约束,所以我不能简单地将其插入输出映射中并稍后取回。
我的问题是,有没有办法让变量在规则之间保持不变,而无需在 kie 会话中定义它?
TLDR:以上问题
谢谢
What I'm trying to do is concatenate various strings that are individually stored into a global outputMap. However, I have some constraints.
- I am not allowed to add any pair into the outputMap that doesn't have a key of "Output".
- I am not allowed to insert/set any globals into the kie session aside from the outputMap.
- These sub-rules that end in letters below need to be defined as separate rules.
So I tried the following solution with another global variable that isn't being set by a kie session.
import java.util.*
global java.util.Map outputMap;
global java.util.List concatList;
rule 101A
salience 3
when
$v1:Map()
$fruit:String() from $v1.get("Strawberries");
then
outputMap.put("Output", $fruit);
if(concatList == null)
{
concatList = new List<String>();
}
concatList.add($fruit);
end
rule 101B
salience 2
when
$v1:Map()
$fruit:String() from $v1.get("Cherries");
then
outputMap.put("Output", $fruit);
if(concatList == null)
{
concatList = new List<String>();
}
concatList.add($fruit);
end
rule 101
salience 1
when
$v1:Map()
$fruit:String() from $v1.get("All");
then
String s = "";
for(int i=0; i<concatList.size(); ++i)
{
s+=concatList.get(i);
}
outputMap.put("Output", s);
end
The problem with this method is that the global concatList's value seems to reset after every rule execution since I can't insert it into a kie session, and since I have the first constraint, I can't just simply insert it into the output map and retrieve it later.
My question is, is there a way to have a variable persist between rules without it being defined in a kie session?
TLDR: Above question
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能依赖这样的全局变量。
通常,您可以通过调用
insert
、update
和modify
对工作内存进行更改来实现此目的。你的例子有点奇怪(这个奇怪的“草莓”地图是什么????),但它通常看起来像这样:
在
Init
规则中,我将一个 ArrayList 插入到工作记忆(如果尚不存在)。然后“草莓”和“樱桃”规则将地图中的所有值添加到该列表中。最后,输出规则连接所有值并将其推入“outputMap”全局。如果您需要规则了解工作内存中对象的更改,您可以调用
modify
或update
。修改将更改工作内存中的对象并使用新值重新评估后续规则。更新就像用新值重新调用“消防规则”一样。You can't rely on globals like that.
Generally you'd do this by calling
insert
,update
, andmodify
to make changes to working memory.Your example is a little strange (what is this weird map of "Strawberries"->???), but it would generally look something like this:
In the
Init
rule, I insert an ArrayList into working memory if it doesn't already exist. Then the "Strawberries" and "Cherries" rules add whatever those values from the Map are into that list. And finally the Output rule joins all the values and shoves it into the "outputMap" global.If you need your rules to be aware of changes to the objects in working memory, you'd call
modify
orupdate
. Modify will change the object in working memory and reevaluate subsequent rules with the new values. Update is like calling "fire rules" anew with the new values.