JBoss application server tutorials

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Drools and Maven example project

 

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.

<properties>
  <droolsVersion>5.5.0.Final</droolsVersion>
</properties>
<dependencies>
  <dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>${droolsVersion}</version>
  </dependency>
</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) {
        super();
        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"
  when
  $account : Account( balance < 100 )
  then
  System.out.println("Warning! money running out!");
end
						

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);
        account.withdraw(150);
        ksession.execute(account);
    }
}

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

    <build>
        <plugins>
            <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.1.1</version>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.sample.app.Test2</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>jboss</id>
            <name>jboss</name>
            <url><a href="https://repository.jboss.org/nexus/content/groups/public/</url>">https://repository.jboss.org/nexus/content/groups/public/</url></a>
        </repository>
    </repositories>

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] BUILD SUCCESS
[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 <

Drools and Maven example project
Francesco Google+
Top Programming Sites
You are here BPM Drools Drools and Maven example project