function

The function keyword defines a function to be called.

Purpose

The function keyword is used to declare a function that can be invoked in the action part of a rule.

Context

At the top level of rulesets

Syntax

function returnType functionName (argList) {statement}  

Description

You can declare functions in a rule file before rule declarations. 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 arguments are listed in pairs: the argument type and the argument name, separated by a comma. The statement block can contain any ILOG® Rule Language side statement, arithmetic expressions, and method calls. A statement block contains one or more statements. Use the keyword return to declare the value returned by the function, of the type returnType.

Example

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 a Client and a ShoppingCart as arguments 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 identify which discount corresponds to the client.

Four cases are treated:

  • The current shopping cart is valued at over $100 and past purchases have exceeded $1000.

  • The current shopping cart is valued at over $100 and past purchases have not exceeded $1000.

  • The current shopping cart is valued at below $100 and past purchases have exceeded $1000.

  • The current shopping cart is valued at below $100 and past purchases have not exceeded $1000.

The Promotion rule 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.