Blog Posts BPMN DMN

Groupby – a new way to accumulate facts in DRL

Blog: Drools & jBPM Blog

A drawing demonstrating using groupby to get the lowest priced fruit of each kind when all fruit come from the same source.

Have you ever wanted to accumulate facts that share a particular property (for instance, the count of people in each department) in DRL? With the addition of groupby to DRL, it is easier to write rules that divide facts into groups where each group is accumulated separately. In this post, we’ll explain what groupby is, how to use it, and give examples of groupby usage.

What is groupby?

Groupby is a new syntax construct introduced in 8.41.0.Final for dividing facts into groups, allowing each group to be accumulated separately. It has the following syntax:

groupby(source-pattern; grouping-key; accumulators [; constraints])

Where:

Info
The examples in this article use the OOPath syntax for source-pattern, but the traditional syntax is also supported. For more details, see the Rule Language Reference.

Combining the above example together, we get:

groupby(
    $a: /applicants[age = 100
)

How to use groupby

You can use groupby by upgrading Drools to 8.41.0.Final or later. Groupby can then be used in your DRL files like any other language construct (such as forall and accumulate).

Example groupby usage

Groupby is a good choice whenever you need to accumulate results for separate groups. Some examples of rules that can be implemented with it include:

rule "Departments over budget"
    groupby($order: /order;
            $department: $order.department;
            $totalCost: sum($order.cost);
            $totalCost > $department.budget
    )
then
    // ...
end
rule "Understaffed Days"
    groupby($shift: /shifts[ employee != null ];
            $date: $shift.date;
            $assignedCount: count();
            $assignedCount < $date.minimumAssignedShifts
    )
then
    // ...
end
rule "Highest bid for a product"
    groupby($bid: /bids;
            $product: $bid.product;
            $highestBid: max($bid)
    )
then
    // ...
end

Conclusion

Groupby is a new language feature introduced in 8.41.0.Final that allows for simpler grouping of objects by a key function. You can use it by upgrading Drools to 8.41.0.Final or later. Groupby is useful for implementing rules that accumulate facts that share a given property, such as getting the total cost by department or getting the highest bidder by product.

The post Groupby – a new way to accumulate facts in DRL appeared first on KIE Community.

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="https://www.businessprocessincubator.com/content/groupby-a-new-way-to-accumulate-facts-in-drl/?feed=html" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples

BPMN.org

XPDL.org

×