Reference > Rule Languages > ILOG Rule Language > IRL Keywords > for

Summary

A statement for executing a statement block numerous times. This keyword is used in rule actions or functions.

Syntax

for (initialize; test; increment) statement;  

Description

The for statement is used in the action part of a rule and in a function. It permits you to execute multiple times the statement or a block of statements enclosed in braces ({}). The initialize, test, and increment may be any legal expression as in the Java programming language. Any ILOG Rule Language statement may be executed within the statement block as well as arithmetic expressions and method calls.

Example

rule ConnectivityUpdate{
   when {
      ?n: Node(state == NEW; ?neighbors: neighbors() );
   }
   then {
      for (int ?i = 0; ?i < ?neighbors().size(); ?i++) {
         ( (Node)?neighbors().elementAt(i) ).addLink(?n); 
      }
      update (?n);
   }
};

The ConnectivityUpdate rule tests if a new node exists in the network and adds a link to such a new node from each of its neighbors. The single rule condition matches an object Node when the field state equals the static value NEW, and it binds the variable ?neighbors with the vector field neighbors(). If such a Node object is found, the action part can be executed. The variable ?i is initialized to 0. The for statement loops for each neighbor, updating the links of the neighbors with the new node using the method addLink(?n). The last action uses the update command to update the agenda concerning the new node.

See Also

break, continue, foreach, while