在 drools 中,如何在 ArrayList 中搜索对象并在不存在时添加

In drools, how to search an object in ArrayList and add if not present

我定义了以下模型 classes -

class A {
 ArrayList<Name> listOfNames
}

class Name {
 String first
 String last
}

当我收到一个新的 Name 实例时,我想通过比较 ArrayList 中每个 Name 的 first/last 字段来检查它是否存在于 class A 的实例中。 如果可能的话,还想将该规则放在 excel 中。

谢谢

一种可能的实现方式是:

rule "test"
when
  $a: A($list: listOfNames)
  $n: Name()
  not Name( first == $n.first, last == $n.last) from $list
then
  modify($a){
    addName($n)
  }
end

希望对您有所帮助,