Sie sind auf Seite 1von 62

Seyed Hashem Davarpanah

Davarpanah@usc.ac.ir
University of Science and
Culture
Expert systems
CLIPS
otivation

CLIPS is a decent example of an


expert system shell

rule!"ased# for$ard!chainin% system

it illustrates many of the concepts


and methods used in other &PS shells

it allo$s the representation of


'no$led%e# and its use for solvin%
suita"le pro"lems
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


procedural
(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 if
necessary# 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 more


efficient implementation.

/ (ete!"ased expert system "uilds a net$or' of


nodes# $here each node -except the root. corresponds
to a pattern occurrin% in the left!hand!side -the
condition 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 memory
of facts $hich satisfy that pattern. /s ne$ facts are
asserted or modified# they propa%ate alon% the
net$or'# causin% nodes to "e annotated $hen that
fact matches that pattern. 3hen a fact or com"ination
of facts causes all of the patterns for a %iven rule to "e
satisfied# a leaf node is reached and the correspondin%
rule is tri%%ered.
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 7


supported "y C4SIC

runs on PC# ac# U6I&# 8/& 8S

CLIPS provides mechanisms for expert


systems

a top!level interpreter

production rule interpreter

o",ect oriented pro%rammin% lan%ua%e

LISP!li'e procedural lan%ua%e


Components of CLIPS

rule!"ased lan%ua%e

can create a fact list

can create a rule set

an inference en%ine matches facts


a%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]
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
Invo'e : Exit CLIPS

enterin% CLIPS
dou"le!clic' on icon# or type pro%ram name
-CLIPS.
system prompt appears)
CLIPS>

exitin% CLIPS
at the system prompt
CLIPS>
type (exit)

6ote) enclosin% parentheses are important;


they indicate a command to "e executed#
not ,ust a sym"ol
0ields ! Examples
0ields -data types.

float <.==# >.=e?># >e!>

inte%er <# ># >>

sym"ol /lpha><@# AB@@C

strin% DEohnny F. GoodH

instance name ItitanicJ# IPPKJ


8aria"les
Bvar# Bx# Bday varia"les for sin%le field value
CBnames varia"le for multi!field value
CLIPS L0acts
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 DEohnny F. GoodH. -a%e >5..
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.
Deftemplate 0acts
Deftemplate!facts

are facts defined "ased on a


template;

slots can "e arran%ed ar"itrarily#


there is no specific order.

Define a template for descri"in% a


set of facts usin% deftemplate -record
structure. .

Use deffacts to create a list of facts


"ased on a template.
Examples of 0acts

ordered fact
(perso!ame "ra# $. %urfess)

deftemplate fact
(&eftemplate perso '&eftemplate
example(
(slot ame)
(slot a)e)
(slot e*e!color)
(slot hair!color))
Definin% 0acts

0acts can "e asserted


CLIPS> (assert (to&a* is su&a*))
<"act!+>

0acts can "e listed


CLIPS> (facts)
f!+ (to&a* is su&a*)

0acts can "e retracted


CLIPS> (retract +)
CLIPS> (facts)
[Jackson 1999]
Instances

an instance of a fact is created "y


(assert (perso (ame '"ra# $.
%urfess')
(a)e ,-)
(e*e!color .ro/)
(hair!color .ro/))
)
Initial 0acts
(&effacts 0urfesses 'some mem.ers of the %urfess
famil*'
(perso (ame '"ra# $. %urfess') (a)e ,-)
(e*e!color .ro/) (hair!color
.ro/))
(perso (ame '1u.ert %urfess') (a)e ,,)
(e*e!color .lue) (hair!color .lo&))
(perso (ame '2erhar& %urfess') (a)e ,3)
(e*e!color .lue) (hair!color .lo&))
(perso (ame '1eirich %urfess') (a)e 45)
(e*e!color .ro/) (hair!color
.lo&))
(perso (ame 'Irm)ar& %urfess') (a)e 46)
(e*e!color )ree) (hair!color
.lo&))
)
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
(ules

%eneral format
(&efrule <rule ame> ['commet']
<patters>* 8 left!ha& si&e (L1S)
8 or atece&et of
the rule
9>
<actios>*) 8 ri)ht!ha& si&e
(:1S)
8 or cose;uet of
the rule
(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 rule


fires
(ule ! Example
-defrule "irthday D/ personPs "irthdayH
-person -name Bname. -a%e B
a%e..
-has!"irthday Bname Ba%e.
QR
-printout t DHappy Firthday# H B
name..
template
fact
ordered
fact
terminal
rule name
comment
variables
variable text function
Examples of (ules

simple rule
(&efrule .irth&a*!"$%
(perso (ame '"ra# $.
%urfess')
(a)e ,-)
(e*e!color .ro/)
(hair!color .ro/))
(&ate!to&a* <pril!34!+=)
9>
(pritout t '1app* .irth&a*> "ra#?')
(mo&if* 3 (a)e ,6))
)
3ildcards

9uestion mar' @

matches any sin%le field $ithin a fact

multi!field $ildcard A@

matches 1ero or more fields in a fact


Salience

3e can use salience measures to prioriti1e


rules.

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 rule


over another. 3e can implement se9uencin%
of rules.
(ule Prioriti1ation in Clips

for example# consider the follo$in%


rules... -forced order of execution.
2$o 6ifty (ules
-defrule fire!first
-declare -salience S=..
-priority first.
QR
-printout t TPrint 0irstT crlf. .
-defrule fire!second
-declare -salience >=..
-priority second.
QR
-printout t TPrint SecondT crlf. .
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 C

the value of the field must match all


specified values

mostly used to place constraints on the


"indin% of a varia"le
athematical 4perators

"asic operators -+>!>*>D) and many functions


-tri%onometric# lo%arithmic# exponential. are
supported

prefix notation

no "uilt!in precedence# only left!to!ri%ht and


parentheses

test feature

evaluates an expression in the LHS instead of matchin% a


pattern 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
Eess is ti%htly inte%rated $ith Eava
Examples of (ules

more complex rule


(&efrule fi&!.lue!e*es
(perso (ame @ame)
(e*e!color .lue))
9>
(pritout t @ame ' has .lue
e*es.'
crlf))

Example (ule $ith 0ield
Constraints
(&efrule sill*!e*e!hair!match
(perso (ame @ame3)
(e*e!color @e*es3C.lue|)ree)
(hair!color @hair3CB.lac0))
(perso (ame @ame=CB@ame3)
(e*e!color @e*es=CB@e*es3)
(hair!color @hair=Cre&|@hair3))
9>
(pritout t @ame3 ' has '@e*es3 '
e*es a& ' @hair3 ' hair.' crlf)
(pritout t @ame= ' has '@e*es= '
e*es a& ' @hair= ' hair.' crlf))
Usin% 2emplates
(&eftemplate stu&et Ia stu&et recor&(
(slot ame (t*pe SH:IEJ))
(slot a)e (t*pe EKL2M:) (&efault 35)))
CLIPS> (assert (stu&et (ame fre&)))
(&efrule prit!a!stu&et
(stu&et (ame @ame) (a)e @a)e))
9>
(pritout t @ame I is I @a)e)
)
[Jackson 1999]
/n Example CLIPS (ule
(&efrule su&a* IHhi)s 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]
8aria"les 7 Pattern
atchin%

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]
(etractin% 0acts from a (ule
(&efrule &o!a!chore
(to&a* is @&a*) 8 @&a* must ha7e a
cosistet .i&i)
@chore <! (&o @No. o @&a*)
9>
(pritout t @No. I &oe()
(retract @chore)
)

a varia"le must "e assi%ned to the item


for retraction
[Jackson 1999]
Procedural Control in /ctions

Procedural Control Elements can


appear on the (HS of a rule or in
messa%e!handlers of classes.
(if <predicate-expression>
ten <expression>!
[else <expression>! ]" ##else-part optional
($ile <predicate-expression>
[do] <expression>* ]" ##%do not mandatory
Example L 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 1anada2,"
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"
Condition Patterns with Logical
Connectives
Complex Conditions $ith lo%ical connectives)
-or -patternM. -pattern>..
(ule "ecomes active if one of the patterns matches.
example) -or -"irthday. -anniversary..
matches fact "ase $ith facts -"irthday. or
-anniversary.
E9uivalent for)
and -is default.
not
exists to "e fulfilled for one matchin% fact
forall to "e fulfilled for all facts $hich
match "ased on first fact and
varia"le "indin%
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 Devacuate D B"uildin%.
.
reports a building if there is a fire or bomb emergency
in this building
Complex Condition Elements L
exists
-defrule emer%ency!report
-exists
-or -emer%ency -emer%ency!type fire..
-emer%ency -emer%ency!type
"om"...
.
QR
-printout t D2here is an emer%ency.D crlf .
.
prints one emergency-message if there is a fire or bomb
emergency. (no further matching, firing, or printout)
Complex Condition Elements L forall
-defrule evacuated!all!"uildin%s
-forall -emer%ency -emer%ency!type fire U "om".
-location B"uildin%. .
-evacuated -"uildin% B"uildin%...
QR
-printout t D/ll "uildin%s $ith emer%ency are
evacuated D crlf..
prints evacuated-message if for all buildings, which have a
fire or bomb emergency, the building is evacuated.
Salience

3e can use salience measures to prioriti1e


rules.

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 rule


over another. 3e can implement se9uencin%
of rules.
(ule Prioriti1ation in Clips

for example# consider the follo$in%


rules... -forced order of execution.
2$o 6ifty (ules
-defrule fire!first
-declare -salience S=..
-priority first.
QR
-printout t TPrint 0irstT crlf. .
-defrule fire!second
-declare -salience >=..
-priority second.
QR
-printout t TPrint SecondT crlf. .
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
"ind!function
"ind!function L explicitly "inds value to
varia"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 -explodeC -readline...
explodeC splits line $hich is read as S2(I6G
into multifield!value $hich is stored in
multislot!varia"le Baddress
4pen# Close 0ile
4pen file for read:$rite)
-open DVfile!nameRH Vlo%ical!nameR DrH.

Vfile!nameR is physical file!name -path.

Vlo%ical!nameR is name used in pro%ram

DrH indicates read!access -D$H# Dr?H.


example) -open Dexample.datH my!file DrH.
-read my!file.
Close file)
-close Vlo%ical!nameR.
Input L read# readline
read L input of sin%le field
readline L input of complete -line as strin%.
%eneral)
-read Vlo%ical nameR.
Vlo%ical nameR refers to file!name in pro%ram
-read. 'ey"oard is default
read $ith "ind!function to "ind input to varia"le)
-"ind Binput -read..
-"ind CBinput -readline..
Input L read# readline
-read : readline Vlo%ical nameR.

default is 'ey"oard:terminal

file has to "e opened usin%


-open DVfile!nameRH Vlo%ical!nameR
DrH.

Vfile!nameR is physical file!name -can


include path.

Vlo%ical!nameR is name used in read


command

DrH indicates read!access


example) -open Dexample.datH example DrH.
-read example.
use $ith "ind!function to "ind input to varia"le
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 DVfile!nameRH Vlo%ical!nameR D$H .

Vfile!nameR is physical file!name -can


include path.

Vlo%ical!nameR is name used in


printout command
W
D$H indicates $rite!access
example) -open Dexample.datH my!output D$H .
-printout my!output Bname crlf.
Pro%ram Execution

a%enda

if all patterns of a rule match $ith facts# it is


put on the a%enda

(a)e&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
Execution of a Pro%ram

(reset) prepares -re.start 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)
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


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


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 compiled
rules
8 ?, ,oin added
8 Q, ,oin shared
important for the efficiency of the (ete pattern matchin%
net$or'

focus

used $ith modules

indicates $hich module is currently active


Definin% 0unctions in CLIPS
Uses a LISP or Scheme!li'e syntax
(&effuctio fuctio!ame (ar) ... ar))
actio ... actio)
(&effuctio h*poteuse (@a @.)
(s;rt (+ (* @a @a) (* @. @.))))
(&effuctio iitiali#e ()
(clear)
(assert (to&a* is su&a*)))
[Jackson 1999]
Definin% Classes 7
Instances

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]
ana%in% Instances

Commands to display instances


CLIPS> (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&er.ir& of car (ma&e!.* for&)))
[Jackson 1999]
Clearin% 7 (esettin%
Instances

deletin% an instance
CLIPS> (se& [cor7ette] &elete)

deletin% all instances


CLIPS> (uma0e!istace *)

resettin% creates an initial o",ect


CLIPS> (reset)
CLIPS> (istaces)
[iitial!o.Nect] of IEIHI<L!G2$MCH
[Jackson 1999]
Limitations of CLIPS

sin%le level rule sets

in L44PS# you could arran%e rule sets in a


hierarchy# em"eddin% one rule set inside
another# etc

loose couplin% of rules and o",ects

rules can communicate $ith o",ects via


messa%e passin%

rules cannot easily "e em"edded in o",ects# as


in Centaur

CLIPS has no explicit a%enda mechanism

the "asic control flo$ is for$ard chainin%

to implement other 'inds of reasonin% you


have to manipulate to'ens in $or'in% memory
[Jackson 1999]
/lternatives to CLIPS

EESS
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 4FEEC2
another rule! and o",ect!"ased system
has facilities for desi%nin% %raphical interfaces
has a Xscript lan%ua%eP for desi%nin% user front!end
$ritten in C# runs on many platforms# hi%hly porta"le
[Jackson 1999]
EESS

EESS stands for Eava Expert System


Shell

it uses the same syntax and a lar%e


ma,ority of the features of CLIPS

ti%ht inte%ration $ith Eava

can "e invo'ed easily from Eava pro%rams

can utili1e o",ect!oriented aspects of Eava

some incompati"ilities $ith CLIPS

C44L replaced "y Eava classes

a fe$ missin% constructs

more and more added as ne$ versions of EESS


are released
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
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
EESS
'no$led%e "ase
'no$led%e
representation

pattern matchin%

refraction

retract
rule
rule header
salience
template
varia"le
$ild card

Das könnte Ihnen auch gefallen