Drools Tutorial for Beginners

What is Drools Rule Engine?

Drools is a Business Rule Management System BRMS or a production rule system PRS that use an enhanced implementation of the Rete pattern matching Algorithm, designed with a rule engine, and maintained by JBoss and Red Hat, Inc. Drools is an open-source project written in Java Programming and supports Java Rules Engine API Java Specification Request 94 standard for its business rules engine and framework to construct, maintain, and enforce business policies in an organization, application or service.
Logic and Data are the two crucial existed keywords in Drools. Drools exist as a standard way for Java enterprise level application to handle that's business logic- a part of the program that
  • encodes the real-world business rules (determine how data can be created, stored, and modified).
  • prescribes how business objects interact with each other.
Drools split into two main parts: Authoring: This process involves the creation of Rules files (DRL/XML files). Runtime: This process creates working memory and handles activation. Drools rule engine is a forward and backward chaining inference based rule engine that processes facts and throws output as a result of rules and facts processing. Rule Engine is sophisticated if/then statement interpreter and the statements it interprets are known as rules. It enables you to define  "What to do"and not "How to do it.": It provides declarative programming, logic and data separation, speed and scalability, centralization of knowledge, tool integration, explanation facility, clear rules.

Some basic concepts of Drools:

Rules are the declarative statements that govern the conduct of business processes. A rule defines a condition and actions such as "When some conditions occur, then do some tasks."
when
<condition is true>
then
<take desired Action>
When is the most important part of a rule body such that if it gets satisfied, then the then part is triggered.
rule  <rule_name>
<attribute><value>
when
<conditions>
then
<actions>
end
Facts: are theJava objects (beans)that you assert in the working memory and upon which rules operate. These are the POJO classes (Plain Old Java Object) from the Java perspective. Working memory: It is the main class in Drools that use rule engine at runtime. It is the storage with Facts and holds references to all the data "asserted" into it (until retracted). Knowledge Session: Created from Knowledge Base is the core component in Drools to fire the rules. It holds all resources for firing the rules such that for the working of the rule engine, all facts are inserted into session and when the condition met, the matching rules get fired. A session is of two types:
  1. Stateless Knowledge Session
  2. Stateful Knowledge Session
Knowledge Base: represents knowledge giving information about resources where Rules are found. An agenda: is a logical place where activations are in wait to be fired. Activations: represent the then part of a rule and placed in the agenda where the appropriate rule is fired.

Pattern Matching

Pattern Matching is the process of matching the new or existing facts against Production Rules with the help of an Inference Engine. Pattern matching can be done by implementing any of the algorithm given below:
  • Linear
  • Rete
  • Treat
  • Leaps
Drools implements, extends the Rete Algorithm and signifies an enhanced and optimized Rete Algorithm implementation for object-oriented systems known as ReteOO implementation.

Why use Drools Rule Engine?

There are many benefits of using Drools Rule Engine as follows:
  • Declarative Programming
Rule Engines enable you to define "What to do" and not "How to do it." Unlike codes, Rules are written in less complex language that makes it easier for any business analyst or a new developer to understand and verify a set of rules. Using rules, they can easily express solutions to severe problems and get the solutions verified as well.
  • Logic and Data Separation
Your data and the business logic reside in your domain objects and rules respectively, this kind of separation break their OO coupling which can be very beneficial depending upon the type of project.
  • Speed and Scalability
Drools is written on a battle proved Rete OO algorithm which offers efficient ways of matching rule patterns to your domain object data. Drools make your application very scalable. If any frequent changes request occur, you can easily add new rules without modifying the existing rules.
  • Centralization of Knowledge
Rules engine provides a Centralization of Knowledge such that using rules, you can create an executable repository of knowledge known as the knowledge base. This is the only point of truth for business policy (for illustration). Ideally, Rules are so bright that they can also serve as documentation.
  • Tool Integration
Rules Engine offers tools like Eclipse using which you will have different ways to edit and manage rules with instant feedback, validation, and content assistance. It also provides auditing and debugging tools.