CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2...

45
SEG 7450 1 CLIPS Tutorial 2 CLIPS Tutorial 2 Advanced Pattern Matching Advanced Pattern Matching

Transcript of CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2...

Page 1: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

SEG 7450 1

CLIPS Tutorial 2CLIPS Tutorial 2

Advanced Pattern MatchingAdvanced Pattern Matching

Page 2: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

2SEG 7450

Variables Variables

�� Variables in CLIPS are always written in a question mark followVariables in CLIPS are always written in a question mark followed by a ed by a symbolic field name.symbolic field name.

�� Some examples of variables:Some examples of variables:

?speed?speed?sensor?sensor?value?value?noun?noun?color?color

�� There should be no space between the question mark and the symboThere should be no space between the question mark and the symbolic field lic field name name

CLIPS>CLIPS>(( defruledefrule findfind -- blueblue -- eyeseyes

(person(person (name ?name)(name ?name) (eyes blue))(eyes blue))=>=>(printout t ?name (printout t ?name ““ has blue eyes.has blue eyes. ”” crlfcrlf ))

Page 3: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

3SEG 7450

Fact AddressFact Address

�� A variable can be bound to the fact address of the fact matchingA variable can be bound to the fact address of the fact matching a pattern on a pattern on

the LHS of a rule by using the pattern binding operator, the LHS of a rule by using the pattern binding operator, ““<<--””. .

�� Once the variable is bound it can be used with the retract, modiOnce the variable is bound it can be used with the retract, modify, or fy, or

duplicate commands in place of a fact index. duplicate commands in place of a fact index.

Page 4: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

4SEG 7450

Fact AddressFact Address

CLIPS> (clear)CLIPS> (clear)

CLIPS>CLIPS>

(( deftemplatedeftemplate personperson

(slot name)(slot name)

(slot address))(slot address))

CLIPS>CLIPS>

(( deftemplatedeftemplate movedmoved

(slot name)(slot name)

(slot address))(slot address))

CLIPS> CLIPS>

(( defruledefrule processprocess -- movedmoved-- informationinformation

?fl <?fl < -- (moved (name ?name)(moved (name ?name)

(address ?address))(address ?address))

?f2 <?f2 < -- (person (name ?name))(person (name ?name))

=>=>

(retract ?fl)(retract ?fl)

(modify ?f2 (address ?address))(modify ?f2 (address ?address))

Page 5: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

5SEG 7450

Fact AddressFact Address

CLIPS>CLIPS>(( deffactsdeffacts exampleexample

(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>(watch rules)CLIPS>(watch rules)CLIPS>(watch facts) CLIPS>(watch facts) CLIPS>(run)CLIPS>(run)FIREFIRE 1 process1 process -- movedmoved-- information: finformation: f -- 2,f2,f -- 11<== f<== f -- 22 (moved (name "John Hill")(moved (name "John Hill")

(address "37 Cherry Lane"))(address "37 Cherry Lane"))<== f<== f -- 11 (( person(nameperson(name "John Hill")"John Hill")

(address "25 Mulberry Lane(address "25 Mulberry Lane ““ ))))==> f==> f -- 33 (( person(nameperson(name ““ John HillJohn Hill ”” ))

(address (address ““ 37 Cherry Lane37 Cherry Lane ”” ))))CLIPS>CLIPS>

Page 6: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

6SEG 7450

SingleSingle--Field WildcardsField Wildcards

�� A A singlesingle--field wildcard field wildcard can be used when a field is required, but the value is can be used when a field is required, but the value is

not important. not important.

�� A A singlesingle--fieldfield wildcard is represented by a question mark. wildcard is represented by a question mark.

�� E.g. E.g.

�� (person (name ?first ?last)) (person (name ?first ?last))

�� (person (name ? ? ?last(person (name ? ? ?last--name))name))

�� When a singleWhen a single--field slot is left unspecified in a pattern, CLIPS treats it as field slot is left unspecified in a pattern, CLIPS treats it as if if

there is a singlethere is a single--field wildcard check for that slot.field wildcard check for that slot.

�� E.g. (person (name ?first ?last))E.g. (person (name ?first ?last))

�� Is equivalent to:Is equivalent to:

�� (person (name ?first ?last) (social(person (name ?first ?last) (social--securitysecurity--number ?))number ?))

Page 7: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

7SEG 7450

Blocks WorldBlocks World

Page 8: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

8SEG 7450

Blocks WorldBlocks World

Page 9: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

9SEG 7450

Blocks WorldBlocks World

�� Types of facts needed:Types of facts needed:

�� The information about which blocks are on top of other blocks isThe information about which blocks are on top of other blocks is crucial. crucial.

This information could be described with the following This information could be described with the following deftemplatedeftemplate: :

(( deftemplatedeftemplate onon-- toptop -- ofof

(slot upper)(slot upper)(slot lower))(slot lower))

�� The facts identifying the blocks from the special words nothing The facts identifying the blocks from the special words nothing and floor.and floor.

�� E.g. (block A)E.g. (block A)

�� A fact is needed to describe the blockA fact is needed to describe the block--moving goals.moving goals.

(( deftemplatedeftemplate goal (slot move) (slot ongoal (slot move) (slot on -- toptop -- of))of))

Page 10: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

10SEG 7450

Blocks WorldBlocks World

�� The initial configuration of the blocks world:The initial configuration of the blocks world:(( deffactsdeffacts initialinitial -- statestate(block A)(block A)(block B)(block B)(block C)(block C)(block D)(block D)(block E)(block E)(block F)(block F)(on(on -- toptop -- of (upper nothing) (lower A) ) of (upper nothing) (lower A) ) (on(on -- toptop -- of (upper A) (lower B)) of (upper A) (lower B)) (on(on -- toptop -- of (upper B) (lower C)) of (upper B) (lower C)) (on(on -- toptop -- of (upper C) (lower floor)) of (upper C) (lower floor)) (on(on -- toptop -- of (upper nothing) (lower D)) of (upper nothing) (lower D)) (on(on -- toptop -- of (upper D) (lower E)) of (upper D) (lower E)) (on(on -- toptop -- of (upper E) (lower F)) of (upper E) (lower F)) (on(on -- toptop -- of (upper F) (lower floor)) of (upper F) (lower floor)) (goal (move C) (on(goal (move C) (on -- toptop -- of E)))of E)))

Page 11: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

11SEG 7450

Blocks WorldBlocks World

�� The moveThe move--directly rule:directly rule:(( defruledefrule movemove-- directlydirectly

?goal <?goal < -- (goal (move ?block1)(goal (move ?block1)(on(on -- toptop -- of ?block2))of ?block2))

(block ?block1)(block ?block1)(block ?block2)(block ?block2)(on(on -- toptop -- of (upper nothing) of (upper nothing) (lower ?block1)) (lower ?block1)) ?stack?stack -- l <l < -- (on(on -- toptop -- of (upper ?block1) (lower ?block3))of (upper ?block1) (lower ?block3))?stack?stack -- 2 <2 < -- (on(on -- toptop -- of (upper nothing)of (upper nothing) (lower ?block2))(lower ?block2))

=>=>(retract ?goal ?stack(retract ?goal ?stack -- l ?stackl ?stack -- 2) 2) (assert (on(assert (on -- toptop -- of (upper ?block1) (lower ?block2))of (upper ?block1) (lower ?block2))(on(on -- toptop -- of (upper nothing) (lower ?block3)))of (upper nothing) (lower ?block3)))(printout t ?block1 " moved on top of" ?block2 (printout t ?block1 " moved on top of" ?block2

““ .. ”” crlfcrlf ))))

Page 12: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

12SEG 7450

Blocks WorldBlocks World

�� The moveThe move--toto--floor rule:floor rule:(( defruledefrule movemove-- toto -- floorfloor

?goal <?goal < -- (goal (move ?block1)(goal (move ?block1)

(on(on -- toptop -- of floor))of floor))

(block ?block1)(block ?block1)

(on(on -- toptop -- of (upper nothing) (lower ?block1)) of (upper nothing) (lower ?block1))

?stack <?stack < -- (on(on -- toptop -- of (upper ?block1)of (upper ?block1)

(lower ?block2))(lower ?block2))

=>=>

(retract ?goal ?stack)(retract ?goal ?stack)

(assert (on(assert (on -- toptop -- of (upper ?block1)of (upper ?block1)

(lower floor))(lower floor))

(on(on -- toptop -- of (upper nothing)of (upper nothing)

(lower ?block2)))(lower ?block2)))

(printout t ?(printout t ? blocklblockl " moved on top of floor." moved on top of floor. ”” crlfcrlf ) )) )

Page 13: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

13SEG 7450

Blocks WorldBlocks World

�� The clearThe clear--upperupper--block ruleblock rule(( defruledefrule clearclear -- upperupper -- block block

(goal (move ?block2)) (goal (move ?block2))

(block ?block1)(block ?block1)

(on(on -- toptop -- of (upper ?block2)(lower ?block1)) of (upper ?block2)(lower ?block1)) (block ?block2) (block ?block2)

=>=>

(assert (goal (move ?block2)(assert (goal (move ?block2)

(on(on -- toptop -- of floor))))of floor))))

Page 14: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

14SEG 7450

Blocks WorldBlocks World

�� The clearThe clear--lowerlower--block ruleblock rule(( defruledefrule clearclear -- lowerlower -- block block

(goal (on(goal (on -- toptop -- of ?block1)) of ?block1))

(block ?block1)(block ?block1)

(on(on -- toptop -- of (upper ?block2) (lower ?block1))of (upper ?block2) (lower ?block1))

(block ?block2)(block ?block2)

=>=>

(assert (goal (move ?block2)(assert (goal (move ?block2)

(on(on -- toptop -- of floor))))of floor))))

Page 15: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

15SEG 7450

Blocks WorldBlocks World

�� A sample run of the programA sample run of the programCLIPS> CLIPS> ((unwatchunwatch all)all)

CLIPS> (reset)CLIPS> (reset)

CLIPS> (run)CLIPS> (run)

A moved on top of floorA moved on top of floor

B moved on top of floorB moved on top of floor

D moved on top of floorD moved on top of floor

C moved on top of EC moved on top of E

CLIPS> CLIPS>

Page 16: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

16SEG 7450

MultifieldMultifield Wildcards and VariablesWildcards and Variables

�� MultifieldMultifield wildcards and variables can be used to match against zero or mowildcards and variables can be used to match against zero or more re

fields of a pattern. fields of a pattern.

�� The The multitieldmultitield wildcard is indicated by a dollar sign followed by a question wildcard is indicated by a dollar sign followed by a question

mark, mark, ““$?$?””, and represents zero or more occurrences of a field. , and represents zero or more occurrences of a field.

�� The The person person pattern will match any name slot that contains at least one fielpattern will match any name slot that contains at least one field and has d and has

as its last field the specified name:as its last field the specified name:

(( defruledefrule printprint -- socialsocial -- securitysecurity -- numbers numbers

(print(print -- ssss -- numbersnumbers -- for ?lastfor ?last -- name)name)

(person (name $? ?last(person (name $? ?last -- name)name)

(social(social -- securitysecurity -- number ?number ? ssss -- number))number))

=>=>

(printout t ?(printout t ? ssss -- number number crlfcrlf ))))

Page 17: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

17SEG 7450

MultifieldMultifield Wildcards and VariablesWildcards and Variables

�� MultifieldMultifield variables are preceded by a "$?"variables are preceded by a "$?"(( deftemplatedeftemplate person person

(( multislotmultislot name) name) (( multislotmultislot children))children))

(( deffactsdeffacts somesome-- peoplepeople(person (name John Q. Public)(person (name John Q. Public)

(children Jane Paul Mary))(children Jane Paul Mary))(person (name Jack R. Public)(person (name Jack R. Public)

(children Rick) ) )(children Rick) ) )

(( defruledefrule printprint -- children children (print(print -- children $?name) children $?name) (person (name $?name)(person (name $?name)

(children $?children))(children $?children))=>=>(printout t ?name " has children" ?children(printout t ?name " has children" ?children

crlfcrlf ) )) )

Page 18: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

18SEG 7450

MultifieldMultifield Wildcards and VariablesWildcards and Variables

�� The following dialog shows how the The following dialog shows how the printprint--children children rule works:rule works:

CLIPS> (reset)CLIPS> (reset)

CLIPS> (assert (printCLIPS> (assert (print -- children John Q. Public) <Factchildren John Q. Public) <Fact -- 3>3>

CLIPS> (run)CLIPS> (run)

(John Q. Public) has children (Jane Paul Mary) (John Q. Public) has children (Jane Paul Mary)

CLIPS>CLIPS>

�� More than one More than one multifieldmultifield variable can be used in a single slot:variable can be used in a single slot:

(( defruledefrule findfind -- childchild

(find(find -- child ?child)child ?child)

(person (name $?name)(person (name $?name)

(children $?before ?child $?after))(children $?before ?child $?after))

=>=>

(printout t ?name (printout t ?name ““ has child has child ““ ?child ?child crlfcrlf ))

(printout t (printout t ““ Other children are Other children are ““ $?before $?before ““ ““ $?after $?after crlfcrlf ))))

Page 19: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

19SEG 7450

Blocks World RevisitedBlocks World Revisited

�� Reimplementation with Reimplementation with multifieldmultifield wildcards and variableswildcards and variables

�� Stacks are represented as single facts.Stacks are represented as single facts.

(( deffactsdeffacts initialinitial -- statestate

(stack A B C)(stack A B C)

(stack D E F)(stack D E F)

(goal (move C) (on(goal (move C) (on -- toptop -- of E)) of E))

(stack) )(stack) )

�� The empty The empty stack stack fact is included to prevent this fact from being added later.fact is included to prevent this fact from being added later.

Page 20: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

20SEG 7450

Blocks World RevisitedBlocks World Revisited

(( defruledefrule movemove-- directlydirectly?goal <?goal < -- (goal (move ?(goal (move ? blocklblockl ))

(on(on -- toptop -- of ?block2))of ?block2))?stack?stack -- l <l < -- (stack ?block1 $?rest1)(stack ?block1 $?rest1)?stack?stack -- 2 <2 < -- (stack ?block2 $?rest2)(stack ?block2 $?rest2)=>(retract ?goal ?stack=>(retract ?goal ?stack -- 1 ?stack1 ?stack -- 2) 2) (assert (stack $?rest1))(assert (stack $?rest1))(assert (stack ?block1 ?block2 $?rest2)) (assert (stack ?block1 ?block2 $?rest2)) (printout t ?block1 " moved on top of (printout t ?block1 " moved on top of ““

?block2 "." ?block2 "." crlfcrlf ))))

Page 21: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

21SEG 7450

Blocks World RevisitedBlocks World Revisited

(( defruledefrule movemove-- toto -- floorfloor

?goal <?goal < -- (goal (move ?(goal (move ? blocklblockl ) )

(on(on -- toptop -- of floor)) of floor))

?stack?stack -- l <l < -- (stack ?block1 $?rest)(stack ?block1 $?rest)

=>=>

(retract ?goal ?stack(retract ?goal ?stack -- l) l)

(assert (stack ?(assert (stack ? blocklblockl ))))

(assert (stack $?rest)) (assert (stack $?rest))

(printout t ?(printout t ? blocklblockl " moved on top of floor" moved on top of floor

crlfcrlf ))))

Page 22: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

22SEG 7450

Blocks World RevisitedBlocks World Revisited

(( defruledefrule clearclear -- upperupper -- block block

(goal (move ?block1)) (goal (move ?block1))

(stack ?top $? ?block1 $?)(stack ?top $? ?block1 $?)

=>=>

(assert (goal (move ?(assert (goal (move ? top)(ontop)(on -- toptop -- of floor))))of floor))))

(( defruledefrule clearclear -- IowerIower -- block block

(goal (on(goal (on -- toptop -- of ?of ? blocklblockl )) ))

(stack ?top $? ?(stack ?top $? ? blocklblockl $?) $?)

=>(assert (goal (move ?=>(assert (goal (move ? top)(ontop)(on -- toptop -- of floor))))of floor))))

Page 23: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

23SEG 7450

Field ConstraintsField Constraints

�� The The NotNot Field ConstraintField Constraint

�� Its symbol is the tilde, Its symbol is the tilde, ““~.~.””

�� The The not not constraint acts on the one constraint or variable that immediateconstraint acts on the one constraint or variable that immediately ly

follows it.follows it.

(( defruledefrule personperson -- withoutwithout -- brownbrown -- hairhair

(person (name ?name) (hair ~brown))(person (name ?name) (hair ~brown))

=>=>

(printout t ?name " does not have brown hair"(printout t ?name " does not have brown hair"

crlfcrlf ) ) ) )

Page 24: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

24SEG 7450

The The OrOr Field ConstraintField Constraint

�� The The or or constraintconstraint�� represented by the bar, represented by the bar, ““||““�� is used to allow one or more possible values to match a field ofis used to allow one or more possible values to match a field of a patterna pattern

(( defruledefrule blackblack -- oror -- brownbrown -- hairhair

(person (name ?name) (hair brown | black))(person (name ?name) (hair brown | black))

=>=>

(printout t ?name " has dark hair" (printout t ?name " has dark hair" crlfcrlf ))))

Page 25: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

25SEG 7450

The The And And Field ConstraintField Constraint

�� The symbol of the The symbol of the and and constraint is the ampersand, constraint is the ampersand, ““&&””

(( defruledefrule blackblack -- oror -- brownbrown -- hairhair

(person (name ?name) (hair ?(person (name ?name) (hair ? color&brown|blackcolor&brown|black ) )) )

=>=>

(printout t ?name " has" ?color " hair" (printout t ?name " has" ?color " hair" crlfcrlf ))))

Page 26: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

26SEG 7450

Functions and Expressions Functions and Expressions

�� CLIPS Elementary Arithmetic OperatorsCLIPS Elementary Arithmetic Operators

�� A numeric expression must be written in prefix form A numeric expression must be written in prefix form

Infix form: (y2 Infix form: (y2 –– y1) / (x2 y1) / (x2 –– x1)>0x1)>0

Prefix form: ( > (/ (Prefix form: ( > (/ ( -- y2 y1)(y2 y1)( -- x2 x1)) 0)x2 x1)) 0)

Page 27: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

27SEG 7450

Functions and ExpressionsFunctions and ExpressionsCLIPS> (+ 2 3.0)CLIPS> (+ 2 3.0)5.05.0CLIPS> (+ 2.0 3)CLIPS> (+ 2.0 3)5.05.0CLIPS> (+ 2 3)CLIPS> (+ 2 3)55CLIPS> CLIPS> CLIPS> (+ 2 3 4) <CLIPS> (+ 2 3 4) < -- Evaluation proceeds from left to rightEvaluation proceeds from left to right

99CLIPS> (assert (answer (+ 2 2)))CLIPS> (assert (answer (+ 2 2)))<Fact<Fact -- 0>0>CLIPS> (facts)CLIPS> (facts)ff -- 0 0 (answer 4)(answer 4)For a total of 1 fact.For a total of 1 fact.CLIPS> (clear)CLIPS> (clear)CLIPS> (assert (expression 2 + 3 * 4))CLIPS> (assert (expression 2 + 3 * 4))<Fact<Fact -- 0>0>CLIPS> (facts)CLIPS> (facts)ff -- 0 0 (expression 2 + 3 * 4)(expression 2 + 3 * 4)For a total of 1 fact.For a total of 1 fact.CLIPS>CLIPS>

Page 28: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

28SEG 7450

Summing Values using RulesSumming Values using Rules

�� As a simple example of using functions to perform calculationsAs a simple example of using functions to perform calculations

�� consider the problem of summing up the area of a group of rectanconsider the problem of summing up the area of a group of rectangles.gles.

�� The heights and widths are specified with the following The heights and widths are specified with the following deftemplatedeftemplate..

(( deftemplatedeftemplate rectangle (slot height) (slot width) )rectangle (slot height) (slot width) )

�� A A deffactsdeffacts containing sample informationcontaining sample information

(( deffactsdeffacts initialinitial -- information information

(rectangle (height 10) (width 6)) (rectangle (height 10) (width 6))

(rectangle (height 7) (width 5)) (rectangle (height 7) (width 5))

(rectangle (height 6) (width 8)) (rectangle (height 6) (width 8))

(rectangle (height 2) (width 5)) (rectangle (height 2) (width 5))

(sum 0))(sum 0))

Page 29: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

29SEG 7450

Summing Values using RulesSumming Values using Rules

�� An attempt to produce a rule to sum the rectangle:An attempt to produce a rule to sum the rectangle:

(( defruledefrule sumsum-- rectanglesrectangles

(rectangle (height ?height) (width ?width))(rectangle (height ?height) (width ?width))

?sum <?sum < -- (sum ?total)(sum ?total)

=>=>

(retract ?sum)(retract ?sum)

(assert (sum (+ ?total (* ?height ?width)))))(assert (sum (+ ?total (* ?height ?width)))))

�� This rule, however, will loop endlessly. This rule, however, will loop endlessly.

�� One solution to solve the problem would be to retract the One solution to solve the problem would be to retract the rectangle rectangle

fact after its area was added to the fact after its area was added to the sum sum fact. fact.

�� If the If the rectangle rectangle fact needs to be preserved, a different approach is fact needs to be preserved, a different approach is

required. required.

Page 30: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

30SEG 7450

Summing Values using RulesSumming Values using Rules

(( defruledefrule sumsum-- rectanglesrectangles

(( rectangle(heightrectangle(height ??height)(widthheight)(width ?width))?width))

=>=>

(assert (add(assert (add -- toto -- sum (* ?height ?width))))sum (* ?height ?width))))

(( defruledefrule sumsum-- areasareas

?sum <?sum < -- (sum ?total)(sum ?total)

?new?new-- area <area < -- (add(add -- toto -- sum ?area) sum ?area)

=>=>

(retract ?sum ?new(retract ?sum ?new -- area)area)

(assert (sum (+ ?total ?area))))(assert (sum (+ ?total ?area))))

Page 31: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

31SEG 7450

TThehe BINDBIND FunctionFunction

�� The The bind bind function can be used to bind the value of a variable to the valufunction can be used to bind the value of a variable to the value of e of an expression. an expression.

(bind <variable> <value)(bind <variable> <value)�� The bound variableThe bound variable��<variable><variable>��uses the syntax of a singleuses the syntax of a single--field variable.field variable.

�� The new valueThe new value��<value><value>��should be an expression that evaluates to either a should be an expression that evaluates to either a singlesingle--field or a field or a multifieldmultifield value.value.

(( defruledefrule sumsum-- areasareas?sum <?sum < -- (sum ?total)(sum ?total)?new?new-- area <area < -- (add(add -- toto -- sum ?area)sum ?area)=>=>(retract ?sum ?new(retract ?sum ?new -- area)area)(printout t "Adding" ?area " to " ?total (printout t "Adding" ?area " to " ?total crlfcrlf ) ) (bind ?new(bind ?new -- total (+ ?total ?area))total (+ ?total ?area))(printout t "New sum is " ?new(printout t "New sum is " ?new -- total total crlfcrlf ) ) (assert (sum ?new(assert (sum ?new -- total)))total)))

Page 32: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

32SEG 7450

I/O FunctionsI/O Functions

�� The The ReadRead Function Function �� CLIPS allows information to be read from the keyboard using the CLIPS allows information to be read from the keyboard using the read function. read function.

CLIPS> {clear)CLIPS> {clear)CLIPS>CLIPS>(( defruledefrule getget -- firstfirst -- namename

=>=>(printout t "What is your first name? ") (printout t "What is your first name? ") (bind ?response (read))(bind ?response (read))(assert (user's(assert (user's -- name ?response)))name ?response)))

CLIPS> {reset)CLIPS> {reset)CLIPS> {run)CLIPS> {run)What is your first name? GaryWhat is your first name? GaryCLIPS> {facts)CLIPS> {facts)ff -- 0 (initial0 (initial -- fact)fact)ff -- 11 (user's(user's -- name Gary)name Gary)For a total of 2 facts.For a total of 2 facts.CLIPS>CLIPS>

Page 33: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

33SEG 7450

I/OI/O FFunctionsunctions

�� The The OpenOpen FunctionFunction

�� Before a file can be accessed for reading or writing, it must beBefore a file can be accessed for reading or writing, it must be opened using the opened using the

open function. open function.

(open <file(open <file -- name> <filename> <file -- ID> [<fileID> [<file -- access>])access>])

�� As an example,As an example,

�� (open "(open " input.datinput.dat " data "r")" data "r")

�� File Access ModesFile Access Modes

Page 34: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

34SEG 7450

I/OI/O FFunctionsunctions

�� If <fileIf <file--access> is not included as an argument, the default value of access> is not included as an argument, the default value of ““rr””will be used. will be used.

�� The The open open function acts as a predicate function. function acts as a predicate function.

�� It returns the symbol It returns the symbol TRUE TRUE if a file was successfully opened; if a file was successfully opened; otherwise the symbol otherwise the symbol FALSE FALSE is returned .is returned .

Page 35: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

35SEG 7450

I/OI/O FFunctionsunctions

�� The The CloseClose FunctionFunction

�� Once access is no longer needed to a file, it should be closed. Once access is no longer needed to a file, it should be closed.

(close [<file(close [<file -- ID>])ID>])�� Where the optional argument <fileWhere the optional argument <file--ID> specifies the logical name of the ID> specifies the logical name of the

file to be closed. If <filefile to be closed. If <file--ID> is not specified, all open files will be closed. ID> is not specified, all open files will be closed.

�� E.g.E.g.

(close data)(close data)

Page 36: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

36SEG 7450

I/OI/O FFunctionsunctions

�� Reading and Writing to a FileReading and Writing to a File�� The use of logical names allows input and output to and from othThe use of logical names allows input and output to and from other er

sources. sources.

CLIPS> (open "CLIPS> (open " example.datexample.dat " example "w" example "w ““ ) ) TRUETRUECLIPS> (printout example "green" CLIPS> (printout example "green" crlfcrlf ))CLIPS> (printout example 7 CLIPS> (printout example 7 crlfcrlf ))CLIPS> (close example) CLIPS> (close example) TRUETRUECLIPS>CLIPS>

Page 37: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

37SEG 7450

I/OI/O FFunctionsunctions

�� The general format of the The general format of the read read function is:function is:

(read [<logical(read [<logical -- name>])name>])�� The The read read function defaults to reading from the standard input device, function defaults to reading from the standard input device, t, t, if it is if it is

given no arguments. given no arguments.

CLIPS> (open "CLIPS> (open " example.datexample.dat " example "r")" example "r")TRUETRUECLIPS>(read example)CLIPS>(read example)GreenGreenCLIPS>(read example)CLIPS>(read example)77CLIPS>(read example)CLIPS>(read example)EOFEOFCLIPS>(read example)CLIPS>(read example)TRUETRUECLIPS>CLIPS>

Page 38: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

38SEG 7450

I/OI/O FFunctionsunctions

�� The The Format Format FunctionFunction

(format <logical(format <logical -- name> <controlname> <control -- string> string> <parameters>*)<parameters>*)

�� control string, which must be contained within double quotes. control string, which must be contained within double quotes.

�� The control string consists of format flagsThe control string consists of format flags

�� The return value of the The return value of the format format function is the formatted string. function is the formatted string.

�� If the logical name If the logical name nil nil is used with the format command, then no output is used with the format command, then no output is printed (either to the terminal or a file), but the formattedis printed (either to the terminal or a file), but the formatted string is still string is still returned.returned.

CLIPS> (format nil "Name: %CLIPS> (format nil "Name: % -- 15s Age: %3d15s Age: %3d ””"Bob Green" 35)"Bob Green" 35)

"Name:"Name: Bob GreenBob Green Age:35Age:35 ““CLIPS> (format nil "Name: %CLIPS> (format nil "Name: % -- 15s Age: %3d15s Age: %3d ””"Ralph "Ralph HeidenHeiden " 32)" 32)"Name:"Name: Ralph Ralph HeidenHeiden Age:Age: 3232““CLIPS>CLIPS>

Page 39: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

39SEG 7450

I/OI/O FFunctionsunctions

�� The format flag The format flag ““%%--15s15s”” is used to print the name in a column that is 15 is used to print the name in a column that is 15 characters wide. characters wide.

�� The The -- sign indicates that the output is to be left justified and the sign indicates that the output is to be left justified and the character s character s indicates that a string or symbol is to be printed. indicates that a string or symbol is to be printed.

�� The general specification of a format flag is:The general specification of a format flag is:

%%--M.NxM.Nx

�� where where ““--”” is optional and means to left justify. The default is to right is optional and means to left justify. The default is to right justify. justify.

�� The letter M is a number specifying the field width in columns. The letter M is a number specifying the field width in columns.

�� The letter N is an optional number specifying the number of digiThe letter N is an optional number specifying the number of digits past ts past the decimal point that will be printed. the decimal point that will be printed.

�� The letter x is a character specifying the display format specifThe letter x is a character specifying the display format specification.ication.

Page 40: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

40SEG 7450

I/OI/O FFunctionsunctions

�� Display format SpecificationsDisplay format Specifications

Page 41: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

41SEG 7450

I/OI/O FFunctionsunctions

�� The The ReadlineReadline FunctionFunction�� The The readlinereadline function can be used to read an entire line of input. function can be used to read an entire line of input.

(( readlinereadline [<logical[<logical -- name>])name>])CLIPS> (clear)CLIPS> (clear)CLIPS>CLIPS>((defruledefrule getget--namename

=>=>(printout t "What is your name? ") (printout t "What is your name? ") (bind ?response ((bind ?response (readlinereadline))))(assert (user's(assert (user's--name ?response)))name ?response)))

CLIPS> (reset)CLIPS> (reset)CLIPS> (run)CLIPS> (run)What is your name? Gary RileyWhat is your name? Gary RileyCLIPS> (facts)CLIPS> (facts)ff--0 0 (initial(initial--fact)fact)ff--11 (user' s(user' s--name "Gary Riley")name "Gary Riley")For a total of 2 facts.For a total of 2 facts.CLIPS>CLIPS>

Page 42: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

42SEG 7450

Predicate FunctionsPredicate Functions

�� A predicate function is defined to be any function that returns A predicate function is defined to be any function that returns either the either the symbol symbol TRUE TRUE or the symbol or the symbol FALSE.FALSE.

�� Predicate functions may be either predefined or userPredicate functions may be either predefined or user--defined functions. defined functions. �� Predefined functions are those functions already provided by CLIPredefined functions are those functions already provided by CLIPS. PS.

�� UserUser--defined or external functions are functions other than predefinedefined or external functions are functions other than predefined functions d functions that are written in C or another language and linked with CLIPS.that are written in C or another language and linked with CLIPS.

CLIPS> (and(> 4 3)(> 4 5))CLIPS> (and(> 4 3)(> 4 5))FALSEFALSECLIPS> (or(> 4 3)(> 4 5))CLIPS> (or(> 4 3)(> 4 5))TRUETRUECLIPS> (> 4 3)CLIPS> (> 4 3)TRUETRUECLIPS> (< 6 2)CLIPS> (< 6 2)FALSEFALSECLIPS> (CLIPS> ( integerpintegerp 3)3)TRUETRUE

Page 43: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

43SEG 7450

TThe Test he Test CConditional Elementonditional Element

�� The test conditional element provides a powerful way to evaluateThe test conditional element provides a powerful way to evaluate expressions expressions on the LHS of the rule. on the LHS of the rule.

�� Instead of pattern matching against a fact in the fact list, theInstead of pattern matching against a fact in the fact list, the test CE evaluates test CE evaluates an expression. an expression.

�� If the expression evaluates to any value other than the symbol If the expression evaluates to any value other than the symbol FALSE, FALSE, the the test CE is satisfied. If the expression evaluates to the symbol test CE is satisfied. If the expression evaluates to the symbol FALSE, FALSE, the test the test CE is not satisfied. CE is not satisfied.

(test <predicate(test <predicate -- function>)function>)

e.g. (test (> ?size 1))e.g. (test (> ?size 1))

Page 44: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

44SEG 7450

TThe Predicate Field Constrainthe Predicate Field Constraint

�� The The predicate field constraint, predicate field constraint, :, is useful for performing predicate tests :, is useful for performing predicate tests directly within patterns. directly within patterns.

�� It can stand by itself in a field or be used as one part of a moIt can stand by itself in a field or be used as one part of a more complex field re complex field using the using the --, &, and | connective field constraints., &, and | connective field constraints.

�� The predicate field constraint is always followed by a function The predicate field constraint is always followed by a function for evaluationfor evaluation

(pile(pile -- size ?size) size ?size)

(test (> ?size 1))(test (> ?size 1))

�� These two patterns could be replaced with the following single pThese two patterns could be replaced with the following single pattern:attern:

(pile(pile -- size ?size&:(> ?size 1))size ?size&:(> ?size 1))

Page 45: CLIPS Tutorial 2 - Old Dominion UniversityCLIPS Tutorial 2 Advanced Pattern Matching. SEG 7450 2 Variables Variables in CLIPS are always written in a question mark follow ed by a symbolic

45SEG 7450

TThe Return Value Field Constrainthe Return Value Field Constraint

�� The The return value field constraint, =, return value field constraint, =, allows the return value of a function allows the return value of a function to be used for comparison inside a pattern. to be used for comparison inside a pattern.

�� Like the predicate field constraint, the return value field consLike the predicate field constraint, the return value field constraint must be traint must be followed by a function. followed by a function.

�� However, the function does not have to be a predicate function. However, the function does not have to be a predicate function.

�� The only restriction is that the function must have a singleThe only restriction is that the function must have a single--field return value. field return value.

�� For example, the field constraint: For example, the field constraint:

=(mod ?size 4)=(mod ?size 4)�� could be read as could be read as ““The field is equal to ?size modulus 4.The field is equal to ?size modulus 4.””