Learn Drools: Part I – DZone Java


Drools Introduction

When we implement a complex software, often we require maintaining a set of rules which will be applied to a set of data for action to be taken on them. In a regular term, we call them rule engine. If there is a small set of rules, we can create our own rule engine that will maintain a certain order and be applied on incoming data to take the decision or categorize the data.

The advantage of maintaining one’s own rule engine is that we have a pure control over the rule engine’s algorithm. This means that we can easily change the algorithm logic to be simple; we don’t have to rely on the third party for the pattern matching logic.

On the other hand, if the rules are ever-changing or there is a huge set of rules, we will probably not want to maintain our own rule engine as it increases our development cost. Who wants to take this responsibility?

It would be nice if we could delegate that work to some third party that is tested and trusted so we could clearly separate the data and logic.

The third party maintains the rule engine application and we just define the rules as a strategy. It would be nice if it were declarative so that business analysts could understand the logic.

What Is Drools?

Drools is a Business Logic integration Platform (BLiP). Drools is an open-source project written in Java. Red Hat and JBoss maintain Drools.

Drools has two main parts:

1. Authoring 

By authoring, we create a rules file for Drools (.drl). This file contains the rule definition in a declarative way. In this file, we can write a set of rules that will be fired at the run time. It is the developer’s responsibility to write these rules as per business requirements.

2. Runtime

With the runtime, we create a working memory. It is the same as a session in Hibernate. As a rules file contains a set of rules, the runtime creates memory load. These rules and apply to the incoming data. In Drools, we called incoming data as facts.

Rule: A rule is nothing but the logic that will be applied to incoming data. It has two main parts: when and then.
When: Determines the condition on which the Rule will be fired.
Then: The action; if the rules met the condition, they defines what work this rule performs.

This is the syntax:

Rule <Rule Name>

when

<condition>

then

<Action>

End

In this example, we will greet a person based on current time. We will define the rules in Drools files. Drool will load these rules and fire on the incoming data.

Step 1: Create a.drl (droolRule.drl)file where we will define the rules.

package com.rules

import com.example.droolsExample.Person

rule "Good Morning"
when
person: Person(time >= 0, time < 12)
then
person.setGreet("Good Morning " + person.getName());
end
rule "Good Afternoon"
when
person: Person(time >= 12, time < 16)
then
person.setGreet("Good Afternoon " + person.getName());
end
rule "Good Night"
when
person: Person(time >= 16, time <= 24)
then
person.setGreet("Good Night " + person.getName());
end

Please note that here we create three rules: “Good Morning”, “Good Afternoon “ and “Good Night.” In the When section, we check the current time based on the Person POJO’s time property. In the Then section, we set the greeting messages accordingly.

Step 2: Create Person POJO class.

package com.example.droolsExample;

public class Person {

    private String name;
    private int time;
    private String greet;

    public String getGreet() {
    return greet;
    }
    public void setGreet(String greet) {
    this.greet = greet;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getTime() {
    return time;
    }
    public void setTime(int time) {
    this.time = time;
    }



} 

Step 3: We create a class named DroolsTest.java.

3a. Load the rule file (i.e., droolsTest.drl) by using InputStream.

3b. Create a package using the above rule and add them into drools PackageBuilder.

3c. Create a RuleBase by using the above Package. Rulebase is the same as Sessionfactory in Hibernate; it is costly.

3d. Create a working memory from this RuleBase. It is same as Session class in Hibernate. This working memory manages the rules and incoming data. Apply the rules on the data.

3e. Add incoming data into working memory. Here, we create a Person Object and add it into Working Memory

3f. Fire all rules.

DroolsTest.java should look like the following:

package com.example.droolsExample;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.compiler.PackageBuilder;
import org.drools.core.RuleBase;
import org.drools.core.RuleBaseFactory;
import org.drools.core.WorkingMemory;

public class DroolsTest {

public static void main(String[] args) throws DroolsParserException,
IOException {
DroolsTest droolsTest = new DroolsTest();
droolsTest.executeDrools();
}

public void executeDrools() throws DroolsParserException, IOException {

PackageBuilder packageBuilder = new PackageBuilder();

String ruleFile = "/com/rules/droolsRule.drl";
InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);

Reader reader = new InputStreamReader(resourceAsStream);
packageBuilder.addPackageFromDrl(reader);
org.drools.core.rule.Package rulesPackage = packageBuilder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(rulesPackage);

WorkingMemory workingMemory = ruleBase.newStatefulSession();

Person person = new Person();
person.setName("Shamik Mitra");
person.setTime(7);

workingMemory.insert(person);
workingMemory.fireAllRules();

System.out.println(person.getGreet());
}

Output:

Good Morning Shamik Mitra.

We set the time for 7 a.m., so it satisfies the Good Morning Rule condition and fires this rule.



Source link

?
WP Twitter Auto Publish Powered By : XYZScripts.com