🏠JavaDrools Rule Engine

Drools Rule Engine

Overview

Drools Rule Engine is a Business Rules Management System (BRMS) solution. Some people also consider drool as a Business Logic integration Platform (BLiP). It provides the following features.

  1. Core Business Rules Engine (BRE),
  2. Drools Workbench (A rule authoring web application)
  3. Support for Decision Model and Notation (DMN) 
  4. Eclipse IDE plugin for core development.

Drools has two main concepts, And they are Authoring and Runtime.

  • Authoring – A process of creating and maintaining rules. Usually in the form of DRL files.
  • Runtime − An engine that matches rules against facts and data to provide results.

What is a Rule Engine?

rules engine is a software system that executes one or more business rules in runtime. These engines take a set of predefined rules and execute them against the data provided to the engine. In layman’s terms, A Rule Engine allows you to express “What to Do” and “When to do“. However, It doesn’t necessarily need to tell you “How to do” it.

Drools Rule Engine

Drools engine operates based on the following components. They are,

  1. Rules
  2. Facts
  3. Production memory
  4. Working memory
  5. Agenda
drools rule engine architecture

Rules

Rules, also known as Business Rules are the decision statements that you define. A rule must contain at least one condition and an action that rule would dictate.

Facts

Facts are the data that enters the drools engine or the results of matching rule conditions.

Production memory

This is the storage place where rules are kept in drools engine.

Working memory

This is the place where facts are stored in drools engine. Both working and production memory are logical concepts. They can be in-memory or stored in a file or even an application could supply them over HTTP.

Agenda

Agenda is the actual arrangement of rules in drools engine. This is where the activated rules are registered. When more than one rule is registered, there may be sorting applicable to provide an order of matching the rules. All of these are done in preparation for the execution of rules.

The following is an example of how a rules file would look like.

import com.springhow.examples.drools.LoanApplication

rule "Infer Adult"
when
  $p : LoanApplication(age >= 18)
then
  $p.setApproved(true);
end Code language: Java (java)

Now lets see how to use this .DRL file in a java application.

Adding Drools engine to a Java Project

The easiest way to implement drools is through standalone deployment or spring integration for the Drools engine. But you could use the same implementation on standard java implementations. First, you need to import the following dependencies to your project.

<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-core</artifactId>
    <version>${drools.version}</version>
</dependency>
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>${drools.version}</version>
</dependency>
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-decisiontables</artifactId>
    <version>${drools.version}</version>
</dependency>Code language: HTML, XML (xml)

At the time of writing the drools.version is 7.54.0.Final.

Important Classes in Drools engine

The drools java library provides certain components for us to use. Let’s go through a simple loan application example to explain these components. This rule engine uses a concept called Knowledge Is Everything(KIE) for its inference logic. So most of the classes we see here will have the word Kie.

KieServices

This class is a singleton acting as a hub for providing services of Kie. A typical usage of this calls would be like below.

KieServices kieServices = KieServices.Factory.get();Code language: Java (java)

With this kieServices object, you can get other services like KieFileSystem, KieBuilder etc.

KieFileSystem

The KieFileSystem is an interface and the drools rule engine java library provides a default in-memory implementation for this. This class is responsible for storing the details of the DRL files that we saw earlier. For example, the following snippet adds a DRL to the system.

kieFileSystem.write(ResourceFactory.newClassPathResource("loanApplication.drl"));
Code language: Java (java)

A bunch of resources in the filesystem helps build a KieModule.

KieModule and KieContainer

A KieModule is a container of all the resources necessary to define a set of KieBases(rules, functions, processes etc.). And the KieContainer is a container for all the KieBases of a given KieModule. So in our case, we can create a kie container as shown below.

KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
KieModule kieModule = kb.getKieModule();
loanApplicationKieContainer = kieServices.newKieContainer(kieModule.getReleaseId());Code language: Java (java)

With kie containers, we can create sessions where you can execute rules.

Executing Rules with Drools in Java

By putting all of them together, We have a simple Utils class with a bunch of static helper methods.

public class DroolsUtil {

    private static final KieServices kieServices = KieServices.Factory.get();

    private static KieContainer loanApplicationKieContainer;

    static {
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.newClassPathResource("loanApplication.drl"));
        KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
        kb.buildAll();
        KieModule kieModule = kb.getKieModule();
        loanApplicationKieContainer = kieServices.newKieContainer(kieModule.getReleaseId());
    }

    public static KieSession getNewKieSessionForLoanApplication() {
        return loanApplicationKieContainer.newKieSession();
    }

    public static LoanApplication validateLoanApplication(LoanApplication loanApplication) {
        KieSession newKieSessionForLoanApplication = getNewKieSessionForLoanApplication();
        newKieSessionForLoanApplication.insert(loanApplication);
        newKieSessionForLoanApplication.fireAllRules();
        newKieSessionForLoanApplication.dispose();
        return loanApplication;
    }

}Code language: Java (java)

With this approach, We just need to call the validateLoanApplication() method with a loan application object.

public static void main(String[] args) {

    LoanApplication loanApplication = new LoanApplication("John Doe", 19, 10000);
    log.info("Application status : {}", loanApplication.isApproved());
    DroolsUtil.validateLoanApplication(loanApplication);
    log.info("Application Approved? : {}", loanApplication.isApproved());
}Code language: Java (java)

This example will result in the loan application being approved. That is because, the age is 19 which matches our rule criteria.

The complete example is available at the GitHub repository.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

One Comment