An Introduction to
   Business Rules with
       JBoss Drools

John Paulett
October 12, 2009
Goals
     Introduction to “business rules”


     High level overview of JBoss Drools


     Discussion of benefits and shortcomings




10/12/2009                                     2
Business Rules




10/12/2009                    3
Problem Domain
     Basic problem:
             how to model “business logic”


     In this context:
             “business logic” ≠ code between UI and DB
             “business logic” = policies of the business




10/12/2009                                                 4
Business Logic Examples
     Rate calculations at insurance company
       ●     Depends on laws, age, sex, location, previous
             history, desired coverage, etc.


     Critical lab values at hospital
       ●     Depends on age, previous values, other lab
             tests, diagnose, mandates, etc.




10/12/2009                                                5
Business Rule Engine
     Type of Expert System


     Two types of rules:
       ●     “Production (or Inference) Rules”
             –   If x, then y
             –   Often stateless
       ●     “Reaction Rules” (Complex Event Processing)
             –   Wait for set of events
             –   Stateful

10/12/2009                                                 6
Forward Chaining
     1) Iterate through antecedents (if …)


     2) Each time an antecedent is matched,
     add knowledge of consequent (then …)


     3) Go to #1, until goal reached




10/12/2009                                    7
Rete Algorithm
     Naïve forward chaining does not scale
     well as number of rules increases


     Efficient implementation using directed
     acyclic graph representation of rules


     Higher memory consumption for
     increased speed

10/12/2009                                     8
JBoss Drools




10/12/2009                  9
JBoss Drools Suite
     Guvnor – Business Rule Management
     System


     Expert – Production Rules


     Flow – Workflow


     Fusion – Complex Event Processing
10/12/2009                               10
Drools Expert
     Core Drools product


     Forward chaining rules with Rete


     Eclipse authoring plugin




10/12/2009                              11
Ways to Write Rules
     Technical Rule Language


     Custom Domain Specific Languages


     Guided Editor


     Decision Table


10/12/2009                              12
Technical Rule Language

  rule "Hello World"
      when
             m : Message( status == Message.HELLO, myMessage : message )
      then
             System.out.println( myMessage );
             m.setMessage( "Goodbye cruel world" );
             m.setStatus( Message.GOODBYE );
             update( m );
  end




10/12/2009                                                                 13
Custom DSLs
     Map “natural language” into Technical
     Rule Language




10/12/2009                                   14
Guided Editor




              From http://www.jboss.org/drools/drools-expert.html




10/12/2009                                                          15
Decision Table
     Excel XLS based




10/12/2009                     16
Drools Guvnor
     BRMS – Business Rule Management
     System


     Centralized knowledgebase


     Web-based rule authoring


     Versioning of rules
10/12/2009                             17
Drools Guvnor
     Manage rules through web console




                 From http://www.jboss.org/drools/drools-guvnor.html




10/12/2009                                                             18
Drools Guvnor
     Edit rules through web console




                  From http://www.jboss.org/drools/drools-guvnor.html




10/12/2009                                                              19
Drools Flow
     Workflow management




                From http://www.jboss.org/drools/drools-guvnor.html




10/12/2009                                                            20
Drools Fusion
     Extension to Drools Expert for temporal
     reasoning


     Detects relevant patterns in “cloud or
     stream of events”




10/12/2009                                     21
Discussion




10/12/2009                22
When To Use a Rules Engine
     Large set of rules, with complex
     branching


     Rules likely to change over time, likely out
     of sync with code changes


     Involve “business users” in creation &
     maintenance of rules

10/12/2009                                      23
Benefits
     Separates “business logic” from
     application code


     “Hot” changes of rules (no recompile)


     Improved performance for large rule sets


     Communicates rules more clearly than
     Java code
10/12/2009                                      24
Downsides
     New tool
       ●     Learning curve
       ●     Extra point of failure


     Overhead
       ●     Not practical for small rule sets




10/12/2009                                       25
Alternatives
     Java Rule Engine API - JSR 94


     Closed Source:
       ●     IBM's ILOG JRules, Fair Isaac's Blaze Advisor


     Open Source:
       ●     Jess, OpenRules, Zilonis, take


10/12/2009                                                   26
References
     http://www.jboss.org/drools/
     http://java.sun.com/developer/technicalArticles/J2SE/JavaRule.html
     http://java.sys-con.com/node/45082
     http://herzberg.ca.sandia.gov/guidelines.shtml




10/12/2009                                                            27

Drools

  • 1.
    An Introduction to Business Rules with JBoss Drools John Paulett October 12, 2009
  • 2.
    Goals Introduction to “business rules” High level overview of JBoss Drools Discussion of benefits and shortcomings 10/12/2009 2
  • 3.
  • 4.
    Problem Domain Basic problem: how to model “business logic” In this context: “business logic” ≠ code between UI and DB “business logic” = policies of the business 10/12/2009 4
  • 5.
    Business Logic Examples Rate calculations at insurance company ● Depends on laws, age, sex, location, previous history, desired coverage, etc. Critical lab values at hospital ● Depends on age, previous values, other lab tests, diagnose, mandates, etc. 10/12/2009 5
  • 6.
    Business Rule Engine Type of Expert System Two types of rules: ● “Production (or Inference) Rules” – If x, then y – Often stateless ● “Reaction Rules” (Complex Event Processing) – Wait for set of events – Stateful 10/12/2009 6
  • 7.
    Forward Chaining 1) Iterate through antecedents (if …) 2) Each time an antecedent is matched, add knowledge of consequent (then …) 3) Go to #1, until goal reached 10/12/2009 7
  • 8.
    Rete Algorithm Naïve forward chaining does not scale well as number of rules increases Efficient implementation using directed acyclic graph representation of rules Higher memory consumption for increased speed 10/12/2009 8
  • 9.
  • 10.
    JBoss Drools Suite Guvnor – Business Rule Management System Expert – Production Rules Flow – Workflow Fusion – Complex Event Processing 10/12/2009 10
  • 11.
    Drools Expert Core Drools product Forward chaining rules with Rete Eclipse authoring plugin 10/12/2009 11
  • 12.
    Ways to WriteRules Technical Rule Language Custom Domain Specific Languages Guided Editor Decision Table 10/12/2009 12
  • 13.
    Technical Rule Language rule "Hello World" when m : Message( status == Message.HELLO, myMessage : message ) then System.out.println( myMessage ); m.setMessage( "Goodbye cruel world" ); m.setStatus( Message.GOODBYE ); update( m ); end 10/12/2009 13
  • 14.
    Custom DSLs Map “natural language” into Technical Rule Language 10/12/2009 14
  • 15.
    Guided Editor From http://www.jboss.org/drools/drools-expert.html 10/12/2009 15
  • 16.
    Decision Table Excel XLS based 10/12/2009 16
  • 17.
    Drools Guvnor BRMS – Business Rule Management System Centralized knowledgebase Web-based rule authoring Versioning of rules 10/12/2009 17
  • 18.
    Drools Guvnor Manage rules through web console From http://www.jboss.org/drools/drools-guvnor.html 10/12/2009 18
  • 19.
    Drools Guvnor Edit rules through web console From http://www.jboss.org/drools/drools-guvnor.html 10/12/2009 19
  • 20.
    Drools Flow Workflow management From http://www.jboss.org/drools/drools-guvnor.html 10/12/2009 20
  • 21.
    Drools Fusion Extension to Drools Expert for temporal reasoning Detects relevant patterns in “cloud or stream of events” 10/12/2009 21
  • 22.
  • 23.
    When To Usea Rules Engine Large set of rules, with complex branching Rules likely to change over time, likely out of sync with code changes Involve “business users” in creation & maintenance of rules 10/12/2009 23
  • 24.
    Benefits Separates “business logic” from application code “Hot” changes of rules (no recompile) Improved performance for large rule sets Communicates rules more clearly than Java code 10/12/2009 24
  • 25.
    Downsides New tool ● Learning curve ● Extra point of failure Overhead ● Not practical for small rule sets 10/12/2009 25
  • 26.
    Alternatives Java Rule Engine API - JSR 94 Closed Source: ● IBM's ILOG JRules, Fair Isaac's Blaze Advisor Open Source: ● Jess, OpenRules, Zilonis, take 10/12/2009 26
  • 27.
    References http://www.jboss.org/drools/ http://java.sun.com/developer/technicalArticles/J2SE/JavaRule.html http://java.sys-con.com/node/45082 http://herzberg.ca.sandia.gov/guidelines.shtml 10/12/2009 27