Drools - Introduction

Any Java enterprise level application can be split into three parts:

  • UI – User Interface (Frontend)
  • Service layer which is in turn connected to a database
  • Business layer

We have a number of frameworks that handle the UI and service layer together, for example, Spring and Struts. Yet, we did not have a standard way to handle the business logic until Drools came into existence.

What is Drools?

Drools is a Business Logic integration Platform (BLiP). It is written in Java. It is an open source project that is backed by JBoss and Red Hat, Inc. It extends and implements the Rete Pattern matching algorithm.

In layman’s terms, Drools is a collection of tools that allow us to separate and reason over logic and data found within business processes. The two important keywords we need to notice are Logic and Data.

Drools is split into two main parts: Authoring and Runtime.

  • Authoring: Authoring process involves the creation of Rules files (.DRL files).
  • Runtime: It involves the creation of working memory and handling the activation.

What is a Rule Engine?

Drools is Rule Engine or a Production Rule System that uses the rule-based approach to implement and Expert System. Expert Systems are knowledge-based systems that use knowledge representation to process acquired knowledge into a knowledge base that can be used for reasoning.

A Production Rule System is Turing complete with a focus on knowledge representation to express propositional and first-order logic in a concise, non-ambiguous and declarative manner.

The brain of a Production Rules System is an Inference Engine that can scale to a large number of rules and facts. The Inference Engine matches facts and data against Production Rules – also called Productions or just Rules – to infer conclusions which result in actions.

A Production Rule is a two-part structure that uses first-order logic for reasoning over knowledge representation. A business rule engine is a software system that executes one or more business rules in a runtime production environment.

A Rule Engine allows you to define “What to Do” and not “How to do it.”

What is a Rule?

Rules are pieces of knowledge often expressed as, "When some conditions occur, then do some tasks."

    <Condition is true>
    <Take desired Action>

The most important part of a Rule is its when part. If the when part is satisfied, the then part is triggered.

rule  <rule_name>
      <attribute> <value>

Pattern Matching

The process of matching the new or existing facts against Production Rules is called Pattern Matching, which is performed by the Inference Engine. There are a number of algorithms used for Pattern Matching including:

  • Linear
  • Rete
  • Treat
  • Leaps

Drools Implements and extends the Rete Algorithm. The Drools Rete implementation is called ReteOO, signifying that Drools has an enhanced and optimized implementation of the Rete algorithm for object-oriented systems.

Advantages of a Rule Engine

Declarative Programming

Rules make it easy to express solutions to difficult problems and get the solutions verified as well. Unlike codes, Rules are written in less complex language; Business Analysts can easily read and verify a set of rules.

Logic and Data Separation

The data resides in the Domain Objects and the business logic resides in the Rules. Depending upon the kind of project, this kind of separation can be very advantageous.

Speed and Scalability

The Rete OO algorithm on which Drools is written is already a proven algorithm. With the help of Drools, your application becomes very scalable. If there are frequent change requests, one can add new rules without having to modify the existing rules.

Centralization of Knowledge

By using Rules, you create a repository of knowledge (a knowledge base) which is executable. It is a single point of truth for business policy. Ideally, Rules are so readable that they can also serve as documentation.

Tool Integration

Tools such as Eclipse provide ways to edit and manage rules and get immediate feedback, validation, and content assistance. Auditing and debugging tools are also available.


Drools - Eclipse Plugin

Here are the prerequisites to install Drools Plugin:

  • Java 1.5 (or higher) SE JDK
  • Eclipse 4.2 (or any version) and the Drools plugin

As Drools is a BRMS (Business Rule Management System) written in Java, we will be covering how to add the desired plugins in this section. Considering maximum Java users use Eclipse, let’s see how to add the Drools 5.x.0 plugin in Eclipse.

Drools - Create a Drools Program

To create a basic Drools program, open Eclipse. Go to Fileb → New → Project.


Select Drools Project. Give a suitable name for the project. For example, DroolsTest.

The next screen prompts you to select some files which you want in your first Drools project. .. .. Next, we will discuss the terms frequently used in a Rule Engine.

Drools - Frequently Used Terms

Rules The heart of the Rules Engine where you specify conditions (if ‘a’ then ‘b’).

Facts Facts are the data on which the rules will act upon. From Java perspective, Facts are the POJO (Plain Old Java Object).

Session A Knowledge Session in Drools is the core component to fire the rules. It is the knowledge session that holds all the rules and other resources. A Knowledge Session is created from the KnowledgeBase.

For the rules engine to work, Facts are inserted into the session and when a condition is met, the subsequent rule gets fired. A Session is of two types:

  • Stateless Knowledge Session
  • Stateful Knowledge Session

Agenda It’s a logical concept. The agenda is the logical place where activations are waiting to be fired.

Activations Activations are the then part of the rule. Activations are placed in the agenda where the appropriate rule is fired.

Drools - Rules Writing

If you see the default rule that is written in the Hello World project (Sample.drl), there are a lot of keywords used which we will be explaining now.



  • Package: Every Rule starts with a package name. The package acts as a namespace for Rules. Rule names within a package must be unique. Packages in Rules are similar to packages in Java.
  • Import statement: Whatever facts you want to apply the rule on, those facts needs to be imported. For example, com.sample.DroolsTest.Message; in the above example.
  • Rule Definition: It consists of the Rule Name, the condition, and the Consequence. Drools keywords are rule, when, then, and end. In the above example, the rule names are “Hello World” and “GoodBye”. The when part is the condition in both the rules and the then part is the consequence. In rule terminology, the when part is also called as LHS (left hand side) and the then part as the RHS (right hand side) of the rule.

Now let us walk through the terms used in the Java file used to load the Drools and execute the rules.

Knowledge Base

Knowledge Base is an interface that manages a collection of rules, processes, and internal types. It is contained inside the package org.drools.KnowledgeBase. In Drools, these are commonly referred to as knowledge definitions or knowledge. Knowledge definitions are grouped into knowledge packages. Knowledge definitions can be added or removed. The main purpose of Knowledge Base is to store and reuse them because their creation is expensive. Knowledge Base provides methods for creating knowledge sessions.

Knowledge Session

The knowledge session is retrieved from the knowledge base. It is the main interface for interacting with the Drools Engine. The knowledge session can be of two types:

  • Stateless Knowledge Session
  • Stateful Knowledge Session

Stateless Knowledge Session

Stateless Knowledge Session is a stateless session that forms the simplest use case, not utilizing inference. A stateless session can be called like a function, passing it some data and then receiving some results back. Common examples of a stateless session include:

  • Validation
    • Is this person eligible for a mortgage?
  • Calculation
    • Compute a mortgage premium.
  • Routing and Filtering
    • Filter incoming messages, such as emails, into folders.
    • Send incoming messages to a destination

Stateful Knowledge Session

Stateful sessions are longer lived and allow iterative changes over time. Some common use cases for stateful sessions include:

  • Monitoring
    • Stock market monitoring and analysis for semi-automatic buying.
  • Diagnostics
    • Fault finding, medical diagnostics
  • Logistics
    • Parcel tracking and delivery provisioning

Knowledge Builder

The KnoledgeBuilder interface is responsible for building a KnowledgePackage from knowledge definitions (rules, processes, types). It is contained inside the package org.drools.builder.KnowledgeBuilder. The knowledge definitions can be in various formats. If there are any problems with building, the KnowledgeBuilder will report errors through these two methods: hasErrors and getError.

The following diagram explains the process


In the above example, as we are taking a simple example of stateless knowledge session, we have inserted the fact in the session, and then fireAllRules() method is called and you see the output.

In case of a stateful knowledge session, once the rules are fired, the stateful knowledge session object must call the method dispose() to release the session and avoid memory leaks.

Drools - Rule Syntax

As you saw the .drl (rule file) has its own syntax, let us cover some part of the Rule syntax in this chapter.

.. ..

Drools Quick Start : https://www.tutorialspoint.com/drools/drools_quick_guide.htm Drools comprahensive document: https://docs.jboss.org/drools/release/5.4.0.Final/droolsjbpm-introduction-docs/html_single/

DROOLS link : http://www.drools.org/

DROOLS - eClipse tutorial : https://www.youtube.com/watch?v=POVe4QY_9p8