In this short tutorial we will learn how to create a Drools Project using Maven's kie-drools-archetype.

I've seen in many forums the questions which is the archetype for creating a Drools project. As you can see from the Maven's repository (https://mvnrepository.com/artifact/org.kie/kie-drools-archetype) the correct archetype is the following one (in its latest version):

<dependency>
    <groupId>org.kie</groupId>
    <artifactId>kie-drools-archetype</artifactId>
    <version>7.7.0.Final</version>
</dependency>

Let's try it!

mvn archetype:generate -B -DarchetypeGroupId=org.kie -DarchetypeArtifactId=kie-drools-archetype -DarchetypeVersion=7.7.0.Final -DgroupId=com.mastertheboss -DartifactId=drools-project -Dversion=1.0-SNAPSHOT -Dpackage=drools.project

The above command will create the drools-project folder with a Maven project. Let's check that everything is in order:

tree drools-project/
drools-project/
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── drools
    │   │       └── project
    │   │           └── Measurement.java
    │   └── resources
    │       ├── META-INF
    │       │   └── kmodule.xml
    │       └── rules.drl
    └── test
        ├── java
        │   └── drools
        │       └── project
        │           └── RuleTest.java
        └── resources
            └── log4j.properties

The archetype has create a very basic Rule file (rules.drl) which loads a simple Fact in it:

package drools.project;

global java.util.Set controlSet;

rule "will execute per each Measurement having ID color"
no-loop
when
	Measurement( id == "color", $colorVal : val )
then
	controlSet.add($colorVal);
end

A Test class is included in the Archetype, which will insert into a KieSession new Measurement. Then it fires the Rule and expects that the Session contains the three different kind of Measurement inserted:

package drools.project;

import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.drools.core.time.SessionPseudoClock;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.definition.KiePackage;
import org.kie.api.definition.rule.Rule;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.conf.ClockTypeOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RuleTest {
    static final Logger LOG = LoggerFactory.getLogger(RuleTest.class);

    @Test
    public void test() {
        KieServices kieServices = KieServices.Factory.get();

        KieContainer kContainer = kieServices.getKieClasspathContainer();
        Results verifyResults = kContainer.verify();
        for (Message m : verifyResults.getMessages()) {
            LOG.info("{}", m);
        }

        LOG.info("Creating kieBase");
        KieBase kieBase = kContainer.getKieBase();

        LOG.info("There should be rules: ");
        for ( KiePackage kp : kieBase.getKiePackages() ) {
            for (Rule rule : kp.getRules()) {
                LOG.info("kp " + kp + " rule " + rule.getName());
            }
        }

        LOG.info("Creating kieSession");
        KieSession session = kieBase.newKieSession();

        LOG.info("Populating globals");
        Set<String> check = new HashSet<String>();
        session.setGlobal("controlSet", check);

        LOG.info("Now running data");

        Measurement mRed= new Measurement("color", "red");
        session.insert(mRed);
        session.fireAllRules();

        Measurement mGreen= new Measurement("color", "green");
        session.insert(mGreen);
        session.fireAllRules();

        Measurement mBlue= new Measurement("color", "blue");
        session.insert(mBlue);
        session.fireAllRules();

        LOG.info("Final checks");

        assertEquals("Size of object in Working Memory is 3", 3, session.getObjects().size());
        assertTrue("contains red", check.contains("red"));
        assertTrue("contains green", check.contains("green"));
        assertTrue("contains blue", check.contains("blue"));

    }
}

In order to build and test the project, the following pom.xml has been included, which uses drools-bom to handle the Drool dependencies version (in this example 7.7.0.Final).

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mastertheboss</groupId>
  <artifactId>drools-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>kjar</packaging>

  <name>drools-project</name>
  <url>http://drools.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <drools-version>7.7.0.Final</drools-version>
    <slf4j-version>1.7.25</slf4j-version>
    <junit-version>4.12</junit-version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-bom</artifactId>
        <type>pom</type>
        <version>${drools-version}</version>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <scope>test</scope>
      </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j-version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.kie</groupId>
        <artifactId>kie-maven-plugin</artifactId>
        <version>${drools-version}</version>
        <extensions>true</extensions>
      </plugin>
    </plugins>

  </build>
</project>

That's all. Compile and build the project with:

$ mvn clean install test

And see it in action in the following Drools screen cast:

0
0
0
s2smodern