Defining a variable

When you define a variable, use the ILOG® Rule Language syntax.

Procedure

  • To define a simple class condition variable, use the following syntax in the condition part of the rule: variable : ClassName();

    It defines a variable of type ClassName. This variable is never null and its value is shown in the rule instance tuple. The scope of this variable is the whole rule.

  • To define a simple variable in a condition, use the following syntax in the test part of a condition: variable : <value>

    To use this syntax in a class condition, follow this pattern: ClassName (variable : <value>). To use this syntax in an evaluate condition, follow this pattern: evaluate (variable : <value>). It defines a variable that has the type of the value. The value of the variable can be null. The scope of this variable depends on the condition in which it is defined: If the variable is defined in an existential condition, its scope is limited to this condition. If the variable is defined in any other type of condition, its scope is the whole rule.

  • To define a simple variable in the action part of the rule, use the following syntax: int variable = <value>;

    It defines a variable. The scope of the variable follows the Java™ language scoping rules.

Example

In this example, the account and contract variables refer to objects in the working memory, objects of the Account BOM class and objects of the Contract BOM class. The balance is defined as the balance of an account in the condition part of the code. Similarly, the expiration is defined as the expiration date of the contract. The variable b is an example of a variable defined in the action part of the rule.

when {
   account: Account(balance : getBalance());
   contract: Contract(expiration : getExpirationDate());
}
then {
  int b = balance;
  System.out.println(contract + expiration);
  System.out.println(account + b);
}

This rule generates a list of all the available instances of each contract and its expiration date, and each account and its balance.