oldDave
oldDave

Reputation: 405

Drools, how to sum values in a List in a Decision Table

Using v 6.5.0 with a Decision table

I have a compact java model, as it is delivered to the web front end intact. Thus the User class has a field that is an array of of Accounts, each with a total, amount and date field. I have written simple rules but this is beyond me ability currently. I realise it would be better to have the Accounts objects separate and with a reference to the Id of the User, but if it is possible to not have to do that then it will save time.

I need to sum the Account.total for a User and test it is within 1 of 3 different range. Then I need to take this sum and work out the % of the sum of the amount fields and test to see if that is in 1 of 3 ranges. All of this is grouped by a date range.

In SQL for one total and % range

SELECT u.id, SUM(a.total) AS TTLSUM, SUM(a.amount)/SUM(a.total) AS PCTSUM
FROM User u, Account a
WHERE u.id = a.userid 
GROUP BY u.id
HAVING SUM(a.total) > 0 AND SUM(a.total) <=1000 
AND SUM(a.amount)/SUM(a.total) > 0.5 AND SUM(a.amount)/SUM(a.total) <=0.75

Upvotes: 0

Views: 795

Answers (1)

laune
laune

Reputation: 31290

What you do is you write two functions, one for the sum and one for the percentage:

function boolean totalInRange( User user, double lo, double hi ){
    // access user's double[] and calculate the sum, do the test
    return lo < sum && sum <= hi;
} 

Then you write a couple of rules

rule "is sum in range x"
when
    $u: User()
    eval( totalInRange( $u, 0, 1000 ) )
then
    // do something
end

Upvotes: 1

Related Questions