start action or call statement. The
start action invokes a procedure and runs it asynchronously. It tells G2 to schedule the new procedure for execution, then continue processing the current procedure. You can also use the start action to invoke a procedure from within a rule, a button, or any other context where you can execute an action. For more information about the start action, see start.
The call statement invokes a procedure and runs it synchronously. It tells G2 to invoke the new procedure and wait until the procedure returns before continuing to execute the calling procedure. Call is a procedure statement, not an action: you can use it only in the body of a procedure. For more information on the call statement, see call.
Passing Arguments to a Procedure
Whenever you invoke a procedure, you must pass it the correct number and type of arguments. When a procedure is defined, the arguments are specified in the procedure header. For example:
plant-record (plant-name: text, inventory-total: quantity)
plant-record procedure has two arguments, whose local names are plant-name and inventory-total. The plant-name is of type text, and inventory-total is of type quantity. You could invoke the plant-record procedure like this:
call plant-record ("soap-plant-1", 245)
plant-name. For example, if
SOAP-1 is a text variable or text parameter whose value is soap-plant-1, then:
call plant-record (SOAP-1, 200+45)
collect data statement whether or not the variable has a value at the time of the reference. For details, see collect data.
Memory Management in Procedures
When a procedure creates an item, that item is not automatically deleted when the procedure exits: the item persists indefinitely until explicitly deleted with a delete action. Therefore, a procedure must delete every item that it creates unless the item is specifically intended to persist and be used elsewhere in the KB.
To prevent such problems, G2 by default executes a procedure without interruption. However, uninterrupted procedure execution prevents G2 from accomplishing any other tasks, such as serving data servers and user interfaces, performing tasks for other procedures and for rules, and so on.
allow other processing
call ... across
collect data
for each ... do in parallel
wait
allow other processing: G2 enters a wait state if the procedure has been running for more than 200 milliseconds; otherwise, it continues executing the procedure. See allow other processing for more information.
call ... across: G2 cannot predict the response time of the remote system, so it enters a wait state and allows other processing for the duration of the call. The wait state avoids needless suspension of the local G2 or timeout of the calling procedure. See call for more information.
collect data: If G2 has to wait for one or more values, it allows other processing to occur so that the appropriate variables can receive values. If the variables all have values, G2 continues without allowing other processing. See collect data for more information.
for each ... do in parallel: G2 enters a wait state between the iteration which launches the parallel iterations and the entrance to each parallel iteration. This enables an arbitrary number of threads to be launched through the iteration without risk of the procedure timing out. For more information see for and do in parallel.
wait: If the condition for the statement is false or the interval for the statement has not yet passed, G2 allows other processing to occur until the appropriate interval has passed or the conditions of the statement are met. If the condition in the statement is true, G2 continues without entering a wait state. See wait for more information.
Statements that can cause wait states should not be inserted into procedures without careful consideration of the possible consequences. To prevent concurrency problems, use them only in controlled ways to serve definite purposes.
Uninterrupted-procedure-execution-limit attribute sets a limit on the amount of execution time a procedure can use without in some way allowing other processing to occur. The possible values are:
use default: The procedure's execution time limit is given by Main Menu > System Tables > Timing Parameters > Uninterrupted-procedure-execution-limit.
none: The procedure has no execution time limit.
none can be dangerous. If the procedure enters an infinite loop, or otherwise fails to return, it will prevent other processing indefinitely, effectively freezing G2. If this happens, you can stop the procedure only by terminating your G2 process from the operating system. called by it, enters a wait state, allowing other processing to occur. If an executing procedure exceeds its specified execution time limit, G2 signals an error. If no user-defined handler exists for the error, G2 aborts the procedure and posts an error message to the Operator Logbook.
When G2 aborts a procedure that was called by another, it also aborts the caller. Thus procedure timeout in the absence of an error handler aborts the entire procedure stack, not just the procedure that timed out.
For more information, refer to Timing Parameters.
Setting Procedure Priority
The Default-procedure-priority attribute controls the default priority at which G2 executes an asynchronously invoked procedure. For information on priorities, see Chapter 46, Task Scheduling.
The default priority is 6. You should accept this default unless you have a specific reason to change it. If you need to change it, set the attribute to an integer between 1 and 10 that indicates the priority at which you want the procedure to run.
called procedure runs at the same priority at which its caller is running, irrespective of the value of its Default-procedure-priority attribute.
started procedure runs by default at the priority declared in its Default-procedure-priority attribute.
at priority integer-expression in the start statement that invokes the procedure. Thus:
start plant-record (SOAP-1, 245) at priority 2
Default-procedure-priority attribute.
Tracing-and-breakpoints attribute allows you to set tracing and breakpoints on the procedure. G2 provides three techniques for debugging procedures:
Notes attribute of a procedure definition provides descriptions of problems involving usage (like an undefined local name) or syntax. You cannot execute the procedure until you correct these errors.
Tracing-and-breakpoints attribute of a procedure definition allows you to step through the procedure to find problems. This attribute overrides the default settings in the Debugging Parameters system table.
inform actions in your procedure to send messages at important steps in the procedure. For example, you can use inform actions to display the values of local names, or to show what statement G2 is executing.
Displaying a Procedure's Invocation Hierarchy
You can use Inspect to display on a workspace:
Inlining a Procedure
A procedure declared as inlineable exists as a separate item, but is compiled as part of the calling code when called from another procedure. Inlining a procedure improves performance by:
Embedded code is incompatible with asynchronous and recursive invocation, so the code for an inlineable procedure is actually inlined only for a nonrecursive call. Inlineable procedures exist as ordinary procedure items; starts and recursive calls to them are handled just as if the procedure were not inlineable.
When declaring a procedure as inlineable, you must also use the configuration clause: stable-for-dependent-compilation.
To declare that a procedure can be inlined:
Add these item configurations:
declare properties as follows: inlineable, stable-for-dependent-compilation
stable-for-dependent-compilation configuration statement can be applied to a workspace. Whenever configurations are applied to workspaces, their effects propagate to every applicable item upon the workspace (procedures in this case), and all item subworkspaces. Item-configuration of a procedure to include these properties, the Notes attribute of the procedure changes to read:
OK, and note that this item needs to be recompiled to generate data needed for inlining
To recompile the inlineable procedure:
edit from the table item menu of the procedure attribute table. G2 displays this dialog, because the procedure has been declared as stable:
![]() |
Notes status will be OK.
To test for an inlined procedure:
Tracing-and-breakpoints-enabled? attribute to yes.
Tracing-message-level attribute to:
1 (trace messages on entry and exit)
Creating Procedure Invocations
A procedure invocation is an item that represents an instance of an executing procedure. You can put procedure invocations into lists, assign them to participate in relations, and manipulate them as you would any other item.
Class-of-procedure-invocation attribute specifies whether G2 automatically creates a procedure invocation when a procedure is called or started. Possible values are none and procedure-invocation. The default is none. Class-of-procedure-invocation is specified as procedure-invocation, G2 automatically creates a procedure invocation each time that procedure is invoked from a start action or call statement. G2 creates a separate procedure invocation each time the procedure is invoked, so a procedure that is recursive or runs concurrently with itself can have more than one procedure invocation at the same time. G2 automatically deletes a procedure invocation when the procedure returns.
Aborting a Runaway Procedure
When a procedure goes into an infinite loop that never enters a wait state, only expiration of the uninterrupted procedure execution time limit can abort it, as described under Limiting Procedure Execution Time. If no such time limit is in effect, the only recourse is to abort G2 itself from the operating system command level.
this procedureThis expression produces the procedure within whose invocation G2 evaluates this expression. For example, the
->procedure
announce-self procedure creates and places a message upon its workspace, then deletes that message 10 seconds later:
announce-self ( )
M : class message ;
begin
create a message M ;
change the text of M to
"[the name of this procedure] is now being performed.";
transfer M to the workspace of this procedure ;
wait for 10 seconds ;
delete M ;
end
this procedure-invocation
->procedure-invocation
Specify this expression only in the text of a procedure or method whose
Class-of-procedure-invocation attribute has the value procedure-invocation. Invoking such a procedure causes G2 to create a procedure invocation item. For more information about procedure invocations, see Creating Procedure Invocations.
For example, the next figure shows a workspace that contains two lists, an
initially rule, and the view-invocation procedure. The initially rule starts the view-invocation procedure once for each list that exists.
![]() |