rules programming tutorial
Post on 02-Nov-2014
20.150 Views
Preview:
DESCRIPTION
TRANSCRIPT
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_productionhttp://en.wikipedia.org/wiki/Forward_chaining
● 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
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
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 -
● 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" } );
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))
andc : 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 plansrule "validate zipcode"when Person( $personAddress : address ) Address( zipcode == "23920W") from $personAddress then # zip code is okend
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 $orderend
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
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.
WhenESB(role !=”lb”);List(size ==2) from collect (WSAS());ESB(role==”lb”);BPS();IS();
thenSystem.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).
Whenwsas:WSAS(state=”Down”);host:Host(state=”Up” && wsas.host=name);
thensystem.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).
Whenwsas: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 $orderend
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
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
Questions?
top related