Understanding Execution Control in Drools using Simple Example | JavaInUse






Drools Tutorials- Understanding Execution Control in Drools using Simple Example



Overview

In previous chapter we implemented a drools project to understand various attributes like salience. In this tutorial we will try to understand the execution controls statements in Drools agenda-group, ruleflow-group and activation-group.
If your KieBase defines multiple rules and if you want to selectively execute a subset of them, Drools provides several features to accomplish that.
  • agenda-group:This is an optional keyword. It can be assigned for all rules. If not assigned to any agenda group, the rule by default belongs to ‘main’ agenda.
  • ruleflow-group: This behaves exactly similar to agenda-group, except that it is generally used for rules used from jBPM process flows.
  • activation-group is a reserved keyword in drools drl file. There can be a single rule or multiple rules that can belong to a particular activation-group. Rules that belong to activation-group fire in similar fashion to "if..else if..else" block in java. In an activation group only one rule can fire at a time

JBoss Drools - Table of Contents

JBoss Drools Hello World JBoss Drools Hello World-Stateful Knowledge Session using KieSession JBoss Drools- Understanding Drools Decision Table using Simple Example Understand Drools Stateful vs Stateless Knowledge Session Drools Tutorials- Backward Chaining simple example Drools Tutorials- Understanding attributes salience, update statement and no-loop using Simple Example Drools Tutorials- Understanding Execution Control in Drools using Simple Example Drools Tutorials- Integration with Spring MVC Drools Tutorials- Integration with Spring Boot

Video

This tutorial is explained in the below Youtube Video.

Lets Begin

Understanding activation-group

For our example here we will use the example of jewellery shop we used before. We will be modifying the files as follows-
In the drl file we add the salience attribute and activation-group.
package com.rule

import com.javainuse.model.Product

rule "Offer for Diamond"
	when 
		productObject: Product(type=="diamond")
	then
		productObject.setDiscount(15);
	end
rule "Offer for Gold"
salience 1
activation-group "gold"
	when 
		productObject: Product(type=="gold")
	then
		productObject.setDiscount(productObject.getDiscount() + 5);
		System.out.println("Offer for Gold");
	end
rule "Offer for Gold on Festival"
salience 2
activation-group "gold"
	when 
		productObject: Product(type=="gold") && Product(event=="sale")
	then
		productObject.setDiscount(productObject.getDiscount() + 5);
		System.out.println("Offer for Gold on Festival");
	end

So in the drl file we have a single activation group named gold. For this activation group only a single rule with higher salience will get fired. So above only the rule "Offer for Gold on Festival" with salience 2 gets fired, while rule "Offer for Gold" with salience 1 does not get fired.

Download Source Code

Download it - Drools execution control example
JBoss Drools Hello World
JBoss Drools Hello World-Stateful Knowledge Session using KieSession
JBoss Drools- Understanding Drools Decision Table using Simple Example
Understand Drools Stateful vs Stateless Knowledge Session
Drools Tutorials- Backward Chaining simple example
Drools Tutorials- Understanding attributes salience, update statement and no-loop using Simple Example  
Drools Tutorials- Integration with Spring
Drools Interview Questions
Drools-Main Menu.