You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
easy-rules/site/tutorials/rule-priority.md

4.3 KiB

layout title header prev_section next_section doc
docs Rule Priority Rule Priority tutorial tutorials/hello-world tutorials/dynamic-configuration true

This tutorial shows how to define priority in which rules must be fired. In Easy Rules, every rule has a priority. Rules are fired by default according to their priorities (this can be changed as described in the user guide).

In this tutorial, we have an application that sells alcohol. The application must flag the customer as adult if his age is greater than 18, and must deny children from buying alcohol. Customers are represented by the following Person class:

public class Person {

    private String name;

    private int age;

    private boolean adult;

    //getters and setters omitted

}

Based on these requirements, we can define the following rules:

  • Rule 1: should operate an a Person instance, check that the person age is greater than 18 and set the adult flag.
  • Rule 2: should operate an a Person instance, check that the person is adult and deny children (ie, non adult) from buying alcohol.

Rule 1 should be fired before rule 2. We will set rule 1 priority to 1 and rule 2 priority to 2 so that Easy Rules engine fire them in this order.

First, let's create a class for rule 1:

@Rule(name = "AgeRule",
      description = "Check if person's age is > 18
                     and marks the person as adult")
public class AgeRule {

    private Person person;

    private int adultAge = 18;

    public AgeRule(Person person) {
        this.person = person;
    }

    @Condition
    public boolean isAdult() {
        return person.getAge() > adultAge;
    }

    @Action
    public void markAsAdult(){
        person.setAdult(true);
        System.out.printf(
            "Person %s has been marked as adult.\n",
             person.getName());
    }

    @Priority
    public int getPriority() {
        return 1;
    }

}

As required, this rule class operates on a person that is passed at construction time.

The isAdult method annotated with @Condition tells Easy Rules engine to call this method to check if the rule should be fired, in this case, if the person's age is greater than 18.

The markAsAdult method annotated with @Action will mark the person as adult by setting the adult flag.

Finally, the getPriority method annotated with @Priority tells Easy Rules engine to fire this rule in first order.

Now, let's create a class for rule 2:

@Rule(name = "alcoholRule",
      description = "Children are not allowed to buy alcohol.")
public class AlcoholRule {

    private Person person;

    public AlcoholRule(Person person) {
        this.person = person;
    }

    @Condition
    public boolean isChildren() {
        return !person.isAdult();
    }

    @Action
    public void denyAlcohol(){
        System.out.printf(
            "Sorry %s, you are not allowed to buy alcohol.\n",
             person.getName());
    }

    @Priority
    public int getPriority() {
        return 2;
    }

}

As for rule 1, the class operates on a person instance and provides methods to define rule condition, action and priority.

To launch the tutorial, we will use the following class:

public class Launcher {

    public static void main(String[] args) {

        //create a person instance
        Person tom = new Person("Tom", 16);
        System.out.println(
            "Tom: Hi! can I have some Vodka please?");

        //create a rules engine
        RulesEngine rulesEngine = aNewRulesEngine().build();

        //register rules
        rulesEngine.registerRule(new AgeRule(tom));
        rulesEngine.registerRule(new AlcoholRule(tom));

        //fire rules
        rulesEngine.fireRules();

    }

}

To run the tutorial, you can follow these instructions from the root directory of Easy Rules :

{% highlight bash %} $ mvn install $ cd easyrules-samples $ mvn exec:java -P runRulePriorityTutorial {% endhighlight %}

If you run this tutorial, you would get the following output:

Tom: Hi! can I have some Vodka please?
INFO: Rule alcoholRule triggered.
Sorry Tom, you are not allowed to buy alcohol.
INFO: Rule alcoholRule performed successfully.

As expected, since Tom's age is under 18, he has not been allowed to buy alcohol.