Creating a Drools Project using kie-drools-archetype

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.41.0.Final</version>
</dependency>

Please note: the version of the kie-drools-archetype matches with the Drools Engine version.

mvn archetype:generate -B -DarchetypeGroupId=org.kie -DarchetypeArtifactId=kie-drools-archetype -DarchetypeVersion=7.41.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:

├── 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.41.0.Final</drools-version>
    <slf4j-version>1.7.26</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

Continue Learning Drools through the following tutorials:

1) Coding Rules in Decision Tables

In Drools, Decision Tables are a way to generate rules from the data entered into a spreadsheet. The spreadsheet can be a standard Excel (XLS) or a CSV File.

Drools decision tables example

If you want to learn how to code your Rules into an excel Spread Sheet, keep reading this tutorial:
Getting started with Drools Decision Tables

2) Getting started with the Kie Execution Server

The Kie Server is a Java web application that allow us to expose rules and business process to be executed remotely using REST and JMS interfaces. The following tutorial shows how to install it on the top of WildFly: Configure Kie Execution Server on WildFly

3) Deploy your assets on the Business Central

Once that you are familiar with the Kie Execution Server, you can learn how install also the Business Central where you can design, build and deploy your assets. Here is the tutorial for you: Getting started with jBPM Business Central

Found the article helpful? if so please follow us on Socials