Droolsand Rule Based Systems 2008 Srping


Published on

Presentation at IU, to the research group

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Droolsand Rule Based Systems 2008 Srping

  1. 1. Drools and Rule Based Systems Srinath Perera
  2. 2. Rule Engine <ul><li>Terms Expert Systems / Business rules engine / Production Systems / Inference Engines are used to address rule engines based on their implementations. </li></ul><ul><li>Usually a Rule engine usually includes three parts. </li></ul><ul><li>Facts represented as working memory or another set of rules e.g. Prolog  road(a,b) or Drools objects </li></ul><ul><li>Set of rules that declaratively define conditions or situations e.g. Prolog route(X,Z) <- road(X,Z) </li></ul><ul><li>Actions executed or inference derived based on the rules </li></ul>
  3. 3. Rules <ul><li>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. </li></ul><ul><li>Four types of rules (from http://www.w3.org/2000/10/swap/doc/rule-systems) </li></ul><ul><li>Derivation or Deduction Rules – Each rules express if some statements are true, another statement must be true. Called logical implication. E.g. Prolog </li></ul><ul><li>Transformation Rules- transform between knowledge bases, e.g. therom proving </li></ul><ul><li>Integrity Constraints – verification rules </li></ul><ul><li>Reaction or Event-Condition-Action (ECA) Rules – includes a actions in addition to inference. e.g. Drools </li></ul>
  4. 4. Production Systems <ul><li>Drools belongs to the category of rule engines called production systems [1] (which execute actions based on conditions) </li></ul><ul><li>Drools use forward chaining[2] (start with data and execute actions to infer more data ) </li></ul><ul><li>Priorities assigned to rules are used to decide the order of rule execution </li></ul><ul><li>They remember all results and use that to optimize new derivations (dynamic programming like) </li></ul><ul><li>http://en.wikipedia.org/wiki/AI_production </li></ul><ul><li>http://en.wikipedia.org/wiki/Forward_chaining </li></ul>
  5. 5. Why rule engines? ~[1],[2][3] <ul><li>Simplify complicated requirements with declarative logic, raising the level of abstraction of the system </li></ul><ul><li>Externalize the business logic (which are too dynamic) from comparatively static code base </li></ul><ul><li>Intuitive and readable than code, easily understood by business people/ non technical users </li></ul><ul><li>Create complex interactions which can have powerful results, even from simple facts and rules. </li></ul><ul><li>Different approach to the problem, some problem are much easier using rules. </li></ul><ul><li>Ability to specify explicit time and dates for rules to take effect </li></ul><ul><li>Real-World Rule Engines http://www.infoq.com/articles/Rule-Engines </li></ul><ul><li>Why are business rules better than traditional code? http://www.edmblog.com/weblog/2005/11/why_are_busines.html </li></ul><ul><li>Rules-based Programming with JBoss Rules/Drools www.codeodor.com </li></ul>
  6. 6. When not to use rule engines? <ul><li>It is slower then usual code most of the time, so unless one of the following is true is should not be used </li></ul><ul><ul><li>Complexity of logic is hard to tackle </li></ul></ul><ul><ul><li>Logic changes too often </li></ul></ul><ul><ul><li>Required to use by non technical users </li></ul></ul><ul><li>Interactions between rules could be quite complex, and one mistake could change the results drastically and unexpected way e.g recursive rules </li></ul><ul><li>Due to above testing and debugging is required, so if results are hard to verified it should not be used. </li></ul>
  7. 7. Drools <ul><li>Facts as a Object repository of java objects </li></ul><ul><li>New objects can be added, removed or updated </li></ul><ul><li>support if <query> then <action> type rules </li></ul><ul><li>Queries use OOP format </li></ul><ul><li>Support not, or, and, forall and exists completing first order logic </li></ul>
  8. 8. Patterns <ul><li>Have a OOP based intuitive rule format. We presents examples using a insurance quota example. </li></ul><ul><li>Following rule reject all customers whose age less than 17. </li></ul><ul><li>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. </li></ul><ul><li>rule &quot;Discount&quot; when     c : Customer( married == true || age > 25) then     c.addDiscount(10); end </li></ul>
  9. 9. OR, AND, eval() <ul><li>OR – true if either of the statements true </li></ul><ul><ul><li>E.g. Customer(age > 50) or Vehicle( year > 2000) </li></ul></ul><ul><li>AND – provide logical, if no connectivity is define between two statements, “and” is assumed by default. For an example. </li></ul><ul><ul><li>c : Customer( timeSinceJoin > 2); not (Accident(customerid == c.name)) </li></ul></ul><ul><ul><li>and </li></ul></ul><ul><ul><li>c : Customer( timeSinceJoin > 2) and     not (Accident(customerid == c.name)) </li></ul></ul><ul><ul><li>are the same. </li></ul></ul><ul><li>eval(boolean expressions) – with eval(..) any Boolean expression can be used. </li></ul><ul><ul><li>E.g. C:Customer(age > 20) </li></ul></ul><ul><ul><li>eval(C.calacuatePremium() > 1000) </li></ul></ul>
  10. 10. Not <ul><li>Not – negation or none can be found. E.g. </li></ul><ul><li>not Plan( type = “home”) </li></ul><ul><li>is true if no plan of type home is found. Following is true if customer has take part in no accidents. </li></ul><ul><li>rule &quot;NoAccident&quot; when     c : Customer( timeSinceJoin > 2);     not (Accident(customerid == c.name)) then     c.addDiscount(10); end </li></ul>
  11. 11. For all <ul><li>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. </li></ul><ul><li>rule &quot;OtherPlans&quot; when     forall ($plan : PlanCategory() c : Customer(plans contains $plan)) then     c.addDiscount(25); end </li></ul>
  12. 12. Exists <ul><li>True if at least one matches the query, </li></ul><ul><li>This is Different for just having Customer(), which is like for each which get invoked for each matching set. </li></ul><ul><li>Following rule give a discount for each family where two members having plans </li></ul>rule “FamilyMembers&quot; when $c : Customer()     exists (Customer( name contains $c.family)) then     c.addDiscount(5); end
  13. 13. Conflict resolution <ul><li>Each rule may define attributes There are other parameters you can found from [1]. E.g. </li></ul><ul><li>rule &quot;MinimumAge&quot; salience = 10 </li></ul><ul><li>when     c : Customer(age < 17) then     c.reject(); end </li></ul><ul><li>salience define priority of the rule and decide their activation order. </li></ul><ul><li>http://labs.jboss.com/drools/documentation.html </li></ul>
  14. 14. Drools Performance <ul><li>Measuring Rule engine performance is tricky. </li></ul><ul><li>Main factors are number of objects and number of rules. But results depends on nature of rules. </li></ul><ul><li>A user feedback [1] claims Drools about 4 times faster than JRules [4]. </li></ul><ul><li>[2] shows a comparison between Drools, Jess [5] and Microsoft rule engine. Overall they are comparable in performance. </li></ul><ul><li>http://blog.athico.com/2007/08/drools-vs-jrules-performance-and-future.html </li></ul><ul><li>http://geekswithblogs.net/cyoung/articles/54022.aspx </li></ul><ul><li>Jess - http://herzberg.ca.sandia.gov/jess/ </li></ul><ul><li>JRules http://www.ilog.com/products/jrules/ </li></ul>(SequentialRete) 16ms/15ms 4ms/4ms 100 1219 JRules Drools Objects rules
  15. 15. Drools Performance Contd. <ul><li>I have ran the well known rule engine bench mark [1] implementation provided with Drools. (On linbox3 - 1GB memory, 4 CPU 3.20GHz ) </li></ul><ul><li>http://www.cs.utexas.edu/ftp/pub/ops5-benchmark-suite/HOW.TO.USE </li></ul>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. 16. Data Mining Use Case
  17. 17. Rule based Solution <ul><li>We represent Queries as Objects that include bounds and list of selected data products </li></ul><ul><li>We represent Data products as Objects that include location and time it was collected. </li></ul><ul><li>Then following two rules will solve the problem </li></ul><ul><ul><li>Rule 1. For each data item, if it match spatial and temporal boundaries, add it to data collected for query </li></ul></ul><ul><ul><li>Rule 2. When temporal end time is passed, invoke the data mining workflow with collected data </li></ul></ul>
  18. 19. Concrete Rules <ul><li>RULE 1. For each data item, if it match spatial and temporal boundaries of a Query, add it to data collected for query </li></ul><ul><li>when      q: Query(completed = false);      d: Data( x > q.minX && x < q.maxX </li></ul><ul><li>&& y > q.minY && y < q.maxY </li></ul><ul><li>&& 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 </li></ul><ul><li>when      system:System()      q: Query(completed = false, end < system.currentTime); then      q.completed = true;      q.runDataMiningAndInvokeWorkflow(); end </li></ul>
  19. 20. Conclusion <ul><li>Drools provide a OOP based intuitive rule language based on Rete (which is state of art public algorithm) </li></ul><ul><li>It has good performance, comparable with Jess (which I not free). </li></ul><ul><li>It is Open source, has a healthy and active community and JBoss cooperation backing it </li></ul><ul><li>Extensively used in business rule community </li></ul>