Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Easy Rules

Easy Rules

The simple, stupid Java rules engine

Mahmoud Ben Hassine

May 23, 2020
Tweet

More Decks by Mahmoud Ben Hassine

Other Decks in Programming

Transcript

  1. Java rules engines overview Good solutions but.. - Lots of

    dependencies - Steep learning curve - Underused most of the time
  2. Is there a simpler way ? “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.” Martin Fowler - http://martinfowler.com/bliki/RulesEngine.html
  3. Easy Rules in a nutshell - Lightweight (36k jar with

    no dependencies, except slf4j API) - Embeddable - POJO based development - Rule composition - Expression Language support (MVEL, SpEL) - Open source : https://github.com/j-easy/easy-rules
  4. Key concepts - Rule: a business rule with a condition

    and one or more actions - Rules: namespace of rules with unique names - Fact: a named fact with a typed value - Facts: namespace of facts with unique names - RulesEngine: main entry point to fire rules on known facts - RuleListener: API to add behaviour before/after rule execution
  5. Defining rules (1/3) public class MyRule extends BasicRule { @Override

    public boolean evaluate() { return true; } @Override public void execute() { print("Hello world!"); } } by extending a base class* *: or implementing the Rule interface
  6. Defining rules (2/3) @Rule (name = "my awesome rule" )

    public class MyRule { @Condition public boolean when() { return true; } @Action(order = 1) public void then() { print("Hello"); } @Action(order = 2) public void after() { print("world!"); } } by annotating a POJO
  7. Defining rules (3/3) name: weather rule description: if it rains

    then take an umbrella priority: 1 condition: "rain == true" actions: - "System.out.println(\"It rains, take an umbrella!\");" by using an Expression Language
  8. Rules engine - Operates on a set of ordered rules

    - Fires rules according to their natural order (priority by default) - Can be configured to: - skip next rules when a rule is applied - skip next rules when a rule is not triggered - skip next rules when a rule fails - skip next rules if priority > threshold
  9. Rules engine example // create a rules engine RulesEngine rulesEngine

    = new DefaultRulesEngine(); //define rules Rules rules = new Rules(); rules.register(new WeatherRule()); //define facts Facts facts = new Facts(); facts.put("rain", true) //fire rules on known facts rulesEngine.fire(rules, facts);
  10. Composite rules - Composite design pattern implementation - Create complex

    rules from primitive ones (UnitRuleGroup, ActivationRuleGroup, ConditionalRuleGroup) - Primitive rules can be reused across multiple composite rules
  11. Composite rule example //Create a composite rule from two primitive

    rules UnitRuleGroup myCompositeRule = new UnitRuleGroup("myCompositeRule"); myCompositeRule.addRule(myRule1); myCompositeRule.addRule(myRule2); //Register the composite rule as a regular rule Rules rules = new Rules(); rules.register(myCompositeRule);
  12. Conclusion Easy Rules ++ : - Lightweight, embeddable - Easy

    to learn and use - POJO based development - Rule composition - Expression Language support Easy Rules - - : - Not compliant with JSR-94 - No UI to define rules