Professional Documents
Culture Documents
Tutorial 4
Tutorial 4
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).
Example
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.
Homework
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
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.
Example
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.
Homework
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
it.
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.