| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (4 out of 7)

Determining Which Type of Processing to Use

The first step in determining which type of processing to use is choosing between rule-based and procedural processing. Rule-based processing and procedural processing have a number of similarities as well as some important differences.

Choosing Between Rules and Procedures

The basic factors in determining whether to use rule-based or procedural processing involve how the processing is initiated and how the actions execute. Follow these general guidelines:

Use... When you want to...
Rule-based processing

Procedural processing

Similarities Between Rules and Procedures

Rules and procedures support these similar syntactic constructs:

Rules Methods and Procedures
Branching, using if rules
Branching, using if-then or case procedure statements
Event detection, using whenever rules or if rules
Event detection, using the wait until procedure statement
Generic references to object classes, using for prefix or by referring to object classes generically within a statement
Generic references to object classes, using for each ... do procedure statement
Iteration, using for prefix
Iteration, using for each ... do procedure statement and repeat statement
Local name declarations within statements
Local name assignments before procedure body
Method and procedure invocations, using start action
Method and procedure invocations, using call statement or start action
Allows periodic processing, using scanning
Allows periodic processing, using the repeat statement for polling

Differences Between Rules and Procedures

The ways in which rules and procedures execute, as well as the types of constructs they support, have subtle differences, which this table summarizes:

Rules Methods and Procedures
Support only if-then control structures
Support all common control structures such as if-then-else, looping, repeating, branching, and recursion
Does not support arguments or return values
Supports arguments and return values
Actions execute simultaneously, by default; use in order for sequential processing
Actions and statements execute sequentially, by default; use do in parallel for simultaneous processing
Invocations are scheduled based on rule priorities or regular scan interval
Invocations can be immediate or scheduled, depending on whether you use the call statement or the start action, respectively
Invoked based on various types of events or forward chaining when a variable receives a value
Invoked explicitly by using a call statement from within another method or procedure, or by using the start action from within a rule, method, or procedure
Referencing a variable does a single implicit collect data statement
Referencing a variable requires a collect data statement each time you reference a variable, which guarantees that variable reference are executed synchronously
Creates a new process for each rule that is triggered, which implements multi-threaded processing
Creates a new process each time a procedure is explicitly invoked, using the start action

Syntactic Differences Between Rules and Procedures

In many ways, the syntax for rules and procedures is similar because each uses actions to process data. However, rules and procedures have significantly different structures, which result in a number of important syntactic differences at the statement level, which this table summarizes:

Rules Methods and Procedures
Declare local names implicitly, for example, for any computer C
Declare local names explicitly, for example, C: class computer
Separate actions by and
Separate actions and other statements by semi-colon (;)
Use every for generic references, for example, move every bottle
Use each for generic references, for example, for B = each bottle do
Supports only invoking methods and procedures by using the start action
Supports invoking methods and procedures by using the call statement or the start action

Emphasizing Procedural Processing

While most applications require some rules, you should only use them where appropriate in an application. Specifically, follow this guideline:


For maximum efficiency, maintainability, and testability of your code, emphasize procedural processing over rule-based processing; use rules primarily as event detectors.

Your application should use procedural processing for several important reasons:

For detailed information about using methods and procedures, see Using Procedural Processing.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (4 out of 7)

Copyright © 1997 Gensym Corporation, Inc.