A module provides a mechanism to group variables and functions in mat|r script. When executing an application, the modules create a global scope where their variables can be accessed. This scope remains alive throughout the life cycle of the application. A function in mat|r script is an autonomous code block which allows to execute code statements. When a function is executed, the control passes to the point where the function starts. Once the execution is ended, the control returns to the point from where the function call was made. Functions overloading is not allowed, that is to say, creating functions of the same name with different parameters.

Content

Modules

The syntax that allows defining a module is the reserved word Module, as shown in the following code example:

Module Salary {
  Double  vacationPercent
  Integer basicSalary 
}

Module Variables

Variables can be simple type, such as Integer,Double,String,Date,Bool, or compound type, as: Model,Array,Map.
Variables can be accessed and assigned by static methods: Salary.basicSalary. This means that to access a module variable, the name module should be specified, followed by a dot and, then, the variable name.

Examples of different types of variables definition:

Module Salary {
  Double                 vacationPercent 
  Integer                basicSalary 
  Bool                   isMonthlyContract
  Array<Integer>   contractMonths
  Map<Person>      employees
}

Access to variables:

Double salary = Salary.basicSalary  + 10 * Salary.vacationPercent 

Assignment of value to a module variable:

Salary.basicSalary  = 12000

Functions

A function is defined inside a module. Functions can have 0, 1 or more parameters and they could have or not a return value. Functions overloading is not allowed because there cannot be two functions with the same name in a module.

Function Parameters

Function parameters have a list of arguments which are comma-separated and between parentheses. For each parameter, its type (simple or compound) and its name should be defined. Parameters in functions are passed by reference.

Next, there are given some examples of parameters definition for a function:

functionBasicSalary (Integer consolidatedYears){
    ...
}

functionEmployeesSalary(Integer consolidatedYears, Array<Person> employees){
    ...
}

Functions with Return Clause

Mat|r allows to define a function with return by specifying: the type of return, name of the function, parameters between parentheses and the function body defined by keys, where local variables and instructions to execute are stated. The function body finishes with the statement return which signals the end of the function.

This is how a callback function is stated:

Person getPersonByName(String name) {
    return broker.localPersistence.get(name)
}

It should be made clear that the statement return is also used to interrupt the execution. For this reason, instructions will not be executed under the statement return.

Functions without Return Clause

In the case of functions without return, the statement void must be specified before the function name definition. The statement return shall not be used, unless needed to interrupt the instructions it has.

Functions without return should be stated the following way:

void processSalaryPayment(Array<Person> employees) {
    service.paymentProcess.call(employees)
}

Access to variables in a function

In a function block, it can be accessed to local variables, defined variables in the module containing the function and global variables.


Module Salary {
  Double                 vacationPercent 
  Integer                basicSalary 
  Bool                   isMonthlyContract
  Array<Integer>   contractMonths
  Map<Person>      employees

  Double getBasicSalary (Integer consolidatedYears) { 
    return (Salary.basicSalary  + (consolidatedYears * Salary.vacationPercent )) 
  } 
}

In addition, to access the module variables in the body function, the module’s name prefix can be omitted. For instance:


Module Salary {
  Double                 vacationPercent 
  Integer                basicSalary 
  Bool                   isMonthlyContract
  Array<Integer>   contractMonths
  Map<Person>      employees

  void increaseBasicSalary (Double increasePercent) {
     basicSalary  = basicSalary *(1 + increasePercent)
  }
} 

Functions Calling

Functions can be called by static method from any execution block:

moduleName.functionName([parameters])

Next, it is shown a two-function-module definition and the way they are called from a rule:


Module Salary {
  Double   vacationPercent 
  Integer  basicSalary 

  Double getBasicSalary (Integer consolidatedYears) {
    return (Salary.basicSalary  + (consolidatedYears * Salary.vacationPercent ))
  }

  void processSalaryPayment(Array<Person> employees) {
    service.paymentProcess.call(employees)
  }
}

RuleContext SalaryRules {
  Rule processBasicSalary {
    // value assigning to variables
    Salary.basicSalary  = 12000
    Salary.vacationPercent  = 0.25
    Array<String> employees = service.getEmployees.call()
    //function calls
    Double basicSalary  = Salary.getBasicSalary (15)
    Salary.processSalaryPayment(empleados)
  }
}