SlideShare a Scribd company logo
Programming With Rules

     Srinath Perera, Ph.D.
     Architect, WSO2 Inc.
Rule based Systems

    ●   Expert Systems / Business
        rules engine / Production
        Systems / Inference Engines
        are used to address rule
        engines based on their
        implementations and use.
    ●   Something most people like to
        have, although not many
        people know why they need it.
Rules
●   Allow users to specify the requirements/
    knowledge about processing using
    – Declarative (Say what should happen, not how to
        do it)
    –   Logic based languages.
Types of Rules
●   Four types of rules (from
    http://www.w3.org/2000/10/swap/doc/rule-systems)
    –   Derivation or Deduction Rules – Each rules express if
        some statements are true, another statement must be
        true. Called logical implication. E.g. Prolog
         –   Transformation Rules- transform between knowledge
               bases, e.g. theorem proving
    –   Integrity Constraints – verification rules
    –   Reaction or Event-Condition-Action (ECA) Rules –
        includes a actions in addition to inference. e.g. Drools
Two types of Rule Engines
●   Forward Chaining – starts with facts/data and
    trigger actions or output conclusions
●   Backward chaining – starts with goals and
    search how to satisfy that (e.g. Prolog)
Production Systems
●   Drools belongs to the category of rule engines called
    production systems [1] (which execute actions based
    on conditions)
●   Drools use forward chaining[2] (start with data and
    execute actions to infer more data )
●   Priorities assigned to rules are used to decide the
    order of rule execution
●   They remember all results and use that to optimize
    new derivations (dynamic programming like)

       http://en.wikipedia.org/wiki/AI_production
       http://en.wikipedia.org/wiki/Forward_chaining
Big Picture
●   Usually a Rule engine
    usually includes three
    parts.
    –   Facts represented as
        working memory
    –   Set of rules that
        declaratively define
        conditions or situations
        e.g. Prolog route(X,Z) <-
        road(X,Z)
    –   Actions executed or
        inference derived based
        on the rules
Why rule engines?
●   Simplify complicated requirements with
    declarative logic, raising the level of abstraction
    of the system
●   Externalize the business logic (which are too
    dynamic) from comparatively static code base
●   Intuitive and readable than code, easily
    understood by business people/ non technical
    users

                          http://en.wikipedia.org/wiki/AI_production
                          http://en.wikipedia.org/wiki/Forward_chaining
Why rule engines? [Contd.]
●   Create complex interactions which can have
    powerful results, even from simple facts and rules.
●   Different approach to the problem, some problem
    are much easier using rules.
●   Can solve hard problems (Problems we only
    understand partially)
●   Fast and scalable when there is a data set that
    changes little by little
When to use Rules?
●   When there is no satisfactory traditional
    programming approach to solve the problem!
●   To separate code and business logic
●   The problem is beyond any obvious algorithmic
    solution. (not fully understood)
●   When the logic changes often
●   When Domain experts are none technical.
1. Real-World Rule Engines
   http://www.infoq.com/articles/Rule-Engines
2. Why are business rules better than traditional code?
   http://www.edmblog.com/weblog/2005/11/why_are_busines.html
3. Rules-based Programming with JBoss Rules/Drools
    www.codeodor.com
When not to use rule engines?
●   It is slower then usual code most of the time, so
    unless one of the following is true is should not
    be used
     – Complexity of logic is hard to tackle
     – Logic changes too often
     – Required to use by non technical users
     – It is a usecase where rules are faster.
●   Interactions between rules could be quite
    complex, and one mistake could change the
    results drastically and unexpected way e.g.
    recursive rules
●   Due to above testing and debugging is required,
    so if results are hard to verified it should not be
    used.
Drools Rule Engine
●   Forward chaining, production system based on
    Rete algorithm.
●   Written in Java, and support OOP based
    model
●   Open source/ Apache License compatible
●   Backed up JBoss, also called JBoss rules
●   Link http://jboss.org/drools
Model




●   Facts as a Object repository of java objects
●   New objects can be added, removed or updated
●   support if <query> then <action> type rules
●   Queries use OOP format
Drools Rule Language
Anatomy of a Rule
●   Has two parts
    rule "rule-name"
    when
         <query in a Object query
           language>
    then
         <any java code>
    end
●   When condition is satisfied, action is carried
     out.
Business Rules
●   Working Memory
    –   Insert, retract, update
●   Stateful sessions
●   Stateless sessions
●   Queries -
        QueryResults results = ksession.getQueryResults(
          "my query", new Object[] { "string" } );

●   Globals ksession.setGlobal("list", list);
●   Agenda filters makes sure only selected rules are
    fired. ksession.fireAllRules( new
    RuleNameEndsWithAgendaFilter( "Test" ) );
Rete Algorithm




●   Dr Charles L. Forgy's Ph.D. Thesis, 1974
●   Creates a tree representing rules, and data
    propagate through the tree and out put actions.
●   Partial results are saved in memory.
A Sample Rule
Following rule increase the premium by 15% if driver is
less than 25 and drives a sport car.
rule "MinimumAge"
when
      $d : Customer(age < 25)
       $c : Car(vin=$d.vin && model =
“sport”)
then
      c.IncreasePrimium(15);
end
Actions
●   Any java code goes in the then clause
●   Also support some level of scripts
●   Operations on working memory
    –   update(object); will tell the engine that an object has
        changed
    –   insert(new Something()); will place a new object of
        your creation into the Working Memory.
    –   insertLogical(new Something()); is similar to insert,
        but the object will be automatically retracted when
        there are no more facts to support the truth of the
        currently firing rule.
    –   retract(handle); removes an object from Working
        Memory.
Rule Patterns
Following rule reject all customers whose age less than 17.
rule "MinimumAge"
when
     c : Customer(age < 17)
then
     c.reject();
end
Conditions support <, >, ==, <=, >=, matches / not matches,
contains / not contains. And following rules provide a discount
if customer is married or older than 25.

rule "Discount"
when
    c : Customer( married == true || age > 25)
then
    c.addDiscount(10);
end
Joins
Following rule increase the premium by 15% if driver is
less than 25 and drives a sport car.
rule "MinimumAge"
when
      $d : Customer(age < 25)
       $c : Car(vin=$d.vin && model =
“sport”)
then
      c.IncreasePrimium(15);
end
Bound variables
Bound variables
 when
          Person( likes : favouriteCheese )
          Cheese( type == likes )
 then
        …
 end
OR, AND
●   OR – true if either of the statements true
     –   E.g. Customer(age > 50) or Vehicle( year > 2000)
●   AND – provide logical, if no connectivity is define
    between two statements, “and” is assumed by default.
    For an example.
      c : Customer( timeSinceJoin > 2);
      not (Accident(customerid == c.name))
    and
    c : Customer( timeSinceJoin > 2) and
            not (Accident(customerid == c.name))
    are the same.
eval(..)
●   eval(boolean expressions) – with eval(..) any
    Boolean expression can be used.
     – E.g. C:Customer(age > 20)
             eval(C.calacuatePremium() > 1000)
●   Can not hold partial results for eval(..) so
    Drools going to recalculate eval(..)
    statements and what ever trigged from that.
●   So it is going to be inefficient, so use
    sparingly
Not
●   Not – negation or none can be found. E.g.
        not Plan( type = “home”)

    is true if no plan of type home is found. Following
    is true if customer has take part in no accidents.
    rule "NoAccident"
    when
         c : Customer( timeSinceJoin > 2);
         not (Accident(customerid == c.name))
    then
         c.addDiscount(10);
    end
For all
●   True if all objects selected by first part of the
    query satisfies rest of the conditions. For an
    example following rule give 25 discount to
    customers who has brought every type of plans
    offered.
    rule "OtherPlans"
    when
         forall ($plan : PlanCategory() c : Customer(plans
    contains $plan))
    then
         c.addDiscount(25);
    end
Exists
●   True if at least one matches the query,
●   This is Different for just having Customer(), which is
    like for each which get invoked for each matching set.
●   Following rule give a discount for each family where
    two members having plans


        rule “FamilyMembers"
        when
           $c : Customer()
           exists (Customer( name contains $c.family))
        then
           c.addDiscount(5);
        end
From
●   True if at least one matches the query,
●   This is Different for just having Customer(), which is
    like for each which get invoked for each matching
    set.
●   Following rule give a discount for each family where
    two members having plans
    rule "validate zipcode"
    when
       Person( $personAddress : address )
       Address( zipcode == "23920W") from $personAddress
    then
       # zip code is ok
    end
Collect
●   True if at least one matches the query,
●   This is Different for just having Customer(), which is
    like for each which get invoked for each matching set.
●   Following rule give a discount for each family where
    two members having plans

rule "Raise priority if system has more than 3 pending alarms"
when
   $system : System()
   $alarms : ArrayList( size >= 3 )
          from collect( Alarm( system == $system, status == 'pending' ) )
then
   # Raise priority, because system $system has
   # 3 or more alarms pending. The pending alarms
   # are $alarms.
end
Accumulate
●   True if at least one matches the query,
●   This is Different for just having Customer(), which is like
    for each which get invoked for each matching set.
●   Following rule give a discount for each family where two
    members having plans

    rule "Apply 10% discount to orders over US$ 100,00"
    when
       $order : Order()
       $total : Number( doubleValue > 100 )
             from accumulate( OrderItem( order == $order, $value : value ),
                        init( double total = 0; ),
                        action( total += $value; ),
                        reverse( total -= $value; ),
                        result( total ) )
    then
       # apply discount to $order
    end
Conflict resolution
•    Each rule may define attributes There are other parameters
     you can found from [1]. E.g.
     rule "MinimumAge"
     salience = 10
     when
           c : Customer(age < 17)
     then
           c.reject();
     end



•    salience define priority of the rule and decide their activation
     order.
1.   http://labs.jboss.com/drools/documentation.html
WSO2 Deployment as an
              Example
●   There are our product Instances (e.g. WSAS, Greg,
    IS, ESB) etc, which has been mapped to a facts in
    the rule system, and it is updated using a system
    management solution.
●   They have a role property, that says what is there
    role. e.g. ESB role=”lb” means it is a load balancer.
●   Each host is represented by Host() object, and each
    server have a property called host.
●   Each server has a state, which is Up, Down
Question 1:
●   A system is considered healthy if there is One ESB,
    two WSAS instances, one load balance instance, one
    BPS server, and one Identity server.
●   Write a rule to detect if system is healthy and print it.
       When
          ESB(role !=”lb”);
          List(size ==2) from collect (WSAS());
          ESB(role==”lb”);
          BPS();
          IS();
       then
              System.out.println(“System Healthy”);
       end
Question 2:
●   If any server has failed but the host is up, restart the
    server using a rule. Assume there is
    system.restart(service-name, host).

       When
          wsas:WSAS(state=”Down”);
          host:Host(state=”Up” && wsas.host=name);
       then
             system.restart(wsas);
       end
Question 3:
●   If a host has failed, start the servers in the host using
    a rule. Assume there is system.start(service-name,
    host).

       When
          wsas:WSAS();
          host:Host(state=”Down” && wsas.host=name);
       then
          system.start(wsas);
       end
Question 4:
●   Assume there are cluster of 10 WSAS nodes, if the
    average TPS is > 300, add a new node to the cluster.
    Assume WASA expose TPS via a property called tps.

      rule "Apply 10% discount to orders over US$ 100,00"
      when
         $order : Order()
         $total : Number( doubleValue > 300)
               from accumulate( WSAS( $tps : tps),
                           init( double tpstot = 0; int count = 0; ),
                          action( tpstot += $tps; count++ ),
                          reverse( tpstot -= $tps; count-- ),
                          result( tpstot/count ) )
      then
         # apply discount to $order
      end
Drools Performance
•    Measuring Rule engine performance is tricky.
•    Main factors are number of objects and number of rules. But results
     depends on nature of rules.
•    A user feedback [1] claims Drools about 4 times faster than JRules [4].


                                          (SequentialRete)

     rules             Objects           Drools               JRules

     1219              100               4ms/4ms              16ms/15ms


•    [2] shows a comparison between Drools, Jess [5] and Microsoft rule
     engine. Overall they are comparable in performance.
1.   http://blog.athico.com/2007/08/drools-vs-jrules-performance-and-future.html
2.   http://geekswithblogs.net/cyoung/articles/54022.aspx
1.   Jess - http://herzberg.ca.sandia.gov/jess/
2.   JRules http://www.ilog.com/products/jrules/
Drools Performance Contd.
I have ran the well known rule engine bench mark [1] implementation provided with Drools. (On linbox3 - 1GB memory, 4 CPU
•
3.20GHz )




       Bench Marks                 Rule Count                  Object                      Time (ms)
                                                               Count
       Waltz                       31                          958                         1582
                                   31                          3873                        9030
       Waltz DB                    34                          393                         420


                                   34                          697                         956
                                   34                          1001                        1661
                                   34                          1305                        2642




http://www.cs.utexas.edu/ftp/pub/ops5-benchmark-suite/HOW.TO.USE
1.
Questions?

More Related Content

What's hot (20)

PPT
Introduction to Drools
giurca
 
PPTX
Kubernetes 101 for Beginners
Oktay Esgul
 
PDF
Intro to containerization
Balint Pato
 
PPT
Coding Standards & Best Practices for iOS/C#
Asim Rais Siddiqui
 
PPTX
The Right (and Wrong) Use Cases for MongoDB
MongoDB
 
PPTX
Introduction To Terraform
Sasitha Iresh
 
PDF
Introduction to Galera Cluster
Codership Oy - Creators of Galera Cluster
 
PDF
Introduction to kubernetes
Raffaele Di Fazio
 
PDF
Drools expert-docs
Erick Ulisses Monfil Contreras
 
PDF
API Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid Rahimian
Vahid Rahimian
 
PPTX
REST API
Tofazzal Ahmed
 
PDF
An Introduction to Kubernetes
Imesh Gunaratne
 
PDF
An Overview of Spinnaker
Pierre-Nicolas Durette
 
PPTX
Rule Engine & Drools
Sandip Jadhav
 
PPTX
Getting started with postgresql
botsplash.com
 
PPTX
Typescript ppt
akhilsreyas
 
PDF
What Is Helm
AMELIAOLIVIA2
 
PDF
Operating PostgreSQL at Scale with Kubernetes
Jonathan Katz
 
PDF
Aem dispatcher – tips & tricks
Ashokkumar T A
 
PDF
Introduction to Microservices
Paulo Gandra de Sousa
 
Introduction to Drools
giurca
 
Kubernetes 101 for Beginners
Oktay Esgul
 
Intro to containerization
Balint Pato
 
Coding Standards & Best Practices for iOS/C#
Asim Rais Siddiqui
 
The Right (and Wrong) Use Cases for MongoDB
MongoDB
 
Introduction To Terraform
Sasitha Iresh
 
Introduction to Galera Cluster
Codership Oy - Creators of Galera Cluster
 
Introduction to kubernetes
Raffaele Di Fazio
 
Drools expert-docs
Erick Ulisses Monfil Contreras
 
API Design, A Quick Guide to REST, SOAP, gRPC, and GraphQL, By Vahid Rahimian
Vahid Rahimian
 
REST API
Tofazzal Ahmed
 
An Introduction to Kubernetes
Imesh Gunaratne
 
An Overview of Spinnaker
Pierre-Nicolas Durette
 
Rule Engine & Drools
Sandip Jadhav
 
Getting started with postgresql
botsplash.com
 
Typescript ppt
akhilsreyas
 
What Is Helm
AMELIAOLIVIA2
 
Operating PostgreSQL at Scale with Kubernetes
Jonathan Katz
 
Aem dispatcher – tips & tricks
Ashokkumar T A
 
Introduction to Microservices
Paulo Gandra de Sousa
 

Viewers also liked (20)

PDF
Drools 6.0 (Red Hat Summit)
Mark Proctor
 
PDF
Drools and jBPM 6 Overview
Mark Proctor
 
PPT
Rules With Drools
Xebia IT Architects
 
PPTX
Jboss drools 4 scope - benefits, shortfalls
Zoran Hristov
 
PPTX
Rule Engine Evaluation for Complex Event Processing
Chandra Divi
 
PDF
Knowledge is Power: Visualizing JIRA's Performance Data
Atlassian
 
PPTX
Business Rules en CRM - Aquima
ExploreDynCRM
 
PPTX
Governança SOA
Vinicius Santos
 
ODP
BRM 2012 (Decision Tables)
Michael Anstis
 
PPTX
Rules engine
Stanimir Todorov
 
ODP
Open source and business rules
Geoffrey De Smet
 
PDF
FOSS in the Enterprise
Crishantha Nanayakkara
 
PDF
Drools & jBPM Workshop London 2013
Mauricio (Salaboy) Salatino
 
PPTX
Apache Beam (incubating)
Apache Apex
 
ODP
Drools BeJUG 2010
Geoffrey De Smet
 
PDF
Drools5 Community Training Module 5 Drools BLIP Architectural Overview + Demos
Mauricio (Salaboy) Salatino
 
PDF
The Next Generation of Data Processing and Open Source
DataWorks Summit/Hadoop Summit
 
ODP
Drools & jBPM Info Sheet
Mark Proctor
 
PDF
Intro to Drools - St Louis Gateway JUG
Ray Ploski
 
Drools 6.0 (Red Hat Summit)
Mark Proctor
 
Drools and jBPM 6 Overview
Mark Proctor
 
Rules With Drools
Xebia IT Architects
 
Jboss drools 4 scope - benefits, shortfalls
Zoran Hristov
 
Rule Engine Evaluation for Complex Event Processing
Chandra Divi
 
Knowledge is Power: Visualizing JIRA's Performance Data
Atlassian
 
Business Rules en CRM - Aquima
ExploreDynCRM
 
Governança SOA
Vinicius Santos
 
BRM 2012 (Decision Tables)
Michael Anstis
 
Rules engine
Stanimir Todorov
 
Open source and business rules
Geoffrey De Smet
 
FOSS in the Enterprise
Crishantha Nanayakkara
 
Drools & jBPM Workshop London 2013
Mauricio (Salaboy) Salatino
 
Apache Beam (incubating)
Apache Apex
 
Drools BeJUG 2010
Geoffrey De Smet
 
Drools5 Community Training Module 5 Drools BLIP Architectural Overview + Demos
Mauricio (Salaboy) Salatino
 
The Next Generation of Data Processing and Open Source
DataWorks Summit/Hadoop Summit
 
Drools & jBPM Info Sheet
Mark Proctor
 
Intro to Drools - St Louis Gateway JUG
Ray Ploski
 
Ad

Similar to Rules Programming tutorial (20)

PPT
Droolsand Rule Based Systems 2008 Srping
Srinath Perera
 
PDF
Drools Introduction
JBug Italy
 
PDF
JBoss Drools - Open-Source Business Logic Platform
elliando dias
 
PPTX
Jboss drools 3 key drools functionalities
Zoran Hristov
 
ODP
Buenos Aires Drools Expert Presentation
Mark Proctor
 
PDF
Drools JBoss Rules 5 0 developer s guide develop rules based business logic u...
eterdavud
 
PPT
Flex 360 Rules Engine
Effective
 
PPT
Flex 360 Rules Engine
EffectiveUI
 
PPT
Obey The Rules: Implementing a Rules Engine in Flex
RJ Owen
 
ODP
JBoss Drools and Drools Fusion (CEP): Making Business Rules react to RTE
tsurdilovic
 
PPT
Expert Systems & Prolog
Fatih Karatana
 
PDF
Model-based programming and AI-assisted software development
Eficode
 
ODP
2011-03-29 London - drools
Geoffrey De Smet
 
PPTX
JEEConf JBoss Drools
Victor_Cr
 
ODP
Hybrid rule engines (rulesfest 2010)
Geoffrey De Smet
 
PPT
Introduction to Rule-based Applications
giurca
 
ODP
JBoss World 2011 - Drools
Geoffrey De Smet
 
PDF
Guvnor presentation jervis liu
jbossug
 
PPTX
Drools
Allan Huang
 
KEY
20110516_ria_ENC
riamaehb
 
Droolsand Rule Based Systems 2008 Srping
Srinath Perera
 
Drools Introduction
JBug Italy
 
JBoss Drools - Open-Source Business Logic Platform
elliando dias
 
Jboss drools 3 key drools functionalities
Zoran Hristov
 
Buenos Aires Drools Expert Presentation
Mark Proctor
 
Drools JBoss Rules 5 0 developer s guide develop rules based business logic u...
eterdavud
 
Flex 360 Rules Engine
Effective
 
Flex 360 Rules Engine
EffectiveUI
 
Obey The Rules: Implementing a Rules Engine in Flex
RJ Owen
 
JBoss Drools and Drools Fusion (CEP): Making Business Rules react to RTE
tsurdilovic
 
Expert Systems & Prolog
Fatih Karatana
 
Model-based programming and AI-assisted software development
Eficode
 
2011-03-29 London - drools
Geoffrey De Smet
 
JEEConf JBoss Drools
Victor_Cr
 
Hybrid rule engines (rulesfest 2010)
Geoffrey De Smet
 
Introduction to Rule-based Applications
giurca
 
JBoss World 2011 - Drools
Geoffrey De Smet
 
Guvnor presentation jervis liu
jbossug
 
Drools
Allan Huang
 
20110516_ria_ENC
riamaehb
 
Ad

More from Srinath Perera (20)

PDF
Book: Software Architecture and Decision-Making
Srinath Perera
 
PDF
Data science Applications in the Enterprise
Srinath Perera
 
PDF
An Introduction to APIs
Srinath Perera
 
PDF
An Introduction to Blockchain for Finance Professionals
Srinath Perera
 
PDF
AI in the Real World: Challenges, and Risks and how to handle them?
Srinath Perera
 
PDF
Healthcare + AI: Use cases & Challenges
Srinath Perera
 
PDF
How would AI shape Future Integrations?
Srinath Perera
 
PDF
The Role of Blockchain in Future Integrations
Srinath Perera
 
PDF
Future of Serverless
Srinath Perera
 
PDF
Blockchain: Where are we? Where are we going?
Srinath Perera
 
PDF
Few thoughts about Future of Blockchain
Srinath Perera
 
PDF
A Visual Canvas for Judging New Technologies
Srinath Perera
 
PDF
Privacy in Bigdata Era
Srinath Perera
 
PDF
Blockchain, Impact, Challenges, and Risks
Srinath Perera
 
PPTX
Today's Technology and Emerging Technology Landscape
Srinath Perera
 
PDF
An Emerging Technologies Timeline
Srinath Perera
 
PDF
The Rise of Streaming SQL and Evolution of Streaming Applications
Srinath Perera
 
PDF
Analytics and AI: The Good, the Bad and the Ugly
Srinath Perera
 
PDF
Transforming a Business Through Analytics
Srinath Perera
 
PDF
SoC Keynote:The State of the Art in Integration Technology
Srinath Perera
 
Book: Software Architecture and Decision-Making
Srinath Perera
 
Data science Applications in the Enterprise
Srinath Perera
 
An Introduction to APIs
Srinath Perera
 
An Introduction to Blockchain for Finance Professionals
Srinath Perera
 
AI in the Real World: Challenges, and Risks and how to handle them?
Srinath Perera
 
Healthcare + AI: Use cases & Challenges
Srinath Perera
 
How would AI shape Future Integrations?
Srinath Perera
 
The Role of Blockchain in Future Integrations
Srinath Perera
 
Future of Serverless
Srinath Perera
 
Blockchain: Where are we? Where are we going?
Srinath Perera
 
Few thoughts about Future of Blockchain
Srinath Perera
 
A Visual Canvas for Judging New Technologies
Srinath Perera
 
Privacy in Bigdata Era
Srinath Perera
 
Blockchain, Impact, Challenges, and Risks
Srinath Perera
 
Today's Technology and Emerging Technology Landscape
Srinath Perera
 
An Emerging Technologies Timeline
Srinath Perera
 
The Rise of Streaming SQL and Evolution of Streaming Applications
Srinath Perera
 
Analytics and AI: The Good, the Bad and the Ugly
Srinath Perera
 
Transforming a Business Through Analytics
Srinath Perera
 
SoC Keynote:The State of the Art in Integration Technology
Srinath Perera
 

Rules Programming tutorial

  • 1. Programming With Rules Srinath Perera, Ph.D. Architect, WSO2 Inc.
  • 2. Rule based Systems ● Expert Systems / Business rules engine / Production Systems / Inference Engines are used to address rule engines based on their implementations and use. ● Something most people like to have, although not many people know why they need it.
  • 3. Rules ● Allow users to specify the requirements/ knowledge about processing using – Declarative (Say what should happen, not how to do it) – Logic based languages.
  • 4. Types of Rules ● Four types of rules (from http://www.w3.org/2000/10/swap/doc/rule-systems) – Derivation or Deduction Rules – Each rules express if some statements are true, another statement must be true. Called logical implication. E.g. Prolog – Transformation Rules- transform between knowledge bases, e.g. theorem proving – Integrity Constraints – verification rules – Reaction or Event-Condition-Action (ECA) Rules – includes a actions in addition to inference. e.g. Drools
  • 5. Two types of Rule Engines ● Forward Chaining – starts with facts/data and trigger actions or output conclusions ● Backward chaining – starts with goals and search how to satisfy that (e.g. Prolog)
  • 6. Production Systems ● Drools belongs to the category of rule engines called production systems [1] (which execute actions based on conditions) ● Drools use forward chaining[2] (start with data and execute actions to infer more data ) ● Priorities assigned to rules are used to decide the order of rule execution ● They remember all results and use that to optimize new derivations (dynamic programming like) http://en.wikipedia.org/wiki/AI_production http://en.wikipedia.org/wiki/Forward_chaining
  • 7. Big Picture ● Usually a Rule engine usually includes three parts. – Facts represented as working memory – Set of rules that declaratively define conditions or situations e.g. Prolog route(X,Z) <- road(X,Z) – Actions executed or inference derived based on the rules
  • 8. Why rule engines? ● Simplify complicated requirements with declarative logic, raising the level of abstraction of the system ● Externalize the business logic (which are too dynamic) from comparatively static code base ● Intuitive and readable than code, easily understood by business people/ non technical users http://en.wikipedia.org/wiki/AI_production http://en.wikipedia.org/wiki/Forward_chaining
  • 9. Why rule engines? [Contd.] ● Create complex interactions which can have powerful results, even from simple facts and rules. ● Different approach to the problem, some problem are much easier using rules. ● Can solve hard problems (Problems we only understand partially) ● Fast and scalable when there is a data set that changes little by little
  • 10. When to use Rules? ● When there is no satisfactory traditional programming approach to solve the problem! ● To separate code and business logic ● The problem is beyond any obvious algorithmic solution. (not fully understood) ● When the logic changes often ● When Domain experts are none technical. 1. Real-World Rule Engines http://www.infoq.com/articles/Rule-Engines 2. Why are business rules better than traditional code? http://www.edmblog.com/weblog/2005/11/why_are_busines.html 3. Rules-based Programming with JBoss Rules/Drools www.codeodor.com
  • 11. When not to use rule engines? ● It is slower then usual code most of the time, so unless one of the following is true is should not be used – Complexity of logic is hard to tackle – Logic changes too often – Required to use by non technical users – It is a usecase where rules are faster. ● Interactions between rules could be quite complex, and one mistake could change the results drastically and unexpected way e.g. recursive rules ● Due to above testing and debugging is required, so if results are hard to verified it should not be used.
  • 12. Drools Rule Engine ● Forward chaining, production system based on Rete algorithm. ● Written in Java, and support OOP based model ● Open source/ Apache License compatible ● Backed up JBoss, also called JBoss rules ● Link http://jboss.org/drools
  • 13. Model ● Facts as a Object repository of java objects ● New objects can be added, removed or updated ● support if <query> then <action> type rules ● Queries use OOP format
  • 15. Anatomy of a Rule ● Has two parts rule "rule-name" when <query in a Object query language> then <any java code> end ● When condition is satisfied, action is carried out.
  • 16. Business Rules ● Working Memory – Insert, retract, update ● Stateful sessions ● Stateless sessions ● Queries - QueryResults results = ksession.getQueryResults( "my query", new Object[] { "string" } ); ● Globals ksession.setGlobal("list", list); ● Agenda filters makes sure only selected rules are fired. ksession.fireAllRules( new RuleNameEndsWithAgendaFilter( "Test" ) );
  • 17. Rete Algorithm ● Dr Charles L. Forgy's Ph.D. Thesis, 1974 ● Creates a tree representing rules, and data propagate through the tree and out put actions. ● Partial results are saved in memory.
  • 18. A Sample Rule Following rule increase the premium by 15% if driver is less than 25 and drives a sport car. rule "MinimumAge" when $d : Customer(age < 25) $c : Car(vin=$d.vin && model = “sport”) then c.IncreasePrimium(15); end
  • 19. Actions ● Any java code goes in the then clause ● Also support some level of scripts ● Operations on working memory – update(object); will tell the engine that an object has changed – insert(new Something()); will place a new object of your creation into the Working Memory. – insertLogical(new Something()); is similar to insert, but the object will be automatically retracted when there are no more facts to support the truth of the currently firing rule. – retract(handle); removes an object from Working Memory.
  • 20. Rule Patterns Following rule reject all customers whose age less than 17. rule "MinimumAge" when c : Customer(age < 17) then c.reject(); end Conditions support <, >, ==, <=, >=, matches / not matches, contains / not contains. And following rules provide a discount if customer is married or older than 25. rule "Discount" when c : Customer( married == true || age > 25) then c.addDiscount(10); end
  • 21. Joins Following rule increase the premium by 15% if driver is less than 25 and drives a sport car. rule "MinimumAge" when $d : Customer(age < 25) $c : Car(vin=$d.vin && model = “sport”) then c.IncreasePrimium(15); end
  • 22. Bound variables Bound variables when Person( likes : favouriteCheese ) Cheese( type == likes ) then … end
  • 23. OR, AND ● OR – true if either of the statements true – E.g. Customer(age > 50) or Vehicle( year > 2000) ● AND – provide logical, if no connectivity is define between two statements, “and” is assumed by default. For an example. c : Customer( timeSinceJoin > 2); not (Accident(customerid == c.name)) and c : Customer( timeSinceJoin > 2) and not (Accident(customerid == c.name)) are the same.
  • 24. eval(..) ● eval(boolean expressions) – with eval(..) any Boolean expression can be used. – E.g. C:Customer(age > 20) eval(C.calacuatePremium() > 1000) ● Can not hold partial results for eval(..) so Drools going to recalculate eval(..) statements and what ever trigged from that. ● So it is going to be inefficient, so use sparingly
  • 25. Not ● Not – negation or none can be found. E.g. not Plan( type = “home”) is true if no plan of type home is found. Following is true if customer has take part in no accidents. rule "NoAccident" when c : Customer( timeSinceJoin > 2); not (Accident(customerid == c.name)) then c.addDiscount(10); end
  • 26. For all ● True if all objects selected by first part of the query satisfies rest of the conditions. For an example following rule give 25 discount to customers who has brought every type of plans offered. rule "OtherPlans" when forall ($plan : PlanCategory() c : Customer(plans contains $plan)) then c.addDiscount(25); end
  • 27. Exists ● True if at least one matches the query, ● This is Different for just having Customer(), which is like for each which get invoked for each matching set. ● Following rule give a discount for each family where two members having plans rule “FamilyMembers" when $c : Customer() exists (Customer( name contains $c.family)) then c.addDiscount(5); end
  • 28. From ● True if at least one matches the query, ● This is Different for just having Customer(), which is like for each which get invoked for each matching set. ● Following rule give a discount for each family where two members having plans rule "validate zipcode" when Person( $personAddress : address ) Address( zipcode == "23920W") from $personAddress then # zip code is ok end
  • 29. Collect ● True if at least one matches the query, ● This is Different for just having Customer(), which is like for each which get invoked for each matching set. ● Following rule give a discount for each family where two members having plans rule "Raise priority if system has more than 3 pending alarms" when $system : System() $alarms : ArrayList( size >= 3 ) from collect( Alarm( system == $system, status == 'pending' ) ) then # Raise priority, because system $system has # 3 or more alarms pending. The pending alarms # are $alarms. end
  • 30. Accumulate ● True if at least one matches the query, ● This is Different for just having Customer(), which is like for each which get invoked for each matching set. ● Following rule give a discount for each family where two members having plans rule "Apply 10% discount to orders over US$ 100,00" when $order : Order() $total : Number( doubleValue > 100 ) from accumulate( OrderItem( order == $order, $value : value ), init( double total = 0; ), action( total += $value; ), reverse( total -= $value; ), result( total ) ) then # apply discount to $order end
  • 31. Conflict resolution • Each rule may define attributes There are other parameters you can found from [1]. E.g. rule "MinimumAge" salience = 10 when c : Customer(age < 17) then c.reject(); end • salience define priority of the rule and decide their activation order. 1. http://labs.jboss.com/drools/documentation.html
  • 32. WSO2 Deployment as an Example ● There are our product Instances (e.g. WSAS, Greg, IS, ESB) etc, which has been mapped to a facts in the rule system, and it is updated using a system management solution. ● They have a role property, that says what is there role. e.g. ESB role=”lb” means it is a load balancer. ● Each host is represented by Host() object, and each server have a property called host. ● Each server has a state, which is Up, Down
  • 33. Question 1: ● A system is considered healthy if there is One ESB, two WSAS instances, one load balance instance, one BPS server, and one Identity server. ● Write a rule to detect if system is healthy and print it. When ESB(role !=”lb”); List(size ==2) from collect (WSAS()); ESB(role==”lb”); BPS(); IS(); then System.out.println(“System Healthy”); end
  • 34. Question 2: ● If any server has failed but the host is up, restart the server using a rule. Assume there is system.restart(service-name, host). When wsas:WSAS(state=”Down”); host:Host(state=”Up” && wsas.host=name); then system.restart(wsas); end
  • 35. Question 3: ● If a host has failed, start the servers in the host using a rule. Assume there is system.start(service-name, host). When wsas:WSAS(); host:Host(state=”Down” && wsas.host=name); then system.start(wsas); end
  • 36. Question 4: ● Assume there are cluster of 10 WSAS nodes, if the average TPS is > 300, add a new node to the cluster. Assume WASA expose TPS via a property called tps. rule "Apply 10% discount to orders over US$ 100,00" when $order : Order() $total : Number( doubleValue > 300) from accumulate( WSAS( $tps : tps), init( double tpstot = 0; int count = 0; ), action( tpstot += $tps; count++ ), reverse( tpstot -= $tps; count-- ), result( tpstot/count ) ) then # apply discount to $order end
  • 37. Drools Performance • Measuring Rule engine performance is tricky. • Main factors are number of objects and number of rules. But results depends on nature of rules. • A user feedback [1] claims Drools about 4 times faster than JRules [4]. (SequentialRete) rules Objects Drools JRules 1219 100 4ms/4ms 16ms/15ms • [2] shows a comparison between Drools, Jess [5] and Microsoft rule engine. Overall they are comparable in performance. 1. http://blog.athico.com/2007/08/drools-vs-jrules-performance-and-future.html 2. http://geekswithblogs.net/cyoung/articles/54022.aspx 1. Jess - http://herzberg.ca.sandia.gov/jess/ 2. JRules http://www.ilog.com/products/jrules/
  • 38. Drools Performance Contd. I have ran the well known rule engine bench mark [1] implementation provided with Drools. (On linbox3 - 1GB memory, 4 CPU • 3.20GHz ) Bench Marks Rule Count Object Time (ms) Count Waltz 31 958 1582 31 3873 9030 Waltz DB 34 393 420 34 697 956 34 1001 1661 34 1305 2642 http://www.cs.utexas.edu/ftp/pub/ops5-benchmark-suite/HOW.TO.USE 1.