[rules-users] Ensure all values in a list are unique?

David Sinclair dsinclair at chariotsolutions.com
Fri Apr 17 09:29:21 EDT 2009


How are items added to the list? Can you just change the data structure to a
set as Wolfgang suggested?

If all the groups are in working memory you can write a rule similar to the
following

rule "check for dups"
     when
             $group1 : Group($index : index)
             $group2 : Group(this != $group1, index == $index)
     then
             System.out.println($group2 + " has a duplicate index ");
end

2009/4/17 Wolfgang Laun <wolfgang.laun at gmail.com>

> Maybe I'm missing something but couldn't you simply create checkGroups as a
> Set<String>and just compare its size() to $fact.Groups.size() ?
>
>
> rule check
>     when
>         $fact : StrColl( $gr : groups, eval( $gr.size() != (new HashSet(
> $gr )).size() ) )
>     then
>         System.out.println( "failed" );
> end
>
> -W
>
>
> On Fri, Apr 17, 2009 at 8:22 AM, campbeln <nicholas.campbell at arc.gov.au>wrote:
>
>>
>> I have an ArrayList of strings that I need to ensure are all internally
>> unique (ie: {"1","2","3"} = OK, {"1","2","1"} = Error). I've managed to
>> cobble together the code below but I'm running into the "forall w/ an
>> internal from" issue. I don't believe an upgrade is feasible at the
>> moment,
>> so are there any alternative solutions?
>>
>> NOTE: The collection of both $factGroups and $checkGroups is almost
>> definitely redundant, so please keep any laughing to a minimum =)
>>
>>
>> rule "BR033"
>>   no-loop true
>>     when
>>                $fact : MyFact()
>>
>>                //# Get 2 copies of all the Groups associated with the
>> $fact
>>                $factGroups : ArrayList() from collect (
>>                        Group() from $fact.Groups
>>                )
>>                $checkGroups : ArrayList() from collect (
>>                        Group() from $fact.Groups
>>                )
>>
>>                //# Traverse the $factGroups comparing each $currentGroup
>> to the list
>>                //# within $checkGroups, erroring out if something other
>> then 1 is found
>>                forall (
>>                        $currentGroup : Group(this memberOf $factGroups)
>>                        $test : ArrayList(size != 1) from collect(
>>                                Group(this == $currentGroup) from
>> $checkGroups
>>                        )
>>                )
>>    then
>>        System.out.println("Failed: BR033");
>>    end
>>
>> I have found a number of postings that seem to be related, namely
>>
>> http://www.nabble.com/Best-practice-for-rules-against-multiple-objects-td5621720.html#a5621720
>> but I've been unable to cobble together a solution thus far. All help is
>> appreciated!
>>
>> Thanks!
>> --
>> View this message in context:
>> http://www.nabble.com/Ensure-all-values-in-a-list-are-unique--tp23071079p23071079.html
>> Sent from the drools - user mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090417/487dba61/attachment.html 


More information about the rules-users mailing list