Java rule engine easy rules
1. Easy rules overview
Easy rules is a Java rule engine, inspired by an article entitled "should I use a rules engine?" Articles
Easy rules provides rule abstraction to create rules with conditions and actions, and provides ruleengine API, which runs through a set of rules to evaluate conditions and execute actions.
Easy rules is easy to use in just two steps:
First, there are many ways to define rules
Method 1: Annotation
Mode 2: chain programming
Method 3: expression
Method 4: YML configuration file
For example: weather rule yml
Next, apply the rules
Introductory case: Hello easy rules
To create a maven project from a skeleton:
A HelloWorld rule is generated for us by default, as follows:
2. Rule definition
2.1. Define rules
Most business rules can be represented by the following definitions:
Easy rules provides an abstraction for each key point to define business rules.
In easy rules, the rule interface represents rules
The evaluate method encapsulates the condition that the evaluation result must be true to trigger the rule. The execute method encapsulates the actions that should be performed when the rule conditions are met. Conditions and actions are represented by the condition and action interfaces.
There are two ways to define rules:
You can add @ rule annotation on a POJO class, for example:
@Condition annotation specifies the rule condition @ fact annotation specifies the parameter @ action annotation specifies the action executed by the rule
Rulebuilder supports chain style definition rules, such as:
Combination rule
Compositerule consists of a set of rules. This is an implementation of a typical composite design pattern.
Composition rules are an abstract concept because they can be triggered in different ways.
Easy rules comes with three compositerule implementations:
Composite rules can be created from basic rules and registered as general rules:
Each rule has a priority. It represents the default order in which registration rules are triggered. By default, a lower value indicates a higher priority. You can override the CompareTo method to provide a custom priority policy.
2.2. Define facts
In easy rules, the fact API represents facts
For example, chestnuts:
Alternatively, you can use this abbreviation
You can inject facts into condition and action methods with @ fact annotation
2.3. Define rule engine
Easy rules provides two RulesEngine interface implementations:
Create rule engine:
Then, register the rules
The rule engine has some configurable parameters, as shown in the following figure:
For example, chestnuts:
2.4. Define rule listener
By implementing the rulelistener interface
3. Examples
4. Expansion
A rule is essentially a function, such as y = f (x1, X2,.., xn)
Rule engine is an engine to solve the separation of business code and business rules. It is a component embedded in the application, which realizes the separation of business decisions from the application code.
Another common way is to implement it in java + groovy. Java embeds groovy script engine to split business rules.
https://github.com/j-easy/easy-rules/wiki