lesson 3 : Some more drools languages

We concentrate on how the rule engine works in the first lesson. In the second lesson, we introduced how to express constraint between facts. In this lesson, we will concentrate on all the drools language possibilities to write constraints on facts for more complex cases. The reader has to create a test classes called TestLesson3 like for lesson2, a new package lesson3 in the src/test/rules and add in the kmodule.xml a new session declaration

     <kbase name="rules3" packages="lesson3">
        <ksession name="ksession-lesson3"/>
    </kbase>

While building the examples, you will see more rules fired than the shown examples. As drools is a declarative language, as soon as the constraint are satisfied, the rule can fire.

Some more classes

To be able to see some more advanced features, we are going to add 2 new classes in src/main/java droolscours package.

package droolscours;

public class Customer {
    private String name;
    private String surname;
    private String country;

    public Customer(String name, String surname, String country) {
        super();
        this.name = name;
        this.surname = surname;
        this.country = country;
    }

    public Customer() {
        super();
        // TODO Auto-generated constructor stub
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    @Override
    public String toString() {
        StringBuffer buff = new StringBuffer();
        buff.append("-----Customer-----)\n");
        buff.append("Name=" + this.name + "\n");
        buff.append("Surname Name=" + this.surname + "\n");
        buff.append("Country=" + this.country + "\n");
        buff.append("-----Customer end-)");
        return buff.toString();
    }

}

In Constraint

This allows to validate an attribute is a list of values

And the console should look as follows :

Nested Accessor

This allows to add a constraint to a attribute class without the need to add the linked object to the session.

As seen here, we do not add the customer instance to the drools session.

The rule has been fired.

And/or

It is possible to do constraints on attribute like in java.

not

This allows to test if no fact of a type is in the session.

exist

On the contrary of previous syntax, this allows to test if there at least one fact type is in the session.

ForAll

We would like to verify that every cashflow instance is linked to an Account instance.

In this rule, in the forall condition, we link the CashFLow instance to the Account instance. We are going to do a test case where all objects are related

When running the test, you should see the following logging.

Just modify the test case by updating the test case :

When you run the test case, the rule ForAll will not be fired.

From

It is sometimes needed to access data from outside the drools session. As it is not possible to insert all objects in the session, we can use the from instruction in the when part.

First let us create a CustomerService class in package Droolscours.sercice

then we shall create the rule that uses the from

and the following test case :

The rule is fired twice as in the service there are two customers with the same name and with a different country.

Collecting

The purpose is to collect a set of fact and constraint if the constraints are true. Let us see the following example int the rule "More then 2 CashFlow Line". in this rule, we want to collect all CashFlow that are in the correct time period and the good account number. The "from collect" syntax returns an arrayList. It is possible to add a condition as in the first rule where we add a constraint that we expect at least 2 items. In the second rule, we do not add this constraint.

You may need to add constructors in the CashFlow and AccountingPeriod classes :

Here is our test case :

Accumulating

In the previous section, we collect data. There is an "from accumulate" that allows us to sum data in one command. the "from collect" instruction takes 5 parameters : 1) a fact constraint expression 2) an init condition 3) the instruction when the rule applies to the fact constraint expression 4) the reverse action when the fact constraint expression is not true anymore 5) The result of the accumulate

Here is our example :

The constraint here is on a fact type CashFlow with the constraints that we already used before (good account number and the good date period and it should be a credit or a debit) Then the initial condition, we initialize a double value we call total. Then in the action/reverse, we add to the total the amount in the CashFlow that we get by using an attribute binding. In the result action we put the total we calculated.

Summary

This lesson was an introduction to the main drools language syntax that are needed for starting a drools project. In the next lesson, we will start learning the ruleflow concept that we encourage to use in drools projects.

Last updated

Was this helpful?