for
for keyword introduces an execution
loop.
Purpose
A statement for executing a statement block as many times as matching objects are found to start the action.
Context
Functions or rule actions
Syntax
for (initialize; test; increment) statement;
Description
Use the for statement
in the action part of rules and in functions to execute multiple times
the statement or a block of statements enclosed in braces ({}). The initialize, test,
and increment arguments can be any valid expression,
as in the Java™ programming language.
The statement block can execute any IRL statement and any arithmetic
expressions and method calls.
Example
The ConnectivityUpdate rule
tests whether a new node exists in the network and adds a link to
it from each of its neighbors.
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);
}
};This rule is processed as follows:
The rule condition matches an object
Nodewhen the fieldstateequals the static valueNEWand it binds the variable?neighborswith the vector fieldneighbors().If such a
Nodeobject is found, the action part can be executed. The variable?iis initialized to0. Theforstatement loops for each neighbor and theaddLink(?n)method updates the links from the neighbors to the new node.The last action uses the
updatecommand to update the agenda with the new node.