Drools 列表迭代问题

Drools list iteration issue

   rule "typeChild should be unique"
when
    RuleActivator( targetMessage == "QWERTY" )
    $o:  Parent()
    $o2: ListGeneric(typeChild != null) from $o.getDetails()
$o3: ListGeneric(typeChild != null && (typeCode == $o2.typeCode)) from $o.getDetails()
then
    insert(new ValidationError(ValidationUtil.qualifyField($DECL_ROOT, $o3, "typeChild"), "UNIQUE"));end

如果 ListGenric 中的任何 typeChild 相同,我想触发规则。 详细信息是存在于 Parent() bean 中的列表,如下所示:

protected List<ListGeneric> details;

并且还有它的 setter 和 getter。 我现在遇到的问题是它开始比较列表中的第一项,并且始终是 same.So,它每次都触发规则。 那么,我怎样才能在其中插入计数,就像比较两次然后应该触发规则一样? 或者如果有其他更好的解决方法,请推荐。

您需要测试是否存在具有特定类型代码的第二个 ListGeneric。

rule "typeChild should be unique"
when
  RuleActivator( targetMessage == "QWERTY" )
  $o:  Parent()
  $lg: ListGeneric( typeChild != null, $tc: typeCode ) from $o.getDetails()
  exists ListGeneric( typeChild != null,
                      this != $lg,
                      typeCode == $tc ) from $o.getDetails()
then
  // duplicate typeCode $tc
end

注意 this != $lg 确定两个 ListGeneric 对象不同。

可能有多个重复的类型代码,规则将针对每个类型代码触发一次。