Writing conditions in IRL

You can write simple, complex, existence, collection, nonexistence, and evaluate conditions. To write conditions, use the ILOG® Rule Language syntax.

About this task

You can define several types of conditions.

Table 1. Conditions you can write in IRL
Condition name Condition role
Simple condition Checks for every instance of a class and runs the rule for every instance.
Existence condition Checks that at least one instance of a class is available. If it is the case, the existence condition runs the rule once.
Collection condition Gathers all instances of a class and runs the rule once per collection found.
Nonexistence condition Checks that no instance of a class is available. If it is the case, the nonexistence condition runs the rule once. This condition is the negation of simple conditions, existence conditions, and collection conditions.
Evaluate condition Runs global tests on one or several bound variables.
Note: You cannot put an evaluate condition at the beginning of the condition part of a rule. You must add one or more conditions that are not evaluate conditions before the evaluate condition.

Procedure

  • To write a simple condition, use the following pattern: variable: ClassName(<tests>);

    In this example, the condition matches for all the accounts in the working memory and the rule runs all instances that match this condition. You can reuse the Account variable in other conditions or actions.

    when {
       account : Account(); 
    }
  • To write an existence condition, use the following pattern: exists ClassName(<tests>);

    In this example, the condition checks that at least one account exists inside the working memory. If it is the case, the rule runs once.

    when {
       exists Account(); 
    }
    then {
       ...
    }
  • To write a collection condition, use the following pattern: collection: collect ClassName(<tests>);

    In this example, the condition gathers all the accounts in a collection and the rule runs once for all the accounts. You can reuse the accounts variable as a collection in other conditions or actions.

    when {
       accounts : collect Account();
    }
    then {
        out.println(accounts.size());
        ...
    }
  • To write a nonexistence condition, use the following pattern: not ClassName(<tests>);

    In this example, the condition checks that no report exists in the working memory. If it is the case, the rule runs once.

    when {
       not Report(); 
    }
    then {
       ...
    }
  • To write an evaluate condition, use the following pattern: evaluate (<test> [;<test>]*);

    In this example, the condition gathers all the sales and purchases of a stock. If the result of a statistics computation of these two groups is less than two percent of the stock value, the condition runs the rule.

    when {
       stock : Stock();
       purchases : collect Purchase(getStock()==stock);
       sales : collect Sales(getStock()==stock);
       evaluate ( Statistics.variance(purchases, sales) < 0.02 *
                   stock.getValue());
       }