Skip to content
Mahmoud Ben Hassine edited this page May 14, 2018 · 18 revisions

1. Why is Easy Rules called the "The stupid Java rules engine"?

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 default rules 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.

As of version 3.1, Easy Rules comes with a new inference rules engine. This engine is less stupid than the default one, it continuously applies rules given a set of facts until no more rules are applicable. You can find an example of how to use it in the air conditioning tutorial.

2. I would like to return a value upon a rule execution, how to do that?

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. This new fact might drive the execution flow of subsequent rules. This is how production systems are designed to work and Easy Rules is no different. Here is an example of a rule that adds the result of its action to the set of known facts:

@Rule(name = "my rule")
public class MyRule {

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

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

}

The result of the rule may or may not be the trigger of next rules in the flow. But you can always make your rule return a result after execution. Here is an example:

@Rule(name = "my rule")
public 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.

3. I've registered multiple instances of the same rule with different inputs, but it seems only the first instance is registered. What's happening?

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.

4. Is Easy Rules usable with Android?

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

5. Can I use Easy Rules in a web application?

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.

6. How to deal with thread safety?

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. However, a Facts object is not thread safe, each thread should have its own set of data to work on.

7. I have another question, how do I do?

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