Title: Introduction to CLIPS: An expert programming language
1Introduction to CLIPS An expert programming
language
- CLIPS (C Language Integrated Production System)
A programming language designed by NASA/Johnson
Space Center with the specific purpose of
providing high portability, low cost, and easy
integration with external systems. - was written using the C programming language
2Introduction to CLIPS An expert programming
language
- Three basic components of CLIPS
- Fact list contains the data on which inferences
are derived - Knowledge base contains all the rules
- Inference engine controls overall execution
3Introduction to CLIPS An expert programming
language
- 3 types of programming paradigms supported by
CLIPS - rule-based
- object-oriented
- Procedural
- We will primarily focus on CLIPS as a rule-
- based programming language in this class
4Introduction to CLIPS An expert programming
language
- The inferencing and representation capabilities
provided by CLIPS rule-based programming language
is similar to, but more powerful than, OPS5. - Syntactically, CLIPS rules closely resemble the
rules of ART, ART-IM, Eclipse, and Cognate. - CLIPS supports only forward chaining rules.
5CLIPS Notation
- Symbols and characters Anything not enclosed by
the character pairs lt gt, , or should be
entered exactly as shown - e.g. (example)
- This syntax description means that (example)
should be entered as shown. The character (
should be entered first, followed by e, x,
a, etc
6CLIPS Notation
- Square brackets, , indicate that the contents
of the brackets are optional. - e.g. (example 1)
- Indicates that the 1 found within the brackets
is optional. So the following would both be
consistent with the syntax - (example)
- OR
- (example 1)
7CLIPS Notation
- The less than and greater than characters, lt gt,
indicate that a replacement is to be made with a
value of the type specified by the contents found
within lt gt. - e.g. ltintegergt
- indicates that a substitution should be made
with an actual integer value - (example ltintegergt) could be
- (example 1)
- (example 5)
- (example -20)
- it is important to note that spaces shown in
the syntax description should be included in the
entry.
8CLIPS Notation
- Another notation is the following a
description. This indicates that the description
can be replaced with zero or more occurrences of
the value specified. Spaces should be placed
after each occurrence of a value. - e.g. ltintegergt could be replaced with
- 1
- 1 2
- 1 2 3
- or with any number of integers, or with nothing
at all
9CLIPS Notation
- A description followed by a indicates that one
or more of the values specified by the
description should be used in placed of the
syntax description. - e.g. ltintegergt is equivalent to
- ltintegergt ltintegergt
10CLIPS Notation
- A vertical bar, , indicates a choice among one
or more of the items separated by the bars. - e.g. all none some could be replaced with
- all
- none
- some
11CLIPS Fields
- Tokens represent groups of characters that have
special meaning to CLIPS as CLIPS reads
characters from the keyboard or files, it groups
them together into tokens. Some tokens such as
left and right parentheses consist of only 1
character. - Fields a group of tokens.
- 7 types of fields (also called the CLIPS
primitive data types) - Float
- Integer
- Symbol
- String
- External address
- Instance name
- Instance address
12CLIPS Notation
- Numeric fields numbers.
- two types
- integers any number consisting of an optional
sign followed by only digits is stored as an
integer. - e.g.
- 1
- 3
- -1
- 65
- floats all other numbers
- e.g.
- 1.5
- 1.0
- 0.7
- 9e1
- 3.5e10
13CLIPS Fields
- Three parts of numeric fields
- sign either or -
- value contains one or more digits with a single
optional decimal point contained with the digits.
- exponent consists of the letter e or E followed
by an optional or - followed by one or more
digits.
14CLIPS Symbols
- symbol a field that starts with a printable
ASCII character and is followed by zero or more
characters. - The end of a symbol is reached when a delimiter
is encountered. - Symbols cannot contain delimiters (with the
exception of lt which may be the first character
in a symbol). - The ? and ? sequence cannot be placed at the
beginning of a symbol since these are used to
denote variables. - A sequence of characters which doesnt follow the
numeric format is treated as a symbol.
15CLIPS Delimiters
- delimiter includes any nonprintable ASCII
character e.g. spaces, tabs, carriage returns,
line feeds, the , ( , ) , , , , , and lt
characters.
16CLIPS An example
- examples of valid symbols
- Space-Station
- February
- fire
- activate_sprinkler_system
- notify-fire-department
- !?
- 345B
- note underscore _ and hyphen - characters
should be used to tie symbols together to make
them a single field.
17CLIPS additional information
- case sensitivity CLIPS will preserve the
uppercase and lowercase letters it finds in
tokens. - e.g.
- case-sensitive
- Case-sensitive
- CASE-SENSITIVE
18CLIPS strings
- strings
- must begin and end with double quotes . The
quotes are part of the fields there can be zero
or more characters of any kind between the double
quotes including characters normally used by
CLIPS as delimiters. - e.g.
- activate the sprinkler system
- Shut down electrical junction 387.
- !7
- John Q. Madison
- spaces normally act as delimiters to separate
fields (such as characters) and other tokens.
Additional spaces used between tokens are
discarded. Spaces included in the string are
preserved. - e.g. all of these are distinct strings
- spaces
- spaces
- spaces
- spaces
- note if surrounding double quotes were
removed, CLIPS would consider
each of the lines to contain the same word since
spaces other than those used
as delimiters would be ignored.
19 CLIPS double quotes
- It is not possible to directly place a double
quote within a string - e.g. three-tokens
- this would be interpreted by CLIPS as 3 separate
tokens since double quotes act as delimiters. It
would read it as -
- three-tokens
-
- to correct this use the backslash \ operator
- e.g.
- \ single-token\ will be interpreted by CLIPS
as - single-token
- the backslash character may be placed within a
string by using two backslashes in succession - e.g.
- \\single-token\\ will be interpreted by CLIPS
as - \single-token\
20CLIPS other types of fields
- external addresses represent the address of an
external data structure returned by a
user-defined function (a function written in a
language such as C or Ada and linked with CLIPS
to add additional functionality) its not
possible to create this type of data in the basic
unmodified version of CLIPS. - instance addresses like external addresses, can
only be obtained as the return value from a
function fields used in conjunction with COOL.
- instance names symbols enclosed within left and
right square brackets e.g. pump-1 fields used
in conjunction with COOL.
21CLIPS multifield value
- multifield value a series of zero or more fields
contained together are usually created by
calling a function or when specifying initial
values for facts. - e.g. a zero length multifield is ( )
- e.g. a multifield containing the symbols this
and that is (this that)
22CLIPS Entering CLIPS
- Entering issue appropriate run command for the
machine the CLIPS prompt will appear - CLIPSgt
- at this point, commands are entered directly into
- CLIPS. This is called the top level.
23CLIPS Exiting CLIPS
- Exiting the normal mode for exiting CLIPS is
with the exit command. - e.g. (exit)
-
- The parentheses around exit is a command to be
executed and not just the symbol exit. The
symbol exit without enclosing parentheses has
quite a different meaning than (exit).
24CLIPS a sample session
- sample session of entering CLIPS, evaluating a
constant field value, evaluating a function, and
then exiting using the exit command - Agt CLIPS press enter
- CLIPS (v6.0 05/12/93)
- CLIPSgt exit press enter because this
exit is not enclosed in parentheses, CLIPS
interprets is as input, not a command - exit
- CLIPSgt ( 3 4) press enter
- 7
- CLIPSgt (exit) press enter interprets it as a
command and exits - Agt exits CLIPS
25CLIPS Facts
- fact a chunk of information in CLIPS they
consist of a relation name (a symbolic field)
followed by zero or more slots (also symbolic
fields) and their associated values. - e.g. of a fact
- (person (name John Q. Public)
- (age 23)
- (eye-color blue)
- (hair-color black))
- the symbol person is the facts relation name and
the fact contains 4 slots name, age, eye-color,
hair-color. The values of the slots are John Q.
Public, 23, blue, black the order in which slots
are specified are irrelevant. - the following fact is treated by CLIPS identical
to the one above - (person (hair-color black)
- (name John Q. Public)
- (eye-color blue)
- (age 23)
26CLIPS Deftemplate Construct
- before facts can be created, CLIPS has to know
the list of valid slots for a given relation
name. - deftemplate construct groups of facts which
share the same relation name and contain common
information analogous to a record structure. - constructs form the core of a CLIPS program by
adding the programmers knowledge to the CLIPS
environment and are different from functions and
commands.
27CLIPS another example of syntax
- general format of a deftemplate
- e.g.
- (deftemplate ltrelation-namegt ltoptional-commentgt
- ltslot-definitiongt)
- the syntax description ltslot-definitiongt is
defined as - (slot ltslot-namegt) (multislot ltslot-namegt)
- using this syntax, person fact could be
- (deftemplate person an example
- deftemplate (slot name)
- (slot age)
- (slot eye-color)
- (slot hair-color))
28CLIPS Multifield slots
- single-field slots slots of a fact that have
been specified with the slot keyword in their
corresponding deftemplate are allowed to contain
only one value - multifield slots slots that are allowed to
contain zero or more values - the name slot of the person deftemplate stores
the persons name as a single string value the
fact that was viewed before is illegal as a
single-field slot, but is legal if name is a
multifield slot.
29CLIPS Ordered facts
- deftemplate facts facts with a relation name
that has a corresponding deftemplate. - ordered facts facts with relation name that
does not have a corresponding deftemplate they
have a single implied multifield slot which is
used to store all values following the relation
name. Whenever CLIPS encounters an ordered fact,
it automatically creates an implied deftemplate
for that fact (as opposed to an explicit
deftemplate created using the deftemplate
construct) since an ordered fact has only one
slot, the slot name isnt required when defining
a fact. - a list of numbers could be represented with the
following fact - (number-list 7 9 3 4 20)
- This is equivalent to defining the deftemplate
- (deftemplate number-list (multislot values))
- and then defining the fact as follows
- (number-list (values 7 9 3 4 20))
30CLIPS Ordered facts (continued)
- deftemplates should be used whenever possible
because the slot names make the facts more
readable and easier to work with. - 2 cases in which ordered facts are useful
- Facts consisting of just a relation name are
useful as flags and look identical regardless of
whether a deftemplate has been defined. - e.g. (all-orders-processed)
- could be used as a flag to indicate when all
orders have been processed. - For facts containing a single slot, the slot name
is usually synonymous with the relation name. - (time 845)
- (food-groups meat dairy bread
fruits-and-vegetables) - are just as meaningful as
- (time (value 845))
- (food-groups (values meat dairy bread
fruits-and-vegetables)) -
31CLIPS Adding and removing facts
- the group of all facts known to CLIPS is stored
in the fact list facts representing information
can be added and removed from the fact list. New
facts can be added to the fact list using the
assert command. - e.g. (assert ltfactgt)
- using the person deftemplate, add John Q. Public
to the fact list - CLIPSgt
- (deftemplate person
- (slot name)
- (slot age)
- (slot eye-color)
- (slot hair-color)) press enter
- CLIPSgt
- (assert (person (name John Q. Public)
- (age 23)
- (eye-color blue)
- (hair-color black)) ) press enter
- ltFact-0gt
- CLIPSgt
32CLIPS adding and removing facts (continued)
- facts command can be used to display the facts in
the fact list. - e.g.
- CLIPSgt (facts) press enter
- f-0 (person (name John Q. Public)
(age 23) - (eye-color blue) (hair-color black))
- For a total of 1 fact.
- CLIPSgt
- the term f-0 is the fact identifier assigned to
the fact by CLIPS. Every fact is assigned a fact
identifier starting with the letter f and
followed by an integer called the fact
index. - Normally, CLIPS doesnt accept duplicate entries
of a fact therefore, attempting to place a
second John Q. Public fact with identical slot
values will have no result. -
33CLIPS viewing a portion of the fact list
- to view a portion of the fact list, do
- (facts ltstartgt ltendgt ltmaximumgt)
- where ltstartgt, ltendgt, and ltmaximumgt are
positive integers. If no arguments are specified,
all facts are displayed. - if the ltstartgt argument is specified, all facts
with fact indexes greater than or equal to
ltstartgt are displayed. - if ltstartgt and ltendgt are specified, all facts
with fact indexes greater than or equal to
ltstartgt and less than or equal to ltendgt are
displayed. - if ltmaximumgt is specified along with ltstartgt and
ltendgt, no more than ltmaximumgt facts will be
displayed.
34CLIPS Removing facts
- retraction removing facts from fact list is
done with the retract command. - e.g. (retract ltfact-indexgt)
- fact indices of one or more facts to be retraced
are included as the arguments of the retract
command. - a single retract command can be used to retract
multiple facts at once - (retract 0 1)
- To remove John Q. Public from the fact list
- (retract 0)
- attempting to retract a nonexistent fact will
produce the following error message (where
PRNTUTIL1 is a key for finding the error
message in CLIPS Reference Manual. - PRNTUTIL1 Unable to find fact ltfact-identifiergt
35CLIPS Modifying and duplicating facts
- modify command slot values of deftemplate facts
can be modified using the modify command. - (modify ltfact-indexgt ltslot-modifiergt)
- where ltslot-modifiergt is
-
- (ltslot-namegt ltslot-valuegt)
- to modify John Q. Public, do
- CLIPSgt (modify 0 (age 24)) press enter
- ltFact-2gt
- CLIPSgt (facts) press enter
- f-2 (person (name John Q. Public) (age
24) - (eye-color blue)
(hair-color black)) - For a total of 1 fact.
- CLIPSgt
36CLIPS duplicate command
- duplicate command works the same as the modify
command with the exception that it does not
retract the original fact. - to add another name but keep the same field
values, do - CLIPSgt (duplicate 2 (name Jack S. Public))
press enter - ltFact-3gt
- CLIPSgt (facts) press enter
- f-2 (person (name John Q. Public)
- (age 24)
- (eye-color blue) (hair-color black))
- f-3 (person (name Jack S. Public)
(age 24) - (age 24)
- (eye-color blue) (hair-color black))
- For a total of 2 facts.
- CLIPSgt
- note modify and duplicate commands can not be
used with ordered facts.
37CLIPS The watch command
- watch command useful for debugging programs.
- (watch ltwatch-itemgt)
-
- where ltwatch-itemgt is one of the symbols
facts, rules, activations, statistics,
compilations, or all. - a. watch can be used to watch more than one
feature of CLIPS execution. - b. all can be used to enable all of the
watch features. - The effects of a watch command may be turned off
by using the corresponding unwatch command. - (unwatch ltwatch-itemgt)
38CLIPS watch command (continued)
- note CLIPS will automatically print a message
indicating that an update has been made to the
fact list whenever facts are asserted or
retracted. - e.g.
- CLIPSgt (FACTS 3 3) press enter
- f-3 (person (name Jack S. Public)
(age 24) - (eye-color blue) (hair-color
black)) - For a total of 1 fact.
- CLIPSgt (watch facts) press enter
- CLIPSgt (modify 3 (age 25) press enter
- ? f-3 (person (name Jack S. Public) (age 24)
- (eye-color blue)
- (hair-color black))
- ?f-4 (person (name Jack S.
Public) (age 25) - (eye-color blue)
- (hair-color black))
- ltFact-4gt
- CLIPSgt
-
- The ? indicates that the fact is being
retracted. - The ? indicates that the fact is being
asserted.
39CLIPS Deffacts Construct
- deffacts construct groups of facts which
represent initial knowledge the following
deffacts statement provides initial information
about people we have already learned about - (deffacts people some people we know
- (person (name John Q. Public) (age 24)
- (eye-color blue) (hair-color
black)) - (person (name John Q. Public) (age 24)
- (eye-color blue) (hair-color
black)) - the general format of a deffacts is
- (deffacts ltdeffacts namegt ltoptional commentgt
- ltfactsgt)
40CLIPS reset command
- reset command removes all facts from the fact
list and then asserts the facts from existing
deffacts statement. - e.g. (reset)
- assuming that the people deffacts had been
entered, the following shows how the reset
command adds facts to the list - CLIPSgt (unwatch facts) press enter
- CLIPSgt (facts) press enter
- f-0 (initial-fact)
- f-1 (person (name John Q. Public)
(age 24) - (eye-color blue) (hair-color black))
- f-2 (name Jack S. Public) (age 24)
- (eye-color blue) (hair-color black))
- For a total of 3 facts.
- CLIPSgt
- note the output shows the facts from the
deffacts statement and a new fact generated by
the reset command called initial-fact.
41CLIPS reset command (continued)
- Upon start-up, CLIPS automatically defines the
following two constructs - (deftemplate initial-fact)
- (deffacts initial-fact (initial-fact))
- Even if you havent identified any deffacts
statements, a reset will assert the fact
(initial-fact). - The fact identifier of the initial fact is
always f-0. -
42CLIPS Final Note
- Any additional information concerning CLIPS or to
download CLIPS version 6.21 go to website - http//www.ghg.net/clips/CLIPS.html