Академический Документы
Профессиональный Документы
Культура Документы
zero or two fields, but to a single field and this creates a matching constraint. For example, the
above rule R1 is not activated by the fact (student John Smith) because the simple variable ?name
cannot be bound to two fields. However, the fact (student 234.45) activates the rule R1, since the
variable ?name can be bound to a field without any restriction about its type (the way constraints on
the type of fields can be included into a rule will be shown latter).
2. A local variable is bound only during the operation of the rule where it appears in; with respect to
this, no connection is created between two rules that use the same variable. Let us consider the
following example:
(defrule R2 (fact ?x) => (assert (a ?x) ) )
(defrule R3 (fact ?x 12) => (assert (b ?x) ) )
Though the two rules use the same variable ?x, no connection is created between rules, since a
variable is bound and stores a value only during the rule activation and execution; after the rule
firing, the local variable does not exist any longer.
3. A variable can be used more times in the RHS of a rule and it can be used in different actions, as
in the following rule:
(defrule R4 (normal-state computer ?computer-name) =>
(assert (can be used ?computer-name) )
(printout t The following computer is available: ?computer-name crlf ) )
4. The same variable can be repeatedly used in the LHS of a rule to create certain constraints in the
matching process (since a variable can be bound to a single value), as in the following example:
(defrule R5 a professor that has published a book is determined
(professor ?name teaches discipline ?dis)
(book on ?dis appeared in year ?y and was written by ?name)
=>
(printout t Professor ?name published a book for the discipline
?dis in year ?y crlf) )
The rule R5 is activated if the WM contains two facts satisfying the following conditions:
the first fact must have 5 fields;
the second fact must have 12 fields;
the first fact must have in the first position the symbol constant professor, and in the third and
fourth positions the symbols teaches and discipline;
the second fact must have in the first two positions the fields book, and on, in positions 4 to 6
the symbols appeared, in, and year, and in positions 8 to 11 the symbols and, was,
written, and by;
the second field of the first fact must be the same as the last field in the second fact;
the last field in the first fact must be the same as the third field in the second fact.
Thus, it results how complex matching conditions can be created by a repeated use of variables in
the LHS of rules.
5. All the variables that were used in previous rules are local, simple and specified variables. The
other types of variables will be introduced in the next sections.
Problems to be solved
1. Include the previous rule R5 in a file, together with the facts needed to obtain at least two
activations of the rule. Test the rule and notice the results it produces.
2. Consider the rule
(defrule R6
(grandfather-of ?name ?name)
=>
(assert (has-nephew ?name named ?name) ) )
Supposing that the WM contains the following facts: (grandfather-of John Dan), (grandfather-of
Dan Dana), (grandfather D D), (grandfather-of 12 12), (grandfather-of Dan Dan)
how many times is the rule R6 placed in agenda? (explain why).
3. Considering that the following rule was loaded:
(defrule R7
(father-of ?child is ?father)
(father-of ?father is ?grandfather)
=>
(printout t Grandfather of ?child is ? grandfather crlf) )
and the following group of facts is loaded from the following command deffacts:
(deffacts kinship
(father-of
(father-of
(father-of
(father-of
(father-of
(father-of
(father-of
John is Michael)
Dan is Michael)
Michael is Andrew)
Jh is 10)
Mary is 10)
10 is 20)
John is John) )
specify how many times is the rule R7 placed in agenda (explain why).
4. Write a rule labeled R8 and specify a group of facts that determines the following agenda:
0
0
R8
R8
f - 1, f - 2
f - 1, f - 1
0
0
R8
R8
f - 2, f - 1
f - 2, f 2
Remarks
where fact-address can be a positive integer, representing the index of the identifier for the fact to be
deleted, or a variable that is bound to the address of the fact to be deleted.
The variables that are bound to a fact address cannot be used to obtain the adding of the
WM with a fact having a content similar to that of the fact whose address is kept in the variable.
Anyhow, the content of a variable being bound to an address can be used in the RHS of the rule, as it
happens in the following two rules:
(defrule R12
?a <- (a ?x)
?b <- (b ?y ?x)
=>
(retract ?b) (assert (fact ?a)) )
(defrule R13
?ad <- (a ?x)
(fact ?ad)
=>
(printout t ?x " " ?ad crlf))
Problem to be solved
Load the rules R12 and R13 and test their operation.
The rule R14 determines all the facts on students that have as the last field (the sixth field) the
symbol Iasi. At execution, the rule displays the values that appear in the positions of fields <name>
and <faculty>. It results that the values of the fields <year-of-study> and <group> doesnt matter,
and that is why for these positions the simple unspecified variable is used. To conclude, the simple
unspecified variable is to be used when a value (a field) within the fact to activate the rule doesnt
matter and that value is not needed in the RHS of the rule. About the simple unspecified variables,
the following remarks are important, too.
In a CE of a rule an unspecified variable can be used more times; it can be also used in
more CEs of the same rule, and these cases do not determine constraints for the matching process, as
it happens when a specified variable is repeatedly used. For the example, the variable ? appears in
rule R14 for two times, without determining a constraint for the values of the forth and fifth fields of
the fact that activates the rule.
Even if the value of a field from a CE in a rule does not matter from the point of view of
the matching process, if that field is needed in the RHS of the rule, the unspecified variable cannot
be used, but a specified variable is needed. This was the case for the fields <name> and <faculty> in
the rule R14.
Problems to be solved
1. Explain which is the difference in operation for the following two rules:
(defrule R15 (person ?name has children ?x) => )
(defrule R16 (person ?name has children ? x) => )
2. Continuing the example with the rule R14, determine the rule which displays all students from the
faculty AC, and the fifth year of study. Test the developed rule operation, by providing an
appropriate group of facts.
Include the rule R17 in a file together with the following group of facts:
(deffacts Lists
(list L1 3)
(list L2)
(list L3 1 3 5)
(list L4 1 2 3)
(list L5 1 3 3 5)
(list L6 3 4 2)
(list L7 3 4)
(list L8 1 3 4 2)
(list L9 1 4 3 2) )
Load the file and run the program; explain the operation.
Multiple specified variables can be also used; they appear under the form: $?<variable-name>,
where variable-name must be a symbol, used in the same conditions as for the simple specified
variables. Multiple variables are bound and influence the matching process in the same way as
simple variables, but they can be assigned to 0, 1, 2, ... , n fields (constants). When the value of a
multiple variable is displayed, it appears between parentheses.
As an example, the previous rule can be modified to display not only the names of lists, but also
their elements, as shown below:
(defrule R18
(list ?name $?elements)
=>
(printout t The list with the name ?name contains the elements $?elements crlf) )
The simple and multiple variables, specified and unspecified can be used together, which allows
complex matching conditions to be obtained. About the decision on using a multiple specified or
unspecified variable, this is to be taken according to the criteria discussed for the simple variables.
Problems to be solved
1. Using the same pattern for the facts representing lists as it was used in the case of rules R17 and
R18, write the rule that finds all the lists containing the number 3 as element, independent of the
position of number 3 in the list, and displays the elements being placed in that list before and after 3,
respectively. Test the rule operation with the same previously used group of facts Lists and explain
the number of rule activations.
Remark
It is to notice how the fact (list L5 1 3 3 5) determines two activations of the rule. This is an
example of how a single fact can determine more activations of the same rule; this is possible
because each rule activation regards a distinct matching and binding of the variables used by the
rule.
2. Regarding the same examples of handling lists, write the rule that determines all the lists
containing the number 3 in the penultimate position, and that contain at least another element before
the number 3.
General remarks on local variables
1. All the variables used in the previous examples were local (simple multiple, specified
unspecified). These possess a certain significance only inside a rule, significance that is lost outside
the rule. For example, if one gives as command in CLIPS ?x, an error is displayed since a local
variable can be used and bound only inside a rule. When a rule is activated a local variable used in
its LHS gets a value, through the matching process, and this value is kept until the end of rule
execution.
2. Though the use of multiple variables can be essential for guiding the matching process, their
excessive use conducts to inefficiency, as they determine greater memory consumption and an
increase of the running time, in comparison with simple variables. The explanation is easy to
understand; for example, if a rule contains the CE (data $?x), then the matching process will have
to try to match the CE against facts with 1, 2, ..., n fields, and the multiple variable $?x would store
0, 1, 2, ..., n fields, depending on the facts existing in the WM. As a general idea, a multiple variable
(be it specified or not, as it is the case) has to be used only when the number of fields of the fact
supposed to satisfy the matching condition is not a priori known. When this number is known, it is
to prefer the use of simple variables, even if they have to be used for more times, since a more
efficient solution is got in this way.
3. A multiple specified variable, be it $?x, used in the LHS of a rule, can be written in the RHS
without the sign $, namely ?x. The sign $ is important only in the LHS, for the matching process,
and in the RHS, when the variable is already bound, the sign $ is not need any longer. Such an
example is:
(defrule R19 (fact
(defrule R20
(a ?x ?y ?z ?t)
=>
(bind ?y as) (bind ?z (* (+ 1 3) 2)) (bind ?t John John)
(printout t ?x ?y ?z ?t crlf) )
To bind a variable to a multifield value, a function that outputs such a result can be used. Several
CLIPS functions exist for handling multifield values; one of them is:
(mv-append <element>*)
The result of this function is a multifield value that is obtained with the elements that are the
function arguments. These elements can be constants, simple or multiple variables that are already
bound, or functions that produce as result a value with zero or more fields. To see the effect of this
command, execute in the CLIPS environment:
(mv-append 1 (+ 12 13) asa (mv-append 2 an))
Remark
The command bind can be used to assign a value to a variable that was already used in the LHS of a
rule, but also to assign a value to a new variable, one that is introduced only in the RHS of the rule,
if this is necessary.
Problem to be solved
Test the following rule and explain its operation (the conditions for the rule activation have to be
created, by using the suggested assert command, or by using another fact correspondingly
introduced in the WM).
(defrule R21
(fact ?x $?y ?z) =>
(bind $?y (mv-append The result is))
(bind ?a 12)
(printout t the new variable is a= ?a crlf ?y (+ ?x ?z ?a) crlf) )
(assert (fact 2 as 34 6) )
variables; this can be done by using the option Globals from the menu Window. Moreover, global
variables can be watched by using the command:
(watch globals)
After the above command, the user is informed by proper messages whenever the values of global
variables are changed. This option can be activated from the menu Execution, too.
Problem to be solved
Write and load some global variable definitions, watch the variables and change their values.
6. The command reset does not delete the global variables. The way this command operates on
global variables depends on the state of a flag, whose state can be changed by the means of the
following command:
(set-reset-globals {TRUE, FALSE})
The result of the above command is the old (previous) value of the flag. The default value of the flag
is TRUE. For this value of the flag the command reset will bring the global variables to their
original values, the ones from their construct defglobal. If the value of the flag is FALSE, then the
command reset does not modify the values of the global variables. The state of the flag that
influences the global variables can be found by using the command (get-reset-globals) or from the
menu Execution, the option Options, where one can see the state of the flag Reset Global
Variables. From the respective option, the state of the flag can be directly changed, without the
need of the command (get-reset-globals). It is to notice that in CLIPS there are more flags that
allow the user to tune the CLIPS environment operation; for all these flags, commands of the type
set and get are available, and these flags can be watched and changed from the above mentioned
menu.
Problem to be solved
Notice the effect of the command reset on global variables in the two states of the flag that
establishes the influence of this command.
7. The global variables can be deleted by using the command clear, the one that removes all the
information from the CLIPS environment. Furthermore, a certain global variable can be deleted by
using the command undefglobal followed by the name of the variable (without the signs ? and *).
For example, the previously defined variable ?*A* can be removed by the command:
(undefglobal A)
The content of the global variable definitions can be seen from the menu Browse, option Defglobal
Manager, from which the removing of a global variable can be also obtained.
Problems to be solved
1. Answer the following questions:
Which are the facts that can be deleted according to the commands included in the RHS of
a rule?
Specify if the following rule is correct or not. If not, indicate which are the errors.
(defrule R26 ?a <- (a) => (retract ?a) (bind ?a 10) (printout t ?a crlf) )
Are the following rules correct? If they are correct load the facts that activate them.
(defrule R27
?a <- (fact)
?b <- (fact-adr ?a) => (retract ?b))
(defrule R28
(fact ?a)
?a <- (new-fact ?b) => (retract ?a))
When is the following rule activated? Which is the result displayed when the rule is
executed?
(defrule R29 (a ?x) (bind ?x 1) => (printout t ?x crlf) )
2. Regarding the example that was used in Section 3, write a rule that finds all students from the
Faculty of Mechanics, the fourth year of study. The rule must display the name of students and their
home address.
3. Regarding the example that was used in Section 4, write the rules that find:
Two lists that have an element in common, independent of the position of that element in
the two lists.
4. Write the definition of a global variable. Write the CLIPS program with the following behavior: if
the WM contains the fact (change), then the value of the global variable must become YES; if the
WM contains the fact (keep), then the value of the global variable must become its original value
(the value from the construction defglobal). It is supposed that the facts (change) and (keep) cannot
simultaneously exist in the WM.
5. Write a CLIPS program that should contain information on a family and is able to determine the
kinship degree of aunt/uncle.
6. A technological process is monitored by 10 sensors. Each sensor has to states: 0 and 1. Write a
CLIPS program that is able to display a warning message if 3 or more sensors are in 0. The program
must display the message only one time, even when more than 3 sensors are in 0.