Skip to content
Mahmoud Ben Hassine edited this page May 30, 2017 · 18 revisions

The goal behind Easy Rules is to provide a lightweight rules engine without features that 80% of application do not need. The term "stupid" is actually the perfect term to describe how the engine works: It iterates over a set of ordered rules and execute them when their conditions are met. This what makes it easy to learn use and use following the KISS principle.

By design, rules do not return values. A rule action is not a function (that returns a value), it should be considered as an action that have side effect, for example, adding a new fact to the set of known facts. But you can always make your rule return a result after execution. Here is an example:

@Rule(name = "my rule")
public static class MyRule<T> {

    private boolean executed;

    private T result;

    //@Condition
    public boolean when() {
        return true;
    }

    //@Action
    public void then() throws MyException {
        try {
            System.out.println("my rule has been executed");
            result = null; // assign your result here
            executed = true;
        } catch (MyException e) {
            // executed flag will remain false if an exception occurs
            throw e;
        }
    }

    public boolean isExecuted() {
        return executed;
    }

    public T getResult() {
        return result;
    }

}

This rule will return a result if it is successfully executed. After firing rules, you query the executed flag on your rule instance and get the execution result.

Another way is to add the result of the rule to the set of known facts:

@Rule(name = "my rule")
public static class MyRule<T> {

    //@Condition
    public boolean when() {
        return true;
    }

    //@Action
    public void then(Facts facts) {
        result = ..
        facts.add("myRuleResult", result)
    }

}

Rules have unique names within a rules set of type Rules. If you register multiple instances of the same rule, only the first instance will be considered. Other instances will be ignored since they have the same name.

Yes. Easy Rules has been made Android compatible since version 1.3

Sure. Easy Rules is very lightweight and can be used both in a standalone application or embedded in an application server, a servlet container or a dependency injection container.

You can find an example of how to use Easy Rules in a web application here

Starting from v3, rules in Easy Rules are stateless, so they are thread safe. The rules engine is also thread safe and can be shared between threads.

Feel free to ask your question in the Gitter channel of the project.