Try OpenEdge Now
skip to main content
BPM Events User's Guide
The rule language : Defining and using global functions
 

Defining and using global functions

When a specific group of actions must be reused several times by the module rules, it is preferable to abstract these in a function. The function is declared at the module level, between the "module ..." statement, and before the first rule group. Any sequence of statements allowed in the action part of a rule can define the body of a function.
The general syntax is:
"fun" <func_name> "(" <list_parameters> ")" [ ":" <returned_type> ] "{" <func_body> "}"
Note: Progress Software Corporation recommends not to use duplicate global function declaration in a module. The returned type may or may not be specified, depending whether the function returns a value or not.
A function can return a value using the statement:
return <expression_or_var> ";"
In the following example, a function is defined to generate events in the Business Process Server event queue in database:
application Tutorial1
module Tutorial1_rules
// TUTORIAL 1: simulate the creation of three purchase orders processes
// (three PI_ACTIVATED events generated as in BP Server processes)
// and decides if a discount applies.
// this function is used to generate BP Server events, and store them in DB queue
fun simulate_BP Server_evt(i:int, total:float, p:string) {
    postDB(event BP Server::PI_ACTIVATED{PROCESSINSTANCEID: i, RPID:i,
PROCESSTEMPLATENAME: "PurchaseOrder", totalamount: total, product: p});
}group discount_policy {
...
Invoke the function from any place where a rule action is executed: either in the initialize{} or finalize{} section, or inside a rule action part, as any other rule action. The following initial section is logging three events (simulating BP Server events) in the Business Process Server event queue in the database:
initialize {
simulate_BP Server_evt(1, 350.25, "video_monitor");
simulate_BP Server_evt(2, 650.45, "personal_computer");
simulate_BP Server_evt(3, 150.0, "modem");
}finalize {
}
When returning a value, use a function inside any expression where this value type is expected.
Although generally defined for the rules of a module, use a function—such as global objects as infopads—from other modules of the same application, when qualified by the module name (import this module by the using module).