Drools Decision Table Tutorial for Beginners using Drools 6.4

Drools Decision Table

In this Drools Decision Table Tutorial for Beginners, we’ll learn how to write first decision table rule, and run it using Drools Eclipse Plugin. We are going to use Drools 6.4.0 here along with Eclipse Mars and Drools plugin for Java. JDK 1.8 is used to implement this example. But before getting into actual code, let us understand these excel based decision tables. What is importance of this format of rule storage? Who will be benefited using decision tables and why? Then we go ahead and discuss the code elements in detail.

What are Decision Tables?

In my previous post, Drools Tutorial for Beginners using Drools 6 and Eclipse, we discussed how to write simple rule using drl file. If we take a close look at this drl file, one can easily say that this is not for people who do not understand code, who are not programmers. Especially where rules are complex. But in actual industry, rule are defined by business users, and most of them do not know programming.

Another important thing is, many times these rules are supposed to be changed frequently, depending on changes in business scenarios. For each change, if business users have to go to developers and get the code changed, it will be very difficult to maintain these rules. Hence there is need of a simple format, which can be understood by non-programmers easily. This format is decision tables.

During 1960-1970, many programming languages for business rule implementation, used this format. After release of Excel in 80s, this format got moved to excel. If you have interacted with business users, and you are a hard core programmer, you must have noticed that business people are, I can confidently say, expert in Excel. Many of them have capability to write small applications in Microsoft Excel.

Hence this format, decision tables, is a very good option to use where something is going to be changed frequently by non-programmers. That is why decision tables are supported by most of the rule engines, including Drools for Authoring rules (Authoring is exact term used for Rule maintenance). This does not mean decision tables are not for programmers.

Now let us get to our example.

What is required for Drools Decision Table Tutorial?

Similar to previous example, here also we require following software –

  • JDK 1.8
  • Eclipse Mars
  • Drools Plugin

How to set up environment is explained in previous tutorial. You can use it to set up environment which includes download and installation of Drools Eclipse Plugin.

Creating First Drools Decision Table Project

Let us create first Drools project from File -> New menu using Drools Project option. While creating project, as shown in following picture, create project with example files. This will avoid hours of effort for a beginner to set the libraries and path correctly. Instead everything will be already set, and you can focus on code.

Create Drools Project

After clicking next in above pop-up, on next screen select only “Hello World Decision Table” option, and complete project creation by giving appropriate name. Here I have given DroolsDecisionTableProject. After creation of project, from files created by plugin, you can delete the java files and rename sample.xls file as CustomerRules.xls, as we are going to write our own Java files.

Set Drools Project Parameters

Code Needed for Drools Decision Table Tutorial

Next step is writing code that we need. Below is the list of files that we need to write.

  1. CustomerRuleObject.java – This is a data object to be used to send data to rules and get data back.
  2. RunRule.java – It contains code to run the rules.
  3. CustomerRules.xls – Decision Table file. (We have renamed Sample.xls in earlier step, hence we use it here)
  4. kmodule.xls – This we do not create, but plugin created it. We are going to use it.

This is how the code structure will look like.

Decision Table Project Structure

Now let us take a look at code


This contains sample attributes of Customer object with getter and setter methods for those attributes. We also have two extra attributes, one is the message, which we are going to use to get response out and other is isValid attribute, which we’ll discuss later.

package net.deepakgaikwad.drools;

public class CustomerRuleObject {
	private String firstName;
	private String lastName; 
	private String message = "";
	private boolean isValid=true;
	public boolean isValid() {
		return isValid;
	public void setValid(boolean isValid) {
		this.isValid = isValid;
	public String getFirstName() {
		return firstName;
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	public String getLastName() {
		return lastName;
	public void setLastName(String lastName) {
		this.lastName = lastName;
	public String getMessage() {
		return message;
	public void setMessage(String message) {
		this.message = message;



This Java file contains step by step code to run the rules.

  • First three lines are to create KieSession, a session required to interface with rule executor. Note the name of KieSession, “ksession-dtables”, this has to match the name in kmodule.xml.
  • After this we are creating CustomerRuleObject instance and setting rule data, firstName. This object is inserted in KieSession, and finally, after executing rules, message is printed.

Ignore crude way of exception catching, it is just for example, you can go for more elegant way of catching specific exception and handling those.

package net.deepakgaikwad.drools;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class RunRule {

	public static void main(String[] args) {
        try {
            // load up the knowledge base
	        KieServices ks = KieServices.Factory.get();
    	    KieContainer kContainer = ks.getKieClasspathContainer();
        	KieSession kSession = kContainer.newKieSession("ksession-dtables");
        	CustomerRuleObject customerRule = new CustomerRuleObject();
        } catch (Exception ex) {



This is Drools rule configuration file used to define all rule sets and giving name to the rule KieSession. Note kbase package name, it has to match the package in which decision table CustomerRules.xls is kept. Second is ksession tag where name matches the session name used in above run code.

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="dtables" packages="dtables">
        <ksession name="ksession-dtables"/>


Rule xls is the most important file to understand for a decision table rule author. Let us understand contents of this excel one by one.

  • First note the structure of this excel, better to start with Sample.xls that creating new own. Rename it to CustomerRule.xls
  • First row contains RuleSet attribute, which says that this excel is a rule set, and what is name of this rule set? We have defined in next cell as net.deepakgaikwad.drools
  • Import row is to define all java classes (custom java classes not Drools java classes) referred in rule definition rules.
  • RuleTable CustomerRule is name of RuleTable below
  • Column B contains the names of rule rows
  • Next row has CONDITION and ACTION columns, which indicates that below this heading there are contents for condition and action on satisfying the condition respectively.
  • “customer:CustomerRuleObject” is defining customer variable of CustomerRuleObject type to use in remaining rule
  • Next is rule condition, note we have used variable defined in above step. Also see how firstName attribute is used, Drools will call getter method of this attribute. Ignore code after &&.
  • Below is name of rule – “First Name”
  • Cell below contains value “Deepak” which will be used in this condition to replace param in it. As it is String, we are using double quote for parameter.
  • ACTION is executed when the condition is satisfied. Ignore first setValid statement and understand next statement how message is returned by reading param value below.

Drools Decision Table

Running Drools Decision Table Tutorial Example

Go to RunRule.java, right click on it and “Run As” a “Java Application”. Following result will be printed on console. We can ignore first three line and check the last line which is output passed from rules excel.

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

First Name is Deepak 

Avoiding Infinite Loop in Drools Decision Tables

Bit complex thing, but, as code is already there, I am explaining it. Your question will be, what code is there? It is the code I asked you to ignore earlier.

  • CustomerRuleObject – isValid attribute
  • xls – Condition is if isValid==true and in Action we are setting isValid as false

What is this all? Wait!! Let me explain what happens if we don’t write this code. As we are modifying same CustomerRuleObject to return message instead of any new Class or object, after setting message in action, Drools comes to know that the object is modified and it fires rules again. Again condition get satisfied and we set message, again Drools thinks that the object is modified, this cycle continues. In short, it enters infinite loop. Nothing is returned to Java program calling.

There are two options, use another object to return or different class all together. But to keep code similar to previous Drools tutorial, I am using this trick.

Here isValid is set to true by default while creating CustomerRuleObject. Rule is executed, and in Action we are setting it false along with message. Drools understands that the object is modified and goes to fire all rules, but the condition does not get satisfied because isValid is false now, no object modification and code returns, we see output on console.

One more thing that might trouble you while modifying decision table excel and running code from Eclipse is excel modification from Eclipse. I have modified excel outside Eclipse and refreshed project in Eclipse. This was handy though sometimes I had to face excel lock.


  1. Hi,

    How to load the DRL / decision table in KIE in runtime and from local path. Since the business rules changes frequently, mainitaining the rules in class path may lead to frequent code deployment. Is it possible to maintain these rule files (.DRL / .XLS) in local path? If we place the files in the defined path, instead of deployment, can the rule engine refer to these new / updated files? Can you please provide the code snippet for the same. I am using KIE in maven. Thank you

Leave a Reply

Your email address will not be published.