| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (14 out of 16)

Referring to Items or Values

The expressions that refer to items or values are:

Existence of an Item or Value

To determine whether an item or value exists:

This expression produces a truth-value that indicates whether the specified item exists in the current KB, or whether the specified value-expression has a value (that is, does not produce a no value condition). Use this expression to avoid references to nonexistent items and attribute values; such references cause G2 to signal an error.

For an item, use this expression to determine whether the item exists and is active. For example:

For a value-expression, use this expression to determine whether evaluating the expression produces a value. For example:


Tip: To test whether a variable or parameter has a value, or whether a variable has a current value, use the has a value and has a current value expressions. See
Variable and Parameter Expressions.

There Exists

To determine whether an item or value exists:

This expression produces a truth-value that indicates whether an item or value exists in the current KB that meets the criterion specified in a truth-value expression. Use this expression to determine whether an item or value referenced generically exists and is active.

If the specified generic reference expression refers to items, this expression produces either truth-value true or false, or a no value condition, as follows:

If this expression produces the truth-value true, you can refer to the generic reference expression's local name in other expressions within this expression's transaction scope. In this case, the local name refers to a value when the such that phrase evaluated to the truth-value true. If there is no such that phrase, the local name refers to the first instance of the generic reference expression. For example, this rule's antecedent contains a there exists expression:

Here, the expression V1 is overflowing produces the truth-value true if at least one vat is overflowing, but produces the truth-value false if no vat is overflowing.

If the there exists expression produces the truth-value false, the value that the local name refers to is not predictable. In the sample rule shown above, if the there exists a vat V1 ... expression produces the truth-value false, then references to the V1 local name in this rule's consequent might produce a different truth-value.


Note: You can optimize the execution of a there exists expression if it references indexed attributes. See Defining an Indexed Attribute for more information about indexed attributes.

Class or Type of Item or Value

To determine the class of an item or type of value:

This expression produces a truth-value that indicates whether an item is an instance of the specified class (or any of its subclasses), or whether a value is an instance of any type. For example:

By Generic Reference

To reference items or values generically:

This expression produces the truth-values true or false, or produces a no value condition, as follows:

This expression allows you to iterate through the items or values referenced in the specified generic reference expression and to detect whether a condition specified in the truth-value expression is true. This is similar to a universal quantifier in traditional logic.

For example:

This expression produces the truth-value true if every valve connected to tank-1 is broken. Otherwise, it produces the truth-value false. Note that if no items or values match the generic reference expression, the expression produces the truth-value true.

Conditional Evaluation

To perform conditional evaluation:

This expression produces a value, based on the result of evaluating a truth-value expression. Parentheses are required around this expression.

If there is no else phrase, and the truth-value expression produces the truth-value false, then the entire expression produces a no value condition.

The expiration time of this expression is the minimum of the expiration times of the specified truth-value expression and of G2's evaluation of either the then phrase or the else phrase.

You can nest this expression within other expressions, even with other conditional expressions. Some examples are:

Value Expressions

These expressions deal with the values of items or values.

Value of an Item or Value

To determine the value or an item or value:

This expression produces the value of the specified expression, which can be either an item or a value. If any variable referenced in the specified expression has never had a value, or if its current value has expired, then G2 performs data seeking for each such variable. If an attempt at data seeking for any variable referenced in the specified expression fails for any reason, this entire expression produces a no value condition.

The expiration time of the entire expression is the expiration time of the value produced by the specified expression. For example:

This expression refers to the value of the custom-variable that is related to the temperature-value variable that gives the Temperature attribute of tank-1. If either that custom-variable or the temperature-value variable that gives the Temperature attribute of tank-1 does not have a current value, then G2 performs data seeking to obtain a value.

Has a Value

To determine whether an item or value has a value:

This expression produces a truth-value that indicates whether the specified value-expression has a valid value. For example:

If the expression the temperature of the most-reliable-measures of tank-1 refers to any variables or parameters, and if those variables or parameters each have a valid current value, then this expression produces the truth-value true. If those referenced variables or parameters have never received a value, or if any of their respective current values have expired, then this expression causes G2 to perform data seeking to obtain new current values. If G2 obtains a value from that data seeking, then the entire expression produces the truth-value true, else the entire expression produces the truth-value false.

First of the Following Expressions That Has a Value

To determine the first item or value with a value:

This expression produces the item or value that is produced by the first expression in the specified list of item-or-value expressions that has a valid value.

G2 attempts to evaluate each item-or-value expression in the specified list, going from left to right. If the expression being evaluated refers to a variable or parameter that has never received a value or that has an expired current value, then G2 performs data seeking for those variables or parameters. If G2 cannot obtain a value for any of the specified item-or-value expressions, the entire expression produces the truth-value false. For example:

G2 evaluates this expression as follows:

  1. If the temperature of tank-1 has a value, G2 produces that value as the value of the entire expression. If not, G2 performs data seeking, as needed, to obtain a new current value for any variables referenced in the the temperature of tank-1 expression.

  2. If G2 cannot obtain a new value from evaluating the the temperature of tank-1 expression, G2 next checks average-system-temperature for a valid value. If it has a valid value, G2 produces that value as the value of the entire expression. If average-system-temperature does not have a valid value, and if it is a variable, then G2 performs data seeking to obtain a new current value.

  3. If G2 cannot obtain a value for average-system-temperature, the value of the entire expression is either 50 or 55, depending on the G2 clock's current time, as specified in the expression (if the current time >= 10 then 50 else 55).

Current Value of an Expression

Whether or not an expression has a current value depends on whether any item referenced in that expression is a variable and, if so, whether the value of each referenced variable has expired.

For the majority of expressions, the expiration time is the minimum of the expiration time of the values of the expression's terms. As an example, if the expiration time of X is 50, and the expiration of Y is 10, the expiration time of the expression X + Y is 10.

Current Value of

To determine the current value of an item or value:

For the specified item-or-value expression, a current value exists if G2 can evaluate the expression without finding a new value for the variables, if any, referenced in that expression. If the specified expression has a current value, this entire expression produces it. Otherwise, this expression produces a no value condition. For example:

The expression in this rule's antecedent produces an item or a value if the Temperature attribute of tank-1 has a current value. If the temperature of tank-1 does not have a value, is no longer current, or refers to an item that does not exist, then this expression produces a no value condition.

Has a Current Value

To determine whether an item or value has a current value:

This expression produces a truth-value that indicates whether the specified expression has a current value. For the specified item-or-value expression, a current value exists if G2 can evaluate that expression without finding a new value for the variables, if any, referenced in that expression.

If this expression produces the truth-value true, its expiration time is the expiration time of the value that the specified item-or-value expression produces. If this expression produces the truth-value false, the expiration time is indefinite.

If a has a value expression is nested within a has a current value expression, G2 evaluates the nested expression no differently than a has a value expression. That is, G2 performs data seeking, if necessary, to obtain a new current value for any variables referenced in the nested expression. For example:

If G2 can evaluate the expression the temperature of the most-reliable-measures of tank-1 without the need to obtain a new current value for any variables referenced in that expression, then this expression produces the truth-value true. Otherwise, this expression produces the truth-value false.

First of the Following Expressions That Has a Current Value

To determine the first item or value with a current value:

This expression produces the value of the first item-or-value expression in the specified list of expressions that has a current value. For each specified expression, a current value exists if G2 can evaluate the expression without finding a new value for any variables referenced in that expression.

G2 attempts to evaluate each expression in the specified list, going from left to right. If none of the specified expressions has a value, or if none have a current value, then this expression produces a no value condition and no error is signalled. For example:

G2 evaluates this expression as follows:

  1. If the temperature of tank-1 has a current value, G2 produces that value as the value of the entire expression.

  2. If the temperature of tank-1 does not have a current value, G2 next checks average-system-temperature for a current value. If it has a current value, G2 produces that value as the value of the entire expression.

  3. If average-system-temperature does not have a current value, the value of the entire expression is either 50 or 55, depending on the G2 clock's current time, as specified in the expression (if the current time >= 10 then 55 else 50).

By Iterating Over a Set

These expressions produce a value by directing G2 to iterate over the set of items specified in a generic reference expression.


Note: Your KB's processing cannot depend upon the same order of iteration over a set of items from one evaluation to another of the same expression.

The Count of

To determine the count of:

This expression produces the number of items or values in the set specified in the generic reference expression that also meet the criterion in the specified truth-value expression.

For example, the following expression finds the number of objects upon this executable item's workspace whose Number-of-edits attribute is greater than four:


Note: You can optimize the execution of a the count of expression if it references indexed attributes. See
Defining an Indexed Attribute for more information about indexed attributes.

The Average Over Each

To determine the average over each:

This expression produces a calculated value of type float from values in the set of items or values in the specified generic reference expression. For example:

Other Operations Over a Set

To perform operations over a set:

This expression produces a calculated value of type quantity from values in the set of quantitative variables, quantitative parameters, or numeric values in the specified generic reference expression.

For example, the following expression computes the minimum of the flows of all valves that are connected to tank-1:

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (14 out of 16)

Copyright © 1997 Gensym Corporation, Inc.