16
Advanced Pattern Advanced Pattern Matching Matching

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

Embed Size (px)

Citation preview

Page 1: 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

Advanced Pattern MatchingAdvanced Pattern Matching

Page 2: 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

Field constraintsField constraints

Used to restrict the values of a Used to restrict the values of a field on LHS of a rulefield 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

Page 3: 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

Other field contraintsOther 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))

Page 4: 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

Functions & Functions & ExpressionsExpressions Numeric Expression Numeric Expression

– Prefix notationPrefix notation– Basic operations : +,-,*,/Basic operations : +,-,*,/

(y2-y1) / (x2-x1) >0\

Will be :

(> (/ (- y2 y1) (- x2 x1)) 0)

Page 5: 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

Functions & Functions & ExpressionsExpressions Clips supports variable numbers Clips supports variable numbers

of argumentsof arguments There is no built-in precedence of There is no built-in precedence of

arithmetic operationsarithmetic operations Everything is simply evaluated Everything is simply evaluated

from left to right, parentheses from left to right, parentheses determining precedencedetermining precedence

Page 6: 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

Functions & Functions & ExpressionsExpressions Be careful!Be careful!

(assert (answer (+ 2 2)))

(assert (expression 2+3*4))

(answer 4)

(expression 2+3*4)

Facts inserted by clips

Page 7: 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

Summing values using Summing values using rulesrules

This will loop endlesslyThis will loop endlessly– Retracting the sum rule and reasserting it Retracting the sum rule and reasserting it

will produce a loop with a single rulewill produce a loop with a single rule– Use a temporary fact!!Use a temporary fact!!

(defrule sum-rectangles (rectangle (height ?height)(width ?width))?sum<- (sum ?total)

=>(retract ?sum)(assert (sum(+ ?total (* ?height ?width)))))

Page 8: 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

Summing values using Summing values using rulesrules

(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))))

Page 9: 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

Variable BindingVariable Binding

Used to store a value in a variable Used to store a value in a variable to prevent recalculationto prevent recalculation– Syntax: (bind <variable> <value>)Syntax: (bind <variable> <value>)– (bind ?new-total (+ ?total ?area))(bind ?new-total (+ ?total ?area))

Page 10: 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

How to read input?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”

Page 11: 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

Predicate FunctionsPredicate Functions

Any function that return the Any function that return the symbol TRUE or FALSEsymbol TRUE or FALSE

Any value other than FALSE is Any value other than FALSE is treated as TRUEtreated as TRUE

Check appendix E in Book!Check appendix E in Book!– (and (> 4 3) (> 4 5))(and (> 4 3) (> 4 5))– (integerp 3.5)(integerp 3.5)

Page 12: 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

The Test conditional The Test conditional elementelement A way to evaluate expressions on A way to evaluate expressions on

the LHS of a rulethe LHS of a rule Instead of pattern matching, test Instead of pattern matching, test

will evaluate an expressionwill evaluate an expression– (test (> ?size 1))(test (> ?size 1))

Page 13: 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

Conditional ElementsConditional Elements

Thus far all rules had an implicit Thus far all rules had an implicit and conditional elements and conditional elements between patternsbetween 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”))

Page 14: 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

Exists and forall Exists and forall conditional Elementsconditional 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)

Page 15: 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

Logical conditional Logical conditional elementelement Allows you to specify that the Allows you to specify that the

existence of a fact depends on existence of a fact depends on the existence of another fact or the existence of another fact or group of factsgroup of facts

(defrule noxious-fumes-present (emergency (type fire))(noxious-fumes-present)=>(assert (use-oxygen-masks)))

Page 16: 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

Logical conditional Logical conditional elementelement After deleting the two After deleting the two

preconditions, use-oxygen-masks preconditions, use-oxygen-masks will remain a factwill 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 patternContained within the logical CE in the LHS of a ruleAnd the facts asserted in the RHS of the rule.So, if either the emergency fact or the noxious-fumes factIs retracted,then use-oxygen-masks fact will be also retracted