startaction 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
startaction 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
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
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-recordprocedure has two arguments, whose local names are
plant-nameis of type
inventory-totalis of type
quantity. You could invoke the
plant-recordprocedure like this:
For example, if
SOAP-1is a text variable or text parameter whose value is
collect datastatement whether or not the variable has a value at the time of the reference. For details, see
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
: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 processingfor 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
callfor more information.
: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 datafor 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
do in parallel.
: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
waitfor 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-limitattribute 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.
nonecan 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
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
started procedure runs by default at the priority declared in its
at priorityinteger-expression in the
startstatement that invokes the procedure. Thus:
Tracing-and-breakpointsattribute allows you to set tracing and breakpoints on the procedure. G2 provides three techniques for debugging procedures:
Notesattribute 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-breakpointsattribute 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.
informactions in your procedure to send messages at important steps in the procedure. For example, you can use
informactions 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-compilationconfiguration 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-configurationof a procedure to include these properties, the
Notesattribute of the procedure changes to read:
table itemmenu of the procedure attribute table. G2 displays this dialog, because the procedure has been declared as stable:
Notes status will be
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
procedure-invocation. The default is
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.
If the procedure does enter a wait state at some point in the loop, you can use several techniques to abort the procedure:
this procedureThis expression produces the procedure within whose invocation G2 evaluates this expression. For example, the
announce-selfprocedure creates and places a message upon its workspace, then deletes that message 10 seconds later:
announce-self ( )
M : class message ;
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 ;
Specify this expression only in the text of a procedure or method whose
Class-of-procedure-invocationattribute 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.
Each invocation of
view-invocation causes G2 to create one procedure-invocation.
this procedure-invocation to the workspace of the list passed as its argument and graphically aligns its icon with that list's icon. Once per second for several seconds,
view-invocation changes the icon-color of
this procedure-invocation to indicate that it is active.