in
in keyword supports relations between
object values.
Purpose
This statement is used in the condition part of a rule to express the relations between values.
Context
Rule conditions
Syntax
condition in expression;
Description
Use this statement to test whether
the value of the condition is a member of the set of values returned
by the in expression. The expression can be an array,
a java.util.Vector, java.util.Enumeration,
or a java.util.Collection instance.
Using
the in statement, you can retrieve not only objects
in the working memory but also objects that are linked to other objects
by fields or by method calls.
In combination with the collect statement,
the in statement expresses logical statements such
as all, at least, or none (see
the examples below).
The in statement provides
performance advantages because the pattern matching is done on a reduced
number of objects and not on all the objects in the working memory.
Example
The first example retrieves objects even if they are
not in the working memory. The other examples combine in and collect statements.
- Example 1
This example uses the
instatement to retrieve the items bought by the customer even if they are not inserted in the working memory. The variable?dreturns the items bought. The methodboughtItems()can return a user-defined array, a java.util.Vector value, or a java.util.Enumeration value.?c: Customer(); ?d: Item() in ?c.boughtItems();- Example 2
This example uses the
instatement within acollectstatement to retrieve all the items bought by the customer with a price higher than 10. Thewherepart uses the methodsize()to test that the collection has at least one element.?c: Customer(); collect Item(price > 10) in ?c.boughtItems() where (size() > 0);- Example 3
This example is similar to the previous example except that the
wherepart now tests that all the items bought had a price higher than 10, by testing the equality of the results returned by thesize()andlength()methods.?c: Customer(); collect Item(price > 10) in ?c.boughtItems() where (size() == ?c.boughtItems().length() );