Thursday, November 27, 2014

Maven Project for Drools

This post summarizes my initial tryout of Maven project for Drools

Eclipse Plugin

Installing Eclipse plugins is optional, but it will make your job easier when using Drools in Eclipse.

Firstly, you might want to install the GEF (Graphical Editing Framework) on your eclipse or STS first. To do so, select "Help-->Install New Software", and enter the following link to add:

http://download.eclipse.org/tools/gef/updates/releases/

Set the GEF as the name. As show in the figure below:

Click Next and Finish to complete the the installation of GEF.

Next install the drools plugin in Eclipse. To do so, select "Help-->Install New Software". and enter the following link to add:

http://download.jboss.org/drools/release/5.5.0.Final/org.drools.updatesite/

Set the DROOLS as the name. As shown in the figure below:


Click Next and Finish to complete the installation of DROOLS.

Configure pom.xml in Maven project

The following is the dependencies setup for Maven project to use Drools:

<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>5.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>5.5.0.Final</version>
</dependency>

To add maven plugins for compiling and executing the project:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals><goal>exec</goal></goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<executable>java</executable>
<classpathScope>compile</classpathScope>
<mainClass>com.memeanalytics.drools_hello_world.App</mainClass>
</configuration>
</plugin>

Where to add the Drool rule files in the project root folder

To create Drools rules *.drl files, first create a "src/main/resources" folder under the project root folder, and the create and save *.drl files into it. The package names in the *.drl files is up to the developer to define. Once they are in that folder, they can be retrieved directly by their name in method such as ResourceFactory.newResourceClassPathResource() method. For example, for "src/main/resources/simplerule.drl", we can write ResourceFactory.newResourceClassPathResource("simplerule.drl").

Tryout Maven Project Source Codes


Below is the source codes for the tryout

https://dl.dropboxusercontent.com/u/113201788/storm/drools-hello-world.zip

Further information can refer to the following link:

http://download.jboss.org/drools/release/5.5.0.Final/org.drools.updatesite/

Simple ways to understand Drools conditions
Drools conditions are implemented in a grammar named, MVEL (MVFLEX Expression Language). The following rule in a rule file:

rule "purchase greater than 15"
when
$p : Purchase ( total > 15)
then
System.out.println("there exists some purchase having total > 15");
end

This rule can be interpreted as "when there exists some purchase having a total > 15, then print the message".  The condition specifies "there exists some purchase having a total > 15" while the consequence is valid java statement.


No comments:

Post a Comment