2015-10-06 102 views
0
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方法。 現在我得到的問題是,它開始比較表格中列表中的第一個項目,這總是相同的。因此,它每次都觸發規則。 那麼,我如何才能在它中插入計數,就好像它比較兩次那麼應該觸發規則? 或者如果有其他更好的解決方案,請推薦。

回答

0

您需要測試是否存在具有特定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對象是不同的。

可能有多個重複類型代碼,並且規則會針對每個代碼觸發一次。

+0

非常感謝兄弟,這完美地解決了我的問題。對不起,我缺乏2點的聲望,所以不能投你的答案。 :P –