<br>&nbsp;&nbsp; Hey Geoffrey,<br><br>&nbsp;&nbsp; Lets talk about short term and long term.<br><br>* Long term: we may need to find a way to support primitives. Right now, primitives are wrapped to simplify custom function developments.<br><br>
* Short term: knowing that all primitives are wrapped, we always recommend you to use Number instead of the concrete wrapper classes implementations.<br><br>&nbsp;&nbsp;&nbsp;&nbsp; public void accumulate(Object context,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object value) {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SumIntegerAccumulateFunction.SumData data = (SumIntegerAccumulateFunction.SumData) context;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data.total += ((Number) value).intValue();<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; Now, the explanation: to add flexibility to the users writing rules with your function, we support expressions that are MVEL evaluated and the result is sent to the function implementation. So, in your example, if the user wanted to sum the double of the distances, he could write instead:
<br><br>&nbsp;&nbsp;&nbsp;&nbsp; $totalDistance : Integer() from accumulate(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hop($distance : distance),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sumInteger( $distance * 2 )<br>&nbsp;&nbsp;&nbsp;&nbsp; );<br><br>&nbsp;&nbsp;&nbsp; So, what goes between () is really any valid mvel expression and as such, MVEL decides what is the wrapper class to return the result. Apparently, it is returning a Long for integer type numbers.
<br><br>&nbsp;&nbsp;&nbsp; Being brief, it is flexible, powerful and expressive, but it may require some refactorings in the future to improve performance, if it becomes an issue.<br><br>&nbsp;&nbsp;&nbsp; Hope it helps.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Edson<br><br><br>
<div><span class="gmail_quote">2007/8/11, Geoffrey De Smet &lt;<a href="mailto:ge0ffrey.spam@gmail.com">ge0ffrey.spam@gmail.com</a>&gt;:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
It looks like the value objects of the accumulate framework are always<br>Long&#39;s - even when they are Integers?<br><br><br>Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast<br>to java.lang.Integer
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at<br>org.drools.base.accumulators.SumIntegerAccumulateFunction.accumulate(SumIntegerAccumulateFunction.java:36)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at<br>org.drools.base.accumulators.JavaAccumulatorFunctionExecutor.accumulate(JavaAccumulatorFunctionExecutor.java
:79)<br><br><br><br>Here&#39;s the piece of code:<br><br>&nbsp;&nbsp;&nbsp;&nbsp; public void accumulate(Object context,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object value) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SumIntegerAccumulateFunction.SumData data =<br>(SumIntegerAccumulateFunction.SumData
) context;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data.total += ((Integer) value).intValue(); // breaks<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br><br><br>Here&#39;s the drl:<br><br>&nbsp;&nbsp; when<br>&nbsp;&nbsp;&nbsp;&nbsp; $totalDistance : Integer() from accumulate(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hop($distance : distance),
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sumInteger($distance)<br>&nbsp;&nbsp;&nbsp;&nbsp; );<br><br><br>Here&#39;s the Hop object:<br><br>&nbsp;&nbsp;&nbsp;&nbsp; public int getDistance() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>Look ma, no longs :) Still a ClassCastException on them though.<br>
I must be missing something really obvious?<br><br>--<br>With kind regards,<br>Geoffrey De Smet<br><br>_______________________________________________<br>rules-dev mailing list<br><a href="mailto:rules-dev@lists.jboss.org">
rules-dev@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-dev">https://lists.jboss.org/mailman/listinfo/rules-dev</a><br></blockquote></div><br><br clear="all"><br>-- <br>&nbsp;&nbsp;Edson Tirelli<br>
&nbsp;&nbsp;Software Engineer - JBoss Rules Core Developer<br>&nbsp;&nbsp;Office: +55 11 3529-6000<br>&nbsp;&nbsp;Mobile: +55 11 9287-5646<br>&nbsp;&nbsp;JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a>