Drools 6.x Decision Table Example


Creating the Project

  • Open Eclipse
  • New -> Maven Project (simple project)
  • Simple Project
  • Group Id: com.skills421.examples.drools
  • Artifact Id: Drools6.xDecisionTableExample
  • Finish

Structure

structure.png

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <!-- This Project -->
  <groupId>com.skills421.examples.drools</groupId>
  <artifactId>Drools6.xDecisionTableExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <!-- Properties -->
	<properties>
		<!-- Drools -->
		<drools.version>6.3.0.Final</drools.version>

		<!-- JUnit -->
		<junit.version>4.12</junit.version>

		<!-- maven-compiler-plugin -->
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.source>1.8</maven.compiler.source>
	</properties>

	<!-- Dependencies -->
	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.drools/drools-core -->
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-core</artifactId>
			<version>${drools.version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.drools/drools-compiler -->
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-compiler</artifactId>
			<version>${drools.version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.drools/drools-compiler -->
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-decisiontables</artifactId>
			<version>${drools.version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
		</dependency>
	</dependencies>
</project>

Facts

Fact.java

package com.skills421.examples.drools.facts;

public interface Fact
{
}

Person.java

package com.skills421.examples.drools.facts;

public class Person implements Fact
{
	private String country;
    private String name;
    private int age;

    public Person()
    {

    }

    public Person(String country,String name, int age)
    {
    	this.country = country;
        this.name = name;
        this.age = age;
    }

    public String getCountry()
	{
		return country;
	}

	public void setCountry(String country)
	{
		this.country = country;
	}

	public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

	@Override
	public String toString()
	{
		return String.format("Person [country=%s, name=%s, age=%s]", country,
				name, age);
	}
}

RuleResponse.java

package com.skills421.examples.drools.facts;

import java.util.ArrayList;
import java.util.List;

public class RuleResponse implements Fact
{
    private List<String> messages = new ArrayList<>();

    public List<String> getMessages()
    {
        return messages;
    }

    public void setMessages(List<String> messages)
    {
        this.messages = messages;
    }

    public void addMessage(String message)
    {
    	this.messages.add(message);
    }
}

RuleRunner

RuleRunnerException.java

package com.skills421.examples.drools.exceptions;

public class RuleRunnerException extends Exception
{
    private static final long serialVersionUID = 1L;

    public RuleRunnerException()
    {
        super();
    }

    public RuleRunnerException(String message, Throwable cause,
            boolean enableSuppression, boolean writableStackTrace)
    {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public RuleRunnerException(String message, Throwable cause)
    {
        super(message, cause);
    }

    public RuleRunnerException(String message)
    {
        super(message);
    }

    public RuleRunnerException(Throwable cause)
    {
        super(cause);
    }

}

RuleRunner.java

package com.skills421.examples.drools.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.skills421.examples.drools.exceptions.RuleRunnerException;
import com.skills421.examples.drools.facts.Fact;
import com.skills421.examples.drools.facts.RuleResponse;

public class RuleRunner
{
    private List<Fact> facts = new ArrayList<>();

    public RuleRunner()
    {
    }

    public RuleRunner insertFact(Fact fact)
    {
        facts.add(fact);

        return this;
    }

    public RuleResponse runRules(String ruleset) throws RuleRunnerException
    {
        RuleResponse response = null;

        try
        {
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();

            KieSession kSession = kContainer.newKieSession(ruleset);

            for (Fact fact : facts)
            {
            	System.out.println("Inserting: "+fact);
                kSession.insert(fact);
            }

            kSession.fireAllRules();

            for (Object fact : kSession.getObjects())
            {
                if (fact instanceof RuleResponse)
                {
                    response = (RuleResponse) fact;
                }
            }

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        return response;
    }
}

Rules

Setup.drl

package com.skills421.examples.drools.people

import com.skills421.examples.drools.facts.RuleResponse

dialect "mvel"

/*
 * This rule checks for a RuleResponse fact
 */
 rule "Ensure Response Object"
    when
        not RuleResponse()
    then
    	System.out.println("Inserting response");
        insert(new RuleResponse());
end

DrinkingRules.xls
decisiontable.pngdrinkingrules

Main

Main.java

package com.skills421.examples.drools;

import com.skills421.examples.drools.controller.RuleRunner;
import com.skills421.examples.drools.exceptions.RuleRunnerException;
import com.skills421.examples.drools.facts.Person;
import com.skills421.examples.drools.facts.RuleResponse;

public class Main
{

    public static void main(String[] args) throws RuleRunnerException
    {
        RuleRunner runner = new RuleRunner();

        RuleResponse response = runner
        		.insertFact(new Person("USA","Jon Doe",21))
        		.insertFact(new Person("USA","Jane Doe",19))
        		.insertFact(new Person("UK","Mick Doe",19))
        		.runRules("people-rules");

        for(String message : response.getMessages())
        {
        	System.out.println(message);
        }
    }

}

kmodule.xml

kmodule.xml

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="test" packages="com.skills421.examples.drools.people">
        <ksession name="people-rules"/>
        </kbase>
</kmodule>

Output

Inserting: Person [country=USA, name=Jon Doe, age=21]
Inserting: Person [country=USA, name=Jane Doe, age=19]
Inserting: Person [country=UK, name=Mick Doe, age=19]
Inserting response
Jon Doe is old enough to drink in their own country (USA)
Mick Doe is old enough to drink in their own country (UK)
Jon Doe is old enough to drink in the USA
Jon Doe is old enough to drink in the UK
Jane Doe is old enough to drink in the UK
Mick Doe is old enough to drink in the UK
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s