Creating the Project

  • Open Eclipse (Kepler)
  • New -> Maven Project
  • Simple Project
  • Group Id: com.skills421.examples.drools
  • Artifact Id: Drools5.6Example
  • 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>Drools5.6Example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-core</artifactId>
			<version>5.6.0.CR1</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>knowledge-api</artifactId>
			<version>5.6.0.CR1</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-compiler</artifactId>
			<version>5.6.0.CR1</version>
		</dependency>
	</dependencies>
</project>

Rule Runner

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

package com.skills421.examples.drools.controller;

import java.util.Collection;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

public class RuleRunner 
{
	public RuleRunner()
	{
	}

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

		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

		for (int i = 0; i < rules.length; i++)
		{
			String ruleFile = rules[i];
			System.out.println("Loading file: " + ruleFile);
			kbuilder.add(ResourceFactory.newClassPathResource(ruleFile, RuleRunner.class), ResourceType.DRL);
		}

		Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
		kbase.addKnowledgePackages(pkgs);
		StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

		for (int i = 0; i < facts.length; i++)
		{
			Object fact = facts[i];
			System.out.println("Inserting fact: " + fact);
			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.drools5_6

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

Loading file: test1.drl
Inserting fact: com.skills421.examples.drools.model.Person@5480e605
Jon Doe
Advertisements

One thought on “Drools5.6 Basic Example

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