Drools6.0 Basic Example


This post has been updated to the very latest Drools6.x – the new post can be found here: Drools6.x Basic Example

Creating the Project

  • Open Eclipse (Kepler)
  • New -> Maven Project
  • Simple Project
  • Group Id: com.skills421.examples.drools
  • Artifact Id: Drools6.0Example
  • Finish

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>
	<groupId>com.skills421.examples.drools</groupId>
	<artifactId>Drools6.0Example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-compiler</artifactId>
			<version>6.0.0.CR1</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-core</artifactId>
			<version>6.0.0.CR1</version>
		</dependency>
	</dependencies>
</project>

Rule Runner

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

package com.skills421.examples.drools.controller;

import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.Message.Level;
import org.kie.api.io.KieResources;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class RuleRunner
{
	public RuleRunner()
	{
	}

	public void runRules(String[] rules, Object[] facts)
	{

		KieServices kieServices = KieServices.Factory.get();
        KieResources kieResources = kieServices.getResources();
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        KieRepository kieRepository = kieServices.getRepository();

        for(String ruleFile : rules)
        {
            Resource resource = kieResources.newClassPathResource(ruleFile);

            // path has to start with src/main/resources
            // append it with the package from the rule
            kieFileSystem.write("src/main/resources/com/skills421/examples/drools6_0/"+ruleFile, resource);
        }

        KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);

        kb.buildAll();

        if (kb.getResults().hasMessages(Level.ERROR))
        {
            throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
        }

        KieContainer kContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());

        KieSession kSession = kContainer.newKieSession();

        for (Object fact : facts)
        {
            kSession.insert(fact);
        }

        kSession.fireAllRules();
	}
}

Person

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

package com.skills421.examples.drools.model;

public class Person
{
	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;
	}

}

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.model.Person;

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

		String[] rules = { "test1.drl" };
		Object[] facts = { new Person("Jon Doe", 21) };

		runner.runRules(rules,facts);
	}
}

Rules

src/main/resources
test1.drl

package com.skills421.examples.drools6_0

import com.skills421.examples.drools.model.Person

dialect "mvel"

/*
 * This rule checks that a person is >= 21
 */
rule "Person is 21"
    when
        $person : Person(age>=21)
    then
    	System.out.println($person.name);
end

Output

Jon Doe
Advertisements

5 thoughts on “Drools6.0 Basic Example

    • Hi, this is a quick note to acknowledge your comment. I will look into the details of your question over the weekend and come back with a full response.

      regards

      John

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