Skip to content
Mahmoud Ben Hassine edited this page May 28, 2017 · 13 revisions

In this tutorial, we have a shop application and we would like to implement the following requirement: deny children from buying alcohol. The minimum legal age to be considered as adult is 18. The shop customers are represented by the Person class:

public class Person {
    private String name;
    private int age;
    private boolean adult;
    //getters and setters omitted
}

We will 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:

public class AgeRule extends BasicRule {

    private static final int ADULT_AGE = 18;

    public AgeRule() {
        super("AgeRule", "Check if person's age is > 18 and marks the person as adult", 1);
    }

    @Override
    public boolean evaluate(Facts facts) {
        Person person = (Person) facts.get("person");
        return person.getAge() > ADULT_AGE;
    }

    @Override
    public void execute(Facts facts) {
        Person person = (Person) facts.get("person");
        person.setAdult(true);
        System.out.printf("Person %s has been marked as adult", person.getName());
        System.out.println();
    }
    
}

As required, this rule class operates on a person that is obtained from the set of facts.

The evaluate method checks if the person's age is greater than 18.

The execute will mark the person as adult by setting the adult flag.

Finally, the third constructor argument which represents the rule priority is set to 1 to tells Easy Rules engine to fire this rule in first order.

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

public class AlcoholRule extends BasicRule {

    public AlcoholRule() {
        super("AlcoholRule", "Children are not allowed to buy alcohol", 2);
    }

    @Override
    public boolean evaluate(Facts facts) {
        Person person = (Person) facts.get("person");
        return !person.isAdult();
    }

    @Override
    public void execute(Facts facts){
        Person person = (Person) facts.get("person");
        System.out.printf("Shop: Sorry %s, you are not allowed to buy alcohol", person.getName());
        System.out.println();
    }

}

As for rule 1, the class operates on a person instance and prints the denial message for children.

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

public class Launcher {

    public static void main(String[] args) {
        //create a person instance (fact)
        Person tom = new Person("Tom", 14);
        Facts facts = new Facts();
        facts.add("person", tom);

        // create a rules set
        Rules rules = new Rules();
        rules.register(new AgeRule());
        rules.register(new AlcoholRule());

        //create a rules engine and fire rules on known facts
        RulesEngine rulesEngine = aNewRulesEngine()
                .named("shop rules engine")
                .build();

        System.out.println("Tom: Hi! can I have some Vodka please?");
        rulesEngine.fire(rules, facts);
    }

}

To run the tutorial, please follow these instructions:

$ git clone https://github.com/j-easy/easy-rules.git
$ cd easy-rules
$ mvn install
$ cd easy-rules-tutorials
$ mvn exec:java -P runShopTutorial

You should get the following output:

Tom: Hi! can I have some Vodka please?
INFO: Rule alcoholRule triggered.
Shop: 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.