Connecting Drools 5.6 with Spring 3.x


This post is concerned predominantly with linking Spring3.x and Drools5.6. We will configure Spring, Drools and create a simple rule to test that we are up and running.

Add the Drools Plug-in to Spring STS

Start Eclipse
Help -> Install New Software
Work With: http://download.jboss.org/drools/release/5.5.0.Final/org.drools.updatesite/
click the “Add” button
Name: Drools
Check the Drools and jBPM checkbox and follow the instructions to get it installed.

Create the Project

In Spring STS or Eclipse

File -> New -> Maven Project

Check Create a Simple Project

Next

Group Id: com.skills421.examples.drools

Artifact Id: DroolsSpring

Edit the pom.xml

Edit the pom.xml to include the Drools API

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.skills421.examples.drools</groupId>
	<artifactId>DroolsSpring</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<drools.version>5.6.0.Final</drools.version>
		<commons-version>1.1.3</commons-version>
		<junit.version>4.11</junit.version>
	</properties>

	<dependencies>
		<!-- drools dependencies -->
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>knowledge-api</artifactId>
			<version>${drools.version}</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-core</artifactId>
			<version>${drools.version}</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-compiler</artifactId>
			<version>${drools.version}</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-spring</artifactId>
			<version>${drools.version}</version>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>${commons-version}</version>
		</dependency>

		<!-- test dependencies -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
		</dependency>

	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

pom.xml

Note that the drools-spring artifact will bring in the Spring dependencies it needs – running at Spring version 3.0.6.RELEASE

applicationContext.xml

In the folder src/main/resources create the file applicationContext.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
    <import resource="classpath:drools-context.xml"/> 
</beans>

applicationContext.xml

Note how this file, relies on a separate drools-context.xml

drools-context.xml

In the folder src/main/resources create the file drools-context.xml as follows:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:drools="http://drools.org/schema/drools-spring"
       
       xsi:schemaLocation="
       	http://www.springframework.org/schema/beans 
       	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       	
  		http://drools.org/schema/drools-spring 
  		http://drools.org/schema/drools-spring.xsd">
 
    <drools:grid-node id="node1" />
    <drools:resource id="resource1" type="DRL" source="classpath:rules/skills421/examples/rules.drl" />
	<drools:kbase id="kbase1" node="node1">
		<drools:resources>
			<drools:resource ref="resource1" />
		</drools:resources>
	</drools:kbase>
	
	<drools:ksession id="ksession1" type="stateful" kbase="kbase1" node="node1" />
	<drools:ksession id="ksession2" type="stateless" kbase="kbase1" node="node1" />
 
</beans>

drools-context.xml

Create a Simple Rule

In src/test/resources create the directory structure rules/skills421/examples.
In the examples folder create the rules.drl file as follows:

package rules.skills421.examples

import drools.cookbook.chapter07.model.Server
import drools.cookbook.chapter07.model.Virtualization

rule "always true"
dialect "mvel"
when
then
	System.out.println("Woohoo - Rules are running!");
end

rules.drl

SimpleRuleRunner JUnit Test

Ok, it’s not really a properly written JUnit test, but in src/test/java create the java file SimpleRuleRunnerTest.java as follows:

package com.skills421.drools.examples;

import static org.junit.Assert.*;

import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SimpleRuleRunnerTest
{

	@Test
	public void test()
	{
		AbstractApplicationContext applicationContext 
			= new ClassPathXmlApplicationContext("applicationContext.xml");		
		applicationContext.start();
		
		StatefulKnowledgeSession ksession1 
			= (StatefulKnowledgeSession) applicationContext.getBean("ksession1");
		
		ksession1.fireAllRules();
		applicationContext.stop();
		
		applicationContext.close();
	}

}

SimpleRuleRunnerTest.java

Run the JUnit Test

Now run the JUnit Test and you should see the following output:

Woohoo - Rules are running!
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