| Reference > Rule Languages > ILOG Rule Language > IRL Keywords > if |
if |
PREVIOUS NEXT |
A statement for executing one of two statement blocks. This keyword is used in rule actions or functions.
if (test) {statement}
[else {statement}]
The if statement is used in the action part of a rule and in a function. The test may be any legal test as in the programming language Java. If the test returns true, the first statement block is executed and the else block is skipped over. If the test returns false, the first block is skipped over and the statement block following the else is executed. Any ILOG Rule Language statement may be executed within the statement block as well as arithmetic expressions and method calls. A statement block may consist of one or more statements. A single statement does not require the braces ({}). The else keyword and its statement block is optional.
rule PromotionLevel {
priority = high;
when {
?s:ShoppingCart(?a:amount;?id:clientNumber);
?c:Client(clientNumber == ?id; ?t:totalPurchaseToDate);
}
then {
if (?a > 100.0 && ?t > 1000.0)
insert Promotion() {level = 1;}
else { if ( ?a > 100.0 )
insert Promotion() {level = 2;}
else if (?t > 1000.0)
System.out.println("Dear "+ ?c.name +
", we can offer you a 10% discount on a shopping cart valued at over $100 today.");
}
}
};
The rule PromotionLevel provides a client with a promotion depending on the value of the shopping cart and past purchases. The rule has a priority value of high. The condition ShoppingCart returns an amount in variable ?a and a client number in variable ?c. The second condition, Client, returns a total amount spent in variable ?t corresponding to the client number equal to ?c. In the action part of the rule, if statements are used to identify which promotion, if any, corresponds to the client. Three cases are treated:
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |