Drools

Drools – salience and activation-group attributes

In previous chapter we implemented a drools project to understand various attributes like update and no-loop. In this tutorial we will try to understand the execution control statements in salience 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.

  1. Salience is a prioritization value. Drools uses it in order figure out which drool to fire first when it is the case that the constraints for more than one rule are satisfied.
  2. 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

1) Create Example

For our example here we will use the example of mobile shop we used before. Create the project structure as follows

Download Drools Attributes
Download counts: 99
  1. After downloading, unzip the project.
  2. Go to Eclipse IDE -> File -> Import -> Maven -> Existing Maven project -> Select the project.

We will be modifying the files as follows. In the product class we add an additional field event

package com.hi.techpoints.model;

public class Product {

	private String model;
	private int discount;
	private String event;
	
	public Product(String model, int discount, String event) {
		super();
		this.model = model;
		this.discount = discount;
		this.event = event;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public int getDiscount() {
		return discount;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}

	public String getEvent() {
		return event;
	}

	public void setEvent(String event) {
		this.event = event;
	}
	
}

2) Understanding salience attribute

In the drl file we add the salience attribute.

import com.hi.techpoints.model.Product

rule "Offer for Iphone"
	when 
		productObject: Product(model == "Iphone")
	then
		productObject.setDiscount(5);
		System.out.println("Offer for Iphone");
	end
	
rule "Offer for Pixel"
salience 10
	when 
		productObject: Product(model == "Pixel")
	then
		productObject.setDiscount(productObject.getDiscount() + 10);
		System.out.println("Offer for pixel");
	end
	
rule "Offer for Pixel on New Year Sale"
salience 20
	when 
		productObject: Product(model == "Pixel" && event == "Sale")
	then
	    productObject.setDiscount(productObject.getDiscount() + 15);
		System.out.println("Offer for Pixel on New Year Sale");
	end	

Both the rules “Offer for Pixel” and “Offer for Pixel on New Year Sale” have condition product model = Pixel.
So in this scenario which rule should applied first for ‘Pixel’ mobile model? For this we use Salience attribute.
The rule with higher salience value will be executed first. So here the rule “Offer for Pixel on New Year Sale” will be executed first.

Output will be:

So the discounts of both rules “Offer for Pixel on New Year Sale” and “Offer for Pixel” get applied in sequence.

3) Understanding activation-group attribute

We will be modifying the files as follows – In the drl file we add the salience and activation-group attributes.

import com.hi.techpoints.model.Product

rule "Offer for Iphone"
	when 
		productObject: Product(model == "Iphone")
	then
		productObject.setDiscount(5);
		System.out.println("Offer for Iphone");
	end
	
rule "Offer for Pixel"
salience 10
activation-group "Pixel Mobile"
	when 
		productObject: Product(model == "Pixel")
	then
		productObject.setDiscount(productObject.getDiscount() + 10);
		System.out.println("Offer for pixel");
	end
	
rule "Offer for Pixel on New Year Sale"
salience 20
activation-group "Pixel Mobile"
	when 
		productObject: Product(model == "Pixel" && event == "Sale")
	then
	    productObject.setDiscount(productObject.getDiscount() + 15);
		System.out.println("Offer for Pixel on New Year Sale");
	end	

So in the drl file we have a single activation group named ‘Pixel Mobile’. For this activation group only a single rule with higher salience will get fired. So above only the rule “Offer for Pixel on New Year Sale” with salience 20 gets fired, while rule “Offer for Pixel” with salience 10 does not get fired.

Output will be:

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs