CLIPS Tutorial Part 1

Working with CLIPS

CLIPS iconTo start CLIPS from the shell in Unix, type clips; in Windows or MacOS, double-click on the CLIPS icon. You'll get a window with nothing in it but the command prompt CLIPS>. For the time being, this is where you type in your commands and programs. To exit CLIPS, type (exit) or shut down the program. Note that CLIPS commands are always encased in parenthesis, as in (assert (foo)). Here is a list of some important ones:

(exit) Shuts down CLIPS
(clear) Removes all program and data from memory. Equivalent to shutting down and restarting CLIPS.
(reset) Removes dynamic information from memory and resets the agenda.
(run) Starts executing a CLIPS program.

The above commands can also be executed from the CLIPS menu bar.

Facts and rules

At its most basic, CLIPS operates by maintaining a list of facts and a set of rules which operate on them. A fact is a piece of information such as (colour green) or (parent_of John Susan). Facts are created by asserting them onto the fact database using the assert command. Hereís an example, complete with the response from CLIPS:

The <Fact-0> part is the response from CLIPS to say that a new fact (fact number 0) has been placed on the fact database. The (facts) command will list all current facts. Try it, and youíll get the following:

Facts may also be retracted (removed) from the fact database by using the retract command. As an example, assert two facts as shown:

Then retract the first fact and display the fact list:

There are two things to note here: firstly, to retract a fact you must specify a number (the fact-index), not the fact itself, and secondly, fact-indices are not reused. Once fact 0 has been retracted, the next fact asserted will have the index 2, not 0.

Facts on their own are of only limited use. The application of rules is necessary to develop a program capable of some useful function. In general, a rule is expressed in the form ëIF something is true THEN do some actioní. This kind of rule is known as a production. For this reason, rule-based expert systems are often known as production systems (CLIPS actually stands for C Language Integrated Production System). In CLIPS, a typical rule looks like this:

The rule consists of three parts. The first part, (defrule duck, simply gives the rule a unique name. The second part, (animal-is duck), is the pattern (the IF part) of the rule and the last part, (assert (sound-is quack)), is the action (the THEN part). In plain language, this rule means ëif there is a fact (animal-is duck) on the fact database, then assert another fact, (sound-is quack), onto the fact databaseí. Try it. Clear the system, then type in the rule exactly as printed above. Typing (rules) will give you a list of rules (just the one, in this case) present in the system. At this point, there are no facts present. Now, type (assert (animal-is duck)). Check the fact list - thereís one fact. To trigger your rule, type (run). Although nothing appears to happen, if you check the fact list again youíll see that there is a new fact, (sound-is quack), which has been inferred by the rule. This is the power of rule-based programming - the ability to make inferences from data, particularly as the results of one rule can be used as the pattern for another. Add the rule

Then type (reset) to clear the facts (the rules will be untouched). Note that this rule has two patterns. Both must be satisfied for the action to be taken. This translates to ëIF the animal has webbed feet AND the animal has feathers THEN the animal is a duckí (taxonomists and pedants may disagree with this rule). If you now assert the facts (animal-has webbed-feet) and (animal-has feathers) there will be two facts present. (run) the rules, and suddenly there are four. Firstly, rule is-it-a-duck has fired, asserting the fact (animal-is duck). This fact has then triggered rule duck, which has asserted the fact (sound-is quack). Very powerful systems can be built using this ability to chain rules.

Asserting facts is a rather unsatisfactory way of presenting results. Type in the first rule again, this time with the multiple actions as shown below:

Next time you run the rules, you'll get a message on screen as well as the asserted quack fact.

CLIPS Editor iconItís rather inefficient having to type all your rules in each time you run CLIPS. Fortunately, you can load them from a file using the ëLoad Constructs..í command on the file menu. CLIPS will expect a file with the extension .CLP, and thereís a handy editor to help you create them. You canít put facts in a .CLP file in the same way as you can from the command prompt, so for now youíll still enter them as before.

Decision treeHereís a more complex example of rules and facts. The decision tree opposite represents a small section of the diagnosis of a carís failure to start. Each rounded box is a recommended remedy. Each rectangular box is piece of evidence, which might be represented by a fact such as (lights-working no) or (petrol yes). Each connecting path to a remedy represents a rule, for example ëIF starter is turning AND there is no petrol THEN buy some petrolí.

Represent this diagnostic system in CLIPS.



Franz Kurfess
Last modified: Wed Jan 9 08:56:51 PST 2002