Contents
What EZ-Rules is?
EZ Rules is a lightweight Java framework that allow the users to design the project business logic as rules.
The framework use some ideas of some of the well know Rule frameworks to create a simple and easy to use library.
Some of the features available in the framework are:
- Powerful Java API
- Rules doesn’t depend of Rule super class, any java class can be a rule with the correct annotations
- Rule grouping with different execution strategy patterns like (stop first run, or execute all)
- Rules prioritization
- Rule grouping
- Generic Context Support, you can pass a set of context to the engine and define the parameters you want in you rules methods. The framework will resolve those parameters for you.
How it work
Declare your Rule
You can convert almost every POJO into a rule, those are the restrictions:
- The POJO need to have the @Rule annotation
- The POJO need to have a method for the precondition annotated with @When annotation this method could have any number of parameters (see documentation for details of how the parameters are resolved) and should return one of the following values:
- boolean
- Boolean
- WhenEnum
- The POJO should have a void method annotated with @Then and any number of parameters (see documentation for details of how the parameters are resolved)
Rule sample:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@Rule public class SimpleRule { public SimpleRule() { //NOP } @When public WhenEnum anyConditionMethodName() { //This method could be also a boolean return WhenEnum.ACCEPT; } @Then public void anyActionMethodName() { System.out.println("Rule execution"); } } |
Executing the rules
An special class was build to manage the application rules, this class was names RulesEngine. The engine class allow you to register @Rules, @Callback, @Group, Exchange classes. For more information look in the documentation.
Rule engine sample:
1 2 3 4 5 6 7 8 9 10 |
RulesEngine rulesEngine = new RulesEngine(); //Let's register the rule, can be registered as class, instance or string! In this sample let's use the class rulesEngine.registerExecutable(SimpleRule.class); //Let execute the rules, this method fail silent if an exception occurs inside the rules, you need to check after the success of the operation RuleEngineExecutionResult execute = rulesEngine.execute(context1); if (!execute.isSuccess()) { throw execute.getException(); } |
Links
Full documentation: EZ-Rules @ Opnitech
Repository: EZ-Rules @ GitHub
Releases: EZ-Rules Releases @ GitHub
Continuous integration: Build Jenkins job @ CloudBees
Licensing: MIT License
You really found a way to make this whole proescs easier.