next up previous

5.4.7 Forall Conditional Element

The forall conditional element provides a mechanism for determining if a group of specified CEs is satisfied for every occurence of another specified CE.

Syntax

<forall-CE> ::= (forall <conditional-element>
                        <conditional-element>+)

The forall CE is implemented by replacing the forall keyword with combinations of not and and CEs. For example, the following rule

(defrule example
   (forall (a ?x) (b ?x) (c ?x))
   =>)

is equivalent to the rule below

(defrule example
   (not (and (a ?x)
             (not (and (b ?x) (c ?x)))))
   =>)

Because of the way the forall CE is implemented using not CEs, the restrictions which apply to CE found within not CEs (such as binding a pattern CE to a factaddress) also apply to the CEs found within an forall CE.

Example

The following rule determines if every student has passed in reading, writing, and arithmetic by using the forall CE.

CLIPS> (clear)
CLIPS>
(defrule all-students-passed
   (forall (student ?name)
           (reading ?name)
           (writing ?name)
           (arithmetic ?name))
   =>
   (printout t "All students passed." crlf))
CLIPS>

The following commands illustrate how the forall CE works in the allstudentspassed rule. Note that initially the allstudentspassed rule is satisfied because there are no students.

CLIPS> (reset)
CLIPS> (agenda)
0      all-students-passed: f-0,
For a total of 1 activation.
CLIPS>

After the (student Bob) fact is asserted, the rule is no longer satisfied since Bob has not passed reading, writing, and arithmetic.

CLIPS> (assert (student Bob))
<Fact-1>
CLIPS> (agenda)
CLIPS>

The rule is still not satisfied after Bob has passed reading and writing, since he still has not passed arithmetic.

CLIPS> (assert (reading Bob) (writing Bob))
<Fact-3>
CLIPS> (agenda)
CLIPS>

Once Bob has passed arithmetic, the allstudentspassed rule is reactivated.

CLIPS> (assert (arithmetic Bob))
<Fact-4>
CLIPS> (agenda)
0      all-students-passed: f-0,
For a total of 1 activation.
CLIPS>

If a new student is asserted, then the rule is taken off the agenda, since John has not passed reading, writing, and arithmetic.

CLIPS> (assert (student John))
<Fact-5>
CLIPS> (agenda)
CLIPS>

Removing both student facts reactivates the rule again.

CLIPS> (retract 1 5)
CLIPS> (agenda)
0      all-students-passed: f-0,
For a total of 1 activation.
CLIPS>


next up previous