To
start CLIPS, double-click on the CLIPSWin.exe file. 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 like any other windows application. Note that CLIPS
commands are always encased in brackets thus: (assert (foo)).
Here is a list of some important commands:
(exit) | Shuts down CLIPS |
(clear) | Removes all rules and facts from memory. Equivalent to shutting down and restarting CLIPS. |
(reset) | Removes facts information from memory (but not rules) and resets the agenda. |
(run) | Starts executing a CLIPS program. |
The above commands can also be executed from the CLIPS menu bar.
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:
CLIPS>(assert (colour green))
<Fact-0>
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:
CLIPS>(facts)
f-0 (colour green)
For a total of 1
fact.
Facts may also be retracted (removed) from the fact database by using the retract command. As an example, assert two facts as shown:
CLIPS>(assert (colour
green))
<Fact-0>
CLIPS>(assert (colour
red))
<Fact-1>
Then retract the first fact and display the fact list:
CLIPS>(retract 0)
CLIPS>(facts)
f-1 (colour
red)
For a total of 1 fact.
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:
(defrule duck (animal-is duck) => (assert (sound-is quack)))
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
(defrule is-it-a-duck (animal-has webbed-feet) (animal-has feathers) => (assert (animal-is duck)))
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:
(defrule duck (animal-is duck) => (assert (sound-is quack)) (printout t "it’s a duck" crlf))
Next time you run the rules, you'll get a message on screen as well as the asserted quack fact.
It’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.
Here’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’.