Drools and Rule Based Systems Srinath Perera
Rule Engine Terms Expert Systems / Business rules engine / Production Systems / Inference Engines are used to address rule engines based on their implementations.  Usually a Rule engine usually includes three parts.  Facts represented as working memory or another set of rules e.g. Prolog  road(a,b) or Drools objects 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
Rules Allow users to specify the requirements declarative, using a logic based languages. (Say what should happen, not how to do it). Rules may trigger other 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. therom proving Integrity Constraints – verification rules  Reaction or Event-Condition-Action (ECA) Rules – includes a actions in addition to inference.  e.g. Drools
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
Why rule engines? ~[1],[2][3] 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 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. Ability to specify explicit time and dates for rules to take effect Real-World Rule Engines  http://www.infoq.com/articles/Rule-Engines   Why are business rules better than traditional code?  http://www.edmblog.com/weblog/2005/11/why_are_busines.html   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 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 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  Support  not, or, and, forall  and  exists  completing first order logic
Patterns Have a OOP based intuitive rule format. We presents examples using a insurance quota example.  Following rule reject all customers whose age less than 17.  rule &quot;MinimumAge&quot; 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 &quot;Discount&quot; when     c : Customer( married == true || age > 25) then     c.addDiscount(10); end
OR, AND, eval() 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(boolean expressions) – with eval(..) any Boolean expression can be used.  E.g.  C:Customer(age > 20) eval(C.calacuatePremium() > 1000)
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 &quot;NoAccident&quot; 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 &quot;OtherPlans&quot; 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&quot; when   $c : Customer()      exists (Customer( name contains $c.family)) then     c.addDiscount(5); end
Conflict resolution Each rule may define attributes There are other parameters you can found from [1]. E.g.  rule &quot;MinimumAge&quot; salience  = 10 when     c : Customer(age < 17) then     c.reject(); end salience define priority of the rule and decide their activation order.  http://labs.jboss.com/drools/documentation.html
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].  [2] shows a comparison between Drools, Jess [5] and Microsoft rule engine. Overall they are comparable in performance.   http://blog.athico.com/2007/08/drools-vs-jrules-performance-and-future.html http://geekswithblogs.net/cyoung/articles/54022.aspx   Jess -  http://herzberg.ca.sandia.gov/jess/ JRules  http://www.ilog.com/products/jrules/   (Sequential\Rete) 16ms/15ms 4ms/4ms 100 1219 JRules Drools Objects rules
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  ) http://www.cs.utexas.edu/ftp/pub/ops5-benchmark-suite/HOW.TO.USE  2642 1305 34 1661 1001 34 956 697 34 420 393 34 Waltz DB 9030 3873 31 1582 958 31 Waltz Time (ms) Object  Count Rule Count Bench Marks
Data Mining Use Case
Rule based Solution We represent Queries as Objects that include bounds and list of selected data products  We represent Data products as Objects that include location and time it was collected.  Then following two rules will solve the problem Rule 1. For each data item, if it match spatial and temporal boundaries, add it to data collected for query Rule 2. When temporal end time is passed, invoke the data mining workflow with collected data
 
Concrete Rules RULE 1. For each data item, if it match spatial and temporal boundaries of a Query, add it to data collected for query  when       q: Query(completed = false);       d: Data( x > q.minX && x < q.maxX  && y > q.minY && y < q.maxY  && timeStamp > q.start && timestamp < q.end)  then       q.addDataProduct(d);  end  RULE 2. When temporal end time is passed, invoke the data mining workflow with collected data when       system:System()       q: Query(completed = false, end < system.currentTime);  then       q.completed = true;       q.runDataMiningAndInvokeWorkflow();  end
Conclusion Drools provide a OOP based intuitive rule language based on Rete (which is state of art public algorithm) It has good  performance, comparable with Jess (which I not free).  It is Open source, has a healthy and active community and JBoss cooperation backing it Extensively used in business rule community

Droolsand Rule Based Systems 2008 Srping

  • 1.
    Drools and RuleBased Systems Srinath Perera
  • 2.
    Rule Engine TermsExpert Systems / Business rules engine / Production Systems / Inference Engines are used to address rule engines based on their implementations. Usually a Rule engine usually includes three parts. Facts represented as working memory or another set of rules e.g. Prolog  road(a,b) or Drools objects 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
  • 3.
    Rules Allow usersto specify the requirements declarative, using a logic based languages. (Say what should happen, not how to do it). Rules may trigger other 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. therom proving Integrity Constraints – verification rules Reaction or Event-Condition-Action (ECA) Rules – includes a actions in addition to inference. e.g. Drools
  • 4.
    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
  • 5.
    Why rule engines?~[1],[2][3] 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 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. Ability to specify explicit time and dates for rules to take effect Real-World Rule Engines http://www.infoq.com/articles/Rule-Engines Why are business rules better than traditional code? http://www.edmblog.com/weblog/2005/11/why_are_busines.html Rules-based Programming with JBoss Rules/Drools www.codeodor.com
  • 6.
    When not touse 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 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.
  • 7.
    Drools Facts asa Object repository of java objects New objects can be added, removed or updated support if <query> then <action> type rules Queries use OOP format Support not, or, and, forall and exists completing first order logic
  • 8.
    Patterns Have aOOP based intuitive rule format. We presents examples using a insurance quota example. Following rule reject all customers whose age less than 17. rule &quot;MinimumAge&quot; 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 &quot;Discount&quot; when     c : Customer( married == true || age > 25) then     c.addDiscount(10); end
  • 9.
    OR, AND, eval()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(boolean expressions) – with eval(..) any Boolean expression can be used. E.g. C:Customer(age > 20) eval(C.calacuatePremium() > 1000)
  • 10.
    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 &quot;NoAccident&quot; when     c : Customer( timeSinceJoin > 2);     not (Accident(customerid == c.name)) then     c.addDiscount(10); end
  • 11.
    For all Trueif 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 &quot;OtherPlans&quot; when     forall ($plan : PlanCategory() c : Customer(plans contains $plan)) then     c.addDiscount(25); end
  • 12.
    Exists True ifat 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&quot; when $c : Customer()     exists (Customer( name contains $c.family)) then     c.addDiscount(5); end
  • 13.
    Conflict resolution Eachrule may define attributes There are other parameters you can found from [1]. E.g. rule &quot;MinimumAge&quot; salience = 10 when     c : Customer(age < 17) then     c.reject(); end salience define priority of the rule and decide their activation order. http://labs.jboss.com/drools/documentation.html
  • 14.
    Drools Performance MeasuringRule 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]. [2] shows a comparison between Drools, Jess [5] and Microsoft rule engine. Overall they are comparable in performance. http://blog.athico.com/2007/08/drools-vs-jrules-performance-and-future.html http://geekswithblogs.net/cyoung/articles/54022.aspx Jess - http://herzberg.ca.sandia.gov/jess/ JRules http://www.ilog.com/products/jrules/ (Sequential\Rete) 16ms/15ms 4ms/4ms 100 1219 JRules Drools Objects rules
  • 15.
    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 ) http://www.cs.utexas.edu/ftp/pub/ops5-benchmark-suite/HOW.TO.USE 2642 1305 34 1661 1001 34 956 697 34 420 393 34 Waltz DB 9030 3873 31 1582 958 31 Waltz Time (ms) Object Count Rule Count Bench Marks
  • 16.
  • 17.
    Rule based SolutionWe represent Queries as Objects that include bounds and list of selected data products We represent Data products as Objects that include location and time it was collected. Then following two rules will solve the problem Rule 1. For each data item, if it match spatial and temporal boundaries, add it to data collected for query Rule 2. When temporal end time is passed, invoke the data mining workflow with collected data
  • 18.
  • 19.
    Concrete Rules RULE1. For each data item, if it match spatial and temporal boundaries of a Query, add it to data collected for query when      q: Query(completed = false);      d: Data( x > q.minX && x < q.maxX && y > q.minY && y < q.maxY && timeStamp > q.start && timestamp < q.end) then      q.addDataProduct(d); end RULE 2. When temporal end time is passed, invoke the data mining workflow with collected data when      system:System()      q: Query(completed = false, end < system.currentTime); then      q.completed = true;      q.runDataMiningAndInvokeWorkflow(); end
  • 20.
    Conclusion Drools providea OOP based intuitive rule language based on Rete (which is state of art public algorithm) It has good performance, comparable with Jess (which I not free). It is Open source, has a healthy and active community and JBoss cooperation backing it Extensively used in business rule community