The deal with Drools

by | July 26, 2023 | Technology

TLDR. Drools is an open-source Business Rule Management System (BRMS) solution written in Java that provides a rule engine which processes facts and produces output as a result of rules and facts processing. It is an open source project that is backed by JBoss and Red Hat and it extends and implements the Rete Pattern matching algorithm.

A rule engine can be viewed as a sophisticated interpreter for if / then statements, where the statements themselves are known as rules.

Drools is relevant because it is a collection of tools that facilitates the separation between the application logic and business logic. By using it, the business rules can be extracted from the application. This will make your application very scalable and also help people that don’t have knowledge of the programming language used to develop the application, to create business rules in a humanly understandable way.

 

Context

As a short description of how Drools work we can say that Drools is a tool that applies a set of rules on a specific dataset.

It 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.

Components

Once we know what Drools is and how it works is time to learn about some essential terms used in Drools:

  • Facts – Fact represent the data which serves as input for rules
  • Working Memory – Storage with Facts, where they are used for pattern matching. It can be modified, insert, and remove.
  • Knowledge Session – This component holds all the resources required for firing rule. Here, all facts are inserted into the single session, and then matching rules are fired.
  • Knowledge Base – It represents the knowledge in the Drools ecosystem. It stores the formation of the resources where rules are found.
  • Module – This is a module which stores multiple Knowledge Bases which can hold different sessions

Concepts

Rules

Rules are parts of knowledge often expressed as “When specific conditions occur, then do some tasks.”

The most crucial part of a rule is its when part. Once when part is satisfied, then the part is triggered.

In this example, we’ve used a few Drools provided keywords. Let’s understand their use:

  • package – the package in which the rule file is located
  • import – this is similar to Java import statement, here we need to specify the classes which we are inserting in the KnowledgeSession
  • global – this is used to define a global level variable for a session; this can be used to pass input parameter or to get an output parameter to summarize the information for a session
  • dialect – a dialect specifies the syntax employed in the expressions in the condition section or action section. By default the dialect is Java. Drools also supports dialect mvel; it is an expression language for Java-based applications. It supports the field and method/getter access
  • rule – this defines a rule block with a rule name
  • when – this specifies a rule condition, in this example the conditions which are checked are Applicant having experienceInYears more than ten years and currentSalary in a certain range
  • then – this block executes the action when the conditions in the when block met. In this example, the Applicant role is set as Manager

 

Patent matching method

Pattern matching method helps you to compare new or old facts with the production rules.

Drools support multiple algorithms for pattern matching like:

Rete Algorithm

Leaps Algorithm

Linear Algorithm

Treat Algorithm

However, the algorithm mostly used by Drools is the Rete Algorithm.

 

Forward Chaining

A forward-chaining engine checks the facts and derives a specific conclusion.

Let’s consider a scenario of the medical diagnosis system. If the patient’s symptoms are put as facts into working memory, then it is easy to diagnose him with an ailment.

Backward Chaining

A backward chaining engine has the set goal, and the engine tries to satisfy it.

Consider the same scenario of medical diagnosis. Assume that an epidemic of a certain disease. This AI could presume a given individual had the disease and attempt to determine if its diagnosis is correct based on available information.

 

Architecture

Now that we learned so much about Drools it is time to see how it’s architecture is and how it is applying a set of predefined rules to some facts.

  1. The rules are loaded into Rule Base, which are available all the time.
  2. Facts are asserted into the Working Memory where they may then be modified or retracted.
  3. The process of matching the new or existing facts against production rules is called pattern matching, which is performed by the Rule engine.
  4. The agenda allows you to manage the execution order of the conflicting rules with the help of a conflict resolution strategy.

 

Pros / Cons

 

Pros:

  1. 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.
  2. 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.
  3. Understandable Rules
    By creating object models you can set yourself up to write rules that are very close to natural language. They lend themselves to logic that is understandable to, possibly nontechnical, domain experts as they are expressed in their language, with all the program plumbing, the technical know-how being hidden away in the usual code.

Cons:

  1. Small applications
    If you are developing a small application with a small amount of rules (~20) it might not be worth it to put effort into integrating rules on your application because the effort may be higher than the advantages that it brings to your application.
  2. Static rules
    If your application has static rules (rules that don’t change very often) integrating Drools might be an effort that is not worth it because your rules are not changing very often and don’t bring a real value to your application.
  3. Learning curve
    Drools has a learning curve because developers have to be aware of the syntax and execution of rules.

 

Conclusion

Drools is a useful tool that can bring a lot of value to medium/big size applications by facilitating the decoupling of the application logic from business logic. It is easy to scale and enable non-technical persons to define business rules in a close to natural language syntax but if it is used for small applications or applications that have static rules can add an unnecessary overhead to the development process without bringing a real value.

Did you find useful information in the article?

Subscribe to our newsletter to be up-to-date with our news!

We don’t like spam either

abac blog about software development projects

Subscribe To Our Newsletter

Our newsletter covers a lot of ground with regards to new and shiny tech stacks and how to use them for great efficiency.

You have Successfully Subscribed!