Getting started with Drools and Netbeans

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.



  • Create a new plain java project HelloDrools

    New project

    New project

  • Add below drools libraries from the distribution zip file
    (except the core file which is not part of the zip file)

    Drools Distribution file

  • We are operating outside the eclipse environment, we need to add this jdt core file from
  • 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"
        message:Message (type=="Hello")
        System.out.println("Hello World, Drools!");
  • The main file
    Update 2012-03-01: Check the updated code with new packages here.

    package hellodrools;
    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) {
        private static void initialiseDrools() {
            //1. Read the DRL File and add to package builder
            try {
                Reader reader = new InputStreamReader(HelloDrools.class.getResourceAsStream(DRL_FILE));
            } catch (Exception ex) {
            //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++) {
                throw new IllegalArgumentException("Could not parse knowledge.");
            //3. Add package to rule base
            try {
            } catch (Exception e) {
                System.out.println("Error: " + e);
        // Method to fire all rules
        private static void runRules() {
        // Method to insert message object in session
        private static void initiliseMessageObject() {
            Message msg = new Message();
            sessionObject = rbase.newStatefulSession();
  • Run the project

    HelloDrools project

    Executed rule


  • 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).


About these ads

12 thoughts on “Getting started with Drools and Netbeans

  1. Pingback: Drools Expert read rules from String and Database « The JavaDude Weblog

  2. Pingback: Debugging Drools Rules « The JavaDude Weblog

  3. Pingback: Getting started with Drools Fusion « The JavaDude Weblog

  4. Pingback: Gettings started with JBoss jBPM5 | The JavaDude Weblog

  5. Pingback: Drools Fusion Samples | The JavaDude Weblog

  6. I am getting this error:

    java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.drools.RuleBaseConfiguration.(
    at org.drools.common.AbstractRuleBase.(
    at org.drools.reteoo.ReteooRuleBase.(
    at org.drools.reteoo.ReteooRuleBase.(
    at org.drools.RuleBaseFactory.newRuleBase(
    at org.drools.RuleBaseFactory.newRuleBase(
    at org.drools.RuleBaseFactory.newRuleBase(
    at hellodrools.HelloDrools.(
    Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    … 8 more
    Could not find the main class: hellodrools.HelloDrools. Program will exit.
    Exception in thread “main” Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    what have I done! :(

  7. hey nice tutorial, but i am having one problem that my is not able to pick up testRules.drl file and throwing NullPointerException. Please Help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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