Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction to CLIPS
SEG 7450
CLIPS in PC
CLIPS is a tool for building expert systems. http://clipsrules.sourceforge.net/index.html CLIPS is a multiparadigm programming language that provides support for rule-based, ob ect!oriented, and procedural programming. "ou can do#nload it from: http://sourceforge.net/pro ect/sho#files.php$group%id&'()*+( ,pac-age%id&'.//.'
0o#nload CLIPS1in.2ip 3xtract the 2ipped file.
SEG 7450
CLIPS in PC
4r accessing CLIPS in S3 laboratories through the path 566Sepc.7+6seg+*)/8
SEG 7450
CLIPS in PC
0ouble clic- the program 5CLIPS#in.exe8
SEG 7450
CLIPS in PC
Select File !9 Load to load a clips program Select Execution !9 Reset to reset memory Select Execution !9 Run to execute the program Select Execution !9 Clear CLIPS to clear the memory Select Browse !9 Deffacts Manager to list the name of deffacts construct Select Browse !9 Deftem late Manager to list the name of 0eftemplate construct Select !indow !9 Facts to list the facts in memory Select !indow !9 "genda to list the rule to be fired at the current step Select !indow !9 #lo$als 1indo# to list the globals variance
SEG 7450 5
CLIPS in PC
:o access CLIPS ;elp
SEG 7450
CLIPS in PC
SEG 7450
CLIPS in PC
Download CLIPS %&'( and its )el
3nter #eb site http://sourceforge.net/pro ect/sho#files.php$group%id&'()*+(,pac-age%id&'. //.' Select <elease ..'*( 0o#nload cli swin*executa$le*%'(1&+i =n2ip and extract CLIPS1in.exe , CLIPS..hlp to the same directory in your PC..
CLIPS Programming
Basic elements of an Ex ert S,stem
>act!list : ?lobal memory for data @no#ledge!base : Contain all the rules Inference 3ngine : Control overall execution
SEG 7450
SEG 7450
10
SEG 7450
11
>ields
:o-ens represent groups of characters that have special meaning to CLIPS. :he group of to-ens -no#n as fields is of particular importance. :here are eight types of fields, also called the CLIPS primitive data types:
float, integer, symbol, string, external address, fact address, instance name, and instance address.
CLIPS is case!sensitive.
SEG 7450
12
>ields
B symbol is a field that starts #ith a printable BSCII character and is follo#ed by 2ero or more characters.
:he end of a symbol is reached #hen a delimiter is encountered. 0elimiters include any nonprintable BSCII character. Cspaces, tabs, returns, line feedsE and CE,FGHA Symbols cannot contain delimiters. 3.g.
Space! Space!Station >ire activate%sprin-ler%system
B string must begin and end #ith double Iuotation mar-s, #hich are part of the field.
3.g. Bctive the sprin-ler system.
3xternal addresses represent the address of an external data structure returned by a user!defined function. B fact address is used to refer to a specific fact. Bn instance address is li-e a fact address, but it refers to an instance rather than a fact. B series of 2ero or more fields contained together is referred to as a multi-field value. B multifield value is enclosed by left and right parentheses.
3.g.
CE Cthis thatE
SEG 7450 13
Facts
:o solve a problem, a CLIPS program must have data or information #ith #hich it can reason. B chun- of information in CLIPS is called a fact. >acts consist of a relation name follo#ed by 2ero or more slots and their associated values. :he follo#ing is an example of a fact:
(person (name "John Q. Public") (age 23) (eye-color blue) (hair-color black))
:he symbol person is the facts relation name and the fact contains four slots: name, age, eye-color, and hair-color.
:he value of the name slot is 5John K. Public8 :he value of the eye!color slot is blue. :he value of the hair!color slot is blac-.
SEG 7450
14
Facts
:he order in #hich slots are specified is irrelevant. :he follo#ing fact is treated as identical to the first person fact by CLIPS.
(person (hair-color black) (name "John Q. Public") (eye-color blue) (age 23))
SEG 7450
15
SEG 7450
16
Multifield Slots
MuItislot -ey#ord in their corresponding deftemplates are allo#ed to contain 2ero or more values (person (name John Q. Public) (age 23 ) (eye-color blue) (hair-color brown)) is illegal if name is a single!field slot, but legal if name is a multifield slot.
SEG 7450
17
4rdered >acts
>acts #ith a relation name that does not have a corresponding deftemplate are called ordered facts. >or example, a list of numbers could be represented #ith the follo#ing fact:
(number-list 7 9 3 4 20)
:here are t#o cases in #hich ordered facts are useful. >acts consisting of ust a relation name are useful as flags and looidentical regardless of #hether a deftemplate has been defined. >or example,
(all-orders-processed)
>or facts containing a single slot, the slot name is usually synonymous #ith the relation name. >or example, the facts:
(time 8:45) (food-groups meat dairy bread fruits-and-vegetables)
4rdered >acts
B deftemplate fact is a non!ordered fact. 4rdered versus Non!ordered 4rdered facts encode information #ith fixed position #hich is not convenient to access Clarity and slot order independence are virtues of deftemplate facts 3xtra command Ce.g. modify, duplicateE for non!ordered facts
SEG 7450
19
0eftemplate 4vervie#
SEG 7450
20
"dding Facts
Ne# facts can be added to the fact list using the assert command.
(assert <fact>+)
Bs an example,
CLIPS> (deftemplate person (slot name) (slot age) (slot eye-color) (slot hair-color)) CLIPS> (assert (person (name "John Q. Public") (age 23) (eye-color blue) (hair-color black))) <Fact-0> CLIPS>
SEG 7450 21
SEG 7450
22
SEG 7450
23
SEG 7450
24
SEG 7450
25
(<slot-name> <slot-value>)
>or example, CLIPS> (modify 0 (age 24)) <Fact-2> CLIPS> (facts) f-2 (person (name "John Q. Public") (age 24) (eye-color blue) (hair-color black)) For a total of 1 fact. CLIPS> B ne# fact index is generated for a modified fact.
SEG 7450 26
:he modify and duplicate commands cannot be used #ith ordered facts. :o enable the duplicate command, the follo#ing command may be reIuired:
(set-fact-duplication TRUE)
SEG 7450 27
SEG 7450
28
SEG 7450
29
SEG 7450
30
SEG 7450
31
Lefore converting the pseudocode to a rule, the deftemplates for the types of facts referred to by the rule must be defined.
(deftemplate emergency (slot type)) #here the type field of the emergency fact #ould contain symbols such as fire, flood, and po#er outage. (deftemplate response (slot action)) #here the action field of the response fact indicates the response to be ta-en.
SEG 7450
33
Bfter the rule header are 2ero or more conditional elements CC3sE. :he simplest type of C3 is a attern CE or simply attern& 3ach pattern consists of one or more constraints intended to match the fields of a deftemplate fact.
SEG 7450 34
SEG 7450
35
If Glimit9 is not included or Glimit9 is !(, rules #ill be fired until none are left on the agenda. Reset
Lecause rules reIuire facts to execute, the reset command is the -ey method for starting or restarting an expert system in CLIPS.
SEG 7450
36
3xample:
CLIPS> (reset) CLIPS> (assert (emergency (type fire))) <Fact-1> CLIPS> (agenda) 0 fire-emergency: f-1 For a total of 1 activation. CLIPS> :he / indicates the salience of the rule on the agenda.
SEG 7450
37
SEG 7450
39
SEG 7450
40
:he mean and maximum numbers of activations statistics indicate the average number of activations per rule firing and the largest number of activations on the agenda for anyone rule firing.
SEG 7450
42
SEG 7450
43
:he logical name t tells CLIPS to send the output to the standard output device of the computer, usually the terminal.
SEG 7450
44
SEG 7450
45
SEG 7450
47
SEG 7450
48
:he sho#!brea-s command can be used to list all brea-points. Its syntax is: (show-breaks) :he remove!brea- command can be used to remove brea-points. Its syntax (remove-break [<rule-name>])
SEG 7450
49
(save <file-name>)
3.g. Csave 5fire.clp8E
SEG 7450
50
Commenting Constructs
B comment in CILPS is any text that begins #ith a semicolon and ends #ith a carriage return. It is good to include comments describing the constructs, and giving information regarding the program.
;*************************** ;* Programmer: G. D. Riley * ;* Title: The Fire Program * ;* Date: 05/17/04 * ; Deftemplate (deftemplate emergency template #1 #1 (slot type)) ; What type of emergency (deftemplate response template #2 #2 (sloto type)) ; How to respond ; The purpose of this rule is to activate ; the sprinkler if there is a fire (defrule firefire-emergency An example rule rule ;IF (emergency (type fire)) => ; THEN ;Activate the sprinkler system (assert (response (action activateactivate-sprinkersprinker-system))))
SEG 7450 51