[rules-users] How to count distinct?

Wolfgang Laun wolfgang.laun at gmail.com
Fri Nov 5 13:36:10 EDT 2010


Without any conditions and constraints, this is just a simple computation
you could code in Java and invoke in some appropriate place. But I
suspect you haven't told us the whole story...

You don't say where this "list" is; so I assume it's a field in some other
fact.

This code is absolutely untested.

rule count
when
    ListHolder( $list : list )
    $map: Map from accumulate ( XXX( $id: id, $type: type ) from $list,
          init( Map map = new HashMap(); ),
          action( Integer count = map.get( $type );
                       count = Integer.valueOf( count == null ? 1 : count+1
);
                       map.put( $type, count ); ),
          result( map ) )
then
   // ... use $map
end

-W


2010/11/5 Yaniv Itzhaki <iyaniv at gmail.com>

> Hi All,
>
> I need to go over a not distinct list and count distinct each element in
> the list.
>
> for example:
>
> Object XXX {
>      int id;
>      String type;
> }
>
> XXX:
> id type
> 1    A
> 2    B
> 3    A
> 4    C
> 5    C
>
> The rule should be activated per type and give as a result the count of
> each type:
> A 2
> B 1
> C 2
>
> Thanks
>
> _______________________________________________
> 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/20101105/ef77eada/attachment.html 


More information about the rules-users mailing list