Drools 6.x Basic Example


Creating the Project

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

Structure

structure

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.xExample</artifactId>
<version>0.0.1-SNAPSHOT</version>

<!-- Properties -->
<properties>
<!-- Drools -->
<drools.version>6.3.0.Final</drools.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>
</dependencies>
</project>

Rule Runner

src/main/java
com.skills421.examples.drools.controller.RuleRunner

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)
{
kSession.insert(fact);
}

kSession.fireAllRules();

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

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

return response;
}
}

RuleRunnerException

src/main/java
com.skills421.examples.drools.exceptions.RuleRunnerException

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);
}

}

Fact

src/main/java
com.skills421.examples.drools.facts.Fact

package com.skills421.examples.drools.facts;

public interface Fact
{
}

RuleResponse

src/main/java
com.skills421.examples.drools.facts.RuleResponse

package com.skills421.examples.drools.facts;

public class RuleResponse implements Fact
{
private String message;

public String getMessage()
{
return message;
}

public void setMessage(String message)
{
this.message = message;
}
}

Person

src/main/java
com.skills421.examples.drools.facts.Person

package com.skills421.examples.drools.facts;

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

public Person()
{

}

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

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;
}

}

Rules

src/main/resources/com/skills421/examples/drools/people
DrinkingRules.drl

package com.skills421.examples.drools.people

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

dialect "mvel"

/*
* This rule checks for a RuleResponse fact
*/
rule "Ensure Response Object"
when
not RuleResponse()
then
insert(new RuleResponse());
end

/*
* This rule checks that a person is >= 21
*/
rule "Person is 21"
when
$person : Person($name:name,age>=21)
$response:RuleResponse()
then
$response.setMessage($name+" is old enough to drink");
end

kmodule.xml

src/main/resources/META-INF
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>

Main

src/main/java
com.skills421.examples.drools.Main

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("Jon Doe",21)).runRules("people-rules");

System.out.println(response.getMessage());
}

}

Output

Jon Doe is old enough to drink

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

Up ↑

%d bloggers like this: