Programming With Rules: Srinath Perera, Ph.D. Architect, WSO2 Inc
Programming With Rules: Srinath Perera, Ph.D. Architect, WSO2 Inc
http://en.wikipedia.org/wiki/AI_production
http://en.wikipedia.org/wiki/Forward_chaining
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
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
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
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))
and
c : 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”)
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 plans
rule "validate zipcode"
when
Person( $personAddress : address )
Address( zipcode == "23920W") from $personAddress
then
# zip code is ok
end
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
When
wsas:WSAS(state=”Down”);
host:Host(state=”Up” && wsas.host=name);
then
system.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).
When
wsas: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.
(Sequential\Rete)
• [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/
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 )
34 697 956
34 1001 1661
34 1305 2642
http://www.cs.utexas.edu/ftp/pub/ops5-benchmark-suite/HOW.TO.USE
1.
Questions?