37
Chapter 8 Pattern Matching

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

Embed Size (px)

Citation preview

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

Chapter 8Pattern Matching

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

2

Variables

Variables in CLIPS are always written in Variables in CLIPS are always written in the syntax of a question mark followed by a the syntax of a question mark followed by a symbolic field name.symbolic field name.

Examples:Examples:?speed?sensor?value?noun?color

8.2

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

3

Bound (Bind)

Variables are used on the LHS of a rule to Variables are used on the LHS of a rule to contain slot values that can later be contain slot values that can later be compared to other values on the LHS of a compared to other values on the LHS of a rule or accessed on the RHS of a rule.rule or accessed on the RHS of a rule.

The term The term boundbound and and bindbind are used to are used to describe the assignment of a value to a describe the assignment of a value to a variable.variable.

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

4

Use of variables

One common use of variables is to bind a One common use of variables is to bind a variable on the LHS of a rule and then use variable on the LHS of a rule and then use that value on the RHS of the rule.that value on the RHS of the rule.

Example:Example:CLIPS> (clear)(clear) CLIPS> (deftemplate person(deftemplate person

(slot name)(slot name)(slot eyes)(slot eyes)(slot hair))(slot hair))

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

5

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

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

6

Multiple Use of Variables

The first time a variable is bound to a value, The first time a variable is bound to a value, it retains that value within the rule. Other it retains that value within the rule. Other variables with the same name that are variables with the same name that are bound to a value must be bound to the first bound to a value must be bound to the first variable’s value.variable’s value.

CLIPS> (deftemplate find (slot eyes)) (deftemplate find (slot eyes))CLIPS> (defrule find-eyes (defrule find-eyes (find (eyes (find (eyes ?eyes?eyes)))) (person (name (person (name ?name?name) (eyes ) (eyes ?eyes?eyes)))) => => (printout t (printout t ?name?name “ has” “ has” ?eyes?eyes “ eyes.” crlf)) “ eyes.” crlf))

8.3

Example2

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

7

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

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

8

Fact Addresses

Retraction, modification, and duplication of Retraction, modification, and duplication of facts are extremely common operations and facts are extremely common operations and are usually done on the RHS of a rule rather are usually done on the RHS of a rule rather than at the top level.than at the top level.

Before a fact can be manipulated from the Before a fact can be manipulated from the RHS of a rule, however, there must be some RHS of a rule, however, there must be some way to specify the fact that matched a way to specify the fact that matched a particular pattern.particular pattern.

8.4

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

9

Pattern Binding

A variable can be bound to the A variable can be bound to the fact addressfact address of the fact matching a pattern on the LHS of of the fact matching a pattern on the LHS of a rule by using the a rule by using the pattern bindingpattern binding operation, “<-”. operation, “<-”.

Once the variable is bound it can be used Once the variable is bound it can be used with the retract, modify, or duplication with the retract, modify, or duplication commands in place of a fact index.commands in place of a fact index.

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

10

CLIPS> (clear)CLIPS> (clear)CLIPS> (deftemplate personCLIPS> (deftemplate person (slot name) (slot name) (slot address)) (slot address))CLIPS> (deftemplate movedCLIPS> (deftemplate moved (slot name) (slot name) (slot address)) (slot address))CLIPS> (defrule process-moved-informationCLIPS> (defrule process-moved-information ?f1 < ?f1 <- (moved (name ?name)- (moved (name ?name) (address ?address)) (address ?address)) ?f2 <- (person (name ?name)) ?f2 <- (person (name ?name)) => => (retract ?f1) (retract ?f1) (modify ?f2 (address ?address))) (modify ?f2 (address ?address)))CLIPS> (deffacts exampleCLIPS> (deffacts example (person (name “John Hill”) (person (name “John Hill”) (address “25 Mulberry Lane”)) (address “25 Mulberry Lane”)) (moved (name “John Hill”) (moved (name “John Hill”) (address “37 Cherry Lane”))) (address “37 Cherry Lane”)))CLIPS> (reset)CLIPS> (reset)CLIPS> (run)CLIPS> (run)

Without the retract command, the program will go into an infinite loop.

Example3

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

11

Single-field Wildcard

It is useful to test for the existence of a field It is useful to test for the existence of a field within a slot without actually assigning a within a slot without actually assigning a value to a variable.value to a variable.

Particularly useful for multifield slots.Particularly useful for multifield slots. Example: Suppose we want to print the Example: Suppose we want to print the

social security number of every person with social security number of every person with a specified last name……a specified last name……

8.5

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

12

((deftemplate persondeftemplate person(multislot name)(multislot name)(slot social-security-number))(slot social-security-number))

(deffacts some-people(deffacts some-people(person (name John Q. Public)(person (name John Q. Public)

(social-security-number 483-98-9083)) (social-security-number 483-98-9083))(person (name Jack R. Public)(person (name Jack R. Public)

(social-security-number 483-98-9084)) (social-security-number 483-98-9084))

(defrule print-social-security-numbers(defrule print-social-security-numbers(print-ss-numbers-for ?last-name)(print-ss-numbers-for ?last-name)

(person (name ?first-name ?middle-name ?last-(person (name ?first-name ?middle-name ?last-name)name) (social-security-number ?ss-number)) (social-security-number ?ss-number)) => => (printout t ?ss-number crlf)) (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.

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

13

((deftemplate persondeftemplate person(multislot name)(multislot name)(slot social-security-number))(slot social-security-number))

(deffacts some-people(deffacts some-people(person (name John Q. Public)(person (name John Q. Public)

(social-security-number 483-98-9083)) (social-security-number 483-98-9083))(person (name Jack R. Public)(person (name Jack R. Public)

(social-security-number 483-98-9084)) (social-security-number 483-98-9084))

(defrule print-social-security-numbers(defrule print-social-security-numbers(print-ss-numbers-for ?last-name)(print-ss-numbers-for ?last-name)

(person (name ? ? ?last-(person (name ? ? ?last-name)name) (social-security-number ?ss-number)) (social-security-number ?ss-number)) => => (printout t ?ss-number crlf)) (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

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

14

Blocks World

A good example of planning, might be applied to A good example of planning, might be applied to automated manufacturing, where a robot arm automated manufacturing, where a robot arm manipulates parts.manipulates parts.

To begin solving, it will be useful to set up a To begin solving, it will be useful to set up a configuration of blocks that can be used for testing configuration of blocks that can be used for testing the program.the program.

What steps must be taken to What steps must be taken to move block C on top of E?move block C on top of E?

C

B

A

F

E

D

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

15

Simple Solution

Rule: directly move block C on top of block E. Rule: directly move block C on top of block E. However, this rule could only be applied if both However, this rule could only be applied if both block C and block E had no blocks on top of them.block C and block E had no blocks on top of them.

Rule move-directlyIF 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 stackTHEN Move block ?upper on top of block ?lower

C F

E

F

E

C

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

16

Simple Solution – cont.

The simple blocks world does not require that The simple blocks world does not require that the blocks be restacked, just move them to the the blocks be restacked, just move them to the floor.floor.RULE Clear-upper-blockIF 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 ?xTHEN The goal is to move block ?above to the floor

RULE Clear-lower-blockIF 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 ?xTHEN The goal is to move block ?above to the floor

X

above

X

above

Xabove

X above

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

17

Blocks World – cont.

Since the floor is really not a block, it may Since the floor is really not a block, it may be necessary to treat it differently.be necessary to treat it differently.

RULE Move-to-floorIF The goal is to move block ?upper on top of the floor and block ?upper is the top block in its stackTHEN Move block ?upper on top of the floor

upper

upper

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

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

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

19

Floor and Nothing

The words The words floor and and nothing might be might be mistaken as the names of blocks.mistaken as the names of blocks.

Use implied deftemplate Use implied deftemplate block to identify to identify the blocks.the blocks.

(block A)(block B)(block C)(block D)(block E)(block F)

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

20

Goals

Goal described:Goal described:(deftemplate goal (slot move) (slot on-top-of)(deftemplate goal (slot move) (slot on-top-of)

Initial goal:Initial goal:(goal (move C) (on-top-of E))(goal (move C) (on-top-of E))

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

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

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

22

Move-directly Rule

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

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

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

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

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

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

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

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

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

1 2

C

B

A

F

E

D

C

B

A F

E

D

CB A F

E

D

CB A F

E

DB A F

E

D

C

Example5

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

26

Step-by-step Method

First: pseudorules were written using English-like First: pseudorules were written using English-like text.text.

Second: the pseudorules were used to determine Second: the pseudorules were used to determine the types of facts that would be required. the types of facts that would be required. Deftemplates describing the facts were designed, Deftemplates describing the facts were designed, and the initial knowledge for the program was and the initial knowledge for the program was coded using these deftemplates.coded using these deftemplates.

Finally, the pseudorules were translated to CLIPS Finally, the pseudorules were translated to CLIPS rules using the deftemplates as a guide for rules using the deftemplates as a guide for translation.translation.

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

27

Best Representation?

It is not always possible to determine the It is not always possible to determine the best method for representing facts or the best method for representing facts or the types of rules that will be needed to build an types of rules that will be needed to build an expert system.expert system.

Following a consistent methodology, Following a consistent methodology, however, can aid in the development of an however, can aid in the development of an expert system even when a great deal of expert system even when a great deal of prototyping and iteration need to be prototyping and iteration need to be performed.performed.

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

28

Multifield Wildcards

Multifield wildcards ($?) can be used to Multifield wildcards ($?) can be used to match against zero or more fields of a match against zero or more fields of a pattern.pattern.

Example: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 factwill not match a person fact(person (name Joe Public) (social-security-number 345-89-3039))

8.7

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

29

Multifield Wildcards

Multifield wildcards ($?) can be used to Multifield wildcards ($?) can be used to match against zero or more fields of a match against zero or more fields of a pattern.pattern.

Example: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 factwill 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.

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

30

Multifield Variables

Multifield variables are preceeded by a Multifield variables are preceeded by a “$?”.“$?”.

Example: construct showing how to print names Example: construct showing how to print names of all the children of a specified person.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))<Fact-3>CLIPS> (run)(John Q. Public) has children (Jane Paul Mary)CLIPS>

Example6

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

31

More than one multifield variable can be used in a single slot Example: find all the people who have a Example: find all the people who have a

child with a specific name.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))<Fact-3>CLIPS> (run)(John Q. Public) has child PaulOther children are (Jane) (Mary)CLIPS> (assert (find-child Rick))<Fact-4>CLIPS> (run)(Jack R. Public) has child RickOther children are () ()CLIPS> (assert (find-child Bill))<Fact-5>CLIPS> (run)CLIPS>

Example6

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

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-30 find-child: f-4, f-30 find-child: f-4, f-3For a total of 3 activations.CLIPS> (run)(Joe Fiveman) Has child JoeOther children are () (Joe Joe) (Joe Fiveman) Has child JoeOther children are (Joe) (Joe) (Joe Fiveman) Has child JoeOther 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

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

33

Implementing a Stack

A stack is an ordered data structure to A stack is an ordered data structure to which items can be added and removed.which items can be added and removed.

Items are added and removed to one “end” Items are added and removed to one “end” of the stack. A new item can be pushed of the stack. A new item can be pushed (added) to the stack or the last item added (added) to the stack or the last item added can be popped (removed) from the stack.can be popped (removed) from the stack.

In a stack the first value added is the last In a stack the first value added is the last item to be removed and the last item added item to be removed and the last item added is the first item to be removed.is the first item to be removed.

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

34

Push

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

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

35

Pop

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

Example7

(stack A X Y Z)

(pop-value)

(stack X Y Z)

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

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

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

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