| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (9 out of 10)

Understanding Rule Invocation and Execution

To invoke a rule means to begin evaluating its antecedent. G2 can invoke a rule by using the mechanisms described in the section Invoking Rules.

When G2 invokes a rule, G2 creates a task called a rule invocation. Each rule invocation is a copy of the information in the invoked rule.

By default, G2 creates one rule invocation for each item or value in the set that the antecedent of the rule identifies. Thus, for a specific rule, G2 creates one rule invocation; for a generic rule, G2 creates one rule invocation for each item or value in the set identified in the generic reference expressions of the antecedent.

To execute a rule means to begin performing the actions in the consequent of a rule. G2 executes a rule if the antecedent of the rule produces a value of true. If you are using fuzzy truth values, the value of true must be greater than or equal to the Truth-threshold attribute of the Inference Engine Parameters system table.

Prioritizing Rules

Every rule has a priority that determines how G2 schedules rule invocations for that rule. By default, the scheduler executes rules at a priority of 6.

To override the default priority for executing a rule:

This attribute accepts a value from one (1) to ten (10), with one indicating the highest priority.

Scheduling tasks by priority becomes important in the unusual case when G2 is working at maximum capacity. G2 can postpone the execution of the lowest priority tasks until the next G2 clock tick. Therefore, you should use priorities to identify what tasks can safely be postponed if necessary.

Each time G2 completes a task, G2 starts executing the next highest task in the task list for the current G2 clock tick. If a task with priority 1 comes in while G2 is performing a priority 2 task, then after the priority 2 task completes, G2 starts executing the priority 1 task.

Rules and Scheduler Tasks

An internal component of G2, called the scheduler, is responsible for managing which activities G2 actually performs starting at each tick of the G2 clock.

At each G2 clock tick, G2 begins performing the tasks that the scheduler has associated with that clock tick. For a given clock tick, G2 begins performing tasks with a higher priority before those with a lower priority. However, G2 must suspend some tasks, or put them into a waiting state, before they can complete. This means that a task with a high priority that has already begun, and that must be suspended for some reason, might not complete before another task with a lower priority begins and completes.

One example of this is the activity of evaluating the antecedent of a rule that can backward chain to other rules. Evaluating the antecedent of a rule might require invoking another rule that concludes a value for a variable or that causes data seeking.

Rule Priorities and Rule Completion

The priorities of rules do not affect the order in which G2 completes particular rule invocations. That is, after G2 invokes a rule with a high priority, G2 might require its rule invocation to wait for a value as G2 evaluates the antecedent of the rule. While this rule invocation waits, another rule invocation with lower priority (that is, one that is not required to wait for the values that it uses) can begin to execute and can complete.

Propagation of Rule Priorities

When backward chaining takes place, the priority of rules propagates from the invoking rule to the invoked rule. This means that if two rules with different priorities backward chain to the same rule, G2 invokes a rule invocation for the third rule at the higher of the two invoking rules' priorities.

For example, if rule R1 is declared to have a Rule-priority of 3, and rule R2 is declared to have a Rule-priority of 5, and both rule R1 and R2 backward chain to rule R3 that declares a Rule-priority of 8, then G2 creates and invokes a rule invocation for rule R3 that has the priority 3.

If G2 cancels the rule invocation for rule R1 for any reason, G2 does not reschedule rule R3 with a Rule-priority of 5; rule R3 retains its Rule-priority setting of 3.


Note: The priorities of rules do not propagate when forward chaining takes place or when a rule is invoked due to a focus or invoke action.

Setting the Timeout Interval for a Rule

You can specify a timeout interval for a rule to specify how long G2 allows the rule invocation to execute after it is invoked. If a rule did not specify a timeout interval, then after being suspended, it could reawaken long after the conditions that caused G2 to invoke it have passed from the KB's knowledge.

To set the timeout interval for a rule:

The value of this attribute in the rule overrides the value of the Timeout-for-inference-completion attribute in the Inference Engine Parameters system table. G2 uses the latter attribute as the timeout interval for rules whose Timeout-for-rule-completion attribute contains the value use default.

Depending on the nature of the values that your rules and procedures manipulate, setting long time intervals for the completion of your rules can lead to inconsistencies in your KB's knowledge.

For instance, if the values of variables are periodically unavailable to your rules, you should create whenever rules that respond to the event expression does not receive a value. A variable does not receive a value when its validity interval has expired, as declared in its Validity-interval attribute.

Creating and Managing Rule Invocations

G2 begins performing a rule invocation when that task reaches the top of the task queue for that rule's declared (or propagated) priority. Note that one rule's priority can propagate to other rules, as described in Prioritizing Rules.

G2 executes each rule invocation in several stages, as follows:

  1. G2 evaluates the truth-value expression (whether it is explicit or implied) in the antecedent of the rule.

  2. If the evaluation of the antecedent cannot be completed within the Timeout-for-rule-completion attribute of the rule, G2 performs the appropriate time-out processing. See Evaluating the Antecedent.

  3. If the antecedent evaluates to a valid truth-value, the evaluation does not time out, and the consequent of the rule specifies the in order phrase, then G2 performs the actions in the consequent sequentially.

  4. If the antecedent evaluates to a valid truth-value, the evaluation does not time out, and the consequent of the rule does not specify the in order phrase, then G2 executes the actions in the consequent in parallel.

  5. If any action in the consequent cannot be completed within the declared Timeout-for-rule-completion time interval, G2 performs the appropriate time-out processing.

  6. If the antecedent evaluates to false, G2 does not perform the actions in the consequent, and the rule invocation completes.

The rest of this section describes how G2 performs each portion of a rule invocation.

Evaluating the Antecedent

G2 begins performing a rule invocation task by attempting to evaluate the antecedent of the rule. Doing so brings about one of three results:

In the unusual case where the time required to evaluate the antecedent exceeds the Timeout-for-rule-completion attribute setting, G2 performs time-out processing. This means G2 makes one final attempt to evaluate the complete antecedent:

Executing Actions in the Consequent in Parallel

When G2 begins executing the consequent of a rule, it first determines whether the phrase in order is present. If the phrase is not present, G2 first attempts to evaluate all the expressions in all the actions in the consequent. After G2 successfully evaluates all expressions in the consequent, G2 schedules all actions to be performed in parallel, in other words, within the same transaction scope. For information about the scope of a transaction, see Understanding Transactions and Transaction Scopes.

Evaluating all the expressions in the consequent might require G2 to suspend this rule invocation, so that G2 can perform data seeking to obtain values.

Time-Out Processing

If G2 cannot evaluate all the expressions in the consequent within the declared Timeout-for-rule-completion time interval, G2 performs time-out processing. This means that G2 makes a final attempt to evaluate all the expressions in the consequent as follows:

Single Transaction Scope for All Consequent Actions

When G2 executes the actions in the consequent in parallel, all actions execute in the same transaction scope.

This means that each action begins executing with the same context of information. For example, if the expressions in any two consequent actions refer to the same item or value, G2 evaluates those expressions by starting with the same set of items or values.

Example 1: The following rule includes two actions that increment the variable X:

In this case, after this rule completes successfully, the value of X will be incremented only by 1. G2 executes the actions in the consequent in parallel and evaluates all expressions in the consequent by using values in effect at the time the rule was invoked.

Example 2: The two actions in the next rule increment the variable X by differing amounts:

However, even though the two conclude actions start with the same source value for X, when the rule completes successfully, the value of X will be incremented by 3. this is because, among actions that execute in parallel and that update the same value, the action specified last in the rule determines the new value after the rule completes.

Example 3: The consequent actions in this rule refer to the variable that gives the value of the Minimum-temperature of tank-1:

Because G2 executes the actions in this consequent in parallel, G2 supplies the same value for the Minimum-temperature of tank-1 for each conclude action.

If the purpose of the Global-minimum-temperature of tank-monitor is to store the minimum temperature ever recorded for any tank monitor in the KB, then this rule concludes the value of the Global-minimum-temperature of tank-monitor incorrectly. This is because the second conclude action uses a value for the Minimum-temperature of tank-1 that does not include the calculation performed in the first conclude action. Therefore, this rule should be rewritten by using sequential execution (described below).

Also, because G2 executes consequent actions within one transaction scope, after the actions start executing, G2 allows no other KB processing to take place until all the consequent actions finish executing.

Executing Actions in the Consequent Sequentially

When G2 begins executing the consequent of a rule invocation, it first determines whether the phrase in order is present. If it is present, G2 prepares to perform the actions in the consequent in sequence. G2 evaluates the expression in the first action in the consequent, then executes the action, and so on for every action in the consequent.

When performing the actions in the consequent of a rule sequentially, G2 executes each action within its own transaction scope, as described in One Transaction Scope Per Consequent Action.

If executing any consequent action depends upon obtaining a new current value for any variable, G2 suspends this rule invocation and sets a wake-up flag on each variable without a current value that is referenced in that action.

When any variable with a wake-up flag receives a new current value, G2 wakes up the rule invocation and reevaluates the expressions in the action whose execution was suspended. In this case, G2 does not reevaluate the antecedent of the rule, and it does not reevaluate the same consequent actions that have already been performed.

Time-Out Processing

If G2 cannot perform all consequent actions in sequence within the declared Timeout-for-rule-completion time interval, G2 performs time-out processing. For instance, if a rule invocation is suspended because an action contains an expression that refers to a variable, and the action has been waiting for a new current value for that variable, G2 wakes up the rule and performs time-out processing, as follows:

One Transaction Scope Per Consequent Action

When performing the consequent actions of a rule sequentially, G2 executes each action within its own transaction scope. This means that before executing each consequent action, G2 evaluates the expressions within the actions by using values that reflect the execution of any previous consequent action in this rule.

For example, G2 performs these consequent actions sequentially:

This rule helps prepare the subworkspace of an object to display a dynamically generated set of items. In this case, the Initial-item-count attribute of a demonstration window is given by an integer variable.

Because each consequent action in this rule takes place within its own transaction scope, this rule cannot prevent other KB processing from taking place between the completion of the change action and the completion of the start action.

In this case, if the variable that gives the value of the Initial-item-count of the demonstration window requires a new current value, G2 might be required to suspend this rule invocation while backward chaining or other data seeking takes place.

This means that G2 cannot prevent other KB processing from changing the color of the demonstration-window's arrow-region to a color other than yellow while the start action is suspended. Depending on the activity that the populate-demonstration-window procedure performs, this possibility might leave some of the current KB's knowledge in an inconsistent state.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (9 out of 10)

Copyright © 1997 Gensym Corporation, Inc.