Drools列表迭代问题
问题描述:
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是相同的,我想触发一条规则。 细节存在于父列表()豆这样的:Drools列表迭代问题
protected List<ListGeneric> details;
而且也有其getter和setter方法。 现在我得到的问题是,它开始比较表格中列表中的第一个项目,这总是相同的。因此,它每次都触发规则。 那么,我如何才能在它中插入计数,就好像它比较两次那么应该触发规则? 或者如果有其他更好的解决方案,请推荐。
答
您需要测试是否存在具有特定typeCode的第二个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对象是不同的。
可能有多个重复类型代码,并且规则会针对每个代码触发一次。
非常感谢兄弟,这完美地解决了我的问题。对不起,我缺乏2点的声望,所以不能投你的答案。 :P –