[rules-users] How to use a result of custom defined function in WHEN part?

Wolfgang Laun wolfgang.laun at gmail.com
Sat Dec 18 07:22:50 EST 2010


createCardinality must return a Collection. I think the simplest way of
solving your problem is to write a simple class LetterCounter implementing
the counting and a DRL function extracting the collection of Tuples from the
LetterCounter:

public class LetterCounter extends HashMap<Character,Integer> {
    public static  Set<Map.Entry<Character,Integer>> counterSet(
Collection<Character> l ){
        return new LetterCounter( l ).entrySet();
    }
    public LetterCounter( Collection<Character> chars ){
        super();
        for( Character c: chars ){
            Integer count = get( c );
            put( c, count == null ? 1 : count + 1 );
        }
    }
}

rule "count"
when
    $t: Something( $l: collectionOfLetters )
    Map.Entry(  $key: key, $val: value > 1 ) from LetterCounter.counterSet(
$l )
then
    System.out.println( "letter " + $key + ": " + $val );
end

-W

On 18 December 2010 12:16, AleBu <aleboo at gmail.com> wrote:

>
> Hi,
> I am new to Drools and do some experiments on it, and encountered on a
> problem which can't solve for a last few days, so maybe someone can explain
> what I am doing wrong?
> I will probably explain my problem with example. Lets say we have a
> collection of letters like 'a', 'b', 'c', 'a', 'd', 'e', 'c'. And I want to
> report all letters that are duplicated, but only once providing a number of
> duplication. In other words, for each letter which is duplicated I need to
> do a report by saying 'Letter X is duplicated N times.
> My idea was to create cardinality collection for letters which is a
> collection of POJOs Tuple where first is letter and second is cound (first
> and second are properties). I created a function for it createCardinality(
> Collection letters ) which returns such cardinality info and tried
> something
> like:
> when
>        Something( $letters: collectionOfLetters )
>        Tuple( $letter: first, $count: second > 1 ) from collect( Tuple()
> from
> createCardinality( $letters ) )
> then
>        System.out.println( "Letter "+letter+" is duplicated "+$count+"
> times" )
>
> But I am reported (at least it looks like this) about the problems with my
> function createCardinality(). Maybe I misunderstood something about usage
> of
> FROM?
> --
> View this message in context:
> http://drools-java-rules-engine.46999.n3.nabble.com/How-to-use-a-result-of-custom-defined-function-in-WHEN-part-tp2110515p2110515.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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20101218/64a5b3fe/attachment.html 


More information about the rules-users mailing list