Fair point :)<br><br>I took "collection" to mean some letters rather than "Collection".<br><br><div class="gmail_quote">2010/12/18 Wolfgang Laun <span dir="ltr"><<a href="mailto:wolfgang.laun@gmail.com">wolfgang.laun@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">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.<br>
-W<br><br><div class="gmail_quote">2010/12/18 Michael Anstis <span dir="ltr"><<a href="mailto:michael.anstis@gmail.com" target="_blank">michael.anstis@gmail.com</a>></span><div><div></div><div class="h5"><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Am I missing something or can't this be achieved purely "in rule":-<br><br><span style="font-family: courier new,monospace;">rule "Letter counter"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">salience -100</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">when</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Letter( $c: character )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> not LetterCount( character == $c )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> List( $s : size > 1 ) </span><span style="font-family: courier new,monospace;">from collect( Letter( character == $c ) )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">then</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> LetterCount lc = new LetterCount();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> lc.setCharacter($c);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> lc.setDuplications($s);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> insert(lc);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">end</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">rule "Totals"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> salience 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">when</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> LetterCount($c : character, $s : duplications);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">then</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> System.out.println("Letter " + $c + " has " + $s + " duplications"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">end</span><br style="font-family: courier new,monospace;"><br><div class="gmail_quote">2010/12/18 Wolfgang Laun <span dir="ltr"><<a href="mailto:wolfgang.laun@gmail.com" target="_blank">wolfgang.laun@gmail.com</a>></span><div>
<div></div><div><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">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><font color="#888888"><br>-W</font><div><div></div><div><br><br><div class="gmail_quote">On 18 December 2010 12:16, AleBu <span dir="ltr"><<a href="mailto:aleboo@gmail.com" target="_blank">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" target="_blank">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>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">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>
<br></blockquote></div></div></div><br>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">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>
<br></blockquote></div></div></div><br>
<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>
<br></blockquote></div><br>