then

The then keyword declares the action part of a rule.

Purpose

This keyword is used to specify the action part of a rule, also known as the right-hand side.

Context

Rule actions

Syntax

then {
  [insert|if|modify|update|retract|timeout|while]
  action1 ...
  [insert|if|modify|update|retract|timeout|while]
  actionn 
} 

Description

It can contain one or more action statements to be done when the rule is executed, or it can be empty. The variables defined in the condition part of a rule can be used in the action part of the rule, except the variables defined within not and exists statements. The timeout action must be associated with a wait condition.

Example

In this example, the ConnectivityUpdate rule tests whether a new node exists in the network and adds a link to such 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 by calling the method addLink(?n). The last action uses the update command to update the agenda concerning the new node.

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);
   }
};