Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Pattern Matching 2 Variables Variables in CLIPS are always written in the syntax of a question mark followed by a symbolic field name. Variables.

Similar presentations


Presentation on theme: "Chapter 8 Pattern Matching 2 Variables Variables in CLIPS are always written in the syntax of a question mark followed by a symbolic field name. Variables."— Presentation transcript:

1

2 Chapter 8 Pattern Matching

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

4 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. 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. The term bound and bind are used to describe the assignment of a value to a variable.

5 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. 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: (clear)  (deftemplate person (slot name) (slot eyes) (slot hair))  Example: CLIPS> (clear)  CLIPS> (deftemplate person (slot name) (slot eyes) (slot hair)) 

6 5 (defrule find-blue-eyes (person (name ?name) (eyes blue)) => (printout t ?name “has blue eyes.” crlf))  (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)))  (reset) (run)  Jack has blue eyes. Jane has blue eyes. 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

7 6 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. 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. (deftemplate find (slot eyes)) (defrule find-eyes (find (eyes ?eyes)) (person (name ?name) (eyes ?eyes)) => (printout t ?name “ has” ?eyes “ eyes.” crlf)) CLIPS> (deftemplate find (slot eyes)) CLIPS> (defrule find-eyes (find (eyes ?eyes)) (person (name ?name) (eyes ?eyes)) => (printout t ?name “ has” ?eyes “ eyes.” crlf)) 8.3 Example2

8 7 (reset) (assert (find (eyes blue))) (run) Jack has blue eyes. Jane has blue eyes. (assert (find (eyes green))) (run) Jeff has green eyes. Joe has green eyes. (assert (find (eyes purple))) (run) CLIPS> (reset) CLIPS> (assert (find (eyes blue))) CLIPS> (run) Jack has blue eyes. Jane has blue eyes. CLIPS> (assert (find (eyes green))) CLIPS> (run) Jeff has green eyes. Joe has green eyes. CLIPS> (assert (find (eyes purple))) CLIPS> (run) CLIPS>

9 8 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. 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. 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. 8.4

10 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, “<-”. 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. Once the variable is bound it can be used with the retract, modify, or duplication commands in place of a fact index.

11 10 CLIPS> (clear) CLIPS> (deftemplate person (slot name) (slot address)) CLIPS> (deftemplate moved (slot name) (slot address)) CLIPS> (defrule process-moved-information ?f1 (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) CLIPS> (clear) CLIPS> (deftemplate person (slot name) (slot address)) CLIPS> (deftemplate moved (slot name) (slot address)) CLIPS> (defrule process-moved-information ?f1 (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

12 11 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. 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. Particularly useful for multifield slots. Example: Suppose we want to print the social security number of every person with a specified last name…… Example: Suppose we want to print the social security number of every person with a specified last name…… 8.5

13 12 (deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people (person (name John Q. Public) (social-security-number 483-98-9083)) (person (name Jack R. Public) (social-security-number 483-98-9084)) (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)) (deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people (person (name John Q. Public) (social-security-number 483-98-9083)) (person (name Jack R. Public) (social-security-number 483-98-9084)) (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.

14 13 (deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people (person (name John Q. Public) (social-security-number 483-98-9083)) (person (name Jack R. Public) (social-security-number 483-98-9084)) (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)) (deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people (person (name John Q. Public) (social-security-number 483-98-9083)) (person (name Jack R. Public) (social-security-number 483-98-9084)) (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

15 14 Blocks World A good example of planning, might be applied to automated manufacturing, where a robot arm manipulates parts. 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. 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? What steps must be taken to move block C on top of E? C B A F E D

16 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: 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 C F E F E C

17 16 Simple Solution – cont. The simple blocks world does not require that the blocks be restacked, just move them to the floor. 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 X X

18 17 Blocks World – cont. Since the floor is really not a block, it may be necessary to treat it differently. 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

19 18 Blocks World – cont. Several types of facts will be needed: Several types of facts will be needed: (deftemplate on-top-of (slot upper) (slot lower)) Facts: 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

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

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

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

23 22 Move-directly Rule (defrule move-directly ?goal (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)) Determine that there is a goal to move a block on top of another block. 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 1 2 3 2 1

24 23 Move-to-floor Rule (defrule move-to-floor ?goal (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 21

25 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 12

26 25 Clear-lower-block Rule (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 212 C B A F E D C B A F E D C B A F E D C B A F E D B A F E D C Example5

27 26 Step-by-step Method First: pseudorules were written using English-like text. 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. 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. Finally, the pseudorules were translated to CLIPS rules using the deftemplates as a guide for translation.

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

29 28 Multifield Wildcards Multifield wildcards ($?) can be used to match against zero or more fields of a pattern. Multifield wildcards ($?) can be used to match against zero or more fields of a pattern. Example: will not match a person fact 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 345-89-3039)) 8.7

30 29 Multifield Wildcards Multifield wildcards ($?) can be used to match against zero or more fields of a pattern. Multifield wildcards ($?) can be used to match against zero or more fields of a pattern. Example: will not match a person fact 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 345-89-3039)) 8.7 Replace the two single-field wildcards With a single multifield wildcard.

31 30 Multifield Variables Multifield variables are preceeded by a “$?”. Multifield variables are preceeded by a “$?”. Example: construct showing how to print names of all the children of a specified person. 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. CLIPS> (reset) CLIPS> (assert (print-children John Q. Public)) CLIPS> (run) (John Q. Public) has children (Jane Paul Mary) CLIPS> Example6

32 31 More than one multifield variable can be used in a single slot Example: find all the people who have a child with a specific name. 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)) CLIPS> (reset) CLIPS> (assert (find-child Paul)) CLIPS> (run) (John Q. Public) has child Paul Other children are (Jane) (Mary) CLIPS> (assert (find-child Rick)) CLIPS> (run) (Jack R. Public) has child Rick Other children are () () CLIPS> (assert (find-child Bill)) CLIPS> (run) CLIPS> Example6

33 32 Match pattern in more than one way CLIPS> (reset) CLIPS> (assert (person (name Joe Fiveman) (children Joe Joe Joe))) CLIPS> (assert (find-child Joe)) CLIPS> (agenda) 0find-child: f-4, f-3 0find-child: f-4, f-3 0find-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

34 33 Implementing a Stack A stack is an ordered data structure to which items can be added and removed. 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. 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. 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.

35 34 Push (defrule push-value ?push-value (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)

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

37 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 (retract ?goal ?stack-1 ?stack-2) (assert (stack $?rest1)) (assert (stack ?block1 ?block2 $?rest2)) (printout t ?block1 “ moved on top of “ ?block2 “.” crlf))

38 37 Blocks World Revisited – cont. (defrule move-to-floor ?goal (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 2 Variables Variables in CLIPS are always written in the syntax of a question mark followed by a symbolic field name. Variables."

Similar presentations


Ads by Google