NETB131 Programming Project
Programming language CLIPS
An overview
Kristian Ognianov Bojinov, F39091
Navigation:
1. History and Special Features
2. "Hello World" Program
3. Fundamental Data Types and Assignment Operators
4. Basic Control Flow
5. Functions
6. Arrays
7. Compilers
8. Projects and software in Groovy
9. Standard
10. References
1.
History and Special Features (^)
The origins of the C Language Integrated
Production System
(CLIPS) date back to 1984 at NASA's
The Artificial Intelligence Section felt that the use of a
conventional
language, such as C, would eliminate most of these problems, and
initially
looked to the expert system tool vendors to provide an expert system
tool
written using a conventional language. Although a number of tool
vendors
started converting their tools to run in C, the cost of each tool was
still
very high, most were restricted to a small variety of computers, and
the
projected availability times were discouraging. To meet all of its
needs in a
timely and cost effective manner, it became evident that the Artificial
Intelligence Section would have to develop its own C based expert
system tool. The prototype version of CLIPS was developed in the spring
of 1985
in a
little over two months. Particular attention was given to making the
tool
compatible with expert systems under development at that time by the
Artificial
Intelligence Section. Thus, the syntax of CLIPS was made to very
closely
resemble the syntax of a subset of the ART expert system tool developed
by
Inference Corporation. Although originally modelled from ART, CLIPS was
developed
entirely without assistance from Inference or access to the ART source
code.The original intent of the prototype was to gain useful insight
and
knowledge about the construction of expert system tools and to lay the
groundwork for the construction of a fully usable tool. The CLIPS
prototype had
numerous shortcomings, however, it demonstrated the feasibility of the
project
concept. After additional development, it became apparent that
sufficient
enhancements to the prototype would produce a low cost expert system
tool that
would be ideal for the purposes of training. Another year of
development and
internal use went into CLIPS improving its portability, performance,
and
functionality. A reference manual and user's guide were written during
this time.
The first release of CLIPS to groups outside of NASA, version 3.0,
occurred in
the summer of 1986.Further enhancements transformed CLIPS from a
training tool into a
tool
useful for the development and delivery of expert systems as well.
Versions 4.0
and 4.1 of CLIPS, released respectively in the summer and fall of 1987,
featured greatly improved performance, external language integration,
and
delivery capabilities. Version 4.2 of CLIPS, released in the summer of
1988,
was a complete rewrite of CLIPS for code modularity. Also included with
this
release were an architecture manual providing a detailed description of
the
CLIPS software architecture and a utility program for aiding in the
verification and validation of rule- based programs. Version 4.3 of
CLIPS,
released in the summer of 1989, added still more
functionality.Originally, the primary representation methodology in
CLIPS was a
forward
chaining rule language based on the Rete algorithm (hence the
Production System
part of the CLIPS acronym). Version 5.0 of CLIPS, released in the
spring of
1991, introduced two new programming paradigms: procedural programming
(as
found in languages such as C and Ada) and object- oriented programming
(as
found in languages such as the Common Lisp Object System and
Smalltalk). The
object- oriented programming language provided within CLIPS is called
the CLIPS
Object- Oriented Language (COOL). Version 5.1 of CLIPS, released in the
fall of
1991, was primarily a software maintenance upgrade required to support
the
newly developed and/or enhanced X Window, MS- DOS, and Macintosh
interfaces.Because of its portability, extensibility, capabilities, and
low-
cost,
CLIPS has received widespread acceptance throughout the government,
industry,
and academia. The development of CLIPS has helped to improve the
ability to
deliver expert system technology throughout the public and private
sectors for
a wide range of applications and diverse computing environments. CLIPS
is being
used by over 4,000 users throughout the public and private community
including:
all NASA sites and branches of the military, numerous federal bureaus,
government contractors, universities, and many private companies. CLIPS
is
available at a nominal cost through COSMIC, the NASA software
distribution
center (for more on COSMIC, see appendix E of the Basic Programming
Guide).
3. Fundamental
Data Types (float, integer, symbol,
string,
externaladdress, factaddress, instancename and instanceaddress)
and Assignment Operators (^)
CLIPS provides eight primitive data types for representing
information.
These types are float, integer, symbol, string, externaladdress,
factaddress,
instancename and instanceaddress. Numeric information
can be
represented using floats and integers. Symbolic information can be
represented
using symbols and strings.
A number consists only of digits (0- 9), a decimal
point (.),
a sign (+ or - ), and, optionally, an (e) for exponential notation with
its
corresponding sign. A number is either stored as a float or an integer.
Any
number consisting of an optional sign followed by only digits is stored
as an
integer (represented internally by CLIPS as a C long integer). All
other
numbers are stored as floats (represented internally by CLIPS as
a C
double- precision float). The number of significant digits will depend
on the
machine implementation. Roundoff errors also may occur, again depending
on the
machine implementation. As with any computer language, care should be
taken
when comparing floating- point values to each other or comparing
integers to
floating- point values. Some examples of integers are
237 15 +12 -32.Some examples of floats are 237e3 15.09 +12.0
-32.3e-7.Specifically, integers use the following
format:<integer> ::= [+ | -] <digit>+ <digit>
::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Floating point numbers use the following format:<float> ::=
<integer> <exponent> |<integer> . [exponent] .
<unsigned integer> [exponent] <integer> . <unsigned
integer> [exponent] <unsigned-integer> ::= <digit>+
<exponent> ::= e | E <integer>
A sequence of characters which does not exactly follow the format of
a
number is treated as a symbol (see the next paragraph).
A symbol in CLIPS is any sequence of characters that starts with any
printable
ASCII character and is followed by zero or more printable ASCII
characters.
When a delimiter is found, the symbol is ended. The following
characters act as
delimiters: any non- printable ASCII character (including
spaces, tabs,
carriage returns, and line feeds), a double quote, opening and closing
parentheses "(" and ")", an ampersand "&", a
vertical bar "|", a less than "<", and a tilde
"~". A semicolon ";" starts a CLIPS comment and also acts
as a delimiter. Delimiters may not be included in symbols with the
exception of
the "<" character which may be the first character in a symbol. In
addition, a symbol may not begin with either the "?" character or the
"$?" sequence of characters (although a symbol may contain these
characters). These characters are reserved for variables. CLIPS is case
sensitive (i.e. uppercase letters will match only uppercase letters).
Note that
numbers are a special case of symbols (i.e. they satisfy the definition
of a
symbol, but they are treated as a different data type). Some simple
examples of
symbols are
foo Hello B76-HI bad_value
127A 456-93-039 @+=-% 2each
A string is a set of characters that starts with a double quote (")
and
is followed by zero or more printable characters. A string ends with
double
quotes. Double quotes may be embedded within a string by placing a
backslash
(\) in front of the character. A backslash may be embedded by placing
two
consecutive backslash characters in the string. Some examples are
"foo" "a and b" "1 number"
"a\"quote"
An externaladdress is the address of an external data structure
returned by
a function (written in a language such as C or Ada) that has been
integrated
with CLIPS. This data type can only be created by calling a function
(i.e. it
is not possible to specify an external- address by typing the value).
In the
basic version of CLIPS (which has no user defined external functions),
it is
not possible to create this data type. External- addresses are
discussed in
further detail in the Advanced Programming Guide. Within CLIPS, the
printed
representation of an external- address is
<Pointer-XXXXXX> where XXXXXX is the external- address.
An instance is an object that is an instantiation or
specific example
of a class. Objects in CLIPS are defined to be floats, integers,
symbols,
strings, multifield values, externaladdresses, factaddresses or
instances of a
user- defined class. A userdefined class is created using the defclass
construct. An instance of a user- defined class is created with the
makeinstance
function, and such an instance can be referred to uniquely by address.
Within
the scope of a module an instance can also be uniquely referred to by
name. An
instancename is formed by enclosing a symbol within left and right
brackets.
Thus, pure symbols may not be surrounded by brackets. If the CLIPS
Object
Oriented Language (COOL) is not included in a particular CLIPS
configuration, then
brackets may be wrapped around symbols. Some examples of instance-
names are:
[pump-1] [foo] [+++] [123-890].In CLIPS, a placeholder that has a value
(one of the primitive data
types)
is referred to as a field. The primitive data types are referred to as
singlefield values. A constant is a non- varying single field
value
directly expressed as a series of characters (which means that
externaladdresses, factaddresses and instanceaddresses cannot be
expressed as
constants because they can only be obtained through function calls and
variable
bindings). A multifield value is a sequence of zero or more single
field
values. When displayed by CLIPS, multifield values are enclosed in
parentheses.
Collectively, single and multifield values are referred to as values.
Some examples of multifield values are
(1 bar foo) () (x 3.0 "red" 567)
4. Basic
Control Flow (conditional and loop
statements) (^)
DEFINING RULES
Rules are defined using the defrule construct.
Syntax
(defrule <rule-name> [<comment>]
[<declaration>] ; Rule Properties
<conditional-element>* ; Left-Hand Side (LHS)
<action>*) ; Right-Hand Side (RHS)
Redefining a currently existing defrule causes the previous defrule
with the
same name to be removed even if the new definition has errors in it.
The LHS is made up
of a
series of conditional elements (CEs) which typically consist of pattern
conditional elements
(or just
simply patterns) to be matched against pattern entities. An implicit
and conditional
element
always surrounds all the patterns on the LHS. The RHS contains a list
of actions to be
performed
when the LHS of the rule is satisfied. In addition, the LHS of a rule
may also contain
declarations
about the rule’s properties immediately following the rule’s name and
comment . The
arrow
(=>) separates the LHS from the RHS. There is no limit to the number
of conditional elements or actions a rule may have. Actions are
performed
sequentially if, and only if, all conditional elements on the LHS are
satisfied. If no conditional elements are on the LHS, the pattern CE
(initial-fact) or
(initial-object) is automatically used. If no actions are on the RHS,
the rule can be
activated and
fired but nothing will happen. As rules are defined, they are
incrementally reset. This means that CEs
in
newly defined rules can be satisfied by pattern entities at the time
the rule is defined,
in
addition to pattern entities created after the rule is defined
Example
(defrule example-rule "This is an example of a simple rule"
(refrigerator light on)
(refrigerator door open)
=>
(assert (refrigerator food spoiled)))
BASIC CYCLE OF RULE EXECUTION
Once a knowledge base (in the form of rules) is built and the fact-list
and
instance-list is prepared, CLIPS is ready to execute rules. In a
conventional language,
the
starting point, the stopping point, and the sequence of operations are
defined explicitly
by the
programmer. With CLIPS, the program flow does not need to be defined
quite so
explicitly. The
knowledge (rules) and the data (facts and instances) are separated, and
the inference
engine
provided by CLIPS is used to apply the knowledge to the data. The basic
execution cycle is
as
follows:
a) If the rule firing limit has been reached or there is no current
focus, then
execution is halted.
Otherwise, the top rule on the agenda of the module which is the
current focus
is selected
for execution. If there are no rules on that agenda, then the current
focus is
removed from
the focus stack and the current focus becomes the next module on the
focus
stack. If the
focus stack is empty, then execution is halted, otherwise step a is
executed
again.
b) The right-hand side (RHS) actions of the selected rule are executed.
The use
of the return
function on the RHS of a rule may remove the current focus from the
focus stack
The number of rules fired is incremented for use with the rule
firing limit.
c) As a result of step b, rules may be activated or deactivated.
Activated
rules (those rules
whose conditions are currently satisfied) are placed on the agenda of
the
module in which
they are defined. The placement on the agenda is determined by the
salience of
the rule and
the current conflict resolution strategy .
Defrule Construct
comparisons that must be performed on the LHS of the rule. Each
comparison to a
constant or previously bound variable adds one to the specificity. Each
function
call made
on the LHS of a
rule as part of the :, =, or test conditional element adds one to the
specificity. The boolean functions and, or, and not do not add to the
specificity of a rule, but
their
arguments do. Function calls made within a function call do not add to
the
specificity of a
rule. For example,the following rule
(defrule example)
(item ?x ?y ?x)
(test (and (numberp ?x) (> ?x (+ 10 ?y)) (< ?x 100)))
has a specificity of 5. The comparison to the constant item, the
comparison of
?x to its previous binding, and the calls to the numberp, <, and
> functions each
add one to
the specificity for a
total of 5. The calls to the and and + functions do not add to the
specificity
of the rule.
MEA Strategy
Among rules of the same salience, newly activated rules are placed
using the
OPS5 strategy of the same name. First the time tag of the pattern
entity associated with
the
first pattern is used to
determine where to place the activation. An activation thats first
pattern’s time tag is greater than another activations first pattern’s
time tag is placed before the other
activation on the agenda. If
both activations have the same time tag associated with the first
pattern, then
the LEX strategy is used to determine placement of the activation.
Again, as with the CLIPS
LEX
strategy, negated
patterns have pseudo time tags. As an example, the following six
activations have been listed in their
MEA
ordering (where the comma at the end of the activation indicates the
presence of a negated
pattern).
(defrule rule-1
(factoid a)
(assert (factoid b)))
(defrule rule-2
?f <- (factoid a)
(factoid d)
(retract ?f)
(assert (factoid c)))
Pattern Conditional Element Pattern conditional elements consist of a
collection of field
constraints,
wildcards, and variables which are used to constrain the set of facts
or instances
which match
the pattern CE. A pattern CE is satisfied by each and every pattern
entity that satisfies
its
constraints. Field constraints are a set of constraints that are used
to test a single
field or
slot of a pattern entity. A
field constraint may consist of only a single literal constraint,
however, it
may also consist of
(deffacts data-facts
(data 1.0 blue "red")
(data 1 blue)
(data 1 blue red)
(data 1 blue RED)
(data 1 blue red 6.9))
(deftemplate person
(slot name)
(slot age)
(multislot friends))
(deffacts people
(person (name Joe) (age 20))
(person (name Bob) (age 20))
(person (name Joe) (age 34))
(person (name Sue) (age 34))
(person (name Sue) (age 20)))
5.
Functions (^)
A function in CLIPS is a piece of executable code identified by a
specific name
which returns a useful value or performs a useful side effect (such as
displaying information). Throughout the CLIPS documentation, the word
function
is generally used to refer only to functions which return a value
(whereas
commands and actions are used to refer to functions which have a side
effect
but generally do not return a value). There are several types of
functions. User defined functions
and system
defined functions are pieces of code that have been written
in an
external language (such as C, FORTRAN, or Ada) and linked with the
CLIPS
environment. System defined functions are those functions that have
been
defined internally by the CLIPS environment. User defined functions are
functions that have been defined externally of the CLIPS environment. A
complete list of system defined functions can be found in appendix I.
The deffunction construct allows users to define new functions directly
in the
CLIPS environment using CLIPS syntax. Functions defined in this manner
appear
and act like other functions, however, instead of being directly
executed (as
code written in an external language would be) they are interpreted by
the
CLIPS environment. Generic functions can be defined using the
defgeneric and defmethod
constructs.
Generic functions allow different pieces of code to be executed
depending upon
the arguments passed to the generic function. Thus, a single function
name can
be overloaded with more than one piece of code.Function calls in
CLIPS use a prefix notation - the arguments to a
function
always appear after the function name. Function calls begin with a left
parenthesis, followed by the name of the function, then the arguments
to the
function follow (each argument separated by one or more spaces).
Arguments to a
function can be primitive data types, variables, or another function
call. The
function call is then closed with a right parenthesis. Some examples of
function calls using the addition (+) and multiplication (*) functions
are
shown following.
(+ 3 4 5)
(* 5 6.0 2)
(+ 3 (* 8 9) 4)
(* 8 (+ 3 (* 2 3 4) 9) (* 3 4))
While a function refers to a piece of executable code identified by a
specific
name, an expression refers to a function which has its arguments
specified
(which may or may not be functions calls as well). Thus the previous
examples
are expressions which make calls to the * and + functions. Information
about
the function in CLIPS can be found here
Task:Input
an integer number n and output the sum: 1+2^2+3^2+...+n^2.
Use input validation for n to be positive.
(defun power (x y) (if (= y 0) 1 (* x (power x (1- y)))))
(defun sumpow (x y) (if (< x 1) 0 (+ (power x y) (power (1- x) y))))
6. Arrays
(^)
An example of array With CLIPSActiveXControl1
DataGrid.Cols =3D 14
DataGrid.Rows =3D .NumberOfFacts + 1
For FactCount =3D 1 To .NumberOfFacts
RowCount =3D RowCount + 1
.NextFact
For SlotCount =3D 1 To 12
If ParseDeftemplateName(.FactString)=3D "Dogs" Then =
'ParseDeftemplateName grabs the deftemp name of the current fact
'now the program should fill in the slots for all of the dogs
DataGrid.Row =3D RowCount
DataGrid.Col =3D SlotCount
DataGrid.Text =3D .GetSlotValue(.InstancePtr, =
SlotName(SlotCount)) 'slotname is an array of the names of slots
End If
Next SlotCount
.NextFact
Next FactCount
End With
7.
Compilers (^)
There is a new release for Windows called CLIPS
6.3 Beta.You
can find it on the download
area
on the CLIPS homepage.And there is also CLIPS Java Native Interface 0.1
Beta.
Version 0.1 demonstrates basic techniques for integrating CLIPS with
a GUI
and comes with three examples of CLIPS programs integrated with a Swing
front
end.
8. Projects
and software in CLIPS (^)
All the information needed for the CLIPS projects can be found on
the CLIPS webpage
When tou subscribe in the google group for CLIPS you can enter the
CLIPS
projects and find more about the software written on CLIPS.
9.
Standard (^)
CLIPS was made to very closely resemble the syntax of a subset of
the ART
expert system tool developed by Inference Corporation. Although
originally
modelled from ART, CLIPS was developed entirely without assistance from
Inference or access to the ART source code.CLIPS is portable,
extensible,
capable, and low- cost language.
10.
References (^)
CLIPS on http://www.comp.rgu.ac.uk/staff/smc/teaching/clips/vol1/
CLIPS on Wikipedia
CLIPS documentation