160 likes | 239 Views
Chapter 8: Advanced Pattern Matching. Presented by: Farnaz Ronaghi. Field constraints. Used to restrict the values of a field on LHS of a rule. Not field constraint. (defrule person-without-brown-hair (person (name ?name) (hair ~brown)) => (printout t ?name “ does not have brown hair”)).
E N D
Chapter 8:Advanced Pattern Matching Presented by: Farnaz Ronaghi
Field constraints • Used to restrict the values of a field on LHS of a rule Not field constraint (defrule person-without-brown-hair (person (name ?name) (hair ~brown)) => (printout t ?name “ does not have brown hair”))
Other field contraints • And field constraint & • Or field constraint | (defrule black-or-brown-hair (person (name ?name) (hair ?color&~brown&~black)) => (printout t ?name “ has ” ?color “ hair ” crlf))
Functions & Expressions • Numeric Expression • Prefix notation • Basic operations : +,-,*,/ (y2-y1) / (x2-x1) >0\ Will be : (> (/ (- y2 y1) (- x2 x1)) 0)
Functions & Expressions • Clips supports variable numbers of arguments • There is no built-in precedence of arithmetic operations • Everything is simply evaluated from left to right, parentheses determining precedence
Functions & Expressions • Be careful! (assert (answer (+ 2 2))) (assert (expression 2+3*4)) Facts inserted by clips (answer 4) (expression 2+3*4)
Summing values using rules (defrule sum-rectangles (rectangle (height ?height)(width ?width)) ?sum<- (sum ?total) => (retract ?sum) (assert (sum(+ ?total (* ?height ?width))))) • This will loop endlessly • Retracting the sum rule and reasserting it will produce a loop with a single rule • Use a temporary fact!!
Summing values using rules (defrule sum-rectangles (rectangle (height ?height) (width ?width)) => (assert (add-to-sum (* ?height ?width)))) (defrule sum-areas ?sum <- (sum ?total) ?new-area <- (add-to-sum ?area) => (retract ?sum ?new-area) (assert (sum (+ ?total ?area))))
Variable Binding • Used to store a value in a variable to prevent recalculation • Syntax: (bind <variable> <value>) • (bind ?new-total (+ ?total ?area))
How to read input? (defrule get-first-name => (printout t “what’s your name?”) (bind ?response (read) (assert (user-name ?response) ))) Sina jafarpour “sina jafarpour”
Predicate Functions • Any function that return the symbol TRUE or FALSE • Any value other than FALSE is treated as TRUE • Check appendix E in Book! • (and (> 4 3) (> 4 5)) • (integerp 3.5)
The Test conditional element • A way to evaluate expressions on the LHS of a rule • Instead of pattern matching, test will evaluate an expression • (test (> ?size 1))
Conditional Elements • Thus far all rules had an implicit and conditional elements between patterns (defrule shutt-off-electricity-1 (emergency (type flood)) => (printout t “shut off electricity” clrf)) (defrule shutt-off-electricity-2 (emergency (type water-sprinkler)) => (printout t “shut off electricity” clrf)) (defrule shut-off-electricity (electrical-power (status on)) (Or (emergency (type flood)) (extinguisher-system (type water-sprinkler) (status on))) => (printout t “shut off electricity”))
Exists and forall conditional Elements (defrule all-fires-being-handled (forall (emergency (type fire) (location ?where)) (fire-squad (location ?where)) (evacuated (building ?where)))) => (printout t “all buildings that are on fire” clrf “have been evacuated and ” clrf “have firefighters on location” clrf)
Logical conditional element • Allows you to specify that the existence of a fact depends on the existence of another fact or group of facts (defrule noxious-fumes-present (emergency (type fire)) (noxious-fumes-present) => (assert (use-oxygen-masks)))
Logical conditional element • After deleting the two preconditions, use-oxygen-masks will remain a fact When the noxious-fumes-present rule is executed A link is created between the facts matching the pattern Contained within the logical CE in the LHS of a rule And the facts asserted in the RHS of the rule. So, if either the emergency fact or the noxious-fumes fact Is retracted,then use-oxygen-masks fact will be also retracted (defrule noxious-fumes-present (logical (noxious-fumes-present)) (emergency (type fire)) => (assert (use-oxygen-masks)))