rules
play

Rules Assume cfile contains: And we issue the commands: (deffacts - PowerPoint PPT Presentation

Rules Assume cfile contains: And we issue the commands: (deffacts initial-colors clips> (load cfile) (colors red blue)) clips> (reset) (defrule rule-1 clips> (facts) (colors red white blue) => (assert (rule 1 fires))


  1. Rules Assume cfile contains: And we issue the commands: (deffacts initial-colors clips> (load “cfile”) (colors red blue)) clips> (reset) (defrule rule-1 clips> (facts) (colors red white blue) => (assert (rule 1 fires)) ) What is in the fact base? (defrule rule-2 f-0 (initial-fact) (colors red blue) f-1 (colors red blue) => …) Suppose that we now: (defrule rule-3 (colors blue red) clips> (run) => …) f-0 (initial-fact) (defrule rule-4 f-1 (colors red blue) (colors red blue) f-2 (rule 2 fired) => …) (defrule rule-4 (colors white red) => …) II-1

  2. Top Level Rule Commands Covered Previously: (watch <X> ) -- where <X> is rules or activations (unwatch <X> ) New Commands: (rules) -- displays names of all rules in K.B. (ppdefrule <r-name> ) -- display specified rule (matches <r-name> ) -- display list of facts that match conditions of rule (set-break <r-name> ) -- sets a break point on rule -- system returns to top level before execution of rule II-2

  3. Top Level Rule Commands (cont.) (remove-break <r-name> ) -- removes break (remove-break) -- removes all breaks (show-breaks) -- display all break points (agenda) -- display all rule instantiations on agenda (run) -- start execution, running until no more rules can execute or (halt) is executed (run <n> ) -- execute at most <n> rules II-3

  4. Salience • Numeric value associated with rule • Range for value: -10000 to 10000 • Default value is 0 • Specifies the priority of rule execution • Do not (over) use! Conflict resolution: (1) Find rules which are satisfied (2) Pick rule with highest salience (3) If multiple rules, then apply conflict resolution to pick II-4

  5. Example (deffacts example-facts (a b c) Which rule should be (Test 1) ) executed first? (defrule r-1 r-2 (a b c) Then => (assert (rule 1 fires))) r-3 (defrule r-2 (declare (salience 1)) Then (Test 1) r-1 => . . .) (defrule r-3 (declare (salience 1)) (a b c) => ( . . .) II-5

  6. Conflict Resolution Strategies Depth Strategy Newly activated rules are placed above all rules of same salience- stack based Suppose: fact-a activates rule-1 and rule-2 fact-b activates rule-3 and rule-4 fact-a is asserted before fact-b Then: rule-3 and rule-4 will be above rule-1 and rule-2 rule-3 and rule-4 ’s order will be arbitrary rule-1 and rule-2’s order will be arbitrary II-6

  7. Conflict Resolution Strategies (cont.) Breadth Strategy Newly activated rules are placed below all rules of same salience- Queue based Suppose: fact-a activates rule-1 and rule-2 fact-b activates rule-3 and rule-4 fact-a is asserted before fact-b Then: rule-1 and rule-2 will be above rule-3 and rule-4 rule-3 and rule-4 ’s order will be arbitrary rule-1 and rule-2’s order will be arbitrary II-7

  8. Conflict Resolution Strategies (cont.) Simplicity Strategy Newly activated rules of same salience are placed above all rules of equal or higher specificity Complexity Strategy Among rules of same salience, newly activated rules are placed below all activations with equal or lower specificity II-8

  9. Conflict Resolution Strategies (cont.) Random Strategy Each activation is assigned a random number This number determines the activations placement among others of same salience II-9

  10. Conflict Resolution Strategies (cont.) LEX Strategy Scheme used in OPS-5 Rules are ordered by their salience An activation with a more recent fact-index is placed before rules with less recent fact indices If two activations have same recency, then one with higher specificity is placed first II-10

  11. Conflict Resolution Strategies (cont.) MEA Strategy Scheme available in OPS-5 Rules are ordered by their salience The recency of the fact associated with the first pattern is used to order If two activations have same fact-index for first pattern, then use the LEX strategy II-11

  12. Pattern Matching ? • single field wildcard • matches anything in corresponding field of fact $? • multi-field wildcard • matches zero or more fields of a fact II-12

  13. Pattern Matching (cont.) ? <var> • single field variable • <var> is some word • this symbol matches anything in the corresponding field of a fact • value matched is bound to ? <var> for scope of rule • examples: ?cat ?color ?machine $? <var> • multi-field variable • matches zero or more fields of a fact • the value(s) of the matched fields is bound to $? <var> for the scope of the rule II-13

  14. Examples Single field wild cards LHS Condition Fact in Fact Base Match? (? ?) (data red) Yes (data ?) (data red) Yes (data ?) (data red green) NO! (data ? ?) (data red green) Yes (data red ?) (data red green) Yes (data ? green) (data green) NO! II-14

  15. Examples Multi-field wild cards LHS Condition Fact in Fact Base Match? ($?) (data red) Yes (data $?) (data red) Yes (data red $?) (data red) Yes (data $?) (data red green) Yes (data red green $?) (data red green) Yes ($? green) (data red green) Yes ($? red $?) (data red green) Yes (data red $?) (data green red) NO! (data $? red $?) (data green red) Yes ($? $?) (data red) Yes II-15

  16. Examples Single Field Variables LHS Condition Fact in Fact Base Match? ?x=green (data red ?x) (data red green) (data red “green”) ?x=“green” (data red ?x) (data red ?x) (data red 17.4) ?x=17.4 ?x=red (data ?x ?x) (data red red) (data ?x ?x) (data red green) NO! (data ?x ?y) (data red green) ?x=red ?y=green (data ?x ?y) (data red red) ?x=red ?y=red II-16

  17. Examples Multi-Field Variables LHS Condition Fact in Fact Base Match? $?x=() (data red $?x) (data red) (data red $?x) (data red green) $?x=(green) (data red $?x) (data red one two) $?x=(one two) (data $?x $?x) (data red red) $?x=(red) (data $?x $?y) (data red green) Multiple matches: $?x=() $?y=(red green) $?x=(red) $?y=(green) $?x=(red green) $?y=() II-17

  18. Example of Rules with Variable Pattern Matching Fact Base: (data red green) (data purple green) Rule Base: (defrule rule-1 (data red ?x) (data purple ?x) => . . .) (defrule rule-2 (data red $?x) (data purple $?x) => . . .) What rules will be instantiated? Both rule-1 and rule-2! II-18

  19. Example of Rules with Variable Pattern Matching Fact Base: (data red green) (data red blue green) (data purple blue) (data purple blue brown) Rule Base: (defrule rule-1 (data red ?x) (data purple ?x) => . . .) (defrule rule-2 (data red $?x) (data purple $?x) => . . .) What rules will be instantiated? None! II-19

  20. Example of Rules with Variable Pattern Matching Fact Base: (data red green) (data red blue green) (data purple blue) (data purple blue brown) Rule Base: (defrule rule-5 (data red ?x) (data purple ?y) => . . .) (defrule rule-6 (data red $?x) (data purple $?y) => . . .) What rules will be instantiated? One instantiation for rule-5, but 4 for rule-6! II-20

  21. Field Constraints Constraints can be placed on the fields of a condition using logical operators: & and | or ~ not Syntax: <val1> & <val2> <val1> | <val2> ~ <val> ? <var> & ( <val1> | <val2> ) ? <var> & ~ <val> II-21

  22. Examples Facts Condition Match NO (data red) (data ~red) (data green) (data ~red&~blue) Yes (data red) (data red|blue) Yes ?col=green (data green) (data ?col&~red) (data red) (data ?col&~red&~blue) NO (data red) (data ?col&red|blue) ?col=red II-22

  23. Sample Rule (defrule complex-eye-hair-match (person ?name1 ?eyes1&blue|green ?hair1&~black) (person ?name2&~?name1 ?eyes2&~?eyes1 ?hair2&red|?hair1) => (printout t ?name1 “ has ” ?eyes1 “ eyes and ” ?hair1 “ hair” crlf) (printout t ?name2 “ has ” ?eyes2 “ eyes and ” ?hair2 “ hair” crlf) ) II-23

  24. Built-in Functions Arithmetic: + - * / ** Relational: != = < <= > >= eq neq Predicate: numberp stringp wordp evenp oddp integerp II-24

  25. Examples (data ?X &: (numberp ?X) ) (data ?X &: (numberp ?X) & ~: (oddp ?X) ) (data ?X &: (numberp ?X) |: (wordp ?X) ) Rule Facts Instantiated? (defrule r-1 (data ?Y) (data 3) ?X = 5 (data ?X &:(> ?X ?Y) ) (data 5) ?Y = 3 => . . . ) (defrule r-2 (data1 ?Y) (data2 4) ?Y = 4 (data2 ?X &:(= ?X ?Y)) (data1 4) ?X = 4 => . . . ) II-25

  26. Examples (cont.) Rule Facts Instantiated? (defrule r-3 (data1 ?Y) (data1 red) ERROR! (data2 ?X &:(= ?X ?Y) ) (data2 4) => . . . ) (defrule r-4 NO! (data1 ?Y) (data1 red) (data2 ?X &:(eq ?X ?Y)) (data2 4) => . . . ) (defrule r-5 ?Y = red (data1 ?Y) (data2 4) ?X = 4 (data2 ?X &:(neq ?X ?Y)) (data1 red) => . . . ) II-26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend