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

Kinds of Rules

G2 offers five kinds of rules, each of which serves a different purpose. You refer to a rule by its reserved word: if, initially, unconditionally, when, or whenever.

This table shows which methods of invocation G2 supports for each kind of rule:

Invocation Mechanism if initially unconditionally when whenever
Event detection





Scanning





Workspace activation





Focusing on or invoking a rule category





For a detailed description of how G2 invokes each kind of rule, see Invoking Rules.

This table shows whether each kind of rule can participate in backward chaining and forward chaining:

Participation in Chaining if initially unconditionally when whenever
Forward chaining from





Forward chaining to





Backward chaining from





Backward chaining to

*




* Only an initially rule can invoke another initially rule via backward chaining.

For information on how rules participate in backward and forward chaining, see Backward Chaining and Forward Chaining.

If Rules

An if rule specifies a condition, in the form of a truth-value expression, and one or more actions.

The syntax for an if rule is:

For example:

An if rule is the most flexible kind of rule. An if rule can be invoked using forward and backward chaining, using scanning, and using the focus an invoke actions. An if rule can invoke other rules due to forward chaining and due to backward chaining.

To make an if rule behave like a when rule, edit its Options attribute to include the phrases not invocable via forward chaining and not invocable via backward chaining.

Initially Rules

An initially rule optionally specifies a condition, in the form of a truth-value expression, and one or more actions.

G2 automatically invokes an initially rule each time its parent KB workspace is activated. After an initially rule executes, G2 automatically disables it.

A workspace is automatically activated when you start or restart the current KB. Also, the activate action explicitly activates a subworkspace configured as an activatable subworkspace. For information about how and when workspaces are activated, see Activating and Deactivating Workspaces.


Tip: If you edit an initially rule after it has been invoked, G2 automatically enables the edited rule, if it otherwise valid to do so.

An initially rule can cause backward chaining and forward chaining to other rules. By default, an initially rule cannot be invoked using forward chaining or backward chaining. You can edit the Options attribute of an initially rule so that it can be invoked using backward chaining, but only from other initially rules.

The syntax for an initially rule is:

Forms of Initially Rules

G2 supports three forms of initially rules:

Effects on Rule Scanning

When the current KB starts, if any enabled workspace or subworkspace contains initially rules, G2 invokes those initially rules. G2 does not begin scanning other rules until the initially rules upon all newly activated workspaces complete or are suspended (due to backward chaining or data seeking). When the current KB starts, if no enabled workspace contains an initially rule, G2 begins scanning other types of rules immediately.

For an activatable subworkspace that contains initially rules, when G2 explicitly activates that subworkspace due to executing an activate action, G2 does not perform additional scanning of other rules until the initially rules on the newly activated subworkspace complete or are suspended (due to backward chaining or data seeking). If the newly activated workspace has no initially rules, G2 does not interrupt scanning other rules.

When any workspace is activated, G2 invokes each initially rule on the workspace at each rule's declared priority. When each initially rule has completed or is suspended (due to backward chaining or data seeking), G2 schedules scanned rules for the next G2 clock tick.

Unconditionally Rules

An unconditionally rule specifies one or more actions to perform without a condition. G2 performs the consequent of an unconditionally rule each time the rule is invoked. For example, this unconditionally rule:

is equivalent to this if rule:

The syntax for an unconditionally rule is:

Like an if rule, an unconditionally rule can be invoked using backward chaining, using scanning, and using the focus or invoke actions. Since an unconditionally rule has no antecedent, it cannot be invoked using forward chaining.

When Rules

A when rule specifies a condition, in the form of a truth-value expression, and one or more actions.

A when rule is like an if rule that cannot be invoked using forward chaining or backward chaining. In this sense, a when rule is like an if rule that includes these phrases in its Options attribute: not invocable via forward chaining, not invocable via backward chaining.

A when rule can be invoked using scanning and using the focus or invoke actions.

The syntax for a when rule is:

This is an example of a when rule:

Whenever Rules

The antecedent of a whenever refers to an event. G2 invokes a whenever rule each time the specific event occurs. The syntax for a whenever rule is:

For example:

You can include more than one event expression in the antecedent of a whenever rule by using the reserved symbol or. This expression directs G2 to execute the consequent of the rule when any event specified in any of the event expressions in the antecedent occurs. For example:

Optionally, you can include the reserved symbol when and specify a truth-value expression, which represents an additional condition that must be satisfied for G2 to perform the consequent of the rule. For example:

Using Whenever Rules

You cannot use an event expression in a whenever rule to respond to events produced due to actions that use indirect referencing to affect an item or the value of an attribute. That is, if your KB's processing performs an action that affects an item or an attribute's value, and the action references that item or attribute in an indirect manner, then updating that item's knowledge or that attribute's value does not produce an event that is detectable using a whenever rule.

Referencing an item or attribute by means of the named by symbolic-expression expression, or referencing an item that is a member of a list or array, are examples of indirect referencing.

G2 cannot invoke a whenever rule by using scanning, forward chaining, or backward chaining. G2 ignores the Focal-objects, Focal-classes, and Categories attributes of a whenever rule.

G2 does not execute a whenever rule that refers to a variable in an expression that uses the named by or nearest to phrases.

Event Expressions

The rest of this section describes the various event expressions that a whenever rule can contain. The general principles described previously in this section apply to all event expressions. The detectable events are:

A Variable, Parameter, or Attribute Receives a Value

The syntax is:

A Variable Fails to Receive a Value

The rule fires when a variable fails to receive a value for the first time after previously having a current value. For information on how variables fail to receive a value, see Handling a Variable Failure. Syntax:

A Variable Loses Its Value

The syntax is:

For example:

An Item Is Created

The syntax is:

For example:

G2 fires the rule after the create action is complete. To detect the creation of an item of any kind, specify ITEM itself as the item.

An Object is Moved on a Workspace

The rule can specify whether the object is moved by the user or by G2 executing a move action, or can fire in either case. Syntax:

An Item is Enabled or Disabled

The syntax is:

For example:

An Item is Activated or Deactivated

The syntax is:

For example:

The use of whenever rules for detecting item deactivation is actually a G2 6.0 capability that is still under development. The capability has been included in G2 5.0 because many requests exist for it, but it does not have a standard interactive interface. Such a rule will not fire unless you set the rule's may-refer-to-inactive-items evaluation attribute to true.

To make a whenever rule for detecting item deactivation operative:


Caution: The evaluation-attributes of items are hidden attributes, which are not fully supported in G2 5.0. Do not use them in any way that is not specifically described in the G2 documentation or recommended by Gensym Customer Support.

Two Items Become or Cease To Be Related by a Specific Relation

The syntax is:

Two Items Become or Cease To Be Related by Any Relation

The syntax is:

For details, examples and further information, see Invoking Rules using Relations.

Two Items Become or Cease to Be Connected

See Detecting Connection and Disconnection Events for information about detecting connection events with whenever rules.

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

Copyright © 1997 Gensym Corporation, Inc.