Drools and Maven example project

User Rating: 3 / 5

Star activeStar activeStar activeStar inactiveStar inactive

In the following tutorial we will learn how to create a Maven based Drools project and run a sample rule on it.

The prerequisite to this tutorial is that you have installed Maven on your pc or Maven2Eclipse plugin which allows running Maven projects from within your Eclipse environment. (You can read more about Maven and Eclipse integration in this tutorial).

In order to get started you just need a basic Maven archetype which can be used to set up an initial structure for our projects. The maven-archetype-quickstart can be used for this purpose.

From a shell, issue the following command:

mvn archetype:generate -DgroupId=com.sample.app -DartifactId=my-app

       -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Once done with the archetype, let's work on the pom.xml file which contains our Maven project model. There, we are going to set the Drools dependencies.



Ok, now we need to set up a simple Rule which is based on the Account class. This rule will issue a warning if the Account class' balance will drop below a certain value.

package com.sample.model;

public class Account {
    private Integer balance;

    public Account() {}
    public Integer getBalance() {
        return balance;
    public void setBalance(Integer balance) {
        this.balance = balance;
    public Account(Integer balance) {
        this.balance = balance;
    public void withdraw(int money) {
        balance -= money;

And here's our simple Rule:

package demo
import com.sample.model.Account
rule "accountBalanceAtLeast"
  $account : Account( balance < 100 )
  System.out.println("Warning! money running out!");

And now, let's code a simple Main class which will instantiate our process:

package com.sample.app;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;

import com.sample.model.Account;

public class Test2 {
    public static final void main(String[] args) {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("myrule.drl"), ResourceType.DRL);
        KnowledgeBase kbase = kbuilder.newKnowledgeBase();
        StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
        Account account = new Account(200);


Now last thing in order to run our main class is a maven compiler plugin and the maven exec plugin which is configured to run as main class com.sample.app.Test1





You can compile and test your example using:

mvn install test

The expected output is:

Warning! money running out!
Today is  Mon Apr 22 17:30:47 CEST 2013
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.649s
[INFO] Finished at: Mon Apr 22 17:30:47 CEST 2013
[INFO] Final Memory: 15M/109M
[INFO] ------------------------------------------------------------------------

Download the demo example