Posts Tagged ‘Drools’

Drools 6 Decision Table with Spring MVC

June 8, 2015 Leave a comment

Drools is the Java Rules engine from Jboss. This article covers on how to use the Drools Decision Table using excel sheets with a Spring MVC project.

To start with, create a basic Spring MVC project using Spring STS. Add the following dependencies for drools on the pom file generated by the template in STS.


Create a spring-context.xml file in the following location /WEB-INF/spring with the following content

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns=""
 xmlns:xsi="" xmlns:beans=""

<kie:kmodule id="kmodule1">
 <kie:kbase name="kbase1" packages="com.codesilo.wordpress.rules">
 <kie:ksession name="ksession1" type="stateless" />

<beans:bean id="kiePostProcessor"
 class="org.kie.spring.KModuleBeanFactoryPostProcessor" />


The package here “com.codesilo.wordpress.rules” denotes the location where the decision tables are located. We will create that one we have created the POJO.

Add the /WEB-INF/spring/spring-context.xml on the contextConfigLocation in web.xml. The contextConfigLocation should now look like


We create the POJO named as RouteAttribute in the following package com.wordpress.codesilo.model. This object will be used as the fact for the rules defined,

package com.wordpress.codesilo.model;

public class RouteAttribute {

private String country;
 private String state;
 private boolean newEntry;
 private String ansOnRule;

public String getCountry() {
 return country;

public void setCountry(String country) { = country;

public String getState() {
 return state;

public void setState(String state) {
 this.state = state;

public boolean isNewEntry() {
 return newEntry;

public void setNewEntry(boolean newEntry) {
 this.newEntry = newEntry;

public String getAnsOnRule() {
 return ansOnRule;

public void setAnsOnRule(String ansOnRule) {
 this.ansOnRule = ansOnRule;

Now we create the decision table (Rules.xls) on the following location com.codesilo.wordpress.rules


The file can also be downloaded from here.

As a last step, we change to fire the rules. This can be later moved to any controller/MVC flow. We inject the KieBase object by the following..

private KieBase kieBase;

Add the following in the default method on the HomeController class.

RouteAttribute routingComponent = new RouteAttribute();


StatelessKieSession statelessKieSession =  kbase.newStatelessKieSession();


Now, if we bring up the server, the default method is going to run and call the rules from above.