John Vernon
John Vernon

Reputation: 1

Applying Drools decision tables to 1-n data structures

I have the following classes, where there's a 1 - n relationship between Customer and Order, i.e. each Customer has many Orders

class Customer {
    string identifier;
    string country;
    Collection orders;
}
class Order {
    string identifier;
    float amount;
}
class Report {
    string identifier;
    string country;
    float amount;
}

I want to write the following in the form of one or more Drools decision tables.

For each Customer c
    if c.country == US then
        for each Order o
            if o.amount > $10 then
                create Report r
                r.country = c.country
                r.amount = o.amount

How possible is this using Drools decision tables?

When a Customer object meets the Customer condition, I then need to run each instance in the collection of Orders through the Order condition. If the order meets the condition, I need to create a report object that has values taken from the Customer and from the Order.

Upvotes: 0

Views: 74

Answers (1)

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15179

Drools will naturally iterate through collections.

This what your rule would look like in DRL:

rule "US Customer - Create Reports"
when
  $customer: Customer( country == "US", $orders: orders != null )
  $order: Order( amount > 10 ) from $orders
then
  Report r = new Report();
  r.country = $customer.country;
  r.amount = $order.amount;
  // TODO: do something with Report r here
end

This flattens naturally into a decision table in a very straight-forward fashion. You can even sub out the "US" (country) and 10 (amount) to variables.

Upvotes: 1

Related Questions