With those four accumulates to compute the four sums I wouldn't worry about the eval.
If there are many Position facts in WM and they are all A1 or A2 or B1 or B2 and you want an efficient solution: consider a single-pass accumulate with custom code to compute the sums, returning a boolean. -W On 19 December 2011 11:00, Harris <[email protected]> wrote: > Hi, > > I have a question regarding pattern binding and eval(). > > The following rule calculates 4 different sums from a list of > "Positions" and determines whether the ratio of the first 2 sums > is smaller than the ratio of the latter 2 sums. > > The first version reads quite intuitively but it uses > eval() which - as far as I gather from the docs - should > be avoided whenever possible. > > The second version doesn't use eval() since it binds to a > patten, namely the "Number" that represents the sum A1. But the > relation cannot be seen as clearly as in version1. > > Is there another, more elegant way to write this rule which I miss? > And does the use of eval() in this case come with any perfomance > or other drawbacks? > > Thanks for your help! > > Harris > > > # --------------------- > rule "Test-version1" > when > > Number( $sumA1: doubleValue ) from accumulate( Position( posType == "A1", > $val : value ), sum( $val ) ) > Number( $sumA2: doubleValue ) from accumulate( Position( posType == "A2", > $val : value ), sum( $val ) ) > Number( $sumB1: doubleValue ) from accumulate( Position( posType == "B1", > $val : value ), sum( $val ) ) > Number( $sumB2: doubleValue ) from accumulate( Position( posType == "B2", > $val : value ), sum( $val ) ) > > eval ( ($sumA1 / $sumA2) < ($sumB1 / $sumB2)); > > then > > System.out.println("Ratio A smaller than ratio B !"); > > end > # --------------------- > rule "Test-version2" > when > > Number( $sumA2: doubleValue ) from accumulate( Position( posType == "A2", > $val : value ), sum( $val ) ) > Number( $sumB1: doubleValue ) from accumulate( Position( posType == "B1", > $val : value ), sum( $val ) ) > Number( $sumB2: doubleValue ) from accumulate( Position( posType == "B2", > $val : value ), sum( $val ) ) > Number( doubleValue < (($sumB1/$sumB2)* $sumA2) ) from accumulate( > Position( posType == "A1", $val : value ), sum( $val ) ) > > then > > System.out.println("Ratio A smaller than ratio B !"); > > end > # --------------------- > > -- > View this message in context: > http://drools.46999.n3.nabble.com/pattern-binding-and-eval-tp3597935p3597935.html > Sent from the Drools: User forum mailing list archive at Nabble.com. > _______________________________________________ > rules-users mailing list > [email protected] > https://lists.jboss.org/mailman/listinfo/rules-users _______________________________________________ rules-users mailing list [email protected] https://lists.jboss.org/mailman/listinfo/rules-users
