-
Notifications
You must be signed in to change notification settings - Fork 1.1k
faqs
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.
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.
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. However, a Facts
object is not thread safe, each thread should have its own set of data to work on.
Easy Rules was designed as an implementation of a production system (and inspired by this article). In these systems, there is no else
statement. The whole point is to be able to design the logic as a number of conditions/actions and let the engine decide which rule to apply given the set of facts. Rules are not meant to replace just any if/then/else logic, it's a different kind of thinking.
However, production systems have always been able to cover any complex if/then/else (nested) statements by using multiple productions
. This is no different in Easy Rules where this is possible to implement using two (or more) rules: one for the condition and another one for the negation of the condition.
Here are some resources on that topic:
- "Productions consist of two parts: a sensory precondition (or "IF" statement) and an action (or "THEN")" [ source ]
- "These types of rules are used to represent behaviors of the type IF condition THEN action." [ source ]
- "Organize logic through a set of production rules, each having a condition and an action." [ source ]
Feel free to ask your question in the Gitter channel of the project.
Easy Rules is created by Mahmoud Ben Hassine with the help of some awesome contributors
-
Introduction
-
User guide
-
Tutorials
-
Get involved