exists
exists keyword tests whether a class
condition is true.
Purpose
This keyword is used in the condition part of a rule to test whether the condition is true.
Context
Rule conditions
Syntax
exists condition;
Description
Use the exists statement
in the condition part of a rule, on a simple condition, an in statement,
or a from statement. The exists statement
tests whether the condition is true. The contrary of exists is not (avoid not
exists).
The exists statement
returns true when any object in the working memory
matches the condition.
It returns
truefor a simple condition if the working memory contains at least one object that can match the condition.It also returns true with an
inorfromstatement when theinorfromstatement returnstrue.
Because the condition does not discriminate which object was
matched, you cannot bind an exists statement to an
external variable. Any variable bound within the exists statement
is local to the statement. No statement of the rule can contain any
reference to the bound variable.
You can use the special variable ?this within
the exists statement to designate the current working
memory object being tested.
Example
The GeneralCustomerRequest rule
uses the exists statement to verify that an item
requested by a customer exists. The exists statement
returns true when a single instance of the object
is found, which is faster than providing all the items that match
the customer’s request.
rule GeneralCustomerRequest {
when {
CustomerRequest(?item:request);
exists Item(?this == ?item);
}
then {
System.out.println(?item + " are in stock.");
}
};In the CustomerRequest condition,
the variable ?item is matched with the field request.
In the exists statement, the Item object
is represented by the variable ?this. The statement
tests whether the Item object is equal to the variable ?item.
If an Item object is matched, the println command
in the action part prints that the items are in stock.