| Reference > Rule Languages > ILOG Rule Language > IRL Keywords > function |
function |
PREVIOUS NEXT |
The keyword for defining a function. This keyword is used at the top level of the ruleset.
function returnType functionName (argList) {statement}
Functions may be declared in a rule file before rule declarations. The function keyword is used to declare a function that may be called in the action part of a rule. The function declaration consists of a header part and the statement block. The header part contains the return type of the function, the function name, and a list of arguments. The argument list consists of pairs: the argument type and name, separated by commas. The statement block may contain any ILOG Rule Language right-hand side statement as well as arithmetic expressions and method calls. A statement block may consist of one or more statements. The keyword return is used to declare the value returned by the function, of the type returnType.
function String buildMessage(Client ?c, ShoppingCart ?s)
{
int ?a = ?s.getAmount();
int ?t = ?c.getTotalAmount();
if (?a > 100.0 && ?t > 1000.0)
return("Dear "+ ?c.getName() + ", you are a GOLD
customer and will receive a 10% discount today!!!");
else if ( ?a > 100.0 )
return("Dear "+ ?c.getName() + ", you are a SILVER
customer and will receive a 5% discount today!!!");
else if (?t > 1000.0)
return("Dear "+ ?c.getName() + ", we can offer you a 10%
discount on a shopping cart valued at over $100 today.");
else
return("Dear "+ ?c.getName() + ", we can offer you a 5%
discount on a shopping cart valued at over $100 today.");
}
rule Promotion
{
when {
?s:ShoppingCart(?a:amount;?id:clientNumber);
?c:Client(clientNumber == ?id; ?t:totalPurchaseToDate);
}
then {
System.out.println(buildMessage(?c,?s));
}
};
The function buildMessage takes as arguments a Client and a ShoppingCart and returns a message with the possible discount. The variable ?a is the current shopping cart amount and the variable ?t is the total purchased to date for the client. Three if statements are used to identify which discount corresponds to the client. Four cases are treated:
The rule Promotion provides a client with a promotion depending on the value of the shopping cart and past purchases. 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, the function buildMessage is called and returns the appropriate message, which is displayed.
evaluate, for, if, modify, retract, return, throw, try, update, while
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |