Rule engines are a rather complex topic with potentially a steep learning curve. I am looking at a few options, Drools being one of them. As usual one can read all the theoretical papers, but rather I have a something to look at and play with fast. Ultimately I need to integrate the rule engine into my application, so I dont want to play with eclipse editors for rules but seeing a rule engine ticking inside my Netbeans project. I found one tutorial from Vishal Akhouri which I updated to the latest version of Drools and some minor fixes. Most of the sample source code is from Vishal.
Pre-Requirements:
- Netbeans 6.x and later
- Stable Drools from http://www.jboss.org/drools/downloads
Tutorial:
- Create a new plain java project HelloDrools
- Add below drools libraries from the distribution zip file drools-distribution-5.3.0.Final.zip
(except the core file which is not part of the zip file) - We are operating outside the eclipse environment, we need to add this jdt core file from http://dist.wso2.org/maven2/org/eclipse/jdt/core/3.4.2.v_883_R34x/
- Following Vishal’s tutorial we create 3 files
- Simple Message Class
package hellodrools; public class Message { private String type; private String msgtext; /** * @return the type */ public String getType() { return type; } /** * @param type the type to set */ public void setType(String type) { this.type = type; } /** * @return the msgtext */ public String getMsgtext() { return msgtext; } /** * @param msgtext the msgtext to set */ public void setMsgtext(String msgtext) { this.msgtext = msgtext; } } - The rule file testrules.drl
import hellodrools.Message rule "Hello World" when message:Message (type=="Hello") then System.out.println("Hello World, Drools!"); end - The main file HelloDrools.java
Update 2012-03-01: Check the updated code with new packages here.package hellodrools; import java.io.InputStreamReader; import java.io.Reader; import org.drools.RuleBase; import org.drools.RuleBaseFactory; import org.drools.StatefulSession; import org.drools.compiler.PackageBuilder; import org.drools.compiler.PackageBuilderErrors; public class HelloDrools { private static RuleBase rbase = RuleBaseFactory.newRuleBase(); private static PackageBuilder pbuilder = new PackageBuilder(); private static StatefulSession sessionObject; private static String DRL_FILE = "/hellodrools/testrules.drl"; public static void main(String[] args) { initialiseDrools(); initiliseMessageObject(); runRules(); } private static void initialiseDrools() { //1. Read the DRL File and add to package builder try { Reader reader = new InputStreamReader(HelloDrools.class.getResourceAsStream(DRL_FILE)); pbuilder.addPackageFromDrl(reader); } catch (Exception ex) { System.out.println(ex.getMessage()); } //2. Check for any errors PackageBuilderErrors errors = pbuilder.getErrors(); if (errors.getErrors().length > 0) { System.out.println("Some errors exists in packageBuilder"); for (int i = 0; i < errors.getErrors().length; i++) { System.out.println(errors.getErrors()[i]); } throw new IllegalArgumentException("Could not parse knowledge."); } //3. Add package to rule base try { rbase.addPackage(pbuilder.getPackage()); } catch (Exception e) { System.out.println("Error: " + e); } } // Method to fire all rules private static void runRules() { sessionObject.fireAllRules(); } // Method to insert message object in session private static void initiliseMessageObject() { Message msg = new Message(); msg.setType("Hello"); sessionObject = rbase.newStatefulSession(); sessionObject.insert(msg); } } - Run the project
Summary:
- We know almost nothing about rules yet (other than ‘rule-when-then-end’), nor have any clue about the drools engine. Up to you to read up about the background. Refer to below references.
- But the most simple rule executed in our beloved Netbeans environment. A starting point to integrate drools into your application and conquer more complex scenarios.
- I recommend to verify if you really need a rules engines or not. Depends very much on the dynamic nature of your rules and complexity (branches,etc).
References:






I can’t create .drl file. It isn’t even offered to me at all. What am I doing wrong or what I didn’t do (yet) ?
Just create a plain text file (New../Empty file..). Netbeans is not aware of a drl type.
Pingback: Drools Expert read rules from String and Database « The JavaDude Weblog
Pingback: Debugging Drools Rules « The JavaDude Weblog
Pingback: Getting started with Drools Fusion « The JavaDude Weblog
Pingback: Gettings started with JBoss jBPM5 | The JavaDude Weblog
Pingback: Drools Fusion Samples | The JavaDude Weblog
thanks man!
Thank you. Awesome.
p.s. I had to add knowledge-internal-api-5.4.0.Final.jar to /lib/ to get it working with 5.4.0
My output is as below

run:
null
Error: java.lang.NullPointerException
BUILD SUCCESSFUL (total time: 2 seconds)