lino vázquez – pjas at cern gs-ais-hr. declarative programming rules rules engines rete...
TRANSCRIPT
Introduction to Rules Engines& Drools
Lino Vázquez – PJAS at CERNGS-AIS-HR
Declarative programmingRulesRules EnginesRete algorithmWhy use rules enginesDrools
Agenda
Declarative programming
Decribe the “what” instead of the “how”
Define the problem, give some conditions and let the system do the rest
Declarative programming
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”
Declarative programming
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
Declarative programming
◦ If low level of batteryGet a charger
◦ If need a chargerGet it from the desk
◦ If need something from the deskGo to the bedroom
Declarative programming
What is a rule?
Drools 6.0 user’s manual:
A Production Rule is a two-part structure to infer conclusions which result in actions.
What is a rule?
A rule is an If – Then where the “then” part is an action over knowledge
What is a rule?
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
What is a rule?
What is a rule engine?
Pattern matcherSystem that matches facts against rules
What is a rule engine?
The matching is performed by the “Inference engine” using an algothim (Rete)
What is a rule engine?
Forward & Backward chaining
Forward chainning – Data driven
Backward chainning – Goal driven
Forward & Backward chaining
Rules enginesArchitecture
Rule base
Inference engine
Working memory
Architecture
ProductionMemory
(Rules)
WorkingMemory
(Facts)
Inference Engine
PatternMatcher
Agenda
Architecture
Rule engine flow
Exit
Agenda evaluation
Select rule
Fire rule
Add
Delete
Edit
Activate possible rules
No rules
Inject facts
WorkingMemory
(Facts)
Inference Engine(Rete / Leaps)
PatternMatcher
ProductionMemory
(Rules)
Agenda
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
FactsFacts
*
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 5
Rule 6
Rule 7
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Rule 6
Rule 7
FactsFacts
*Rule 4
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 5
Rule 6
Rule 7
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Rule 6
Rule 7
Facts Rule 4
Agenda
WorkingMemory
(Facts)
Inference Engine(Rete / Leaps)
PatternMatcher
ProductionMemory
(Rules)
Agenda
Pattern matcher
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)
Pattern matcher
Person (age >= 18, age < 65)
Pattern matcher
Pattern
Type Constrain
Field Restriction
Evaluator Value
Rete algorithm
Since 1975
Independent of the number or rules
Heavy memory use
Rete algorithm
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
Rete algorithm
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
Why use a rule engine?
When to use a rule engine?
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
When to use a rule engine?
Born in 2001
Rete algorithm
Bought by Red Hat in 2005
Last stable version 6.1
Drools
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)
Drools syntax
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
Drools syntax
Features
when $p : Person (isAdult == true)then logicalInsert(new IdCard($p,‘Adult’); end
If a person is an adult, we issue an adult ID card
| Features
Truth Maintenanc
e
Conditional Elements
◦ not Person( age >=18)
◦ exists Person( age >=18)
◦ forall (Person( age >=18))
| Features
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()
| Features
Keyword ‘collect’
when $list : List( intValue > 100)
from collect (Person(age >= 18))then
print “People“ + $list;end
| Features
Keyword ‘accumulate’
when $sum : Number( intValue > 100)
from accumulate (Person(age >= 18, $s : savings ) sum
($s))then
print “Total savings “ + $sum;end
| Features
Timers & Calendars◦ Delays and Intervals◦ Cron
Time operators◦ before, after, coincides, overlaps, during…
Time windows+aggregations (Keyword ‘over’)
| Features
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
| Features
Drools at CERN
Article II 1.01Retirement AgeThe 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.
Drools at CERN
Rule “Retirement age” when
$m : Member()JoinedFund(member == $m,
date <= “31 –Dec-2011”)then
insert(new RetirementAge($m, 65)
Drools at CERN
Thank you