Skip to main content
Rules-based Programming

Rules-Based Programming – Part 2

Introduction

Drools is a Business Rules Engine. It has an enhanced and optimized implementation of
the Rete algorithm for Object Oriented systems. It is a declarative, rule-based programming
environment and a  forward chaining engine i.e. “data-driven” and thus starts with a fact,
propagates and ends in a conclusion.

ReteOO

Rete algorithm was invented by Dr. Charles Forgy.  It supports only Boolean, first order
logic. ReteOO an enhanced and optimized implementation of the Rete algorithm for object
oriented systems can be broken into 2 parts:

I. Rule compilation and
II. Runtime execution.

Rule-base is compiled into discrimination network. Discrimination network is used to filter data as
it propagates through the network.

Rete algorithm ReteOO

Drools:

Drools is a Business Rules Management System (BRMS) solution. It provides a core Business Rules Engine (BRE), a web authoring and rules management application (Drools Workbench) and an Eclipse IDE plugin for core development.

To make our applications rules-driven using Drools Expert, an understanding of the following important concepts is necessary:

  1. Knowledge Base – a repository of rule / knowledge
  2. Knowledge Session – an established interaction between the application and the rules engine. A session is created from a KnowledgeBase and can be either Stateless or Stateful. The application uses the established session to run the engine against the inserted facts.

Drools execution is split into two main parts:

Authoring: Authoring process involves the creation of Rules files (.DRL files).

Runtime: It involves the creation of working memory and handling the activation.

Sample Rules: DRL File

Drools

The above DRL file has two rules:

Hello World Rule – “When the Message object’s status is HELLO, display the message attribute of the Message object and change its message and status to “Goodbye Cruel World” and GOODBYE respectively”.

Goodbye Rule – “When the Message object’s status is GOODBYE, display the message attribute of the Message object.”

Executing Rules using DRL file:

DRL file

The code above contains two helper methods:

createKnowledgeBuilder(), and

createKnowledgeBase().

The createKnowledgeBuilder() method creates an instance of KnowledgeBuilder which is responsible for taking source files such as .drl (Drools Rule) files and turning them into a KnowledgePackage of rule and process definitions which a KnowledgeBase can consume.

In this method we addto the KnowledgeBuilder the rule file “hello-world-rule.drl” which is a ClassPath resource. Rules can also obtained as a File resource or a URL resource.

The createKnowledgeBase() method creates an instance of KnowledgeBase which is a repository of all the application’s knowledge definitions. This method uses the createKnowledgeBuilder() and gets all knowledge packages from the created KnowledgeBuilder to be used by the created KnowledgeBase.

In the main method after creating our KnowledgeBase, we then created a StatelessSession and then asked the created session to execute all rules against the Message object with a status Hello.

Conclusion:

By using a business rule engine and allowing business rules to drive our applications, we can increase our development agility, improve software maintainability, and allow us to deal much easier with evolving requirement complexity. However, by introducing business rule engine to your applications, you also add another layer of complexity to your application architecture. Using a business rule engine should be considered when we need to create pluggable systems, expert systems or when the need to handle ever-changing requirements is very high. For application requirements of moderate complexity, the standard approach of embedding logic through imperative programming language may suffice.

 

Ved Shankar

Ved Shankar

Ved Shankar works as Software Engineer with Trigent Software. With over three years experience, he has strong expertise in Java/J2EE, Spring, JPA, Hibernate, JMS, Pentaho, Drools, JavaScript, Oracle & MySQL Database.

6 thoughts on “Rules-Based Programming – Part 2

Comments are closed.