有没有办法让 drools 变量在规则之间持续存在?

发布于 2025-01-17 02:15:48 字数 1340 浏览 4 评论 0原文

我想做的是将单独存储到全局输出映射中的各种字符串连接起来。但是,我有一些限制。

  1. 我不允许将任何没有“Output”键的对添加到outputMap 中。
  2. 除了outputMap 之外,我不允许在 kie 会话中插入/设置任何全局变量。
  3. 这些以下面字母结尾的子规则需要定义为单独的规则。

因此,我使用 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.

  1. I am not allowed to add any pair into the outputMap that doesn't have a key of "Output".
  2. I am not allowed to insert/set any globals into the kie session aside from the outputMap.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2025-01-24 02:15:48

你不能依赖这样的全局变量。

通常,您可以通过调用 insertupdatemodify 对工作内存进行更改来实现此目的。

你的例子有点奇怪(这个奇怪的“草莓”地图是什么????),但它通常看起来像这样:

rule "Init"
when
  not( ArrayList() )
then
  insert( new ArrayList() )
end

rule "Strawberries"
salience 1
when
  $concatList: ArrayList()
  Map( $fruit: this["Strawberries"] )
then
  $concatList.add($fruit)
end

rule "Cherries"
salience 1
when
  $concatList: ArrayList()
  Map( $fruit: this["Cherries"] )
then
  $concatList.add($fruit)
end

rule "Output"
when
  $concatList: ArrayList()
then
  String s = String.join(";", $concatList);
  outputMap.put("Output", s);
end

Init 规则中,我将一个 ArrayList 插入到工作记忆(如果尚不存在)。然后“草莓”和“樱桃”规则将地图中的所有值添加到该列表中。最后,输出规则连接所有值并将其推入“outputMap”全局。

如果您需要规则了解工作内存中对象的更改,您可以调用 modifyupdate。修改将更改工作内存中的对象并使用新值重新评估后续规则。更新就像用新值重新调用“消防规则”一样。

You can't rely on globals like that.

Generally you'd do this by calling insert, update, and modify 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:

rule "Init"
when
  not( ArrayList() )
then
  insert( new ArrayList() )
end

rule "Strawberries"
salience 1
when
  $concatList: ArrayList()
  Map( $fruit: this["Strawberries"] )
then
  $concatList.add($fruit)
end

rule "Cherries"
salience 1
when
  $concatList: ArrayList()
  Map( $fruit: this["Cherries"] )
then
  $concatList.add($fruit)
end

rule "Output"
when
  $concatList: ArrayList()
then
  String s = String.join(";", $concatList);
  outputMap.put("Output", s);
end

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 or update. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文