Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Pattern Matching

Similar presentations


Presentation on theme: "Chapter 8 Pattern Matching"— Presentation transcript:

1 Chapter 8 Pattern Matching

2 8.2 Variables Variables in CLIPS are always written in the syntax of a question mark followed by a symbolic field name. Examples: ?speed ?sensor ?value ?noun ?color

3 Bound (Bind) Variables are used on the LHS of a rule to contain slot values that can later be compared to other values on the LHS of a rule or accessed on the RHS of a rule. The term bound and bind are used to describe the assignment of a value to a variable.

4 Use of variables One common use of variables is to bind a variable on the LHS of a rule and then use that value on the RHS of the rule. Example: CLIPS> (clear)  CLIPS> (deftemplate person (slot name) (slot eyes) (slot hair)) 

5 CLIPS> (defrule find-blue-eyes (person (name
CLIPS> (defrule find-blue-eyes (person (name ?name) (eyes blue)) => (printout t ?name “has blue eyes.” crlf))  CLIPS> (deffacts people (person (name Jane) (eyes blue) (hair red)) (person (name Joe) (eyes green) (hair brown)) (person (name Jack) (eyes blue) (hair black)) (person (name Jeff) (eyes green) (hair brown)))  CLIPS> (reset) CLIPS> (run)  Jack has blue eyes. Jane has blue eyes. CLIPS> Example1

6 Multiple Use of Variables
8.3 Multiple Use of Variables The first time a variable is bound to a value, it retains that value within the rule. Other variables with the same name that are bound to a value must be bound to the first variable’s value. CLIPS> (deftemplate find (slot eyes)) CLIPS> (defrule find-eyes (find (eyes ?eyes)) (person (name ?name) (eyes ?eyes)) => (printout t ?name “ has” ?eyes “ eyes.” crlf)) Example2

7 CLIPS> (reset) CLIPS> (assert (find (eyes blue))) <Fact-5> CLIPS> (run) Jack has blue eyes. Jane has blue eyes. CLIPS> (assert (find (eyes green))) <Fact-6> CLIPS> (run) Jeff has green eyes. Joe has green eyes. CLIPS> (assert (find (eyes purple))) <Fact-7> CLIPS> (run) CLIPS>

8 8.4 Fact Addresses Retraction, modification, and duplication of facts are extremely common operations and are usually done on the RHS of a rule rather than at the top level. Before a fact can be manipulated from the RHS of a rule, however, there must be some way to specify the fact that matched a particular pattern.

9 Pattern Binding A variable can be bound to the fact address of the fact matching a pattern on the LHS of a rule by using the pattern binding operation, “<-”. Once the variable is bound it can be used with the retract, modify, or duplication commands in place of a fact index.

10 Without the retract command,
CLIPS> (clear) CLIPS> (deftemplate person (slot name) (slot address)) CLIPS> (deftemplate moved (slot name) (slot address)) CLIPS> (defrule process-moved-information ?f1 <- (moved (name ?name) (address ?address)) ?f2 <- (person (name ?name)) => (retract ?f1) (modify ?f2 (address ?address))) CLIPS> (deffacts example (person (name “John Hill”) (address “25 Mulberry Lane”)) (moved (name “John Hill”) (address “37 Cherry Lane”))) CLIPS> (reset) CLIPS> (run) Without the retract command, the program will go into an infinite loop. Example3

11 Single-field Wildcard
8.5 Single-field Wildcard It is useful to test for the existence of a field within a slot without actually assigning a value to a variable. Particularly useful for multifield slots. Example: Suppose we want to print the social security number of every person with a specified last name……

12 (deftemplate person. (multislot name)
(deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people (person (name John Q. Public) (social-security-number )) (person (name Jack R. Public) (social-security-number )) (defrule print-social-security-numbers (print-ss-numbers-for ?last-name) (person (name ?first-name ?middle-name ?last-name) (social-security-number ?ss-number)) => (printout t ?ss-number crlf)) Not referred to by actions on the RHS or other pattern or slots on the LHS of the rule. NO USE! Instead of using a variable, a single-field wildcard can be used when a field is required, but the value is not important.

13 (deftemplate person. (multislot name)
(deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people (person (name John Q. Public) (social-security-number )) (person (name Jack R. Public) (social-security-number )) (defrule print-social-security-numbers (print-ss-numbers-for ?last-name) (person (name ? ? ?last-name) (social-security-number ?ss-number)) => (printout t ?ss-number crlf)) Instead of using a variable, a single-field wildcard can be used when a field is required, but the value is not important. Example4

14 Blocks World A good example of planning, might be applied to automated manufacturing, where a robot arm manipulates parts. To begin solving, it will be useful to set up a configuration of blocks that can be used for testing the program. What steps must be taken to move block C on top of E? A D B E C F

15 Simple Solution Rule: directly move block C on top of block E. However, this rule could only be applied if both block C and block E had no blocks on top of them. Rule move-directly IF The goal is to move block ?upper on top of block ?lower and block ?upper is the top block in its stack and block ?lower is the top block in its stack THEN Move block ?upper on top of block ?lower F E C C F E

16 Simple Solution – cont. The simple blocks world does not require that the blocks be restacked, just move them to the floor. RULE Clear-upper-block IF The goal is to move block ?x and block ?x is not the top block in its stack and block ?above is on top of block ?x THEN The goal is to move block ?above to the floor RULE Clear-lower-block IF The goal is to move another block on top of block ?x and block ?x is not the top block in its stack and block ?above is on top of block ?x THEN The goal is to move block ?above to the floor X above X above X above X above

17 Blocks World – cont. Since the floor is really not a block, it may be necessary to treat it differently. RULE Move-to-floor IF The goal is to move block ?upper on top of the floor and block ?upper is the top block in its stack THEN Move block ?upper on top of the floor upper upper

18 Blocks World – cont. Several types of facts will be needed: (deftemplate on-top-of (slot upper) (slot lower)) Facts: (on-top-of (upper A) (lower B)) (on-top-of (upper B) (lower C)) (on-top-of (upper D) (lower E)) (on-top-of (upper E) (lower F)) (on-top-of (upper nothing) (lower A)) (on-top-of (upper C) (lower floor)) (on-top-of (upper nothing) (lower D)) (on-top-of (upper F) (lower floor)) C B A F E D

19 Floor and Nothing The words floor and nothing might be mistaken as the names of blocks. Use implied deftemplate block to identify the blocks. (block A) (block B) (block C) (block D) (block E) (block F)

20 Goals Goal described: (deftemplate goal (slot move) (slot on-top-of)
Initial goal: (goal (move C) (on-top-of E))

21 Initial Configuration
(deffacts initial-state (block A) (block B) (block C) (block D) (block E) (block F) (on-top-of (upper A) (lower B)) (on-top-of (upper B) (lower C)) (on-top-of (upper D) (lower E)) (on-top-of (upper E) (lower F)) (on-top-of (upper nothing) (lower A)) (on-top-of (upper C) (lower floor)) (on-top-of (upper nothing) (lower D)) (on-top-of (upper F) (lower floor)) (goal (move C) (on-top-of E)))

22 Update the stack information.
Move-directly Rule Determine that there is a goal to move a block on top of another block. (defrule move-directly ?goal <- (goal (move ?block1) (on-top-of ?block2)) (block ?block1) (block ?block2) (on-top-of (upper nothing) (lower ?block1)) ?stack-1 <- (on-top-of (upper ?block1) (lower ?block3)) ?stack-2 <- (on-top-of (upper nothing) (lower ?block2)) => (retract ?goal ?stack-1 ?stack-2) (assert (on-top-of (upper ?block1) (lower ?block2)) (on-top-of (upper nothing) (lower ?block3))) (printout t ?block1 “ moved on top of “ ?block2 “.” crlf)) Ensure that a goal to move a block onto the floor will not be processed by this rule. Match against information necessary to update the stacks that the moving block is being taken from and moved to. Update the stack information. 3 2 1 3 1 2

23 Move-to-floor Rule (defrule move-to-floor ?goal <- (goal (move ?block1) (on-top-of floor)) (block ?block1) (on-top-of (upper nothing) (lower ?block1)) ?stack <- (on-top-of (upper ?block1) (lower ?block2)) => (retract ?goal ?stack) (assert (on-top-of (upper ?block1) (lower floor)) (on-top-of (upper nothing) (lower ?block2))) (printout t ?block1 “ moved on top of floor.” crlf)) 2 1 2 1

24 Clear-upper-block Rule
(defrule clear-upper-block (goal (move ?block1)) (block ?block1) (on-top-of (upper ?block2) (lower ?block1)) (block ?block2) => (assert (goal (move ?block2) (on-top-of floor))) 1 2 1 2

25 Clear-lower-block Rule
1 2 (defrule clear-lower-block (goal (on-top-of ?block1)) (block ?block1) (on-top-of (upper ?block2) (lower ?block1)) (block ?block2) => (assert (goal (move ?block2) (on-top-of floor)))) Execution CLIPS> (unwatch all) CLIPS> (reset) CLIPS> (run) A moved on top of floor. B moved on top of floor. D moved on top of floor. C moved on top of E. CLIPS> 1 2 B A F E D C C B A F E D C B A F E D C B A F E D C B A F E D Example5

26 Step-by-step Method First: pseudorules were written using English-like text. Second: the pseudorules were used to determine the types of facts that would be required. Deftemplates describing the facts were designed, and the initial knowledge for the program was coded using these deftemplates. Finally, the pseudorules were translated to CLIPS rules using the deftemplates as a guide for translation.

27 Best Representation? It is not always possible to determine the best method for representing facts or the types of rules that will be needed to build an expert system. Following a consistent methodology, however, can aid in the development of an expert system even when a great deal of prototyping and iteration need to be performed.

28 8.7 Multifield Wildcards Multifield wildcards ($?) can be used to match against zero or more fields of a pattern. Example: (defrule print-social-security-numbers (print-ss-numbers-for ?last-name) (person (name ? ? ?last-name) (social-security-number ?ss-number)) => (printout t ?ss-number crlf)) will not match a person fact (person (name Joe Public) (social-security-number ))

29 8.7 Multifield Wildcards Multifield wildcards ($?) can be used to match against zero or more fields of a pattern. Example: (defrule print-social-security-numbers (print-ss-numbers-for ?last-name) (person (name $? ?last-name) (social-security-number ?ss-number)) => (printout t ?ss-number crlf)) will not match a person fact (person (name Joe Public) (social-security-number )) Replace the two single-field wildcards With a single multifield wildcard.

30 Multifield Variables Multifield variables are preceeded by a “$?”.
CLIPS> (reset) CLIPS> (assert (print-children John Q. Public)) <Fact-3> CLIPS> (run) (John Q. Public) has children (Jane Paul Mary) CLIPS> Multifield variables are preceeded by a “$?”. Example: construct showing how to print names of all the children of a specified person. (deftemplate person (multifield name) (multifield children)) (deffacts some-people (person (name John Q. Public) (children Jane Paul Mary)) (person (name Jack R. Public) (children Rick))) (defrule print-children (print-children $?name) (person (name $?name) (children $?children)) => (printout t ?name “has children “ ?children crlf)) The $ is only used on the LHS to indicate that zero or more fields can be bound to the variable. Example6

31 More than one multifield variable can be used in a single slot
CLIPS> (reset) CLIPS> (assert (find-child Paul)) <Fact-3> CLIPS> (run) (John Q. Public) has child Paul Other children are (Jane) (Mary) CLIPS> (assert (find-child Rick)) <Fact-4> (Jack R. Public) has child Rick Other children are () () CLIPS> (assert (find-child Bill)) <Fact-5> CLIPS> Example: find all the people who have a child with a specific name. (defrule find-child (find-child ?child) (person (name $?name) (children $?before ?child $?after)) => (printout t ?name “has child” ?child crlf) (printout t “Other children are” ?before “ “ ?after crlf)) Example6

32 Match pattern in more than one way
CLIPS> (reset) CLIPS> (assert (person (name Joe Fiveman) (children Joe Joe Joe))) <Fact-3> CLIPS> (assert (find-child Joe)) <Fact-4> CLIPS> (agenda) 0 find-child: f-4, f-3 0 find-child: f-4, f-3 0 find-child: f-4, f-3 For a total of 3 activations. CLIPS> (run) (Joe Fiveman) Has child Joe Other children are () (Joe Joe) (Joe Fiveman) Has child Joe Other children are (Joe) (Joe) (Joe Fiveman) Has child Joe Other children are (Joe Joe) () There are three different ways in which the variables ?child ?before, and ?after can be bound with the fact f-3. Example6

33 Implementing a Stack A stack is an ordered data structure to which items can be added and removed. Items are added and removed to one “end” of the stack. A new item can be pushed (added) to the stack or the last item added can be popped (removed) from the stack. In a stack the first value added is the last item to be removed and the last item added is the first item to be removed.

34 Push (stack X Y Z) (push-value A) (stack A X Y Z)
(defrule push-value ?push-value <- (push-value ?value) ?stack <- (stack $?rest) => (retract ?push-value ?stack) (assert (stack ?value $?rest)) (printout t “Pushing value “ ?value crlf)) (stack X Y Z) (push-value A) (stack A X Y Z)

35 Pop (stack A X Y Z) (pop-value) (stack X Y Z)
(defrule pop-value-valid ?pop-value <- (pop-value) ?stack <- (stack ?value $?rest) => (retract ?pop-value ?stack) (assert (stack $?rest)) (printout t “Popping value “ ?value crlf)) (defrule pop-value-invalid ?pop-value <- (pop-value) (stack) => (retract ?pop-value) (printout t “Popping from empty stack” crlf)) (stack X Y Z) Example7

36 Blocks World Revisited
(deffacts initial-state (stack A B C) (stack D E F) (goal (move C) (on-top-of E) (stack)) (defrule move-directly ?goal <- (goal (move ?block1) (on-top-of ?block2)) ?stack-1 <- (stack ?block1 $?rest1) ?stack-2 <- (stack ?block2 $?rest2) => (retract ?goal ?stack-1 ?stack-2) (assert (stack $?rest1)) (assert (stack ?block1 ?block2 $?rest2)) (printout t ?block1 “ moved on top of “ ?block2 “.” crlf))

37 Blocks World Revisited – cont.
(defrule move-to-floor ?goal <- (goal (move ?block1) (on-top-of floor)) ?stack-1 <- (stack ?block1 $?rest) => (retract ?goal ?stack-1) (assert (stack ?block1)) (assert (stack $?rest)) (printout t ?block1 “ moved on top of floor.” crlf )) (defrule clear-upper-block (goal (move ?block1)) (stack ?top $? ?block1 $?) => (assert (goal (move ?top) (on-top-of floor)))) (defrule clear-lower-block (goal (on-top-of ?block1)) (stack ?top $? ?block1 $?) => (assert (goal (move ?top) (on-top-of floor)))) Example8


Download ppt "Chapter 8 Pattern Matching"

Similar presentations


Ads by Google