next up previous

5.4.6 Exists Conditional Element

The exists conditional element provides a mechanism for determining if a group of specified CEs is satisfied by a least one set of pattern entities.

Syntax

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

The exists CE is implemented by replacing the exists keyword with two nested not CEs. For example, the following rule

(defrule example
   (exists (a ?x) (b ?x))
   =>)

is equivalent to the rule below

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

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

Example

Given the following constructs,

CLIPS> (clear)
CLIPS>
(deftemplate hero
   (multislot name)
   (slot status (default unoccupied)))
CLIPS>
(deffacts goal-and-heroes
   (goal save-the-day)
   (hero (name Death Defying Man))
   (hero (name Stupendous Man))
   (hero (name Incredible Man)))
CLIPS>
(defrule save-the-day
   (goal save-the-day)
   (exists (hero (status unoccupied)))
   =>
   (printout t "The day is saved." crlf))
CLIPS>

the following commands illustrate that even though there are three facts which can match the second CE in the savetheday rule, there is only one partial match generated.

CLIPS> (reset)
CLIPS> (agenda)
0      save-the-day: f-1,
For a total of 1 activation.
CLIPS> (facts)
f-0     (initial-fact)
f-1     (goal save-the-day)
f-2     (hero (name Death Defying Man) (status unoccupied))
f-3     (hero (name Stupendous Man) (status unoccupied))
f-4     (hero (name Incredible Man) (status unoccupied))
For a total of 5 facts.
CLIPS> (matches save-the-day)
Matches for Pattern 1
f-1
Matches for Pattern 2
f-0
Matches for Pattern 3
f-2
f-3
f-4
Partial matches for CEs 1 - 2
f-1,
Activations
f-1,
CLIPS>


next up previous