Download presentation
Presentation is loading. Please wait.
Published byFerdinand McGee Modified over 9 years ago
1
Lino Vázquez – PJAS at CERN GS-AIS-HR
2
Declarative programming Rules Rules Engines Rete algorithm Why use rules engines Drools
4
Decribe the “what” instead of the “how” Define the problem, give some conditions and let the system do the rest
5
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”
6
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
7
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
9
Drools 6.0 user’s manual: A Production Rule is a two-part structure to infer conclusions which result in actions.
10
A rule is an If – Then where the “then” part is an action over knowledge
11
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
13
Pattern matcher System that matches facts against rules
14
The matching is performed by the “Inference engine” using an algothim (Rete)
16
Forward chainning – Data driven Backward chainning – Goal driven
18
Rule base Inference engine Working memory
19
Production Memory (Rules) Working Memory (Facts) Inference Engine Pattern Matcher Agenda
20
Exit Agenda evaluation Select rule Fire rule Add Delete Edit Activate possible rules No rules Inject facts
21
Working Memory (Facts) Inference Engine (Rete / Leaps) Pattern Matcher Production Memory (Rules) Agenda
22
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*
23
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
24
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
25
Working Memory (Facts) Inference Engine (Rete / Leaps) Pattern Matcher Production Memory (Rules) Agenda
26
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)
27
Person (age >= 18, age < 65) Pattern TypeConstrain FieldRestriction Evaluator Value
29
Since 1975 Independent of the number or rules Heavy memory use
30
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
31
New Costumer More than 100 Birthday Rule 1* Rule 2 Rule 1 New Costumer More than 100
32
New Costumer More than 100 Birthday Rule 1* Rule 2 Rule 1
33
New Costumer More than 100 Birthday Rule 1* Rule 2 Rule 1
36
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
38
Born in 2001 Rete algorithm Bought by Red Hat in 2005 Last stable version 6.1
39
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)
40
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
42
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
43
Conditional Elements ◦ not Person( age >=18) ◦ exists Person( age >=18) ◦ forall (Person( age >=18))
44
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()
45
Keyword ‘collect’ when $list : List( intValue > 100) from collect (Person(age >= 18)) then print “People“ + $list; end
46
Keyword ‘accumulate’ when $sum : Number( intValue > 100) from accumulate (Person(age >= 18, $s : savings ) sum ($s)) then print “Total savings “ + $sum; end
47
Timers & Calendars ◦ Delays and Intervals ◦ Cron Time operators ◦ before, after, coincides, overlaps, during… Time windows+aggregations (Keyword ‘over’)
48
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
50
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.
51
Rule “Retirement age” when $m : Member() JoinedFund(member == $m, date <= “31 –Dec-2011”) then insert(new RetirementAge($m, 65)
52
Thank you
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.