The MVEL trick works. That was exactly what I was hoping to find.
thanks,
-Jess
________________________________
From: rules-users-bounces(a)lists.jboss.org
[mailto:rules-users-bounces@lists.jboss.org] On Behalf Of Edson Tirelli
Sent: Tuesday, November 11, 2008 3:40 PM
To: Rules Users List
Subject: Re: [rules-users] top n - accumulate function parameters
Accumulate functions support only a single parameter, as they are
defined now, but would be great if someone can come up with a good way
of adding support to variable number of parameters.
Having said that, you can probably try a quick trick for your rules.
The way accumulate function works now is that it takes an expression as
the parameter, evaluate the expression and sends the result of the
expression to the accumulate function implementation. Example:
... accumulate( Cheese( $price: price ),
sum( $price * 10 ) )
So the engine will calculate $price * 10 and will send the result
into the function implementation.
So, if you are using MVEL dialect, you can use the list creation
simplified syntax to pass multiple parameters to the function. I never
tried, but should work:
rule xyz
dialect "mvel"
when
...
... accumlate( Cheese( $price : price ),
averageTop( [10, $price ] ) )
then
...
end
The syntax [ ] will create a list in MVEL and will pass it into the
method call:
public void accumulate(Serializable context, Object value) {
List values = (List) value;
...
}
Problem is you only have the parameters available in the
accumulate() and reverse() call... you don't have access to them in the
init() and result() call.
If you want to improve this, you are more than welcome!
[]s
Edson
2008/11/11 Evans, Jess <JEvans(a)collegeboard.org>
I'm trying to average top N items. I can of course collect the
items and do the rest in the consequence, but thought it might be a
scenario applicable to a custom accumulate function. However, the
accumulate function interface methods only take one parameter. If I
were to declare a function top(N, things) within an accumulate call
would drools autobox the parameters into some sort of collection?
Maybe this is a naive solution anyway. I am still learning to
think declaratively.
cheers,
-Jess
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
--
Edson Tirelli
JBoss Drools Core Development
JBoss, a division of Red Hat @
www.jboss.com