首页  

easyrules 简介     所属分类 easyrules 浏览量 176
The simple, stupid rules engine for Java
https://github.com/j-easy/easy-rules
github.com/j-easy/easy-rules/wiki

Easy Rules is a simple yet powerful Java rules engine providing the following features:
Lightweight framework and easy to learn API
POJO based development
Useful abstractions to define business rules and apply them easily
The ability to create composite rules from primitive ones
The ability to define rules using an Expression Language (Like MVEL, SpEL and JEXL)

You can build a simple rules engine yourself. 
All you need is to create a bunch of objects with conditions and actions, 
store them in a collection, and run through them to evaluate the conditions and execute the actions.
This is exactly what Easy Rules does, 
it provides the Rule abstraction to create rules with conditions and actions, 
and the RulesEngine API that runs through a set of rules to evaluate conditions and execute actions.


Easy Rules  RulesEngine接口的两种实现
DefaultRulesEngine    根据规则的自然顺序(默认为优先级)应用规则
InferenceRulesEngine  持续对已知事实应用规则,直到不再应用规则为止
Inference 推理 

DefaultRulesEngine   设置priority属性,从小到大顺序执行

InferenceRulesEngine  通过循环不停的通过事实去匹配规则,直到全部事实都不匹配规则才停止循环

规则引擎参数 默认都为false 

skipOnFirstAppliedRule   规则被触发时跳过后面的规则
skipOnFirstFailedRule    规则失败时跳过后面的规则
skipOnFirstNonTriggeredRule   规则不被触发跳过后面的规则
rulePriorityThreshold         优先级超过定义的阈值,则跳过下一个规则 


RulesEngineParameters parameters = new RulesEngineParameters()
    .rulePriorityThreshold(10)
    .skipOnFirstAppliedRule(true)
    .skipOnFirstFailedRule(true)
    .skipOnFirstNonTriggeredRule(true);
 
RulesEngine rulesEngine = new DefaultRulesEngine(parameters);


Facts  事实  ,规则引擎 判断这些Facts  是否符合规则

org.jeasy:easy-rules-core:4.1.0


Most business rules can be represented by the following definition: Name: a unique rule name within a rules namespace Description: a brief description of the rule Priority: rule priority regarding to other rules Facts: set of known facts at the time of firing rules Conditions: set of conditions that should be satisfied given some facts in order to apply the rule Actions: set of actions to perform when conditions are satisfied (and that may add/remove/modify facts) public interface Rule extends Comparable< Rule > { // This method encapsulates the rule's conditions. // return true if the rule should be applied given the provided facts, false otherwise boolean evaluate(Facts facts); // This method encapsulates the rule's actions. // throws Exception if an error occurs during actions performing void execute(Facts facts) throws Exception; //Getters and setters for rule name, description and priority omitted. } The evaluate method encapsulates conditions that must evaluate to TRUE to trigger the rule. The execute method encapsulates actions that should be performed when rule's conditions are satisfied. Conditions and actions are represented by the Condition and Action interfaces. Rules can be defined in two different ways: Declaratively by adding annotations on a POJO Programmatically through the RuleBuilder API @Rule(name = "my rule", description = "my rule description", priority = 1) public class MyRule { @Condition public boolean when(@Fact("fact") fact) { //my rule conditions return true; } @Action(order = 1) public void then(Facts facts) throws Exception { //my actions } @Action(order = 2) public void finally() throws Exception { //my final actions } } Rule rule = new RuleBuilder() .name("myRule") .description("myRuleDescription") .priority(3) .when(condition) .then(action1) .then(action2) .build();
Composite rules A composite rule is an abstract concept since composing rules can be triggered in different ways. Easy Rules comes with 3 implementations of CompositeRule that can be found in the easy-rules-support module: UnitRuleGroup: A unit rule group is a composite rule that acts as a unit: Either all rules are applied or nothing is applied. ActivationRuleGroup: An activation rule group is a composite rule that fires the first applicable rule and ignores other rules in the group (XOR logic). Rules are first sorted by their natural order (priority by default) within the group. ConditionalRuleGroup: A conditional rule group is a composite rule where the rule with the highest priority acts as a condition: if the rule with the highest priority evaluates to true, then the rest of the rules
defining facts public class Fact< T > { private final String name; private final T value; } Fact< String> fact = new Fact("foo", "bar"); Facts facts = new Facts(); facts.add(fact);
Easy Rules provides two implementations of the RulesEngine interface: DefaultRulesEngine: applies rules according to their natural order (which is priority by default). InferenceRulesEngine: continuously applies rules on known facts until no more rules are applicable. RulesEngine rulesEngine = new DefaultRulesEngine(); RulesEngine rulesEngine = new InferenceRulesEngine(); rulesEngine.fire(rules, facts);
defining rules engine listener public interface RulesEngineListener { // Triggered before evaluating the rule set. default void beforeEvaluate(Rules rules, Facts facts) { } // Triggered after executing the rule set default void afterExecute(Rules rules, Facts facts) { } } Unlike the RuleListener that is executed for each rule, the RulesEngineListener allows you to provide custom behavior before/after firing the entire rule set. DefaultRulesEngine rulesEngine = new DefaultRulesEngine(); rulesEngine.registerRulesEngineListener(myRulesEngineListener); You can register as many listeners as you want, they will be executed in their registration order.
expression language support Easy Rules provides support for defining rules with MVEL, SpEL and JEXL. Rule ageRule = new MVELRule() .name("age rule") .description("Check if person's age is > 18 and marks the person as adult") .priority(1) .when("person.age > 18") .then("person.setAdult(true);"); MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader()); MVELRule alcoholRule = ruleFactory.createRule(new FileReader("alcohol-rule.yml")); MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader()); Rules rules = ruleFactory.createRules(new FileReader("rules.yml"));
Error handling in rule definitions Engine behaviour regarding incorrect expressions in conditions For any runtime exception that may occur during condition evaluation (missing fact, typo in the expression, etc), the engine will log a warning and consider the condition as evaluated to false. It is possible to listen to evaluation errors using RuleListener#onEvaluationError. Engine behaviour regarding incorrect expressions in actions For any runtime exception that may occur while performing an action (missing fact, typo in the expression, etc), the action will not be performed and the engine will log an error. It is possible to listen to action performing exceptions using RuleListener#onFailure. When a rule fails, the engine will move to the next rule unless the skipOnFirstFailedRule parameter is set.

上一篇     下一篇
flink 反压监控

OOM kill 监控

grafana 图表 变量

easyrules 例子 基于注解

easyrules 例子 从 nacos里读取规则

统计学术语