Unsupported content

 

This version of the product is no longer supported. However, the documentation is available for your convenience. You will not be able to leave comments.

User defined functions

User defined functions can be imported from other modules and reused. User defined functions cannot be used in CMDB synchronization mappings.

Defining functions

User defined functions are specified in definitions blocks. In order to specify a function the type parameter within the definitions block must be set to the value function or omitted.

Each define within the definitions block specifies a function. A function definition consists of:

  • A pair of parentheses containing an optional list of identifiers. The identifiers represent the number of parameters specified in a call to the function and the identifier used to reference each parameter
  • An optional return list specified by following the close parenthesis with '->' and a list of identifiers, one for each return value.
  • A description string
  • A list of statements

The following example shows a simple example function:

tpl 1.5 module example;

definitions functions 1.0
  'User defined functions'
  type := function; // Optional, default if no "type" specified.

  define square(x) -> y
    'functions.square is a function which takes a parameter x and returns a value.'

    return x * x;
  end define;
end definitions;

You can return a comma separated list of values using the return statement. This list must have the same number of values as specified by the function definition. The return value identifiers are purely symbolic and not referenced anywhere within the function call or definition. For example:

  define reverse(x, y) -> a, b
    'swap two values'

    return y, x;
  end define;

You can specify a default value for function parameters. However, a default value must be a simple value, such as a number or a string. You cannot use either a table or a list as a default value. Any parameter following the first that has a default value must also have a default value. In the following example, calling add with a single parameter x returns x+2+3.

  define add(x, y:=2, z:=3) -> a
    'add values, using defaults where parameters are not passed'

    return x+y+z;
  end define;

Here are some more simple examples of functions.

  define rnd() -> y
    'functions.rnd is a function which returns a value.'

    return 16;
  end define;

  define update(node, key, value)
    '''functions.update is a function which takes 
     three parameters and does not return a value.'''

    node[key] := value;
  end define;

Using functions

The following pattern contains calls to functions defined in the same TPL module. The functions are the examples given in the preceding section.

pattern function_caller 1.0
  'Pattern which calls functions.'
  overview
    tags example;
  end overview;
  triggers
    on process := DiscoveredProcess;
  end triggers;
  body
    a := functions.square(5); // call square function
                              // which returns 25
    b := functions.rnd();     // call rnd function 
                              //which returns 16
    functions.update(process, 'key', 4); // call update, equivalent 
                                         // to "process['key'] := 4;"
    m, n := functions.reverse(1, 2);     // call reverse which returns
                                         // two values, 2 and 1.
  end body;
end pattern;

Recursion

Recursive and mutually recursive functions are not supported. Attempts to use recursion will be diagnosed, preventing the pattern module from being activated.

Functions calling functions

If you define a function which calls another function, the function being called must be defined before the function doing the calling, otherwise the pattern will not activate.

Was this page helpful? Yes No Submitting... Thank you

Comments