of 34
8/12/2019 CLIPS 3
1/34
CLIPSProgramming3
ModularDesign,Execution
Control,andRuleEfficiency
SEEM 5750 1
8/12/2019 CLIPS 3
2/34
CLIPSprovi esanum ero s otattri utest atcan especi ie w ena
deftemplatesslotsaredefined.
Itis ossibletodefinetheallowedt esandvaluesthatcanbestoredina
slot.
SEEM 5750 2
8/12/2019 CLIPS 3
3/34
T etypeattri ute e inest e atatypest atcan ep ace inas ot.
Thegeneralformatofthetypeattributeis
t e
whereiseither
?VARIABLEor
SYMBOL,
STRING,
LEXEME,
INTEGER,
FLOAT,
NUMBER,
INSTANCENAME
INSTANCEADDRESS,
INSTANCE, FACTADDRESS,or
- .
SEEM 5750 3
8/12/2019 CLIPS 3
4/34
xamp e:
( def t empl at e per son
( mul t i sl ot name ( t ype SYMBOL) )( sl ot age ( t ype I NTEGER) ) )
Forexample,assigningthesymbolfourtotheageslotratherthanthe
integer4will
cause
an
error
as
shown:
CLI PS> ( asser t ( per son ( name Fr ed Smi t h)
[CSTRNCHK1] Aliteralslotvaluefoundintheassertcommanddoesnotmatchtheallowedtypesforslotage.
>
SEEM 5750 4
8/12/2019 CLIPS 3
5/34
CLIPSa soa owsyoutospeci ya isto a owe va ues oraspeci ictype.
Forexample,ifagenderslotisaddedtothepersondeftemplate,the
alloweds mbolsforthatslotcanberestrictedtomaleand emale:
( def t empl at e per son(
mul t i sl ot name ( t ype SYMBOL) )
sl ot a e t e I NTEGER
( sl ot gender ( t ype SYMBOL)( al l owed- symbol s mal e f emal e) ) )
SEEM 5750 5
8/12/2019 CLIPS 3
6/34
erearee g eren a owe va uea r u esprov e y :
allowedsymbols,allowedstrings,allowedLexemes,allowedintegers,allowedfloats,
allowednumbers,
allowed
instance
names,
and
allowed
values.
,genderslottobeingasymbol.
Itmerelyindicatesthatiftheslotsvalueisasymbol,thenitmustbeoneofthetwos mbols:eithermaleor emale.
Anystring,
integer,
or
float
would
be
alegal
value
for
the
gender
slot
if
the
(typeSYMBOL)attributewereremoved
Theallowedvaluesattributecanbeusedtocom letel restrictthesetofallowedvaluesforaslottoaspecifiedlist.
( def t empl at e per son
( mul t i sl ot name ( t ype SYMBOL) )( sl ot age ( t ype I NTEGER) )( sl ot gender ( al l owed- val ues mal e f emal e) ) )
SEEM 5750 6
8/12/2019 CLIPS 3
7/34
T erangeattri utea owst especi icationo minimuman maximum
numericvalues.
The eneralformatoftheran eattributeis
(range)
whereandareeither?VARlABLEoranumeric
.
Example:
( def t empl at e per son
( mul t i sl ot name ( t ype SYMBOL) )
( sl ot age ( t ype I NTEGER) ( r ange 0 ?VARI ABLE) ) )
SEEM 5750 7
8/12/2019 CLIPS 3
8/34
maximumnumberofvaluesthatcanbestoredinamultislot.
Thegeneral
format
of
the
cardinality
attribute
is
- - whereandareeither?VARIABLEorapositive
integer.
, ,
valuecontained
in
amultislot.
Example:
( def t em l at e vol l e bal l - t eam( sl ot name ( t ype STRI NG) )( mul t i sl ot pl ayer s ( t ype STRI NG)
( car di nal i t y 6 6) )( mul t i sl ot al t er nat es ( t ype STRI NG)
( car di nal i t y 0 2) ) )
SEEM 5750 8
8/12/2019 CLIPS 3
9/34
so enconven en oau oma ca y aveaspec e va ues ore naslotifnovalueisexplicitlystatedinanassertcommand.
Thegeneral
format
of
the
default
attribute
is
(default)
whereiseither?DERIVE,?NONE,asingleexpression(forasinglefieldslot),orzeroormoreexpressions(foramultifieldslot).
If
the
default
attribute
is
not
specified
for
a
slot,
then
it
is
assumed
to
be
(default?DERIVE).
Forasinglefieldslot,thismeansthatavalueisselectedthatsatisfiesthetype,range,andallowedvaluesattributesfortheslot.
Thederiveddefaultvalueforamultifieldslotwillbealistofidenticalvalues
thatare
the
minimum
allowed
cardinality
for
the
slot
(zero
by
default).
SEEM 5750 9
8/12/2019 CLIPS 3
10/34
nexamp eo er ve va ues s e o ow ng:CLI PS> ( cl ear )
CLI PS> ( def t empl at e exampl e
( s l ot a)
( sl ot b ( t ype I NTEGER) )
( sl ot c ( al l owed- val ues r ed gr een bl ue) )
( mul t i s l ot d)
( mul t i s l ot e ( car di nal i t y 2 2)
( t ype FLOAT)
( r ange 3. 5 l 0. 0) ) )
CLI PS> ( asser t ( exampl e) )
CLI PS> ( f act s)
f 0 ( exampl e ( a ni l )
( b 0)
(c red)
( d)
( e 3. 5 3. 5) )
For a t ot al of 1 f act.
SEEM 5750 10
8/12/2019 CLIPS 3
11/34
.whenthefactisasserted.
CLI PS> cl earCLI PS>( def t empl at e exampl e( sl ot a)
( sl ot b ( def aul t ?NONE) ) )CLI PS> ( asser t ( exampl e) )[ TMPLTRHS1] Sl ot b r equi r es a val ue because of i t s( def aul t ?NONE) at t r i but e.CLI PS> ( asser t ( exampl e ( b 1) ) )
CLI PS> ( f act s)f - 0 ( exampl e ( a ni l ) ( b 1) )For a t ot al of 1 f act .LI PS>
SEEM 5750 11
8/12/2019 CLIPS 3
12/34
:
CLI PS> ( cl ear )
CLI PS>
( sl ot a ( def aul t 3) )( sl ot b ( def aul t ( + 3 4) ) )
( mul t i sl ot d ( def aul t ( + 1 2) ( + 3 4) ) ) )CLI PS> ( asser t ( exampl e) ) ( f act s)f - 0 ( exampl e ( a 3) ( b 7) ( c a b c) ( d 3 7) )
For a t ot al of 1 f act .CLI PS>
SEEM 5750 12
8/12/2019 CLIPS 3
13/34
New unctionsare e ine usingt e e unctionconstruct.
Thegeneralformatofadeffunctionis:
- -
( r egul ar - par amet er >* [ ] )
*)
ere regu arparame er sas ng e e var a ean w car
parameter>is
amultifield
variable.
Thenameofthedeffunction,,mustbedistinct.
Thebodyofthedeffunction,representedby*,isaseriesof
expressionssimilartotheRHSofarulethatareexecutedinorderwhenthe
deffunctionis
called.
Unlikepredefinedfunctions,deffunctionscanbedeletedandthewatch
commandcanbeusedtotracetheirexecution.
SEEM 5750 13
8/12/2019 CLIPS 3
14/34
T ean ec arat ons
Specifytheargumentsthatwillbepassedintothedeffunctionwhenitis
called.
Adeffunctioncanreturnvalues.
Thereturnvalueisthatvalueofthelastexpressionevaluatedwithinthebody
of the deffunction.
E.g.( def f unct i on hypot enuse- l engt h ( ?a ?b)
( ** ( + ( * ?a ?a) ( * ?b ?b) ) 0. 5) )
Wherethe**functionwithitssecondargumentof0.5
computethe
square
root
(**)isthefirstargumentraised
tothepowerofthesecondargument
SEEM 5750 14
8/12/2019 CLIPS 3
15/34
Itcan eca e romt ecomman prompt:
CLI PS> ( hypot enuse- l engt h 3 4)
.
CLI PS>
Inamorereadableformat:
( def f unct i on hypot enuse- l engt h ( ?a ?b)( bi nd ?t emp ( + ( * ?a ?a) ( * ?b ?b) ) )
emp .
SEEM 5750 15
8/12/2019 CLIPS 3
16/34
e e urn unc on
Itallowsthecurrentlyexecutingdeffunctiontobeterminated.
Itssyntax
for
use
with
deffunctions:
( r et ur n [ ] )
Ifisspecified,theresultofitsevaluationisusedasthereturnvalue.
E.g.
( def f unct i on hypot enuse- l engt h ( ?a ?b)
( bi nd ?t emp ( + ( * ?a ?a) ( * ?b ?b) ) )
( r et ur n ( ** ?t emp 0. 5) ) )
OR
( def f unct i on hypot enuse- l engt h ( ?a ?b)( bi nd ?t emp ( + ( * ?a ?a) ( * ?b ?b) ) )
( bi nd ?c ( ** ?t emp 0. 5) )
r et ur n ?c
SEEM 5750 16
8/12/2019 CLIPS 3
17/34
Watc ingDe unctions
Whendeffunctionsarewatchedusingwatchcommand,aninformational
messageis
printed
whenever
adeffuction
begins
or
ends
execution.
CLI PS> ( wat ch def f ucnt i ons)
CLI PS> ( unwat ch def f unct i ons)
Watchspecificdeffucntions
CLI PS> ( wat ch def f ucnt i ons hypot enuse- l engt h)
CLI PS>
Wildcardparameter
Ifthelastparameterdeclaredinadeffunctionisamultifieldvariable,whichis
,
moreargumentsthanarespecifiedintheparameterlist.
SEEM 5750 17
8/12/2019 CLIPS 3
18/34
De unctioncomman s
Displaythetextrepresentationsofadeffunction:
deffunction
Deleteadeffunction:
(undeffunction)
sp ay e s o e unc ons e n e :
(listdeffunctions
[])
Returnsamultifieldvaluecontainningthelistofdeffucntions.
(getdeffunctionlist[])
SEEM 5750 18
8/12/2019 CLIPS 3
19/34
G o a varia es:
CLIPSallowsonetodefinevariablesthatretaintheirvaluesoutsidethescope
ofaconstruct
Localvariables:
(defruleexample1
data1?x
=>(printoutt?x=?xcrlf))
defrule exam le1
(data2?x)
=>
=
Thevalueof?xinruleexample1doesnotconstraininanywaythevalueof?xin
ruleexample2.
SEEM 5750 19
8/12/2019 CLIPS 3
20/34
T egenera ormato a e g o a is:
(defglobal[]*)
Where< lobal
assi nment>
is:
=
Andis:
* *
Globalvariable
names
begin
and
end
with
the
*character.
?xisalocalvariable
x sag o a var a e.
E.g.
CLI PS> ( def gl obal ?*x* = 3
* * * *
CLI PS> ?*x*
3
CLI PS> ?*y*
4
CLI PS>SEEM 5750 20
8/12/2019 CLIPS 3
21/34
Examp e:
CLI PS> ( def r ul e ar ea
( r adi us ?r )
=>
( bi nd ?ar ea ( * ?*pi * ?*pi * ?r ) )
" " =
CLI PS> ( def f act s ar ea_ci r cl e ( r adi us 4) )
CLI PS> ( r eset )
CLI PS> ( r un)
Ar ea = 39. 47841751413609
Thevalueofadefglobalcanbechangedusingthebindcommand.
SEEM 5750 21
8/12/2019 CLIPS 3
22/34
rules:salienceandmodules.
Theuse
of
the
keyword
salience
allows
the
priority
of
rules
to
be
explicitly
.
Normallytheagendaactslikeastack.
themostrecentactivationplacedontheagendaisthefirsttofire.
,
regardlessof
when
the
rules
were
added.
Salienceissetusinganumericvaluerangingfromthesmallestvalueof
, , .
Ifarulehasnosalienceexplicitlyassignedbytheprogrammer,CLIPSassumesasalienceof0.
orlessersalienceandafterallruleswithgreatersalience.
SEEM 5750 22
8/12/2019 CLIPS 3
23/34
:
( def rul e f i r e- f i r st
( pr i or i t y f i r st )=>( pr i nt out t " Pr i nt f i r st " cr l f ) )
( def r ul e f i r e- second
( pr i or i t y second)=>( r i nt out t "Pr i nt second" cr l f ) )
( def rul e f i r e- t hi r d( pr i or i t y t hi r d)
=( pr i nt out t "Pr i nt t hi r d" cr l f ) )
SEEM 5750 23
8/12/2019 CLIPS 3
24/34
:
CLI PS> (unwatch al l )
CLI PS> ( r eset )LI PS> (assert (priority i r s t ) )
CLI PS> (assert (priority second) )
CLI PS> (assert (priority t hi r d) )CLI PS> ( r un)Pr i nt t hi r d
Pr i nt f i r stCLI PS>
SEEM 5750 24
8/12/2019 CLIPS 3
25/34
:
( def r ul e f i r e- f i r st
( decl ar e ( sal i ence 30) )pr i or i t y f i r st )
=>( pr i nt out t " Pr i nt f i r st " cr l f ) )
( def r ul e f i r e- second( decl ar e ( sal i ence 20) )( r i or i t second)=>( pr i nt out t "Pr i nt second" cr l f ) )
-( decl ar e ( sal i ence 10) )( pr i or i t y t hi r d)
( pr i nt out t "Pr i nt t hi r d" cr l f ) )SEEM 5750 25
8/12/2019 CLIPS 3
26/34
:
CLI PS> ( r eset )
CLI PS> ( asser t ( pr i or i t y second)pr i or i t y f i r st )
( pr i or i t y t hi r d) )CLI PS> ( agenda)30f i r e- f i r st : f - 220f i r e- second: f - 110f i r e- t hi r d: f - 3For a t ot al of 3 act i vat i ons.
SEEM 5750 26
8/12/2019 CLIPS 3
27/34
Forprogramsinvolvinghundredsorthousandsofrules,theintermixingof
maintenanceamajorproblem.
Asan
example,
consider
the
problem
of
performing
fault
detection,
isolation,andrecoveryofasystemsuchasanelectronicdevice.
DifferentPhasesforFaultDetection,Isolation,andRecoveryProblem
SEEM 5750 27
8/12/2019 CLIPS 3
28/34
Tousesaliencetoor anizetherules
Assignmentofsaliencefordifferentphases
controlknowledgeisstillbeingembeddedintotherulesusingsalience.
doesnotguaranteethecorrectorderofexecution.
controlknowledgefromthedomainknowledge,asshowninthefollowingfigure.
.
Control
rules
are
then
written
to
transfer
control
between
the
different
phases
SEEM 5750 28
8/12/2019 CLIPS 3
29/34
SeparationofExpertKnowledgefromControlKnowledge
SEEM 5750 29
8/12/2019 CLIPS 3
30/34
( def r ul e det ect i on- t o- i sol at i on( decl ar e ( sal i ence - 10) )
?phase ( r et r act ?phase)( asser t ( phase i sol at i on) ) )
u - - v y
( decl ar e ( sal i ence - 10) )?phase ( r et r act ?phase)( asser t ( phase r ecover y) ) )
( def r ul e r ecover y- t o- det ect i on( decl ar e ( sal i ence - 10) )?phase
( asser t ( phase det ect i on) ) )
SEEM 5750 30
8/12/2019 CLIPS 3
31/34
Eac o t eru esapp ica e oraparticu arp aseist engivenacontro
pattern.
( def r ul e f i nd- f aul t - l ocat i on- and- r ecover y( phase r ecover y)
( r ecover y- sol ut i on swi t ch- devi ce
?r epl acement on)
=>
( pr i nt out t "Swi t ch devi ce" ?r epl acement "on"
cr l f ) )
Asaliencehierarchy isadescriptionofthesaliencevaluesusedbyan
expertsystem.
membersareallgiventhesamesalience.
SEEM 5750 31
8/12/2019 CLIPS 3
32/34
W i et e act p ase etection isint e act ist,t e etectionto
isolationrulewillbeontheagenda.
Sinceit
has
alower
salience
than
the
detection
rules,
it
will
not
fire
until
all
of
thedetectionruleshavehadanopportunitytofire.
SEEM 5750 32
8/12/2019 CLIPS 3
33/34
FourLeve Sa ienceHierarc y
SEEM 5750 33
8/12/2019 CLIPS 3
34/34
Overuseofsalienceresultsinapoorlycodedprogram.
Amainadvantageofarulebasedprogramisthattheprogrammer
.
Salienceshouldprimarilybeusedasamechanismfor
determinin theorderinwhichrulesfire.
Salienceshould
not
be
used
as
amethod
for
selecting
asingle
rule
fromagroupofruleswhenpatternscanbeusedtoexpressthe
SEEM 5750 34