in

The 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 since 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 in statement to retrieve the items bought by the customer even if they are not inserted in the working memory. The variable ?d returns the items bought. The method boughtItems() 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 in statement within a collect statement to retrieve all the items bought by the customer with a price higher than 10. The where part uses the method size() 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 where part now tests that all the items bought had a price higher than 10, by testing the equality of the results returned by the size() and length() methods.

?c: Customer();
collect Item(price > 10) in ?c.boughtItems() where (size() == ?c.boughtItems().length() );