如何在Drools文件中使用索引循环遍历列表

huangapple go评论74阅读模式
英文:

How to loop through a list using indexes/indices in drools file

问题

我有一个要求,需要在 drools 文件中使用索引循环遍历列表,因为我想将列表中的一个值与列表中的下一个值进行比较。一种方法是使用 from 关键字获取单个列表项,但我也需要索引。

 value : String() from $listOfString 
英文:

I had a requirement to loop through a list using indices in drools file because I wanted to compare an value in the list to its next value in the list. One way was to use from keyword to get individual list items but I needed indices too

 value : String() from $listOfString  

答案1

得分: 1

已解决此问题,通过在"when"部分访问列表,并在"then"子句中进行循环遍历。

rule "迭代并比较"
when
    $list : ArrayList()
then
  for (int i = 0; i < $list.size(); i++) {
    if (list.get(i) 与 list.get(i+1) 相比较) {
        // 插入/聚合结果
    }
  }
end
英文:

solved it by accessing the list in when, and for-looping through it in then clause

rule &quot;Iterate and compare&quot;
when
    $list : ArrayList()
then
  for (int i = 0; i &lt; $list.size(); i++) {
    if (list.get(i) comapred to list.get(i+1)) {
        // insert/aggregate the result
    }
  }
end

huangapple
  • 本文由 发表于 2020年7月30日 21:57:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63174758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定