Académique Documents
Professionnel Documents
Culture Documents
AgentleintroductiontoCLIPSforCogSciStudents
(seealsoClipsBlurb)
Lastmodified:3/11/06
CLIPSisanindustrialstrengthexpertsystemshell.Thatis,onecanuseCLIPStogeneratepowerful
industrialstrengthexpertsystems.Inthisclasswewillexamineonlyafewofthebasicelementsof
thesystem.
Templates,facts,andrules
Recallthatanexpertsystemcontainsadatabasewhichinturncontainsaseriesoffactsandrules
(productions).
Factscomeintwovarieties:structuredfacts(templates)andadhocfacts.Thetemplatesfor
structuredtasksarespecifiedbythedeftemplatecommand:
(deftemplateaTemplate"somedocumentation"
(slotslot1)
(slotslot2)
(etc.)
)
Forexample,supposethatwewanttoestablishastructureforastudentatPugetSound.Wemight
wanttostorethestudent'sstudentnumber,name,major,andnameofadvisor.Atemplatefordoing
thisisgivenby
(deftemplatestudent"Astudenttemplate"
(slotsno)
(slotsname)
(slotmajor)
(slotadvisor)
)
Assertingfacts
TherearetwowaysofintroducingfactsintotheCLIPSdatabase.Onewayistoincludetheminaset
ofinitialfacts
(deffactsinitialfacts
(student(sno123)(snamemaigret)(majorpremed)
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
1/11
27/11/2014
(advisorsimenon))...)
deffactsareassertedaftertheCLIPSfilecontainingthemhasbeenloadedintoCLIPS(seebelow)
andthenafterthe(reset)command.
Anotherwaytoassertafactistoassertit"onthefly",generallyasanactioninarule:
(assert(student(sno123)(snamemaigret)
(majorpremed)(advisorsimenon)))
Noticethat,justasinLISP,parenthesisareimportant.AlthoughCLIPSwaswritteninC(theletters
CLIinCLIPSstandfor"CLanguageImplementation"),thesyntaxforusingCLIPSisverymuch
LISPlike.
Note:Whilerunningclips,keepyoureyeonthefactswindow,andyouwillseethefactsyouhave
writtenbeingaddedtothedatabase.
Nonstructuredfacts
Itissometimesconvenienttohaveaseriesofadhocfactsinthedatabase,aswellasthestructured
ones.Thesearesimplylistsofwordsbetweenparenthesis,suchas:
(alarmon)
(alarmoff)
(temperaturehigh)
(valvea3572open)
Rules
Thebasisforaproductionsystem,ofcourse,isthecollectionofconditionactionrules.Thesyntax
fordefiningaruleis
(defrulerulename"somedocumentation"
(ifcondition)
(ifcondition)
(etc.)
=>
(action1)
(action2)
(etc.)
)
Forexample:
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
2/11
27/11/2014
(defruleusefulrule"ausefulrule"
(animalfierce)
(animalbig)
(animalhungry)
=>
(assert(runaway))
)
Whatthismeansisthatifwehavethefollowing(unstructured)factsinourdatabase
(animalfierce)
(animalbig)
(animalhungry)
thenallofthepreconditionshavebeenmettofireusefulrule,andweassert(runaway)intothe
database.
Wewillseesomemoreexamplesinamoment.First,let'stalkaboutrunningCLIPS.
RunningCLIPS
Youneedtohavetwoprogramstorunclips
clipsedteditclipsfiles
clipswintheCLIPSprogramforwindows
CopiesoftheseareavailableinmyhandoutsfolderunderCLIPS.
Example1:Thereisafileinthehandoutsfoldercalledexample1.clp
1. Copythisintoyourworkspace
2. Doubleclickclipsedttostarttheprogram
3. Inclipsedt,opentheexample1.clpfile.Lookitover.
4. Openclipswin(doubleclick)
5. Inthefilemenu,selectload(cntrlL)andloadexample1.clp
6. Selectthefactsandagendawindows(orallofthemifyouwouldprefer)
7. Type(reset)orselectitfromtheexecutionmenu.Notethefactsdefinedindeffactsappearing
inthefactswindow.Noticealsothatouronlyruleappearsintheagendawindow.
8. Stepthroughtheprogram,watchingwhathappenstothefactswindow.
Congratulations!YouhaverunyourfirstCLIPSprogram!
Someotherfeaturesinrules:
Patternmatching
Supposethatwehaveafactsuchas
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
3/11
27/11/2014
(deftemplatestudent"astudenttemplate"
(slotsname)
(slotmajor)
(slotinterest))
(deffactsinitalfacts"someinitialfacts"
(student(snamedee)(majorlaw))
)
Andwewouldliketolookthroughourdatabaseandcollectstudentsmajoringinlawandaddtoour
databasethefactthatthesestudentsareinterestedinlaw.Thefollowingrulewoulddothis(thisisin
example2.clp).:
(defrulerule1"afirstrule"
(student(sname?name)(majorlaw))
=>
(assert(lawinterest?name))
)
Thisrulesaysthatifthereisanythinginourdatabaseassertingthatastudentwithsomenameis
majoringinlaw(majorlaw),thenwewanttoaddtoourdatabasethefactthatthisstudentisinterested
inlaw.Theuseof?nameisimportant.Puttingaquestionmarkbeforeanamesaysthatifwefindin
ourdatabaseastudentwithanameandwithalawmajor,weputthenameofthatstudentinto?name
andthenusethesamenameinfurtherpatternmatchingintheifpartandthenpartoftherule.Ifwe
usethesamevariableindifferentpartsoftheifpart,thesamevaluesmustbeused.Forexample(this
fromstudent.clp)ifwehavearulewith
(defrulesuggestmathrule
(interest?snomath)
(ability?snomath)
(student(sno?sno)(sname?sname))
=>
(assert(suggest?snotakemath))
(printoutt"Wesuggestthat"?sname"takesomemoremath")
)
the?snomatchedinthefirstrulemustbethesameintherestoftherule.
Notetheprintoutstatement.Thisisasfarasweneedtakethatstatementforthiscourse.
Exercise:Tryplayingwithexample2.clp.ImportantNote:beforeloadingthenewfileintoCLIPS,
itisnecessarytoclearCLIPSmemory.Wedothiswiththe(clear)command(orusethemenus).
Onefinalbit,andwewillhaveallweneedforCLIPSforthiscourse.Thereare,ofcourse,many
moredetailswhichcanbefoundinthepowerpointslidesandinvariousbooksonexpertsystems
(Giatannoisoneexcellentsource).Intheabove,weaddedanewfacttoourdatabasetoexpressthe
factthatourstudentwasinterestedinlaw.Butwhyaddarulewhenthereisalreadyaslotinthe
templateforstudenttostorethestudent'sinterest?Wewouldliketoaddthisfacttoourexisting
studentrecord.Todothisweneedtomodifyanexistingfact.Thefollowingruledoesthis:
(defrulerule2"asecondrule"
?f1<(student(sname?name)(majorlaw)(interestnil))
=>
(modify?f1(interestlaw))
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
4/11
27/11/2014
)
The"(interestnil)saysthatthisruleshouldfireonlyifwedon'thaveavalueforinterestyet.The?f1
doestwothings(althoughanyvariablenamewilldo,thatis?anyname,CLIPSprogrammersgenerally
use?f1,?f2,andsoforth).Inthefirstoccurrenceof?f1,?f1<(student(sname?name)(majorlaw)),
webindthefactnumberofthefactintothevariable?f1.Wecanthenusethatstoredvaluetomodify
therulebychangingthevalueoftheexisting(slotinterest)withthevalue(interestlaw).Theremust
beaslotwiththisname,otherwisenothingworks.
Exercise:Trythisout.Thisisalsoinexample2.clp.Whenyourunrule2,notethattheoriginalfact
aboutDeehasbeenreplacedbyanewfact.Modifyworksbythrowingtheoldfactawayand
assertinganewfact(behindthescenes,ofcourse).
That'sit!YouhaveenoughforthecurrentCLIPSexercise.Anyquestions,pleaseask!
Afewmoreexamples.
Thefollowingexamplescanbefoundonthelabserver\\hedwigunderMatthewsHandouts:
Example2.clp
;
; This example illustrates three ideas:
; 1. The use of a structured frame (deftemplate)
; 2. The use of variables (?name)
; 3. A way to modify an existing frame
;
(deftemplate student "a student template"
(slot sname)
(slot major)
(slot interest))
;
; This defines a frame with three slots (sname, major, interest)
;
(deffacts initial-facts "some initial facts"
(student (sname dee) (major law))
)
;
; Remember that initial-facts will be loaded into the
; CLIPS database when a (reset) command is issued.
;
(defrule rule-1 "a first rule"
(student (sname ?name) (major law))
=>
(assert (law-interest ?name))
)
;
; This first rule says that if we find a student frame
; in the database, we will grab the value in the 'sname' slot
; and place the value we find there into the variable ?name. All
; variables in CLIPS begin with an initial question mark.
; After doing this, we then assert into the database a fact
; (law-interest ?name)
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
5/11
27/11/2014
Example3.clp
;
; This example illustrates a basic use of the printout command:
;
(deftemplate student "a student template"
(slot sname)
(slot major)
(slot interest))
;
; This defines a frame with three slots (sname, major, interest)
;
(deffacts initial-facts "some initial facts"
(student (sname dee) (major law))
)
;
; Remember that initial-facts will be loaded into the
; CLIPS database when a (reset) command is issued.
;
(defrule rule-1 "a first rule"
(student (sname ?name) (major law))
=>
(assert (law-interest ?name))
(printout t ?name " would be interested in Law" crlf)
)
;
; Notice the format of the printout command. The 't' is
; there to say that the output should go to the terminal
; (stdout). Variables can be mixed with text (in double; quotes). crlf means produce a carriage return at the
; end of the text.
Example4.clp
Thisexampleillustratesabasicuseofreadandtest:
(deftemplatestudent"astudenttemplate"
(slotsname)
(slotmajor)
(slotinterest))
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
6/11
27/11/2014
Thisdefinesaframewiththreeslots(sname,major,interest)
(deftemplateenroll"enrollmentrecords"
(slotsname)
(slotcname)
(slotgrade))
(deffactsinitialfacts"someinitialfacts"
(student(snamedee)(majorlaw))
(enroll(snamedee)(cnameSTS350)(gradenil))
)
Rememberthatinitialfactswillbeloadedintothe
CLIPSdatabasewhena(reset)commandisissued.
(defruleaskgraderule
(student(sname?name))
?f1<(enroll(sname?name)(cname?cnme)(gradenil))
=>
(printoutt"Pleaseenterthegradein"?cnme"for"?name">")
(bind?score(read))
(modify?f1(grade?score))
)
(defrulecheckgraderule
(student(sname?name))
(enroll(sname?name)(cname?cnme)(grade?sgrade))
(test(numberp?sgrade))
(test(>=?sgrade3.0))
=>
(printoutt"Student"?name"didwellin"?cnmecrlf)
)
SCXTAdvise.clp
(deftemplate student "A student frame"
(slot sno)
(slot sname)
(slot major)
(multislot interests))
(deftemplate enroll "students enrolled in classes"
(slot sno)
(slot cno)
(slot grade (type NUMBER)))
(deftemplate class "classes students take"
(slot cno)
(slot cname)
(slot dept))
(defrule cogsci-rule-1
(student (sno ?sno) (sname ?sname) (major ?major)
(interests $? psych $?))
=>
(printout t ?sname "would be interested in SCXT 350" crlf)
)
;
(deffacts Initial-facts
(student (sno s01) (sname Poirot) (major csci)
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
7/11
27/11/2014
Aslightlymorecomplicatedexample:student.clp
(deftemplatestudent"Astudentrecord"
(slotsno)
(slotsname)
(slotmajor)
(slotwcomm)
(slotscxt)
(slotunits)Numberofunitspassed
(slotsatm)
(slotsatv))
(deftemplateenroll
(slotsno)
(slotcno))
(deftemplateclass
(slotcno)
(slotcname)
(slotdept))
(defrulesuggestmathrule
(interest?snomath)
(ability?snomath)
=>
(assert(suggest?snotakemath))
)
(defrulefindmathinterest
(student(sno?snumb))
(enroll(sno?snumb)(cno?cnumb))
(class(cno?cnumb)(deptmath))
=>
(assert(interest?snumbmath))
)
(defrulefindmathability
(student(sno?snumb)(satm?score))
(test(and(numberp?score)
(>?score600)))
=>
(assert(ability?snumbmath))
)
(deffactsinitialfacts
(student(sno123)(sname"Marple"))
(enroll(sno123)(cno321))
(class(cno321)(cname"NaiveQuantumMechanics")(deptmath))
)
(defruleasksatmrule
?f1<(student(sno?snumb)(sname?name)(satmnil))
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
8/11
27/11/2014
=>
(printoutt"Pleaseenterthesatmathscorefor"?name"")
(bind?score(read))
(modify?f1(satm?score))
)
Finally,two(orthree)examplesof"FuzzyClips"YoucaneditthesefilesusingCLIPSEdt,butyou
mustrunthemusingFZClips(inthehandoutsfolder)
FZStudent.clp
(deftemplate FZscore
0 800 pts
((high (400 0) (800 1))))
;
; This template tells us that if a "fuzzy score"
; is 400 or less, we do not consider it a high score.
; If the score is 800 or greater we consider it a
; high score with 100% confidence. Between 400 points
; and 800 points, our confidence in calling the score
; a high score increases (linearly) as the score moves
; between 400 and 800.
;
(deftemplate student "A student record"
(slot sno)
(slot sname)
(slot major)
(slot wcomm)
(slot scxt)
(slot units) ; Number of units passed
(slot satm (type FUZZY-VALUE FZscore))
(slot satv))
;
(deftemplate enroll
(slot sno)
(slot cno))
;
(deftemplate class
(slot cno)
(slot cname)
(slot dept))
;
(defrule suggest-math-rule
(interest ?sno math)
(ability ?sno math)
=>
(assert (suggest ?sno take-math))
)
;
(defrule find-math-interest
(declare (CF 0.9))
;
; CF is a "confidence factor".
;
(student (sno ?snumb))
(enroll (sno ?snumb) (cno ?cnumb))
(class (cno ?cnumb) (dept math))
=>
(assert (interest ?snumb math))
;
; The assertation will come with a confidence factor
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
9/11
27/11/2014
;
)
;
(defrule find-math-ability
(student (sno ?snumb) (satm high))
=>
(assert (ability ?snumb math))
)
;
(deffacts initial-facts
(student (sno 123) (sname "Marple")
(satm (699 0) (700 1) (701 0)))
;
; This is a way of saying that the sat math score is 700,
; but presenting it as a distribution.
;
(enroll (sno 123) (cno 321))
(class (cno 321) (cname "Naive Quantum Mechanics") (dept math))
)
;
Severalmoreexamples:
FZ1.clp
;
; Example from page 351 of Durkin written in Fuzzy Clips
;
(deffacts initial-list "initial facts"
(I-believe-in-rain) CF 0.95
(Weatherman-believes-in-rain) CF 0.85
)
;
(defrule Rule1a
(declare (CF 0.9))
(weather lousy)
=>
(assert (no-ballgame))
)
;
(defrule Rule1b
(declare (CF 0.9))
(mood lousy)
=>
(assert (no-ballgame))
)
;
(defrule Rule2
(declare (CF 0.8))
(I-believe-in-rain)
=>
(assert (weather lousy))
)
;
(defrule Rule3
(declare (CF 0.9))
(I-believe-in-rain)
(Weatherman-believes-in-rain)
=>
(assert (mood lousy))
)
;
(defrule Rule4
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
10/11
27/11/2014
FZ2.clp:
; An example of a fuzzy fact
;
; (from the Fuzzy Clips documentation, page 5.2)
;
(deftemplate temperature
0 100 C
( (cold (0 1) (25 0))
(hot (20 0) (100 1))
))
;
(defrule rule1
(temperature hot)
=>
(printout t "it's hot" crlf)
(assert (fact1))
)
;
(defrule rule2
(temperature cold)
=>
(printout t "it's cold" crlf)
(assert (fact 2))
)
;
(deffacts initial-facts (temperature (67 0) (68 1) (69 0)))
;(deffacts initial-facts (temperature (22 0) (23 1) (24 0)))
http://www.math.ups.edu/~matthews/STS350_S2006/CLIPS.htm
11/11