8/12/2019 230639
http://slidepdf.com/reader/full/230639 1/62
Seyed Hashem Davarpanah
[email protected] of Science and
Culture
Expert systemsCLIPS
8/12/2019 230639
http://slidepdf.com/reader/full/230639 2/62
otivation
• CLIPS is a decent example of anexpert system shell– rule!"ased# for$ard!chainin% system
• it illustrates many of the conceptsand methods used in other &PS shells
• it allo$s the representation of'no$led%e# and its use for solvin%suita"le pro"lems
8/12/2019 230639
http://slidepdf.com/reader/full/230639 3/62
Introduction
• CLIPS stands for– C Lan%ua%e Implementation Production
System
• for$ard!chainin%– startin% from the facts# a solution is
developed
• pattern!matchin%– (ete matchin% al%orithm) find **fittin%++
rules and facts• 'no$led%e!"ased system shell
– empty tool# to "e filled $ith 'no$led%e
• multi!paradi%m pro%rammin% lan%ua%e– rule!"ased# o",ect!oriented -Cool and
8/12/2019 230639
http://slidepdf.com/reader/full/230639 4/62
(ete atchin% /l%orithm
• /n expert system mi%ht chec' each rule a%ainst the'no$n facts in the 'no$led%e "ase# firin% that rule ifnecessary# then movin% on to the next rule.
• 0or even moderate si1ed rules and facts 'no$led%e!"ases# this approach performs far too slo$ly.
• 2he (ete al%orithm provides the "asis for a moreefficient implementation.
• / (ete!"ased expert system "uilds a net$or' ofnodes# $here each node -except the root correspondsto a pattern occurrin% in the left!hand!side -thecondition part of a rule.
• 2he path from the root node to a leaf node defines a
complete rule left!hand!side. Each node has a memoryof facts $hich satisfy that pattern. /s ne$ facts areasserted or modified# they propa%ate alon% thenet$or'# causin% nodes to "e annotated $hen thatfact matches that pattern. 3hen a fact or com"inationof facts causes all of the patterns for a %iven rule to "esatisfied# a leaf node is reached and the correspondin%rule is tri%%ered.
8/12/2019 230639
http://slidepdf.com/reader/full/230639 5/62
2he CLIPS Pro%rammin% 2ool
• history of CLIPS– influenced "y 4PS5 and /(2– implemented in C for efficiency and
porta"ility
– developed "y 6/S/# distri"uted 7supported "y C4SIC– runs on PC# ac# U6I&# 8/& 8S
• CLIPS provides mechanisms for expertsystems– a top!level interpreter– production rule interpreter– o",ect oriented pro%rammin% lan%ua%e
– LISP!li'e procedural lan%ua%e
8/12/2019 230639
http://slidepdf.com/reader/full/230639 6/62
Components of CLIPS
• rule!"ased lan%ua%e– can create a fact list– can create a rule set
– an inference en%ine matches factsa%ainst rules
• o",ect!oriented lan%ua%e -C44L
– can define classes– can create different sets of instances– special forms allo$ you to interface
rules and o",ects
[Jackson 1999]
8/12/2019 230639
http://slidepdf.com/reader/full/230639 7/62
6otation• sym"ols# characters# 'ey$ords
– entered exactly as sho$n)– (example)
• s9uare "rac'ets [...]– contents are optional)– (example [test])
• pointed "rac'ets -less than : %reater than si%ns < ... >– replace contents "y an instance of that type– (example <char>)
• star * – replace $ith 1ero or more instances of the type– <char>*
• plus + – replace $ith one or more instances of the type– <char>+ -is e9uivalent to <char> <char>*
• vertical "ar | – choice amon% a set of items)
– true | false
8/12/2019 230639
http://slidepdf.com/reader/full/230639 8/62
Invo'e : Exit CLIPS
• enterin% CLIPSdou"le!clic' on icon# or type pro%ram name
-CLIPS
system prompt appears) CLIPS>
• exitin% CLIPSat the system prompt
CLIPS> type (exit)
– 6ote) enclosin% parentheses are important;they indicate a command to "e executed#not ,ust a sym"ol
8/12/2019 230639
http://slidepdf.com/reader/full/230639 9/62
0ields ! Examples
0ields -data types• float <.==# >.=e?># >e!>
• inte%er <# ># >>
• sym"ol /lpha><# AB@
• strin% ohnny F. Good
• instance name titanicJ# PPKJ
8aria"lesBvar# Bx# Bday varia"les for sin%le field value
Bnames varia"le for multi!field value
8/12/2019 230639
http://slidepdf.com/reader/full/230639 10/62
CLIPS 0acts
0acts• a relation!name#• an ordered se9uence of values -ordered
facts# or• a set of -slot!name slot!value!pairs -i.e.deftemplate!facts
examples)
-today is 2hursday-person -name ohnny F. Good -a%e >5
8/12/2019 230639
http://slidepdf.com/reader/full/230639 11/62
4rdered 0acts
4rdered facts• are facts defined $ithout -explicit template;• the field!values are ordered.
Examples)
-num"er!list M > 55 N O <>
-today is Sunday
8/12/2019 230639
http://slidepdf.com/reader/full/230639 12/62
Deftemplate 0acts
Deftemplate!facts
• are facts defined "ased on atemplate;
• slots can "e arran%ed ar"itrarily#there is no specific order.
•Define a template for descri"in% aset of facts usin% deftemplate -recordstructure .
• Use deffacts to create a list of facts
8/12/2019 230639
http://slidepdf.com/reader/full/230639 13/62
Examples of 0acts
• ordered fact(perso!ame "ra# $. %urfess)
• deftemplate fact(&eftemplate perso '&eftemplateexample
(slot ame)
(slot ae) (slot ee!color)
(slot hair!color))
8/12/2019 230639
http://slidepdf.com/reader/full/230639 14/62
8/12/2019 230639
http://slidepdf.com/reader/full/230639 15/62
Instances
• an instance of a fact is created "y
(assert (perso (ame '"ra# $.
%urfess')
(ae ,-)
(ee!color ro/)
(hair!color ro/))
)
8/12/2019 230639
http://slidepdf.com/reader/full/230639 16/62
Initial 0acts
(&effacts 0urfesses 'some memers of the %urfessfamil'
(perso (ame '"ra# $. %urfess') (ae ,-)
(ee!color ro/) (hair!colorro/))
(perso (ame '1uert %urfess') (ae ,,) (ee!color lue) (hair!color lo&))
(perso (ame '2erhar& %urfess') (ae ,3)
(ee!color lue) (hair!color lo&))
(perso (ame '1eirich %urfess') (ae 45)
(ee!color ro/) (hair!colorlo&))
(perso (ame 'Irmar& %urfess') (ae 46)
(ee!color ree) (hair!colorlo&))
)
8/12/2019 230639
http://slidepdf.com/reader/full/230639 17/62
Usa%e of 0acts
• addin% facts– (assert <fact>+)
• deletin% facts– (retract <fact!i&ex>+)
• modifyin% facts– (mo&if <fact!i&ex> (<slot!ame> <slot!7alue>)+ )
• retracts the ori%inal fact and asserts a ne$# modified fact
• duplicatin% facts– (&uplicate <fact!i&ex> (<slot!ame> <slot!7alue>)+ )
• adds a ne$# possi"ly modified fact
• inspection of facts– (facts)
• prints the list of facts
– (/atch facts)
• automatically displays chan%es to the fact list
8/12/2019 230639
http://slidepdf.com/reader/full/230639 18/62
(ules
• %eneral format(&efrule <rule ame> ['commet']
<patters>* 8 left!ha& si&e (L1S)
8 or atece&et ofthe rule
9>
<actios>*) 8 riht!ha& si&e
(:1S)8 or cose;uet of
the rule
8/12/2019 230639
http://slidepdf.com/reader/full/230639 19/62
(ule Components
• rule header– defrule 'ey$ord# name of the rule#
optional comment strin%
• rule antecedent -LHS– patterns to "e matched a%ainst facts
• rule arro$
– separates antecedent and conse9uent• rule conse9uent -(HS
– actions to "e performed $hen the rulefires
8/12/2019 230639
http://slidepdf.com/reader/full/230639 20/62
(ule ! Example
-defrule "irthday / persons "irthday-person -name Bname -a%e B
a%e-has!"irthday Bname Ba%e
QR
-printout t Happy Firthday# Bname
template
fact
ordered
fact
terminal
rule name comment
variables
variabletextfunction
8/12/2019 230639
http://slidepdf.com/reader/full/230639 21/62
Examples of (ules
• simple rule(&efrule irth&a!"$%
(perso (ame '"ra# $.%urfess')
(ae ,-) (ee!color ro/)
(hair!color ro/))
(&ate!to&a pril!34!=)
9>
(pritout t '1app irth&a "ra#?')
(mo&if 3 (ae ,6))
)
8/12/2019 230639
http://slidepdf.com/reader/full/230639 22/62
3ildcards
• 9uestion mar' @
– matches any sin%le field $ithin a fact
• multi!field $ildcard A@ – matches 1ero or more fields in a fact
8/12/2019 230639
http://slidepdf.com/reader/full/230639 23/62
Salience
• 3e can use salience measures to prioriti1erules.
• CLIPS provides a "uilt!in method for
prioriti1in% rules)-declare -salience value
• Salience values can ran%e from !M==== to?M====. Default is =.
• 3e can thus force the execution of one ruleover another. 3e can implement se9uencin%of rules.
8/12/2019 230639
http://slidepdf.com/reader/full/230639 24/62
(ule Prioriti1ation in Clips
• for example# consider the follo$in%rules... -forced order of execution
8/12/2019 230639
http://slidepdf.com/reader/full/230639 25/62
2$o 6ifty (ules
-defrule fire!first-declare -salience =-priority first
QR-printout t TPrint 0irstT crlf
-defrule fire!second
-declare -salience >=-priority secondQR
-printout t TPrint SecondT crlf
8/12/2019 230639
http://slidepdf.com/reader/full/230639 26/62
0ield Constraints
• not constraint B– the field can ta'e any value except the one
specified
• or constraint |– specifies alternative values# one of $hich
must match
• and constraint – the value of the field must match all
specified values
– mostly used to place constraints on the
"indin% of a varia"le
8/12/2019 230639
http://slidepdf.com/reader/full/230639 27/62
athematical 4perators
• "asic operators -+!*D) and many functions-tri%onometric# lo%arithmic# exponential aresupported
• prefix notation
• no "uilt!in precedence# only left!to!ri%ht andparentheses• test feature
– evaluates an expression in the LHS instead of matchin% apattern a%ainst a fact
• pattern connectives– multiple patterns in the LHS are implicitly EF!connected– patterns can also "e explicitly connected via EF G: EGH
• user!defined functions– external functions $ritten in C or other lan%ua%es can "e
inte%rated
8/12/2019 230639
http://slidepdf.com/reader/full/230639 28/62
8/12/2019 230639
http://slidepdf.com/reader/full/230639 29/62
Example (ule $ith 0ieldConstraints
(&efrule sill!ee!hair!match(perso (ame @ame3)
(ee!color @ees3lue|ree)
(hair!color @hair3Blac0))
(perso (ame @ame=B@ame3)(ee!color @ees=B@ees3)
(hair!color @hair=re&|@hair3))
9>
(pritout t @ame3 ' has '@ees3 'ees a& ' @hair3 ' hair.' crlf)
(pritout t @ame= ' has '@ees= 'ees a& ' @hair= ' hair.' crlf))
8/12/2019 230639
http://slidepdf.com/reader/full/230639 30/62
Usin% 2emplates
(&eftemplate stu&et a stu&et recor&
(slot ame (tpe SH:IEJ))
(slot ae (tpe EK2M:) (&efault 35)))
CLIPS> (assert (stu&et (ame fre&)))
(&efrule prit!a!stu&et
(stu&et (ame @ame) (ae @ae))
9>(pritout t @ame is @ae)
)
[Jackson 1999]
8/12/2019 230639
http://slidepdf.com/reader/full/230639 31/62
/n Example CLIPS (ule
(&efrule su&a Hhis to &o o Su&a
(saliece ) 8 saliece i the iter7al [!
3 3]
(to&a is Su&a)
(/eather is su)
9>
(assert (chore /ash car))
(assert (chore chop /oo&))
)
[Jackson 1999]
8/12/2019 230639
http://slidepdf.com/reader/full/230639 32/62
8aria"les 7 Patternatchin%
• 8aria"les ma'e rules more applica"le
(&efrule pic0!a!chore
(to&a is @&a)
(chore is @No)9>
(assert (&o @No o @&a)))
• if conditions are matched# then"indin%s are used
[Jackson 1999]
8/12/2019 230639
http://slidepdf.com/reader/full/230639 33/62
(etractin% 0acts from a (ule
(&efrule &o!a!chore
(to&a is @&a) 8 @&a must ha7e acosistet i&i
@chore <! (&o @No o @&a)
9>(pritout t @No &oe)
(retract @chore))
• a varia"le must "e assi%ned to the itemfor retraction
[Jackson 1999]
8/12/2019 230639
http://slidepdf.com/reader/full/230639 34/62
Procedural Control in /ctions
• Procedural Control Elements canappear on the (HS of a rule or inmessa%e!handlers of classes.
(if <predicate-expression>ten <expression>!
[else <expression>! ]" ##else-part optional
( $ile <predicate-expression>
[do] <expression>* ]" ##%do’ not mandatory
8/12/2019 230639
http://slidepdf.com/reader/full/230639 35/62
Example if!then!else
(defrule special-a&e '1) *1) 1++,(or (person (name name" (a&e a&e.1""
(person (name name" (a&e a&e.*1""
(person (name name" (a&e a&e.1++"""
/>(if (/ a&e 1" ten (printout t name ' can bu0 beer
in anada2,"
else
(if (/ a&e *1" ten (printout t name ' can
bu0 beer in te 3452,"
else
(if (/ a&e 1++" ten (printout t '6e
ma7or $ill visit , name ""222"
8/12/2019 230639
http://slidepdf.com/reader/full/230639 36/62
8/12/2019 230639
http://slidepdf.com/reader/full/230639 37/62
Complex Condition Elements ! or
-defrule report!emer%ency
-or -emer%ency -emer%ency!type fire -location B
"uildin%
-emer%ency -emer%ency!type "om" -location B
"uildin%
QR-printout t evacuate B"uildin%
reports a building if there is a fire or bomb emergencyin this building
8/12/2019 230639
http://slidepdf.com/reader/full/230639 38/62
8/12/2019 230639
http://slidepdf.com/reader/full/230639 39/62
Complex Condition Elements forall
-defrule evacuated!all!"uildin%s -forall -emer%ency -emer%ency!type fire "om"
-location B"uildin% -evacuated -"uildin% B"uildin%
QR-printout t /ll "uildin%s $ith emer%ency areevacuated crlf
prints evacuated-message if for all buildings, which have a
fire or bomb emergency, the building is evacuated.
8/12/2019 230639
http://slidepdf.com/reader/full/230639 40/62
Salience
• 3e can use salience measures to prioriti1erules.
• CLIPS provides a "uilt!in method for
prioriti1in% rules)-declare -salience value
• Salience values can ran%e from !M==== to?M====. Default is =.
• 3e can thus force the execution of one ruleover another. 3e can implement se9uencin%of rules.
8/12/2019 230639
http://slidepdf.com/reader/full/230639 41/62
8/12/2019 230639
http://slidepdf.com/reader/full/230639 42/62
2$o 6ifty (ules
-defrule fire!first-declare -salience =-priority first
QR-printout t TPrint 0irstT crlf
-defrule fire!second
-declare -salience >=-priority secondQR
-printout t TPrint SecondT crlf
8/12/2019 230639
http://slidepdf.com/reader/full/230639 43/62
anipulation of Constructs
• sho$ list of constructs(list!&efrules) (list!&eftemplates) (list!&effacts)
• prints a list of the respective constructs
• sho$ text of constructs
(pp&efrule <&efrule!ame>) (pp&eftemplate<&eftemplate!ame>) (pp&effacts <&effacts!ame>)• displays the text of the construct -**pretty print++
• deletin% constructs(u&efrule <&efrule!ame>) (u&eftemplate<&eftemplate!ame>) (u&effacts <&effacts!ame>)
• deletes the construct -if it is not in use• clearin% the CLIPS environment
(clear) • removes all constructs and adds the initial facts to the CLIPS
environment
8/12/2019 230639
http://slidepdf.com/reader/full/230639 44/62
"ind!function
"ind!function explicitly "inds value tovaria"le
-"ind Ba%e -read
stores value of sin%le field $hich is read
into sin%le!field varia"le Ba%e-"ind Bname -readline
stores line $hich is read as S2(I6G into
sin%le!field S2(I6G!varia"le Baddress-"ind Baddress -explode -readline
explode splits line $hich is read as S2(I6G
into multifield!value $hich is stored in
8/12/2019 230639
http://slidepdf.com/reader/full/230639 45/62
4pen# Close 0ile
4pen file for read:$rite)-open Vfile!nameR Vlo%ical!nameR r
– Vfile!nameR is physical file!name -path
– Vlo%ical!nameR is name used in pro%ram
– r indicates read!access -$# r?
example) -open example.dat my!file r
-read my!file
Close file)-close Vlo%ical!nameR
8/12/2019 230639
http://slidepdf.com/reader/full/230639 46/62
8/12/2019 230639
http://slidepdf.com/reader/full/230639 47/62
Input read# readline
-read : readline Vlo%ical nameR– default is 'ey"oard:terminal
– file has to "e opened usin%
-open Vfile!nameR Vlo%ical!nameRr• Vfile!nameR is physical file!name -can
include path
• Vlo%ical!nameR is name used in readcommand
• r indicates read!access
example) -open example.dat example r
-read example
8/12/2019 230639
http://slidepdf.com/reader/full/230639 48/62
4utput ! printout
-printout Vlo%ical!nameR ... W t terminal is standard
other$ise Vlo%ical!nameR refers to a file!name
file has to "e opened usin%
-open Vfile!nameR Vlo%ical!nameR $
– Vfile!nameR is physical file!name -caninclude path
– Vlo%ical!nameR is name used inprintout command
W $ indicates $rite!access
example) -open example.dat my!output $
8/12/2019 230639
http://slidepdf.com/reader/full/230639 49/62
Pro%ram Execution
• a%enda– if all patterns of a rule match $ith facts# it is
put on the a%enda– (ae&a) displays all activated rules
• salience– indicates priority of rules
• refraction
– rules fire only once for a specific set of facts• prevents infinite loops
– (refresh <rule!ame>) • reactivates rules
8/12/2019 230639
http://slidepdf.com/reader/full/230639 50/62
Execution of a Pro%ram
• (reset) prepares -restart of a pro%ram)– all previous facts are deleted– initial facts are asserted– rules matchin% these facts are put on the
a%enda• (ru [<limit>]) starts the execution• "rea'points
– (set!rea0 [<rule!ame>])• stops the execution "efore the rule fires#• continue $ith -ru
– (remo7e!rea0 [<rule!ame>])
– (sho/!rea0s)
8/12/2019 230639
http://slidepdf.com/reader/full/230639 51/62
3atchin%
• $atchin% the execution– (/atch </atch!item>) prints messa%es
a"out activities concernin% a </atch!
item>• (facts rules acti7atios
statistics compilatio focus all)
– (u/atch </atch!item>) • turns the messa%es off
8/12/2019 230639
http://slidepdf.com/reader/full/230639 52/62
3atchin% 0acts# (ules and /ctivations
• facts– assertions -add and retractions -delete
– of facts
• rules– messa%e for each rule that is fired
• activations– activated rules) matchin% antecedents
– these rules are on the a%enda
8/12/2019 230639
http://slidepdf.com/reader/full/230639 53/62
ore 3atchin% ...• statistics
– information a"out the pro%ram execution– -num"er of rules fired# run time# ...
• compilation -default– sho$s information for constructs loaded "y(loa&)
• Fefii &eftemplateO ...• Fefii &efruleO ... +N9N
– ?,# Q, indicates the internal structure of the compiledrules
8 ?, ,oin added8 Q, ,oin shared
– important for the efficiency of the (ete pattern matchin%net$or'
• focus
– used $ith modules
8/12/2019 230639
http://slidepdf.com/reader/full/230639 54/62
Definin% 0unctions in CLIPS
• Uses a LISP or Scheme!li'e syntax
(&effuctio fuctio!ame (ar ... ar)
actio ... actio)
(&effuctio hpoteuse (@a @)
(s;rt (+ (* @a @a) (* @ @))))
(&effuctio iitiali#e ()(clear)
(assert (to&a is su&a)))
[Jackson 1999]
Definin% Classes 7
8/12/2019 230639
http://slidepdf.com/reader/full/230639 55/62
Definin% Classes 7Instances
• definin% the class C/((&efclass car
(is!a user)
(ame)(ma&e!))
• definin% an instance of C/((ma0e!istace cor7ette of car
(ma&e! che7rolet))
[Jackson 1999]
8/12/2019 230639
http://slidepdf.com/reader/full/230639 56/62
ana%in% Instances
• Commands to display instancesCLIPS> (istaces)
[cor7ette] of car
CLIPS> (se& [cor7ette] prit)
[cor7ette] of car(ma&e! che7rolet)
• Command to %roup instances -in a file(&efistaces
(cor7ette of car (ma&e! che7rolet))
(thu&erir& of car (ma&e! for&)))
[Jackson 1999]
Clearin% 7 (esettin%
8/12/2019 230639
http://slidepdf.com/reader/full/230639 57/62
Clearin% 7 (esettin%Instances
• deletin% an instanceCLIPS> (se& [cor7ette] &elete)
• deletin% all instancesCLIPS> (uma0e!istace *)
• resettin% creates an initial o",ect
CLIPS> (reset)CLIPS> (istaces)
[iitial!oNect] of IEIHIL!G2$MCH
[Jackson 1999]
8/12/2019 230639
http://slidepdf.com/reader/full/230639 58/62
l i
8/12/2019 230639
http://slidepdf.com/reader/full/230639 59/62
/lternatives to CLIPS
• ESS
– see "elo$• Eclipse
– enhanced# commercial variant of CLIPS– has same syntax as CLIPS -"oth are "ased on /(2– supports %oal!driven -i.e.# "ac'$ards reasonin%
– has a truth maintenance facility for chec'in% consistency– can "e inte%rated $ith C?? and dFase– ne$ extension (E2E?? can %enerate C?? header files– not related to the -ne$er IF Eclipse environment
• 6E&PE(2 4FEC2
– another rule! and o",ect!"ased system– has facilities for desi%nin% %raphical interfaces– has a Xscript lan%ua%e for desi%nin% user front!end– $ritten in C# runs on many platforms# hi%hly porta"le
[Jackson 1999]
8/12/2019 230639
http://slidepdf.com/reader/full/230639 60/62
ESS
• ESS stands for ava Expert SystemShell
• it uses the same syntax and a lar%ema,ority of the features of CLIPS
• ti%ht inte%ration $ith ava– can "e invo'ed easily from ava pro%rams– can utili1e o",ect!oriented aspects of ava
•some incompati"ilities $ith CLIPS– C44L replaced "y ava classes– a fe$ missin% constructs
• more and more added as ne$ versions of ESSare released
8/12/2019 230639
http://slidepdf.com/reader/full/230639 61/62
CLIPS Summary
• notation– similar to Lisp# re%ular expressions
• facts– (&eftemplate) (&effacts) assert D retract
• rules
– (&efrule ...)# a%enda• varia"les# operators# functions
– advanced pattern matchin%
• input:output– (pritout ...) (rea& ...) (loa& ...)
• pro%ram execution– (reset) (ru) rea0poits
• user interface– command line or GUI
Important Concepts and
8/12/2019 230639
http://slidepdf.com/reader/full/230639 62/62
Important Concepts and 2erms
– a%enda– antecedent– assert– "ac'$ard chainin%– conse9uent– CLIPS
– expert system shell– fact– field– for$ard chainin%– function– inference– inference mechanism– instance– If!2hen rules– ESS
– 'no$led%e "ase– 'no$led%e
representation– pattern matchin%– refraction
– retract– rule– rule header– salience– template– varia"le– $ild card