List of Rules Engine in Java

In this article, we will check some of the most popular rule engines in Java. Rule engine makes it easy to separate the business logic from the source code. We may view a rule engine as a sophisticated if/then statement interpreted. In Java most of the popular rule engines implement JSR94.

 

Introduction

Imagine a rule engine as a system which takes data and rules as input. It will apply those rules on the data and will give us an output based on the rule definition. Let’s take an example of an online shopping where we like to give customer certain promotions or discounts.

  1. Give 20% off to the customer if shopping cart total is greater than $400.
  2. Give 10% for the first order.

Above define shopping cart and customer as the data where rules will be executed if they meet the conditions defined in the rule set.

 

Rules Engines in Java

There are several advantages of using rule engines:

  1. It provides a great flexibility where we can change the rules making no major changes in the source code.
  2. It reduced the complexity as we no longer need to build the rule engine logic in the source.
  3. By keeping the rule engine separate, it provides a greater reusability.

Let’s look at some popular rule engines in Java.

 

1. 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), full runtime support for Decision Model and Notation (DMN) models at Conformance level 3 and an Eclipse IDE plugin for core development.

 

2. Easy Rules

EasyRule is a lightweight rule engine API. It provides the Rule abstraction to create rules with conditions and actions, and the RulesEngine API that runs through a set of rules to test conditions and execute actions. Here are some core feature of the EasyRule:

  1. A lightweight rule engine API.
  2. Annotation and POJO based.
  3. Supports composite rule constructions.
  4. Supports expression languages (Like MVEL and SpEL) to define rules.

Let’s have  an overview of the Easy Rules API:

 

2.1 Maven Setup

Add the following dependency to the pom.xml file to include Easy Rules core API:

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

Easy Rules provides the following options to create the rules

  1. Declaratively using annotations.
  2. Pragmatically using fluent API.
  3. Using expression language 
  4. Use rule descriptor.

Let’s see few examples:

@Rule(name = "cart total rule", description = "Give 10% off when shopping cart is greater than $200" )
public class CartTotalRule {

    @Condition
    public boolean cartTotal(@Fact("cart") Cart cart) {
        return cart.isGreaterThanTwoHundered;
    }
    
    @Action
    public void giveDiscount(@Fact("cart") Cart cart) {
       cart.setTotalDiscount(200);
    }
}

The last part of the rule engine is the execution of the rule based on the facts and rule data.

public class CartTotalRuleTest {
    public static void main(String[] args) {
        // define facts
        Facts facts = new Facts();
        facts.put("cart", get_customer_cart);

        // define rules
        Rule cartTotalRUle = CartTotalRule
        Rules rules = new Rules();
        rules.register(cartTotalRUle);

        // fire rules on known facts
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
    }
}

 

3. RuleBook

RuleBook provides a simple and intuitive DSL that is flexible. If you have a larger collection of Rules, we can build them as annotated POJOs and RuleBook can transform the whole package into a RuleBook instantly. It provides an easy-to-use Lambda enabled Domain Specific Language or using POJO.Let’s see a simple rule definition using the RuleBook:

 

3.1 Maven Depedencies

To add the RuleBook in your project, add the following dependencies in your pom.xml file:

<dependency>
    <groupId>com.deliveredtechnologies</groupId>
    <artifactId>rulebook-core</artifactId>
    <version>${version}</version>
</dependency>

Let’s see one example:

public class Cart{
    private double cartTotal;
    private String cartId;
    private Customer user;
    private List cartEntries;

    //getter and setter
}

public class ShoppingCartRule extends CoRRuleBook {

    @Override
    public void defineRules() {
     
        //give 10% off when cart total is greater than $200
      addRule(RuleBuilder.create().withFactType(Cart.class).withResultType(Double.class)
        .when(facts -> facts.getOne().getCartTotal() > 200)
        .then((facts, result) -> result.setValue(20))
        .stop()
        .build());
}

Let’s execute the rule:

public class CartPromotionRule {
    public static void main(String[] args) {
      RuleBook cartPromotion = RuleBookBuilder.create(ShoppingCartRule.class).withResultType(Double.class)
        .withDefaultResult(0.0)
        .build();
      NameValueReferableMap facts = new FactMap();
      facts.setValue("cart", new Cart(450.0, 123456, customer, entries));
      cartPromotion.run(facts);
    }
}

 

4. OpenL Tablets

OpenL Tablets business rules engine (BRE) and business rules management system (BRMS). It comprises the following major components:

  1. Business Rules Engines
  2. WebStudio
  3. Web services
  4. Rule repository (JCR based implementation)

To add the dependencies, add the following to your pom.xml:

<dependency>
    <groupId>org.openl</groupId>
    <artifactId>org.openl.core</artifactId>
    <version>${version}</version>
</dependency>
<dependency>
    <groupId>org.openl.rules</groupId>
    <artifactId>org.openl.rules</artifactId>
    <version>${version}</version>
</dependency>

This

public class Main {
    private CartPromotionRules instance;
 
    public static void main(String[] args) {
        Main rules = new Main();
        // setup user and case here
        rules.process(aCase);
    }
 
    public void process(Case aCase) {
        EngineFactory engineFactory = new RulesEngineFactory(
          getClass().getClassLoader()
            .getResource("rules.xls"), CartPromotionRules.class);
        instance = engineFactory.newEngineInstance();
        instance.executePromotion(aCase, new Response());
    }
}

There are few more rule engines, which are worth pointing

  1. Jess – A Clips-like Rule engine accessible from Java with the full power of Common Lisp
  2.  JLisa – A small, light and fast rule engine and scripting environment written entirely in Java; Gives access to all Java APIs.

 

Summary

In this post, we talked about popular rule engines in Java. These rule engines provide flexibility for the business logic abstraction. Of all these rule engines, Drools is the most advance and most active rule engine out there.

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of