createCardinality must return a Collection. I think the simplest way of<br>solving your problem is to write a simple class LetterCounter implementing<br>the counting and a DRL function extracting the collection of Tuples from the LetterCounter:<br>
<br>public class LetterCounter extends HashMap<Character,Integer> {<br> public static Set<Map.Entry<Character,Integer>> counterSet( Collection<Character> l ){<br> return new LetterCounter( l ).entrySet();<br>
}<br> public LetterCounter( Collection<Character> chars ){<br> super();<br> for( Character c: chars ){<br> Integer count = get( c );<br> put( c, count == null ? 1 : count + 1 );<br>
}<br> }<br>}<br><br>rule "count"<br>when<br> $t: Something( $l: collectionOfLetters )<br> Map.Entry( $key: key, $val: value > 1 ) from LetterCounter.counterSet( $l )<br>then<br> System.out.println( "letter " + $key + ": " + $val );<br>
end<br><br>-W<br><br><div class="gmail_quote">On 18 December 2010 12:16, AleBu <span dir="ltr"><<a href="mailto:aleboo@gmail.com">aleboo@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Hi,<br>
I am new to Drools and do some experiments on it, and encountered on a<br>
problem which can't solve for a last few days, so maybe someone can explain<br>
what I am doing wrong?<br>
I will probably explain my problem with example. Lets say we have a<br>
collection of letters like 'a', 'b', 'c', 'a', 'd', 'e', 'c'. And I want to<br>
report all letters that are duplicated, but only once providing a number of<br>
duplication. In other words, for each letter which is duplicated I need to<br>
do a report by saying 'Letter X is duplicated N times.<br>
My idea was to create cardinality collection for letters which is a<br>
collection of POJOs Tuple where first is letter and second is cound (first<br>
and second are properties). I created a function for it createCardinality(<br>
Collection letters ) which returns such cardinality info and tried something<br>
like:<br>
when<br>
Something( $letters: collectionOfLetters )<br>
Tuple( $letter: first, $count: second > 1 ) from collect( Tuple() from<br>
createCardinality( $letters ) )<br>
then<br>
System.out.println( "Letter "+letter+" is duplicated "+$count+"<br>
times" )<br>
<br>
But I am reported (at least it looks like this) about the problems with my<br>
function createCardinality(). Maybe I misunderstood something about usage of<br>
FROM?<br>
<font color="#888888">--<br>
View this message in context: <a href="http://drools-java-rules-engine.46999.n3.nabble.com/How-to-use-a-result-of-custom-defined-function-in-WHEN-part-tp2110515p2110515.html" target="_blank">http://drools-java-rules-engine.46999.n3.nabble.com/How-to-use-a-result-of-custom-defined-function-in-WHEN-part-tp2110515p2110515.html</a><br>
Sent from the Drools - User mailing list archive at Nabble.com.<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</font></blockquote></div><br>