Save 37% off PRO during our Black Friday Sale! »

Easy Rules

Easy Rules

The simple, stupid Java rules engine

18b9001fd20c1e089d19f4a1e994bcdc?s=128

Mahmoud Ben Hassine

May 23, 2020
Tweet

Transcript

  1. Easy Rules The simple, stupid Java rules engine Mahmoud Ben

    Hassine https://benas.github.io
  2. Java rules engines overview Good solutions but.. - Lots of

    dependencies - Steep learning curve - Underused most of the time
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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);
  11. Composite rules - Composite design pattern implementation - Create complex

    rules from primitive ones (UnitRuleGroup, ActivationRuleGroup, ConditionalRuleGroup) - Primitive rules can be reused across multiple composite rules
  12. 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);
  13. Demo

  14. 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
  15. Thank you! https://github.com/j-easy/easy-rules https://benas.github.io