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

Michael Anstis michael.anstis at gmail.com
Sat Dec 18 09:40:59 EST 2010


Fair point :)

I took "collection" to mean some letters rather than "Collection".

2010/12/18 Wolfgang Laun <wolfgang.laun at gmail.com>

> Looks somewhat different to me; originally there's a fact containing a
> Collection of letters, not individual Letter() facts, each with its own
> character. Therefore, you don't have the fact for the initial pattern.
> -W
>
> 2010/12/18 Michael Anstis <michael.anstis at gmail.com>
>
> Am I missing something or can't this be achieved purely "in rule":-
>>
>> rule "Letter counter"
>> salience -100
>> when
>>     Letter( $c: character )
>>     not LetterCount( character == $c )
>>     List( $s : size > 1 ) from collect( Letter( character == $c ) )
>> then
>>     LetterCount lc = new LetterCount();
>>     lc.setCharacter($c);
>>     lc.setDuplications($s);
>>     insert(lc);
>> end
>>
>> rule "Totals"
>>     salience 0
>> when
>>     LetterCount($c : character, $s : duplications);
>> then
>>     System.out.println("Letter " + $c + " has " + $s + " duplications"
>> end
>>
>> 2010/12/18 Wolfgang Laun <wolfgang.laun at gmail.com>
>>
>> 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
>>>>
>>>
>>>
>>> _______________________________________________
>>> 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
>>
>>
>
> _______________________________________________
> 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/8578461b/attachment.html 


More information about the rules-users mailing list