Zilonis Tutorial 2
Facts and rules
At its most basic, Zilonis 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 known as the Working Memory using the deffact command. Here’s an example, complete with the response from Zilonis:
$(deffact (colour green))
<Fact-0>
The <Fact-0> part is the response from Zilonis to say that a new fact (fact number 0) has been placed on the Working Memory. The (facts) command will list all current facts. Try it, and you’ll get the following:
$ (facts)
f-0 (colour green)
For a total of 1 fact.
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. In Zilonis, a typical rule looks like this:
(defrule duck (animalIs duck) => (assert (soundIs quack)))
The rule consists of three parts. The first part, (defrule
duck, simply gives the rule a unique name. The second part, (animalIs
duck), is the pattern (the IF part) of the rule and the last part, (assert
(soundIs quack)), is the action (the THEN part). In plain language,
this rule means ‘if there is a fact (animalIs duck)
on the fact database, then assert another fact, (soundIs
quack), onto the Working Memory’. Try it. Type in the rule
exactly as printed above. Clicking in the Lobby Hyperlink at
the left-top will take you back to the main screen, or the lobby. In
there you can click on the Rules Analysis icon.
The Rules Analysis graphs. You can see all the Rules that have been
defined and their internal representation (more on that later). It
should look something similar to:
Go back to the Zilonis shell.
At this point, there are no facts present.
Now, type:
(deffact (animalIs 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, (soundIs 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 isItADuck (animalHas webbedFeet) (animalHas feathers) => (assert (animalIs duck)))
Then type (retractAll) 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 (animalHas webbedFeet) and (animalHas feathers) there will be two facts present. (run) the rules, and suddenly there are four. Firstly, rule isItADuck has fired, asserting the fact (animalIs 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 (animalIs duck) => (assert (soundIs quack)) (printout t "it’s a duck"))
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 Zilonis. Fortunately, you can load
them from a file using the (load "filename") command on the Zilonis Shell.
For historical reasons, as a convention we use the extension .clp
However, you can use any extension of your preference.
A More Interesting Example
Here’s a more complex example of rules and facts. We could define a set of rules
to represent a decision tree for the diagnosis of a car’s
failure to start. With facts we can represent pieces of evidence, such as
(lights-working no) or (petrol yes).
Each path to a remedy can be represented as a rule, for example ‘IF
starter is turning AND there is no petrol THEN buy some petrol’.
Continue with Matching
