Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A model for Reflection in Rule-Based Languages

Avatar for Simon Simon
October 30, 2016

A model for Reflection in Rule-Based Languages

Production systems are used to detect patterns in large sets of facts. These patterns are described by rules.

Rules frequently need to be modified to adapt to changing requirements, for instance to update the security policies encoded in the rules. The production system however needs to remain operational throughout these updates. Current systems provide no reflective language constructs to inspect rules and/or change them. Instead, updates are achieved by unloading the current ruleset and loading an updated set. This is not only a costly operation which renders the system unresponsive while the new ruleset is being loaded, it also necessitates implementing the update-logic elsewhere.

In this position-paper we introduce a meta-level to RETE; a well established pattern-matching algorithm which is used in many production systems. This meta-level embodies a reification of all the rules in the ruleset, enabling us to provide language support for reflective rules. The envisioned language constructs make it possible to write rules that (a) leverage introspection to reason about the state of business rules, and (b) adapt to changing requirements by changing business rules at runtime.

Avatar for Simon

Simon

October 30, 2016
Tweet

Other Decks in Research

Transcript

  1. Running Example IF client makes purchase
 THEN client gets one

    point extra IF client is a premium member AND makes purchase AND purchase amount > 25
 THEN client gets a discount IF client has more than 500 points
 THEN client is Premium Member 7
  2. Running Example IF client makes purchase
 THEN client gets one

    point extra 7 IF client has more than 500 points
 THEN client is Premium Member IF client is a premium member AND makes purchase AND purchase amount > 25
 THEN client gets a discount
  3. Running Example: Business Rule 1 (rule "DetectPremiumMember" 2 (bindings 3

    ((c 'Client)) 4 (where 5 ((> (points c) 500) "points-condition") 6 (then 7 (assert (create-premium-member (client-id c))))))) 8 IF client has more than 500 points
 THEN client is Premium Member
  4. 1 (rule "ProcessPurchasePremiumMember" 2 (bindings 3 ((c 'Client) 4 (pm

    'PremiumMember) 5 (p 'Purchase)) 6 (where 7 ((eq? (client-id c) (client-id p))) 8 ((> (amount p) 25)) 9 ((not (paid-for p))) 10 ((eq? (client-id c) (client-id pm)) 'condition1) 11 (then 12 (apply-discount c p 0.05)))) 9 Running Example: Business Rule IF client is a premium member AND makes purchase AND purchase amount > 25
 THEN client gets a discount
  5. 1 (rule "ProcessPurchasePremiumMember" 2 (bindings 3 ((c 'Client) 4 (pm

    'PremiumMember) 5 (p 'Purchase)) 6 (where 7 ((eq? (client-id c) (client-id p))) 8 ((> (amount p) 25)) 9 ((not (paid-for p))) 10 ((eq? (client-id c) (client-id pm)) 'condition1) 11 (then 12 (apply-discount c p 0.05)))) 9 Running Example: Business Rule IF discount was applied < 25 times in last 24 hours THEN give discount to everyone IF client is a premium member AND makes purchase AND purchase amount > 25
 THEN client gets a discount
  6. Running Example: Meta Rule 11 1 (rule "ProcessPurchasePremiumMember" 2 (bindings

    3 ((c 'Client) 4 (pm 'PremiumMember) 5 (p 'Purchase)) 6 (where 7 ((eq? (client-id c) (client-id p))) 8 ((> (amount p) 25)) 9 ((not (paid-for p))) 10 ((eq? (client-id c) (client-id pm)) 'condition1) 11 (then 12 (apply-discount c p 0.05)))) IF discount was applied < 25 times in last 24 hours THEN give discount to everyone
  7. Running Example: Meta Rule 11 1 (rule "ProcessPurchasePremiumMember" 2 (bindings

    3 ((c 'Client) 4 (pm 'PremiumMember) 5 (p 'Purchase)) 6 (where 7 ((eq? (client-id c) (client-id p))) 8 ((> (amount p) 25)) 9 ((not (paid-for p))) 10 ((eq? (client-id c) (client-id pm)) 'condition1) 11 (then 12 (apply-discount c p 0.05)))) 1 (meta-rule "GiveDiscountToRegularClients" 2 (bindings 3 ((r 'Rule) 4 (m 'Match)) 5 (where 6 ((eq? r "ProcessPurchasePremiumMember")) 7 (( < (accumulative-count 8 (eq? (rule-name m) (rule-name r)) 9 '24h) 10 25))) 11 (then 12 (remove-condition r 'condition1)))) IF discount was applied < 25 times in last 24 hours THEN give discount to everyone
  8. Problem Statement Revisited • Static Rulesets • Constructs to change

    ruleset at runtime • No Separation of Concerns and Unmaintainable 12
  9. Problem Statement Revisited • Static Rulesets • Constructs to change

    ruleset at runtime • No Separation of Concerns and Unmaintainable 13
  10. Time specific Rules 1 (rule "MondayDiscount2" 2 (bindings 3 ((d

    'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 0)) 8 ((contains? (articles p) 5)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (give-article c 5)))) 1 (rule "TuesdayDiscount1" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 1)) 8 ((> (amount p) 100)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (charge-purchase c p 15)))) 1 (rule "TuesdayDiscount2" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 1)) 8 ((contains? (articles p) 10)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (give-article c 10)))) 1 (rule "MondayDiscount1" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 0)) 8 ((> (amount p) 20)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (charge-purchase c p 10)))) 14
  11. Time specific Rules 1 (rule "MondayDiscount2" 2 (bindings 3 ((d

    'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 0)) 8 ((contains? (articles p) 5)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (give-article c 5)))) 1 (rule "TuesdayDiscount1" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 1)) 8 ((> (amount p) 100)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (charge-purchase c p 15)))) 1 (rule "TuesdayDiscount2" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 1)) 8 ((contains? (articles p) 10)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (give-article c 10)))) 1 (rule "MondayDiscount1" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 0)) 8 ((> (amount p) 20)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (charge-purchase c p 10)))) 14 Separation of Concerns
  12. Time specific Rules 1 (rule "MondayDiscount2" 2 (bindings 3 ((d

    'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 0)) 8 ((contains? (articles p) 5)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (give-article c 5)))) 1 (rule "TuesdayDiscount1" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 1)) 8 ((> (amount p) 100)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (charge-purchase c p 15)))) 1 (rule "TuesdayDiscount2" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 1)) 8 ((contains? (articles p) 10)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (give-article c 10)))) 1 (rule "MondayDiscount1" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 0)) 8 ((> (amount p) 20)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (charge-purchase c p 10)))) 14 Separation of Concerns M aintainability
  13. 1 (rule "MondayDiscount2" 2 (bindings 3 ((d 'day) 4 (p

    'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 0)) 8 ((contains? (articles p) 5)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (give-article c 5)))) 1 (rule "TuesdayDiscount3" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 1)) 8 ((> (amount p) 100)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (charge-purchase c p 15)))) 1 (rule "TuesdayDiscount4" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 1)) 8 ((contains? (articles p) 10)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (give-article c 10)))) 1 (rule "MondayDiscount1" 2 (bindings 3 ((d 'day) 4 (p 'purchase) 5 (c 'customer)) 6 (where 7 ((eq? (week-day d) 0)) 8 ((> (amount p) 20)) 9 ((eq? (client-id c) (client-id p)))) 10 (then 11 (charge-purchase c p 10)))) 15 One Concern per Rule
  14. Separate Concerns 1 (rule "Discount1" 2 (bindings 3 ((p 'purchase)

    4 (c 'customer)) 5 (where 6 (> (amount p) 20) 7 (eq? (client-id c) (client-id p))) 8 (then 9 (charge-purchase c p 10)))) 1 (rule "Discount2" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 (contains? (articles p) 5) 7 (eq? (client-id c) (client-id p))) 8 (then 9 (give-article c 5)))) 1 (rule "Discount3" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 (> (amount p) 100) 7 (eq? (client-id c) (client-id p))) 8 (then 9 (charge-purchase c p 15)))) 1 (rule "Discount4" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 (contains? (articles p) 10) 7 (eq? (client-id c) (client-id p))) 8 (then 9 (give-article c 10)))) 16
  15. Separate Concerns 1 (rule "Discount1" 2 (bindings 3 ((p 'purchase)

    4 (c 'customer)) 5 (where 6 (> (amount p) 20) 7 (eq? (client-id c) (client-id p))) 8 (then 9 (charge-purchase c p 10)))) 1 (rule "Discount2" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 (contains? (articles p) 5) 7 (eq? (client-id c) (client-id p))) 8 (then 9 (give-article c 5)))) 1 (rule "Discount3" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 (> (amount p) 100) 7 (eq? (client-id c) (client-id p))) 8 (then 9 (charge-purchase c p 15)))) 1 (rule "Discount4" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 (contains? (articles p) 10) 7 (eq? (client-id c) (client-id p))) 8 (then 9 (give-article c 10)))) 1 (meta-rule "Monday" 2 (bindings 3 ((d 'day) 4 (r1 'rule) 5 (r2 'rule) 6 (r3 'rule) 7 (r4 'rule)) 8 (where 9 (eq? (week-day d) 0) 10 (eq? (name r1) "Discount1") 11 (eq? (name r2) "Discount2") 12 (eq? (name r3) "Discount3") 13 (eq? (name r4) "Discount4")) 14 (then 15 (enable-rule r1) 16 (enable-rule r2) 17 (disable-rule r3) 18 (disable-rule r4)))) 16
  16. Rule Categories 1 (rule “Discount1” “Discounts" 2 (bindings 3 ((p

    'purchase) 4 (c 'customer)) 5 (where 6 ((> (amount p) 20)) 7 ((eq? (client-id c) (client-id p)))) 8 (then 9 (charge-purchase c p 10)))) 1 (rule “Discount2" “Discounts" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 ((contains? (articles p) 5)) 7 ((eq? (client-id c) (client-id p)))) 8 (then 9 (give-article c 5)))) 1 (rule “Discount3" “Discounts" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 ((> (amount p) 100)) 7 ((eq? (client-id c) (client-id p)))) 8 (then 9 (charge-purchase c p 15)))) 1 (rule “Discount4" “Discounts" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 ((contains? (articles p) 10)) 7 ((eq? (client-id c) (client-id p)))) 8 (then 9 (give-article c 10)))) 17
  17. Rule Categories 1 (rule “Discount1” “Discounts" 2 (bindings 3 ((p

    'purchase) 4 (c 'customer)) 5 (where 6 ((> (amount p) 20)) 7 ((eq? (client-id c) (client-id p)))) 8 (then 9 (charge-purchase c p 10)))) 1 (rule “Discount2" “Discounts" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 ((contains? (articles p) 5)) 7 ((eq? (client-id c) (client-id p)))) 8 (then 9 (give-article c 5)))) 1 (rule “Discount3" “Discounts" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 ((> (amount p) 100)) 7 ((eq? (client-id c) (client-id p)))) 8 (then 9 (charge-purchase c p 15)))) 1 (rule “Discount4" “Discounts" 2 (bindings 3 ((p 'purchase) 4 (c 'customer)) 5 (where 6 ((contains? (articles p) 10)) 7 ((eq? (client-id c) (client-id p)))) 8 (then 9 (give-article c 10)))) 1 (meta-rule "Monday" 2 (bindings 3 ((d 'day) 4 (r1 'rule) 5 (r2 'rule) 6 (c 'category)) 7 (where 8 ((eq? (week-day d) 0)) 9 ((eq? (name r1) “Discount1")) 10 ((eq? (name r2) “Discount2")) 11 ((eq? (name c) “Discounts"))) 12 (then 13 (disable-category c) 14 (enable-rule r1) 15 (enable-rule r2)))) 17
  18. Problem Statement Revisited • Static Rulesets • Constructs to change

    ruleset at runtime • No Separation of Concern & Unmaintainable • Put different concerns at different levels 18