You are on page 1of 4


Stuctured CLIPS Facts

1. Types of CLIPS facts
Two types of facts can be used in CLIPS. The facts used in the previous tutorials were
ordered facts, or un-structured facts. For this type, the order of the fields within a fact can be
decided by programmer, but once this order is established, it must be respected throughout the
program. This means that for an ordered fact both the content of each field and the order of fields
matter. As an example, after the CLIPS command:
(assert (a b c) (a c b))
two distinct facts will be present in the WM. That is why these facts are named ordered facts: for
them, the order of fields is significant and, mainly, to get a certain field the programmer should
know its position within the ordered fact (the use of multifield variables can help when the
position of a field in an ordered fact is not known).
The second type of facts are regarded as un-ordered, structured or deftemplate facts.
Read about deftemplate facts from Giarratano & Riley, pp. 334 336 (7.6 Facts; The
Deftemplate Construct; Multifield Slots); 337 (Fig. 7.1); 340 341 (7.8 Modifying and
Duplicating Facts).
2. Using structured facts
The structured facts can be introduced into the WM in the same way as the un-structured
facts, namely by the means of assert and deffacts constructs (recall the difference between these
two constructs). As a major difference, a structured fact can be used in a command only after
being defined by a deftemplate construct definition. This command, as being a definition, can be
included into a file to be loaded in CLIPS. When introducing a deftemplate fact in the WM
through an assert or deffacts command all or part of its slots can be specified; for the slots that
are not present in the respective command, their default value is used (see below).
One can use the following sequence of commands (see how the requested parentheses are
used). As already told, the deftemplate and deffacts commands can be included into files.
(deftemplate microprocessor this is a structured fact
(multislot name)
(slot bus)
(slot speed)
(slot technology) )
(deffacts initial (microprocessor (name A) (technology CMOS) (bus 8) (speed high))
(microprocessor (bus 16) (speed high) (name B) (technology NMOS)) )
(assert (microprocessor (bus 8) (name 8080) (speed low) (technology CMOS))
(microprocessor (name 80286) (technology CMOS) (speed high) (bus 16) ) )

Notice that the order of slots in a structured fact has no importance. In a structured fact,
either of the slot or field keywords can be used.
Deftemplate facts can be used together with rules, in the same way as for the un-structured
facts. They determine the rules activations, according to the matching mechanism. The
difference is the one already mentioned: the order of slots in a deftemplate fact has no
importance, neither when the fact is introduced into the WM, nor when it determines the rules
matching (this is contrasting with the un-structured facts, for which the order of fields is critical
during the matching process).
Using the above deftemplate definition, the following rule is activated by a
microprocessor fact, if this complies with the condition to have the value of the slot bus a
number greater than 8.
(defrule R1
(microprocessor (name $?n)
(bus ?x&:(and (numberp ?x)(> ?x 8)))
(printout t ?n complies with the request crlf) )
It is to mention that in the LHS of a rule when a condition regards a deftemplate fact this
can refer all or only part of the slots contained by the deftemplate definition, as needed; for sure,
the slots can be used in any order. The only condition is that first the deftemplate definition has
to be provided, and only then that deftemplate can be referred in a defrule definition.
The two types of facts can be used together in a rule, and the variables can be implied too,
as in the following example.
(defrule R2
(phase choice)
?a <- (microprocessor (technology CMOS)
(speed ?v)
(bus ?p1&:(<= ?p1 4) ) )
?b <- (microprocessor (technology CMOS)
(bus ?p2&:(> ?p2 4) ) )
(if (numberp ?v) then (modify ?a (speed (- ?v 1) ) ) )
(duplicate ?b (technology NMOS) (bus (+ ?p2 2) ) ) )
It is to remark in the above rule the way a variable can be used in a duplicate or modify
command, as being bound in the LHS of rule to a fact address. To simplify the rule, a procedural
if command is used; such a command can only be used in the RHS of a rule.
Find out some facts that determine the above rule activation. Explain the rule behaviour
in the two states of the flag for fact duplication (this is accessible from the options of the
Execution menu; according to the state of the flag, the WM can contain or not facts with the
same content). Discover the weak point of the above rule and propose a solution.

2. Deftemplate attributes
Read about deftemplate attributes from Giarratano & Riley, pp. 437 445 (10.2
Deftemplate Attributes; The Type Attribute; Static and Dynamic Constraint Checking; The
Allowed Value Attributes; The Range Attribute; The Cardinality Attribute; The Default
Attribute; The Default-Dynamic Attribute; Conflicting Slot Attributes).
3. Other commands for structured facts
Some commands exist in CLIPS for handling structured facts. As deftemplate is a
construct definition, these commands are similar to those provided for managing the other
construct definitions. Such commands are: list-deftemplates, ppdeftemplate, with the same
significance as for list-deffacts and ppdeffacts, used for a deffacts command, namely to
show all the deftemplate definitions already existing, and to show the content of a certain
deftemplate definition, respectively. The same effects can be obtained by using the Browse
menu, option Deftemplate Manager.
A deftemplate construct can be removed either by using the command:
(undeftemplate <relation-name>)
or directly from the Deftemplate Manager options. If a deftemplate construction is introduced
with the same argument as relation-name then the new definition takes the place of the
previous one; in fact, this is the CLIPS behaviour for any type of construct definition: when a
new definition is considered, this replaces the older one.
There is a further constraint on removing or re-defining a deftemplate construction, for
example when this is done by the means of the undeftemplate command. This is accepted only
if no fact complying with the deftemplate definition is present into the WM and no rule that
refers (either in its RHS or LHS) the structured fact is present into the RB.
Consider first the following definition and introduce it in CLIPS:
(deftemplate F (field a) )
Then introduce a fact into the WM according to the command:
(assert (F) )
From now on, the removal of deftemplate definition F or its re-definition is possible only after
the fact F that is present into the WM is retracted. Furthermore, if after the deftemplate F was
defined, the following two rules have been introduced:
(defrule R1 (F) =>)
(defrule R2 => (assert (F) ) )
then the removal of the deftemplate F construct becomes not be possible. The deftemplate F can
be removed, only if previously the two definitions of rules are removed.

1. Determine which are the default values of the slots for the following deftemplate definition
and explain the results obtained:
(deftemplate Aa (field b1) (field b2 (allowed-symbols as bas) )
(field b3 (allowed-values 1 3 2) ) (field b4 (type INTEGER) )
(field b5 (type STRING) (allowed-strings lk 1 2 3) ) )
2. Consider the following rule:
(defrule R (student (residence $? Iasi $?) (name $?n) )
(not (student (name $?n) (residence $? Vaslui $?) ) ) => )
Specify the needed deftemplate construct definition that must be introduced before the rule and
give some examples of facts (at least two) that activate the rule and respectively do not activate
3. Write a CLIPS program with structured facts that should contain information on the students
of a group and determine:
- the place where the greatest number of students have their residence;
- the name of the student that has the greatest average grade (the facts should contain
information on the grades obtained at different disciplines of a semester, the average grade being
accordingly calculated only for those students having at the most one absent);
- the number of students that passed all the exams;
- the number of students that did not pass exactly one exam (either they were absent or
their grade was below five);
- the number of students that did not pass exactly two exams;
- the number of students that did not pass more than two exams;
- the discipline with the greatest number of students that did not pass the exam.