首页 > 解决方案 > 如何使用drools文件中的索引/索引遍历列表

问题描述

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

 value : String() from $listOfString  

标签: javadroolsrule-engine

解决方案


通过在 when 中访问列表并在 then 子句中循环遍历它来解决它

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

推荐阅读