Rule Engine– Java & RubyDrools & Ruleby               Ing. Martín Cabrera               martin.cabrera@moove-it.com       ...
Content   Rules Engine ? What is this ?   When to use ?   RETE algorithm   In Java - JBoss Drools       features    ...
Motor de Reglas ¿what a #@!$ ?                    Application                    Rule engine    Rule def.                 ...
When to use?   Infrastructure decoupled from the source   System to allow the final user to define their rules    and ch...
Tipical Code01     if ((user.isMemberOf(AdministratorGroup) && user.isMemberOf(teleworkerGroup)) ||02             user.isS...
Rete Algorithm – theSolution !   Pattern matching algorithm for implementing    production rule systems (wikipedia)     ...
Rete 2   RETE – characteristics:       It reduces or eliminates certain types of redundancy through the use of        no...
Drools –   http://jboss.org/drools   Introduction video -    http://blog.athico.com/2010/03/fosdem-50-minute-introductio...
Eclipse & Drools
Syntax or rules definitionrule “<name>”   <attribute> <value>   when      <LHS>   then      <RHS>end
Example – rule definition rule.drlpackage com.mooveit.ruleimport com.mooveit.rule.Globalglobal Global global;rule "r1 - fi...
Example                                Business Action                                                         1StatefulSe...
Drools SchemaRules files                Repository of                instances                   assert                   ...
JBoss Rules Overview                                                                     AgendaRule                       ...
Examples in our projects   (demo)
Rule Engine in
Rules Engine In ruby   Ruleby - https://github.com/codeaspects/ruleby       Rule engine for Ruby       Active project b...
Ruleby Description   Provide a pure Ruby Domain Specific Language (DSL)     rule [m.status == :HELLO] do |v|       puts v...
Ruleby links   The Ruleby DSL for more information.       https://github.com/codeaspects/ruleby/wiki/The-Ruleby   Downl...
Thats it !
Upcoming SlideShare
Loading in...5
×

Rules Engine - java(Drools) & ruby(ruleby)

8,970

Published on

Rules Engine ? What is this ?
When to use ?
RETE algorithm
Rules Engine In Java - JBoss Drools
- features
- Syntax
Rules Engine In ruby - ruleby

Published in: Technology

Rules Engine - java(Drools) & ruby(ruleby)

  1. 1. Rule Engine– Java & RubyDrools & Ruleby Ing. Martín Cabrera martin.cabrera@moove-it.com Moove-IT
  2. 2. Content Rules Engine ? What is this ? When to use ? RETE algorithm In Java - JBoss Drools  features  Syntax In ruby ?
  3. 3. Motor de Reglas ¿what a #@!$ ? Application Rule engine Rule def. knowledge base Rule def
  4. 4. When to use? Infrastructure decoupled from the source System to allow the final user to define their rules and change them dynamically  Developers are not the primary responsibility for changing rules Moderate or high complexity of business rules Performance ! Conclusion: down the rules of your system, so that they can be reused, decouple the code and allow the user to know the rules ... to modify !
  5. 5. Tipical Code01 if ((user.isMemberOf(AdministratorGroup) && user.isMemberOf(teleworkerGroup)) ||02 user.isSuperUser() {03 // more checks for specific cases04 if ((expenseRequest.code().equals("B203") ||05 (expenseRequest.code().equals("A903") && (totalExpenses < 200) &&06 (bossSignOff > totalExpenses)) && (deptBudget.notExceeded)) {07 // issue payments08 }09 else if {10 // check lots of other conditions11 }12 } Change this rules ! ... %&”@ !13 else {14 // even more business logic15 }16 }
  6. 6. Rete Algorithm – theSolution ! Pattern matching algorithm for implementing production rule systems (wikipedia)  http://en.wikipedia.org/wiki/Rete_algorithm replaces if ... Then with an optimized logic network
  7. 7. Rete 2 RETE – characteristics:  It reduces or eliminates certain types of redundancy through the use of node sharing.  It stores partial matches when performing joins between different fact types.  It allows for efficient removal of memory elements when facts are retracted from working memory. Widely used to implement matching functionality within pattern- matching engines that exploit a match-resolve-act cycle to support forward and backward chaining and inferencing.  Forward-chaining (facts -> goals)  Backguard chaining (goals -> facts)
  8. 8. Drools – http://jboss.org/drools Introduction video - http://blog.athico.com/2010/03/fosdem-50-minute-introduction-into.h Introduction  Rules engine framework for java  It is used in other jboss projects like JBPM, SEAM, Jboss ESB and others  The rules can be written in Java, Python, Groovy Ruby and your own rule language  Jboss tools – plugin Eclipse to use this framework
  9. 9. Eclipse & Drools
  10. 10. Syntax or rules definitionrule “<name>” <attribute> <value> when <LHS> then <RHS>end
  11. 11. Example – rule definition rule.drlpackage com.mooveit.ruleimport com.mooveit.rule.Globalglobal Global global;rule "r1 - first rule" when instance : Entity( field1 > 2010) then ...end
  12. 12. Example Business Action 1StatefulSession session = getRulesBase().newStatefulSession();session.setGlobal("rulesImpl", this);session.insert(instance);session.fireAllRules(); 2 3 knowledge base +instance Execute the “then” part for the instance that match the facts
  13. 13. Drools SchemaRules files Repository of instances assert modify retract
  14. 14. JBoss Rules Overview AgendaRule Activation Rule Rule 1.Parse DRL RuleBase Rule Fact Rule Rule Activation Fact 2. Create Rule FactFact Fact Fact 3. Assert Facts WorkingMemory Fact Fact Fact Fact Fact es Fact ul R Fact ll A re Rule (5) activation Fi Rule -> consequence 4. Rule Rule Rule
  15. 15. Examples in our projects (demo)
  16. 16. Rule Engine in
  17. 17. Rules Engine In ruby Ruleby - https://github.com/codeaspects/ruleby  Rule engine for Ruby  Active project but with few people dedicated  October 11 of 2010 last push in the master branch  Install: gem install ruleby Others  Ruby Rools - http://rools.rubyforge.org/  Last activity (ago 2009 in rools users)  Status: Beta  2 members  A few projects without activity: rein, ruby-rules  Treetop  language for describing languages  Its not a pure rule engine ... but can serve
  18. 18. Ruleby Description Provide a pure Ruby Domain Specific Language (DSL) rule [m.status == :HELLO] do |v| puts v[:m].message end (LHS) Left hand side of the production (the ‘when’) is represented by the Array parameter (RHS) The right hand side (the ‘then’) is represented by the block. engine :hello_engine do |e| HelloWorldRulebook.new(e).rules assert e, Message.new(:HELLO, ‘Hello World’) e.match end Result:Hello World
  19. 19. Ruleby links The Ruleby DSL for more information.  https://github.com/codeaspects/ruleby/wiki/The-Ruleby Download  http://gemcutter.org/gems/ruleby User Group  http://groups-beta.google.com/group/ruleby
  20. 20. Thats it !
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×