How to debug drools in eclipse with example

    2 Votes

In this article, we are going to discuss the steps that needs to be undertaken to debug a drools project with the help of an example. For better understanding of the debugging process, we had created a simple maven project.

Problem Statement : The Application calculates the discount based on the Total amount and the number of cookies bought. The Rules are

  • When a customer makes a purchase greater than  15$, then provide a 10 percent discount
  • When a customer makes a purchase greater than 25$, then provide a 15 percent discount
  • When a customer makes a purchase containing two or more cookies and at least one drink, then provide an additional 5 percent discount

So to implement the above problem statement, We will create a fact call "Purchase". Class implementation of Purchase class is given below

import java.math.BigDecimal;

public class Purchase {
	private BigDecimal total;  
	private int cookieCount;
	private boolean drinkIncluded;
        private double discount;

	public Purchase(BigDecimal total, int cookieCount, boolean drinkIncluded) {
		super(); = total;
		this.cookieCount = cookieCount;
		this.drinkIncluded = drinkIncluded; = 0;

	public BigDecimal getTotal() {
		return total;

	public void setTotal(BigDecimal total) { = total;

	public int getCookieCount() {
		return cookieCount;

	public void setCookieCount(int cookieCount) {
		this.cookieCount = cookieCount;

	public boolean isDrinkIncluded() {
		return drinkIncluded;

	public void setDrinkIncluded(boolean drinkIncluded) {
		this.drinkIncluded = drinkIncluded;

	public double getDiscount() {
		return discount;

	public void setDiscount(double discount) { = discount;

To run our sample DROOLS program we need main method. In the class given below, we will load the knowledgebase and inserts facts into the Drools working memory. Once facts and rules are inserted into working memory, we will fire rules.

import java.math.BigDecimal;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.runtime.StatefulKnowledgeSession;

public class DroolsTest {
    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            Purchase first = new Purchase(new BigDecimal("17"), 1, false);
             Purchase second = new Purchase(new BigDecimal("27"), 1, false);
             Purchase third =new Purchase(new BigDecimal("27"), 3, true);
        } catch (Throwable t) {

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("Rules.drl"), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
            throw new IllegalArgumentException("Could not parse knowledge.");
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        return kbase;

We had written our rules in Rules.drl which contains our business logic.

package droolsdebug

//list any import classes here.
import Purchase;

//declare any global variables here
dialect "mvel"

rule "purchase over 15$ and less than 25$"
  $purchase : Purchase ( total > 15, total <= 25 ) 
  System.out.println("The Discount when total between 15 and 25 is: " + $purchase.getDiscount());

rule "purchase over Rs 25"
  $purchase : Purchase ( total > 25  )
  System.out.println("The Discount when total is greater then 25 is: " + $purchase.getDiscount());

rule "purchase contains combo" 
salience -1
  $purchase : Purchase ( drinkIncluded, cookieCount >= 2 )
  $purchase.setDiscount($purchase.getDiscount() + 0.05);
  System.out.println("Total discount is: " + $purchase.getDiscount());

Now to run the example in debug mode, just put a debug point in the then part of the rule. I have put the debug points in all the then part of the rule. I go to my class and Right click on the class and debug it as drools application as shown in screenshot below:

DROOLS Debug Start

Click on debug as Drools Application, you get the below screen


Click on yes and you see the debug perspective open.

DROOLS Debug perspective

I modified the and commented the below lines

/*Purchase second = new Purchase(new BigDecimal("27"), 1, false);
 Purchase third =new Purchase(new BigDecimal("27"), 3, true);

and re ran the program in debug mode and got the pointer to my first rule which matches.

DROOLS Debug Output

This is how you can debug the drools application.

Related Articles

Basic Drools Concepts
Steps to install DROOLS plugin and create DROOLS Project in Eclipse
Creating Drools Knowledgebase, Knowledgesession and Facts
How to call external function from drools drl files

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know