What is a rule engine ?

A rule engine is a piece of software that executes rules according to some algorithm. A rule engine combines a set of facts that are inserted in to the system with its own Rule Set to reach a conclusion of triggering one or several actions. These rules typically describe in a declarative manner the business logic which needs to be implemented in our environment (which we assume rarely changes). The facts, on the other had, describe the conditions of the system that is to be operated on; they may change frequently.

Defining Rules

Logic, or rules in our case, are pieces of knowledge often expressed as, “When some conditions are evaluated to true, then do some tasks“.

A system with a large number of rules and facts may result in many rules being true for the same facts; in this case, we say that rules are said to be in conflict. A rule engine can use different conflict resolver strategies to determine the order of execution of the conflict rules.  In a rule engine, there are two execution methods:

  • Forward Chaining: is a “data-driven” method. Upon facts being inserted or updated, the rule engine uses available facts and inference rules to extract more facts until a goal is reached, where one or more matching rules will be concurrently true and scheduled for execution. Hence the rule engine starts with facts and ends with conclusion.
  • Backward chaining is a “goal-driven” or inference method, which is reversed with forward chaining. Backward chaining starts with a conclusion or a list of goals that the engine tries to satisfy. If it cannot satisfy these goals, then it searches for sub-goals that it can satisfy that will help satisfy some part of the current goals. The engine continues this process until either the initial conclusion is proven or there are no more sub-goa

What is the difference between a Rule engine and a process?

A rule differs from a process mainly because:

A business processes represent what the business does
A business rules represent decisions that the business does

A rule engine may be viewed as a sophisticated if/then statement interpreter. The if/then statements that are interpreted are called rules.

The if portions of rules contain conditions such as account.getMoney() < 0. The then portions of rules contain actions such as sendWarning(account).

if (account.getMoney() < 0)
sendWarning(account);

The inputs to a rule engine are a rule execution set and some data objects. The outputs from a rule engine are determined by the inputs and may include the original input data objects with possible modifications, new data objects, and side effects such as sendMail(‘Thank you for shopping’).

what is a rule engine
Rules are stored in a forward-chaining rule engine, i.e. the engine implements an execution cycle that allows the action of one rule to cause the condition of other rules to become met. In this way, a cascade of rules may become activated and each rule action executed. Forward-chaining rule engines are suitable for problems that require drawing higher-level conclusions from simple input facts.

So, here’s how you could rewrite the above check as a rule:

global AccountManager manager;

rule "checkMoney"
when
$account : Account( money < 0 )
then
  manager.warn($account);
end

As you can see, a Rule file is based on two basic concepts:
Rules: declarative statements that govern the conduct of business processes. A rule consists of a condition and actions. The condition is evaluated, and if it evaluates to true, the rule engine initiates one or more actions.

Facts: are the data upon which rules operate. In our example, the available money are facts.

Advantages of Rule Engines

Turning to rules adds a set of advantages to your applications:

Greater flexibility: keeping your rules into a Knowledge base let you adapt easily your decisions when they are changing.

Easier to grasp: Rules are easier to understand than procedural code so they can be effectively used to bridge the gap between business analyst and developers.

Reduced complexity: When embedding lots of decision points to your procedural codes it can easily turn your application into a nightmare. On the other hand rules can handle much better increasing complexity because they use a consistent representation of business rules.

Reusability: By keeping rules are kept in one place leads to a greater reusability of your business rules. Also, traditional procedural code often impose unnecessary variations of base rules which are therefore more difficult to reuse in other contexts.

On the other hand, using a rule engine might prove to be unnecessary if your business logic is static and the number of business rules are pretty simple and limited in number.

Open source rule engines

The Drools engine (https://drools.org/) is the rules engine in Drools project. The Drools engine stores, processes, and evaluates data to execute the business rules or decision models that you define. The basic function of the Drools engine is to match incoming data, or facts, to the conditions of rules and determine whether and how to execute the rules.

The Drools engine operates using the following basic components:

  • Rules: Business rules or DMN decisions that you define. All rules must contain at a minimum the conditions that trigger the rule and the actions that the rule dictates.
  • Facts: Data that enters or changes in the Drools engine that the Drools engine matches to rule conditions to execute applicable rules.
  • Production memory: Location where rules are stored in the Drools engine.
  • Working memory: Location where facts are stored in the Drools engine.
  • Agenda: Location where activated rules are registered and sorted (if applicable) in preparation for execution.

When a business user or an automated system adds or updates rule-related information in Drools, that information is inserted into the working memory of the Drools engine in the form of one or more facts. The Drools engine matches those facts to the conditions of the rules that are stored in the production memory to determine eligible rule executions. When rule conditions are met, the Drools engine activates and registers rules in the agenda, where the Drools engine then sorts prioritized or conflicting rules in preparation for execution.

The following picture, summarizes the concept:

what is a rule engine what is a rule engine

The Drools engine in turn is part of the KIE (Knowledge Is Everything) umbrella project which includes, besides Drools also:

  • jBPM is a flexible Business Process Management suite allowing you to model your business goals by describing the steps that need to be executed to achieve those goals.
  • OptaPlanner is a constraint solver that optimizes use cases such as employee rostering, vehicle routing, task assignment and cloud optimization.
  • Business Central is a full featured web application for the visual composition of custom business rules and processes.
  • UberFire is a web-based workbench framework inspired by Eclipse Rich Client Platform.

As an example of Drool Rule, consider the following Java Bean model and sample DRL rule:

public class Applicant {
  private String name;
  private int age;
  private boolean valid;
  // Getter and setter methods
}

And here is a sample DRL rule for to check the Applicant model:

package com.company.license

rule "Is of valid age"
when
  $a : Applicant(age < 18)
then
  $a.setValid(false);
end

The “Is of valid age” rule disqualifies any applicant younger than 18 years old.

//Create the KIE container
KieServices kieServices = KieServices.Factory.get();

KieContainer kContainer = kieServices.getKieClasspathContainer();

//Instantiate the stateless KIE session and enter data
StatelessKieSession kSession = kContainer.newStatelessKieSession();

Applicant applicant = new Applicant("Mr John Smith", 16);

assertTrue(applicant.isValid());

ksession.execute(applicant);

assertFalse(applicant.isValid());

OpenL Tablets

OpenL Tablets (http://openl-tablets.org/) is a business rules management system and business rules engine based on tables presented in Excel documents. Using unique concepts, OpenL Tablets facilitates treating business documents containing business logic specifications as executable source code. Since the format of tables used by OpenL Tablets is familiar to business users, OpenL Tablets bridges a gap between business users and developers, thus reducing costly enterprise software development errors and dramatically shortening the software development cycle. In a very simplified overview, OpenL Tablets can be considered as a table processor that extracts tables from Excel documents and makes them accessible from the application.

The major advantages of using OpenL Tablets are as follows:

  • OpenL Tablets removes the gap between software implementation and business documents, rules, and policies.
  • Business rules become transparent to developers.
  • OpenL Tablets verifies syntax and type errors in all project document data, providing convenient anddetailed error reporting. OpenL Tablets can directly point to a problem in an Excel document.
  • OpenL Tablets provides calculation explanation capabilities, enabling expansion of any calculation result by pointing to source arguments in the original documents.
  • OpenL Tablets enables users to create and maintain tests to insure reliable work of all rules.
  • OpenL Tablets provides cross-indexing and search capabilities within all project documents.
  • OpenL Tablets provides full rules lifecycle support through its business rules management applications.
  • OpenL Tablets supports the .xls and .xlsx file formats.

For access to rules and data in Excel tables, OpenL Tablets API is used. OpenL Tablets provides a wrapper to facilitate easier usage.

For example consider the following rule coded in an Excel document:

drools

There is only one rule hello1:

public interface Simple {
void hello1(int i);
}

Here is the wrapper which runs the rule::

import static java.lang.System.out;
import org.openl.rules.runtime.RulesEngineFactory;
public class Example {
    public static void main(String[] args) {
        //define the interface
        RulesEngineFactory < Simple > rulesFactory =
            new RulesEngineFactory < Simple > ("TemplateRules.xls",
                Simple.class);
        Simple rules = (Simple) rulesFactory.newInstance();
        rules.hello1(12);
    }
}

In order to use OpenL Tablets, the following dependency needs to be included:

<dependency>
    <groupId>com.deliveredtechnologies</groupId>
    <artifactId>rulebook-core</artifactId>
    <version>0.11</version>
</dependency>

Easy Rules

Easy Rules (https://github.com/j-easy/easy-rules) is a simple yet powerful Java rules engine providing the following features:

  • Lightweight framework and easy to learn API
  • POJO based development
  • Useful abstractions to define business rules and apply them easily
  • The ability to create composite rules from primitive ones
  • The ability to define rules using an Expression Language (Like MVEL and SpEL)

In a nutshell, Easy Rules provides the Rule abstraction to create rules with conditions and actions, and the RulesEngine API that runs through a set of rules to evaluate conditions and execute actions.

To use Easy Rules, you have to add the following dependency to your pom.xml :

<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-rules-core</artifactId>
    <version>3.4.0</version>
</dependency>

We will create a rule that is always triggered and that will print “hello world” to the console when executed. Here is the rule:

@Rule(name = "Hello World rule", description = "Always say hello world")
public class HelloWorldRule {

    @Condition
    public boolean when() {
        return true;
    }

    @Action
    public void then() throws Exception {
        System.out.println("hello world");
    }

}

Now let’s create a rules engine and fire this rule:

public class Launcher {

    public static void main(String[] args) {

        // create facts
        Facts facts = new Facts();

        // create rules
        Rules rules = new Rules();
        rules.register(new HelloWorldRule());

        // create a rules engine and fire rules on known facts
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);

    }
}

RuleBook Rules Engine

Tired of classes filled with if/then/else statements? Need a nice abstraction that allows rules to be easily specified in a way that decouples them from each other? Want to write rules the same way that you write the rest of your code? RuleBook just might be the rules abstraction you’ve been waiting for!

Rule book is a rule engine built to create rules in a way familiar to Java developers. RuleBook also allows you to specify rules using an easy to use Lambda enabled Domain Specific Language or using POJOs that you define!

To build projects with RuleBook, add the code below to your pom.xml

<dependency>
    <groupId>com.deliveredtechnologies</groupId>
    <artifactId>rulebook-core</artifactId>
    <version>0.11</version>
</dependency>

Here is an example:

public class ExampleRule {
    public RuleBook<Object> defineHelloWorldRules() {
        return RuleBookBuilder
          .create()
            .addRule(rule -> rule.withNoSpecifiedFactType()
              .then(f -> System.out.print("Hello ")))
            .addRule(rule -> rule.withNoSpecifiedFactType()
              .then(f -> System.out.println("World")))
            .build();
    }
}

You can run the ExampleRule as follows:

public static void main(String[] args) {
    ExampleRule ruleBook = new ExampleRule();
    ruleBook
      .defineHelloWorldRules()
      .run(new FactMap<>());
}
Found the article helpful? if so please follow us on Socials