This document provides an overview of rule-based programming and the Drools rule engine. It discusses how rule engines allow users to specify requirements and logic declaratively using rules. Drools is introduced as a popular open-source rule engine that uses the Rete algorithm for efficient forward-chaining rule execution. The document explains key concepts like the anatomy of rules, different rule types, and how Drools represents facts and rules. It also discusses when rule engines are suitable and compares their performance to other approaches.
In this document
Powered by AI
Introduction to programming with rules by Srinath Perera, Ph.D., Architect at WSO2 Inc.
Overview of rule-based systems, including expert systems and business rules.
Rules allow users to specify requirements using declarative logic and logic-based languages.
Four types of rules: derivation, transformation, integrity constraints, and event-condition-action.
Distinguishes between forward chaining and backward chaining in rule execution.
Overview of production systems and the functionality of Drools, emphasizing forward chaining.
Describes the three parts of a rule engine: facts, rules, and actions.
Reasons to use rule engines, including abstraction, dynamic logic, and ease of understanding.
Indications for using rule engines, including complex logic and when domain experts are non-technical.
Discusses disadvantages of rule engines, including performance issues and testing complexity.
Introduction to Drools, its forward chaining capabilities, and its open-source nature.
Details on representing facts as Java objects and rule query formats.
Introduction to the syntax and structure of the Drools rule language.
Anatomy of a rule, including conditions and actions.
Working memory and sessions in the context of Drools, including global variables.
Explanation of the Rete algorithm and its functionality related to rule processing.
An example of a rule that adjusts insurance premium based on age and car type.
Discusses the actions that can be executed in rule conditions, including Java code.
Examples of using condition statements like rejects based on customer age.
Demonstrates the use of bound variables in rule conditions.
Explains the use of logical operators OR and AND in rule conditions.
Discussing eval for boolean expressions and negation with 'not' in rules.
Describes how to use 'forall' and 'exists' to impose conditions on rule execution.
Demonstrates how to use the 'from' clause to relate different object conditions.
Explains how to collect data and utilize it in rules, including conditions on size.
Discusses using accumulate for operations over collections in rule conditions.
Describes conflict resolution in Drools including salience for rule priority.
Example of how rule systems map facts to product instances in WSO2 deployment.
Creating a rule to determine system health based on specific component instances.
Rule to restart a server based on its state and host status.
A rule to start servers in a host based on the host's state.
Creating a rule to add a new WSAS node based on average TPS.
Evaluating Drools performance in comparison to other rule engines.
Results from benchmarks highlighting Drools performance across various test scenarios.
Open session for questions about the presentation and content discussed.
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 ofRule 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 useRules? ● 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 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 – 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
Anatomy of aRule ● 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 Followingrule 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 rulereject 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 increasethe 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
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 asan 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. Ihave 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.