in — !in (predicates)

In any expression, the in keyword tests whether a value belongs to a collection and or the !in keyword tests whether a value does not belong to a collection.

Purpose

A predicate to test whether a value belongs or does not belong to a collection.

Context

Any expression

Syntax

(1) [?var:] className (?var1 in|!in ?var2);
(2) [?var:] className (?var1 in|!in {? var1, var2, ..., varn});

Description

Use the in keyword as a binary predicate in a condition to test whether an operand is part of another operand. The !in keyword does the negation of this test.

The operand can be expressed in two ways:

  • In syntax (1), the operand type must be an array or implement the java.util.Collection package.

  • In syntax (2), the values that compose the enumeration must be homogeneous. The enumeration contains either primitive types only or objects only.

The tests are done with equals, as in Java™ collections.

Example

rule InTestEnum
{
   when {
      ?i: Integer(intValue() in {1, 2, 3});
   }
   then {
      out.println("InTestEnum rule");
   }
}
 
 
rule InTestCollection
{
   when {
      ?i: Integer(intValue() in getValues());
   }
   then {
      out.println("InTest rule");
   }
}

with:

function int[] getValues()
{
   int values = new int[2];
   values[0] = 1;
   values[1] = 2;
   return values;
}