1 / 16

Advanced Pattern Matching

Advanced Pattern Matching. 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.

fell
Download Presentation

Advanced Pattern Matching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Pattern Matching

  2. 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”))

  3. 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))

  4. Functions & Expressions • Numeric Expression • Prefix notation • Basic operations : +,-,*,/ (y2-y1) / (x2-x1) >0\ Will be : (> (/ (- y2 y1) (- x2 x1)) 0)

  5. 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

  6. Functions & Expressions • Be careful! (assert (answer (+ 2 2))) (assert (expression 2+3*4)) Facts inserted by clips (answer 4) (expression 2+3*4)

  7. 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!!

  8. 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))))

  9. Variable Binding • Used to store a value in a variable to prevent recalculation • Syntax: (bind <variable> <value>) • (bind ?new-total (+ ?total ?area))

  10. 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”

  11. 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)

  12. 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))

  13. 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”))

  14. 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)

  15. 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)))

  16. 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)))

More Related