Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Pattern Matching. Field constraints Used to restrict the values of a field on LHS of a rule Used to restrict the values of a field on LHS of.

Similar presentations


Presentation on theme: "Advanced Pattern Matching. Field constraints Used to restrict the values of a field on LHS of a rule Used to restrict the values of a field on LHS of."— Presentation transcript:

1 Advanced Pattern Matching

2 Field constraints Used to restrict the values of a field on LHS of a rule Used to restrict the values of a field on LHS of a rule (defrule person-without-brown-hair (person (name ?name) (hair ~brown)) => (printout t ?name “ does not have brown hair”)) Not field constraint

3 Other field contraints And field constraint  & And field constraint  & Or 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 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 Clips supports variable numbers of arguments There is no built-in precedence of arithmetic operations There is no built-in precedence of arithmetic operations Everything is simply evaluated from left to right, parentheses determining precedence Everything is simply evaluated from left to right, parentheses determining precedence

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

7 Summing values using rules This will loop endlessly This will loop endlessly –Retracting the sum rule and reasserting it will produce a loop with a single rule –Use a temporary fact!! (defrule sum-rectangles (rectangle (height ?height)(width ?width)) ?sum<- (sum ?total) => (retract ?sum) (assert (sum(+ ?total (* ?height ?width)))))

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 Used to store a value in a variable to prevent recalculation –Syntax: (bind ) –(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 function that return the symbol TRUE or FALSE Any value other than FALSE is treated as TRUE Any value other than FALSE is treated as TRUE Check appendix E in Book! 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 A way to evaluate expressions on the LHS of a rule Instead of pattern matching, test will evaluate an expression 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 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 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 After deleting the two preconditions, use-oxygen-masks will remain a fact (defrule noxious-fumes-present (logical (noxious-fumes-present)) (emergency (type fire)) => (assert (use-oxygen-masks))) 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


Download ppt "Advanced Pattern Matching. Field constraints Used to restrict the values of a field on LHS of a rule Used to restrict the values of a field on LHS of."

Similar presentations


Ads by Google