| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (8 out of 25)

Procedures and Rules

Procedures and rules have much in common, but their purposes are different, and their syntax differs slightly. This section compares and contrasts procedures with rules.

Rules are better than procedures for monitoring events because they do not consume resources with busywaiting as procedures would.

Within a procedure, you control exactly when G2 waits and allows other processing to occur. This differs from the way rules operate, because you cannot control when a rule waits for a value. As a result, you are guaranteed that G2 executes the tasks for a procedure without interruption, except where you indicate in the procedure that other processing can occur.

Because a procedure only waits at specific points, all of the expressions in it must have current values. Data-seeking implies waiting and allowing other processing to take place, so a procedure does not cause data-seeking unless you instruct it to do so by using a collect data or wait statement. This is another difference between procedures and rules: rules automatically manage data collection and waiting for values, while procedures do not.

The following table summarizes the major differences between procedures and rules:

Capability Procedures Rules
May cause event updating (forward chaining)


Can only seek data explicitly


Always allows other processing to take place while it waits for a value


Explicitly allows other processing to take place while it waits for a value


Automatically manages data collection and waiting for values


May call functions


May start procedures (so that they run in parallel)


May call procedures (so that they run in sequence)


Can contain actions


The following table shows minor differences between procedures and rules that can cause trouble when they are written. Action buttons use the same syntax that rules do, so the table applies to them also.

Rules/Action Buttons Procedures
Procedures can only be started.
Procedures can be started or called.
No control structures are available.
All standard programmatic control structures are available.
Default: parallel execution. Use in order to specify sequential execution.
Default: sequential execution. Use do in parallel to specify parallel execution.
Local names are defined by context: for any bottle B ...
Local names are explicitly declared in the local declarations section: B: class bottle;
Iterate over items using every, e.g. move every bottle ...
Iterate over items using each, e.g. for B = each bottle do ... end;
Separate actions with and.
Separate actions and other procedure statements with semicolons.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (8 out of 25)

Copyright © 1997 Gensym Corporation, Inc.