Drools is a slim Business Rules Management System (BRMS) solution with different integrations and tools available.

In the following short snippet I’d like to demonstrate how to integrate a simple rule engine into an application using this library.

example drools app in eclipse ide 1024x792
Figure 1. Drools Example running in Eclipse IDE

Dependencies

Using Gradle here we only need to add one dependency for drools-compiler to our project’s build.gradle. In addition, we’re adding a task to execute our application.

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile 'org.drools:drools-compiler:6.5.0.Final'
}

task runExample(type:JavaExec) {
    main = "com.hascode.tutorial.Main"
    classpath = sourceSets.main.runtimeClasspath
}

Sample Application

This is our short example application simulating a book order service.

BookService

Our book service is responsible for handling orders and also initializes the drools API using the KnowledgeBuilder, KieBase and Kie-Session.

package com.hascode.tutorial.boundary;

import java.util.ArrayList;
import java.util.List;

import org.kie.api.KieBase;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;

import com.hascode.tutorial.control.BillingService;
import com.hascode.tutorial.entity.Order;

public class BookService {

    private final BillingService billingService = new BillingService();
    private final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    private final KieBase kbase;
    private final StatelessKieSession ksession;

    public BookService() {
        kbuilder.add(ResourceFactory.newClassPathResource("rules/book-order.drl"), ResourceType.DRL);
        kbase = kbuilder.newKnowledgeBase();
        ksession = kbase.newStatelessKieSession();
    }

    public void order(String bookId, int quantity) {
        Order order = new Order(bookId, quantity);
        List<Object> environment = new ArrayList<>();
        environment.add(billingService);
        environment.add(order);
        ksession.execute(environment);
    }
}

BillingService

Our billing service “stores” orders..

package com.hascode.tutorial.control;

import java.util.ArrayList;
import java.util.List;

import com.hascode.tutorial.entity.Order;

public class BillingService {
    private final List<Order> orders = new ArrayList<>();

    public void addOrder(Order order) {
        System.out.printf("adding order to the billing service: %s\n", order);
        orders.add(order);
    }
}

Order

This is our simple order entity.

package com.hascode.tutorial.entity;

public class Order {
    private final String bookId;
    private final int quantity;
    private boolean processed;

   // constructor, getter/setter, toString ommitted..
}

Business Rules

This is our simple business rule stored in a file named book-order.drl.

The rule adds a new incoming order to the billing service and alters its state.

A complete overview of the rule syntax can be found in the JBoss documentation here.

package com.hascode.rule

import com.hascode.tutorial.entity.Order;
import com.hascode.tutorial.control.BillingService;

rule "Book Order"
    when
        $o : Order(processed == false,  bookId: bookId, quantity : quantity )
        $b : BillingService()
    then
        System.out.println("new book order - title:"+ bookId+", quantity: "+quantity);
        $b.addOrder($o);
        update($b);
        $o.setProcessed(true);
        update($o);
end

Running the Example

This is a sample code that initializes our book service and adds an order:

package com.hascode.tutorial;

import com.hascode.tutorial.boundary.BookService;

public class Main {

    public static void main(String[] args) {
        BookService srv = new BookService();
        srv.order("aaa", 30);
    }

}

We’re now ready to run our example in our IDE of choice or using Gradle in the command-line:

$ gradle runExample
[..]
new book order - title:aaa, quantity: 30
adding order to the billing service: Order [bookId=aaa, quantity=30, processed=false]

BUILD SUCCESSFUL

Total time: 4.468 secs
example drools app in eclipse ide 1024x792
Figure 2. Drools Example running in Eclipse IDE

Tutorial Sources

Please feel free to download the tutorial sources from my GitHub repository, fork it there or clone it using Git:

git clone https://github.com/hascode/drools-tutorial.git

Alternative: Activiti

Another interesting choice here is Activity, please feel free to have a look at my tutorial: "Business Process Modeling with Activiti and BPMN 2.0".