Lino Vázquez – PJAS at CERN GS-AIS-HR
Declarative programming Rules Rules Engines Rete algorithm Why use rules engines Drools
Decribe the “what” instead of the “how” Define the problem, give some conditions and let the system do the rest
In imperative programming we provide the logic and the control in an algorithm Implicit sequence In declarative programming we provide logic and we leave the control to the system Sequence is “irrelevant”
Imperative programming 1.Check the battery level of your cellphone 2.If battery is low, go to the bedroom and get the charger from the desk
Declarative programming ◦ If low level of battery Get a charger ◦ If need a charger Get it from the desk ◦ If need something from the desk Go to the bedroom
Drools 6.0 user’s manual: A Production Rule is a two-part structure to infer conclusions which result in actions.
A rule is an If – Then where the “then” part is an action over knowledge
The condition is usually simple (Properties of a POJO) The consequence is a modification of the knowledge, adding more knowledge or ereasing knowledge. Facts fire rules and consequences work on them
Pattern matcher System that matches facts against rules
The matching is performed by the “Inference engine” using an algothim (Rete)
Forward chainning – Data driven Backward chainning – Goal driven
Rule base Inference engine Working memory
Production Memory (Rules) Working Memory (Facts) Inference Engine Pattern Matcher Agenda
Exit Agenda evaluation Select rule Fire rule Add Delete Edit Activate possible rules No rules Inject facts
Working Memory (Facts) Inference Engine (Rete / Leaps) Pattern Matcher Production Memory (Rules) Agenda
Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Facts Facts*
Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Facts Facts* Rule 4
Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 5 Rule 6 Rule 7 Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 Rule 6 Rule 7 Facts Rule 4
Working Memory (Facts) Inference Engine (Rete / Leaps) Pattern Matcher Production Memory (Rules) Agenda
If a person is 18 years old or older and less than 65, is an adult If a person is an adult, we issue an adult ID card Person (age >= 18, age < 65) Person (isAdult == true)
Person (age >= 18, age < 65) Pattern TypeConstrain FieldRestriction Evaluator Value
Since 1975 Independent of the number or rules Heavy memory use
Rule 1: If a new costumer spends more than 100€ and is his birthday, gets a 20% discount Rule 2: If a new costumer spends more than 100€, gets a 10% discount
New Costumer More than 100 Birthday Rule 1* Rule 2 Rule 1 New Costumer More than 100
New Costumer More than 100 Birthday Rule 1* Rule 2 Rule 1
New Costumer More than 100 Birthday Rule 1* Rule 2 Rule 1
Your logic involves significant decision-making You can write decision rules for your logic Your logic is complex (3 or more nested if- statements) Your logic needs to be flexible ◦ During development ◦ After development Execution speed is not your main goal You want domain experts to review/maintain the logic
Born in 2001 Rete algorithm Bought by Red Hat in 2005 Last stable version 6.1
If a person is 18 years old or older and less than 65, is an adult If a person is an adult, we issue an adult ID card Person (age >= 18, age < 65) Person (isAdult == true)
when $p : Person (age >= 18, < 65) then modify ($p){ setIsAdult(true) } end If a person is 18 years old or older and less than 65, is an adult
when $p : Person (isAdult == true) then logicalInsert(new IdCard($p,‘Adult’); end If a person is an adult, we issue an adult ID card Truth Maintenance
Conditional Elements ◦ not Person( age >=18) ◦ exists Person( age >=18) ◦ forall (Person( age >=18))
Keyword ‘from’ when $p : Person( zipcode== “1205”) Car( color == “red” ) from $p.cars when $z : ZipCode() Person() from $hbn.getQuery(“FindPeople”).setParameters([“zipCode” : $z]).list()
Keyword ‘collect’ when $list : List( intValue > 100) from collect (Person(age >= 18)) then print “People“ + $list; end
Keyword ‘accumulate’ when $sum : Number( intValue > 100) from accumulate (Person(age >= 18, $s : savings ) sum ($s)) then print “Total savings “ + $sum; end
Timers & Calendars ◦ Delays and Intervals ◦ Cron Time operators ◦ before, after, coincides, overlaps, during… Time windows+aggregations (Keyword ‘over’)
rule “High number of orders” when Number( nbOrders : intValue > 100 ) from accumulate( e: PlaceOrderEvent(session.storeId == “00001”) over window:size(12h), count(e) ) then System.err.println( "WARNING: Number of orders in the last 12 hours > 100: " + nbOrders ); end
Article II 1.01Retirement Age The retirement age shall be as follows: a) For members who joined the Fund on or before 31December 2011, 65 years; b) For members who joined the Fund on or after 1 January 2012, 67 years.
Rule “Retirement age” when $m : Member() JoinedFund(member == $m, date <= “31 –Dec-2011”) then insert(new RetirementAge($m, 65)
Thank you