[rules-users] pattern binding and eval()

Harris harrisx at gmx.de
Mon Dec 19 05:00:10 EST 2011


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.



More information about the rules-users mailing list