A gentle introduction to CLIPS for CogSci Students

(see also Clips Blurb)

CLIPS is an industrial strength expert system shell.  That is, one can use CLIPS to generate powerful industrial-strength expert systems.  In this class we will examine only a few of the basic elements of the system.

Templates, facts, and rules

Recall that an expert system contains a database which in turn contains a series of facts and rules (productions).

Facts come in two varieties:  structured facts (templates) and ad-hoc facts.  The templates for structured tasks are specified by the deftemplate command:

    (deftemplate aTemplate "some documentation"

        (slot slot1)

        (slot slot2)

            (etc.)

    )

For example, suppose that we want to establish a structure for a student at Puget Sound.  We might want to store the student's student number, name, major, and name of advisor.  A template for doing this is given by

    (deftemplate student "A student template"

        (slot sno)

        (slot sname)

        (slot major)

        (slot advisor)

    )

 

Asserting facts

There are two ways of introducing facts into the CLIPS database.  One way is to include them in a set of initial facts

    (deffacts initial-facts

        (student (sno 123) (sname maigret) (major pre-med)

            (advisor simenon)) ... )

 

deffacts are asserted after the CLIPS file containing them has been loaded into CLIPS (see below) and then after the (reset) command.

Another way to assert a fact is to assert it "on the fly", generally as an action in a rule:

    (assert (student (sno 123) (sname maigret)

        (major pre-med) (advisor simenon)))

Notice that, just as in LISP, parenthesis are important.  Although CLIPS was written in C (the letters CLI in CLIPS stand for "C Language Implementation"), the syntax for using CLIPS is very much LISP-like.

Note:  While running clips, keep your eye on the facts window, and you will see the facts you have written being added to the database.

Non-structured facts

It is sometimes convenient to have a series of ad-hoc facts in the database, as well as the structured ones.  These are simply lists of words between parenthesis, such as:

    (alarm on)

    (alarm off)

    (temperature high)

    (valve a3572 open)

Rules

The basis for a production system, of course, is the collection of condition-action rules.  The syntax for defining a rule is

(defrule rule-name "some documentation"

    (if-condition)

    (if-condition)

    (etc.)

=>

    (action 1)

    (action 2)

    (etc.)

)

For example:

(defrule useful-rule "a useful rule"
                    (animal fierce)
                    (animal big)
                    (animal hungry)
                =>
                    (assert (run away))
                )
 

What this means is that if we have the following (unstructured) facts in our database

                    (animal fierce)
                    (animal big)
                    (animal hungry)
 

then all of the preconditions have been met to fire useful-rule, and we assert (run away) into the database.

 

We will see some more examples in a moment.  First, let's talk about running CLIPS.

 

Running CLIPS

You need to have two programs to run clips

    clipsedt - edit clips files

    clipswin - the CLIPS program for windows

Copies of these are available in my handouts folder under CLIPS.

Example 1:  There is a file in the handouts folder called example1.clp

  1. Copy this into your workspace
  2. Double-click clipsedt to start the program
  3. In clipsedt, open the example1.clp file.  Look it over.
  4. Open clipswin (double-click)
  5. In the file menu, select load (cntrl-L) and load example1.clp
  6. Select the facts and agenda windows (or all of them if you would prefer)
  7. Type (reset) or select it from the execution menu.  Note the facts defined in deffacts appearing in the facts window.  Notice also that our only rule appears in the agenda window.
  8. Step through the program, watching what happens to the facts window.

Congratulations!  You have run your first CLIPS program!

 

Some other features in rules:

Pattern matching

Suppose that we have a fact such as

    (deftemplate student "a student template"
        (slot sname)
        (slot major)
        (slot interest))

(deffacts inital-facts "some initial facts"
    (student (sname dee) (major law))
)

And we would like to look through our database and collect students majoring in law and add to our database the fact that these students are interested in law.  The following rule would do this (this is in example2.clp).:

(defrule rule-1 "a first rule"
    (student (sname ?name) (major law))
=>
    (assert (law-interest ?name))
)

This rule says that if there is anything in our database asserting that a student with some name is majoring in law (major law), then we want to add to our database the fact that this student is interested in law.  The use of ?name is important.  Putting a question mark before a name says that if we find in our database a student with a name and with a law major, we put the name of that student into ?name and then use the same name in further pattern matching in the if-part and then-part of the rule.  If we use the same variable in different parts of the if-part, the same values must be used.  For example (this from student.clp) if we have a rule with

(defrule suggest-math-rule
    (interest ?sno math)
    (ability ?sno math)
    (student (sno ?sno) (sname ?sname))
=>
    (assert (suggest ?sno take-math))
    (printout t "We suggest that " ?sname "take some more math")
)

the ?sno matched in the first rule must be the same in the rest of the rule.

Note the printout statement.  This is as far as we need take that statement for this course.

Exercise:  Try playing with example2.clp.  Important Note:  before loading the new file into CLIPS, it is necessary to clear CLIPS memory.  We do this with the (clear) command (or use the menus).

One final bit, and we will have all we need for CLIPS for this course.  There are, of course, many more details which can be found in the powerpoint slides and in various books on expert systems (Giatanno is one excellent source).  In the above, we added a new fact to our database to express the fact that our student was interested in law.  But why add a rule when there is already a slot in the template for student to store the student's interest?  We would like to add this fact to our existing student record.  To do this we need to modify an existing fact.  The following rule does this:


(defrule rule-2 "a second rule"
    ?f1 <- (student (sname ?name) (major law))
=>
    (modify ?f1 (interest law))
)

The ?f1 does two things (although any variable name will do, that is ?anyname, CLIPS programmers generally use ?f1, ?f2, and so forth).  In the first occurrence of ?f1,  ?f1 <- (student (sname ?name) (major law)), we bind the fact number of the fact into the variable ?f1.  We can then use that stored value to modify the rule by changing the value of the existing (slot interest) with the value (interest law).  There must be a slot with this name, otherwise nothing works.

Exercise:  Try this out.  This is also in example2.clp.  When you run rule-2, note that the original fact about Dee has been replaced by a new fact.  Modify works by throwing the old fact away and asserting a new fact (behind the scenes, of course).

That's it!  You have enough for the current CLIPS exercise.  Any questions, please ask!