Download - Rules Programming tutorial
![Page 1: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/1.jpg)
Programming With Rules
Srinath Perera, Ph.D.Architect, WSO2 Inc.
![Page 2: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/2.jpg)
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.
![Page 3: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/3.jpg)
Rules
● Allow users to specify the requirements/ knowledge about processing using– Declarative (Say what should happen, not how to
do it)
– Logic based languages.
![Page 4: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/4.jpg)
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
![Page 5: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/5.jpg)
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)
![Page 6: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/6.jpg)
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_productionhttp://en.wikipedia.org/wiki/Forward_chaining
![Page 7: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/7.jpg)
● 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
Big Picture
![Page 8: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/8.jpg)
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_productionhttp://en.wikipedia.org/wiki/Forward_chaining
![Page 9: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/9.jpg)
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
![Page 10: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/10.jpg)
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
![Page 11: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/11.jpg)
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.
![Page 12: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/12.jpg)
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
![Page 13: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/13.jpg)
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
![Page 14: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/14.jpg)
Drools Rule Language
![Page 15: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/15.jpg)
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.
![Page 16: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/16.jpg)
Business Rules● Working Memory
– Insert, retract, update
● Stateful sessions● Stateless sessions ● Queries -
● Globals ksession.setGlobal("list", list); ● Agenda filters makes sure only selected rules are
fired. ksession.fireAllRules( new RuleNameEndsWithAgendaFilter( "Test" ) );
QueryResults results = ksession.getQueryResults( "my query", new Object[] { "string" } );
![Page 17: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/17.jpg)
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.
![Page 18: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/18.jpg)
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
![Page 19: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/19.jpg)
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.
![Page 20: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/20.jpg)
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
![Page 21: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/21.jpg)
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
![Page 22: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/22.jpg)
Bound variables Bound variables
when
Person( likes : favouriteCheese ) Cheese( type == likes )
then …end
![Page 23: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/23.jpg)
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))
andc : Customer( timeSinceJoin > 2) and not (Accident(customerid == c.name))
are the same.
![Page 24: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/24.jpg)
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
![Page 25: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/25.jpg)
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
![Page 26: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/26.jpg)
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
![Page 27: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/27.jpg)
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
![Page 28: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/28.jpg)
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 plansrule "validate zipcode"when Person( $personAddress : address ) Address( zipcode == "23920W") from $personAddress then # zip code is okend
![Page 29: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/29.jpg)
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
![Page 30: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/30.jpg)
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 $orderend
![Page 31: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/31.jpg)
Conflict resolution
• Each rule may define attributes There are other parameters you can found from [1]. E.g. rule "MinimumAge"salience = 10when 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
![Page 32: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/32.jpg)
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
![Page 33: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/33.jpg)
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.
WhenESB(role !=”lb”);List(size ==2) from collect (WSAS());ESB(role==”lb”);BPS();IS();
thenSystem.out.println(“System Healthy”);
end
![Page 34: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/34.jpg)
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).
Whenwsas:WSAS(state=”Down”);host:Host(state=”Up” && wsas.host=name);
thensystem.restart(wsas);
end
![Page 35: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/35.jpg)
Question 3:
● If a host has failed, start the servers in the host using a rule. Assume there is system.start(service-name, host).
Whenwsas:WSAS();host:Host(state=”Down” && wsas.host=name);
then system.start(wsas);end
![Page 36: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/36.jpg)
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 $orderend
![Page 37: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/37.jpg)
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.
1. http://blog.athico.com/2007/08/drools-vs-jrules-performance-and-future.html2. http://geekswithblogs.net/cyoung/articles/54022.aspx 1. Jess - http://herzberg.ca.sandia.gov/jess/2. JRules http://www.ilog.com/products/jrules/
(Sequential\Rete)
rules Objects Drools JRules
1219 100 4ms/4ms 16ms/15ms
![Page 38: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/38.jpg)
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 )
1.http://www.cs.utexas.edu/ftp/pub/ops5-benchmark-suite/HOW.TO.USE
Bench Marks Rule Count Object Count
Time (ms)
Waltz 31 958 1582
31 3873 9030
Waltz DB 34 393 420
34 697 956
34 1001 1661
34 1305 2642
![Page 39: Rules Programming tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020122/5459157faf795953128b4c49/html5/thumbnails/39.jpg)
Questions?