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

Using Operators in Expressions

An operator specifies a type-specific operation. Each operator combines with one or two expression terms, also called operands. For most operators, each operand can be a distinct expression.

Each operator in an expression must be appropriate for the types of its operands. For instance, it is invalid for G2 to combine an arithmetic operator with an operand that does not produce a value of type integer or float.

G2 offers these operators:

The following sections describe in detail how these operators and their operands participate in an expression.

Using Arithmetic Operators

G2 uses the following reserved characters to signify arithmetic operators:

Reserved Character Arithmetic Operation
- (hyphen)
Negation
+ (plus)
Addition
- (hyphen)
Subtraction
* (asterisk)
Multiplication
/ (forward slash)
Division
^ (caret)
Exponentiation


Tip: For G2 to interpret the - (hyphen) character as an operator, rather than as a character in a symbol, include at least one space before and after it.

Identifying the Default Order of Evaluation

By default, G2 evaluates an expression with an arithmetic operator from left to right. For example, G2 evaluates this expression:

as follows:

  1. Obtain the value of the term W.

  2. Obtain the value of the term X and multiply it by the previous value.

  3. Obtain the value of the term Y and subtract it from the previous product.

  4. Obtain the value of the term Z and add it to the previous difference.

Using Parentheses to Affect the Order of Evaluation

You can use parentheses to override the default order in which G2 evaluates an expression that includes an arithmetic operator. In the following expression, enclosing the middle two terms and their operator in parentheses changes how G2 evaluates the expression:

In this case, G2 evaluates this expression as follows:

  1. Obtain the value of the term W and remember it.

  2. Obtain the value of the term X.

  3. Obtain the value of the term Y and subtract it from the previous value.

  4. Multiply the memorized value of W by the previous difference.

  5. Obtain the value of the term Z and add it to the previous product.

Precedence of Arithmetic Operators

When evaluating expressions that include arithmetic operators, G2 observes the following precedence among operators.

Precedence Reserved Character Arithmetic Operation(s)
1
-
Negation
2
^
Exponentiation
3
* and /
Multiplication and division
4
+ and -
Addition and subtraction

For example, in this expression:

because the * (multiplication) operator has higher precedence than the + (addition) operator, G2 evaluates the entire expression as if its terms are combined within parentheses as follows:

Coercion of Values Returned from Arithmetic Operators

When evaluating an expression that includes an arithmetic operator, the type of value obtained depends upon a combination of the operator and the types of the values obtained from the operand terms. The following table summarizes this behavior:

Reserved Character (Operation) Types of Operand Values Type of Resulting Value
- (negation)
quantity
integer
float

quantity
integer
float

+ (addition)
and
- (subtraction)
and
* (multiplication)

quantity, quantity
quantity, integer
quantity, float
integer, integer
integer, float
float, float

quantity
quantity
float
integer
float
float

/ (division)
quantity, quantity
quantity, integer
quantity, float
integer, integer
integer, float
float, float

float
float
float
float
float
float

^ (exponentiation)
quantity, quantity
quantity, integer
quantity, float
integer, integer
integer, float
float, integer
float, float

float
float
float
float
float
float
float

Constraints on Exponentiation Operations

G2 disallows certain values to participate in an exponentiation operation, as follows:

Using Logical Operators

Logical operators specify boolean operations on either one or two operand terms for which G2 obtains values of type truth-value, as summarized in this table:

Operator Resulting Value Example
and
(two operands)

true,
only if the values obtained for both operand terms are true
false,
otherwise

X and Y
or
(two operands)

true,
if the value obtained for either operand term is true
false,
otherwise

X or Y
not
(one operand)

true,
if the value obtained for the operand term is false
false,
if the value obtained for the operand term is true

not X

An expression that includes a logical operator produces a value of type truth-value.

These expressions include logical operators:

Short-Circuited (Lazy) Evaluation of Logical Operators

G2's and and or operators are short-circuited:

Short-circuited (also called lazy) evaluation avoids wasting processor time evaluating terms in a logical expression whose value is already known.

Affecting the Expiration Time

For an expression that includes a logical operator and that refers to one or more logical variables, the expiration time of the entire expression depends on which logical operator is used, as follows:


Tip: Logical operators can also combine with terms that produce fuzzy truth values. For more information, see the section
Producing Fuzzy Truth Values From Relational Operations.

Precedence and Order of Evaluation

The precedence for logical operators is as follows:

An example is:

Because the not operator has higher precedence than and, and because the and operator has higher precedence than the or operator, G2 associates the operators and operands as follows:

By default, logical operators have lower precedence than arithmetic and relational operators. So, G2 evaluates these two expressions in the same manner:

Using Relational Operators

A relational operator causes G2 to compare the values obtained for its two operands and to return a value of type truth-value.

Use these relational operators to compare two terms for which G2 obtains a value of type quantity, integer, or float:

Reserved Character Relational Operation Usage
=
Is equal to
A = B
/=
Is not equal to
A /= B
>
Is greater than
A > B
<
Is less than
A < B
>=
Is greater than or equal to
A >= B
<=
Is less than or equal to
A <= B

Use these relational operators to compare two terms for which G2 obtains a value of type text:

Reserved Character Relational Operation Usage
=
Is equal to
A = B
/=
Is not equal to
A /= B
>
Is greater than
A > B
<
Is less than
A < B
>=
Is greater than or equal to
A >= B
<=
Is less than or equal to
A <= B

G2 ignores the alphabetic case when comparing two text values.

Use these relational operators to compare two terms for which G2 obtains a value of type symbol:

Reserved Symbols Relational Operation Usage
=
Is equal to
A = B
/=
Is not equal to
A /= B

Use these relational operators to compare a term for which G2 obtains a value of type symbol with a literal symbol:

Reserved Symbols Relational Operation Usage
is
Is the same symbol as
A is red
is not
Is not the same symbol as
A is not red


Tip: There are corresponding relational operators for fuzzy truth expressions. See the section Fuzzy Truth Operators for more information.

Producing Fuzzy Truth Values From Relational Operations

A truth-value expression that includes a relational operator produces a value of type truth-value. As introduced in Using the Truth-Value Type, a value of type truth-value can range from -1.0 true to +1.0 true, where -1.0 true signifies complete certainty that a comparison is false and +1.0 true signifies complete certainty that a comparison is true. Thus, such an expression can also produce a fuzzy truth value, which signifies a partial certainty in the truth of a comparison.

You produce a fuzzy truth value from a truth-value expression with a relational operator when you also specify a fuzzy truth band subexpression. In the following expressions that contain a relational operator, the fuzzy truth band subexpressions appear in boldface:

Specifying a Fuzzy Truth Band Subexpression

To produce a fuzzy truth value from an expression that includes a relational operator, specify a fuzzy truth band subexpression. Its syntax is:

For example, this truth-value expression compares an attribute's value with a literal integer, according to a fuzzy truth band subexpression:

The fuzzy truth band subexpression ( +- 5 ) specifies that the fuzzy truth value produced corresponds to a range of values, from 45 (50 minus 5) to 55 (50 plus 5). That is, as the value of the Flow attribute of pump-1 ranges from 45 to 55, the corresponding fuzzy truth values range from -1.0 true to +1.0 true.

Thus, the expression the flow of pump-1 > 50 ( +- 5 ) has a +1.0 true truth-value when the the flow of pump-1 is 55 or greater, and a -1.0 true truth value when the flow of pump-1 is 45 or less, as shown in the next diagram:


As the diagram indicates, the truth-values in the fuzzy-truth band fall along a line between the specified end-points of the band.

You can specify a fuzzy truth band subexpression in the antecedent of a rule. G2 executes such a rule after consulting the value of the Truth-threshold attribute in the Inference Engine Parameters system table. Its value can range from 0.0 to 1.0, and its default value is .800 (or +0.8 true). When G2 invokes a rule, for G2 to execute the actions in the rule's consequent, the truth-value that results from evaluating the rule's antecedent must be greater than or equal to the Truth-threshold.

For example, the truth-value expression in this rule's antecedent specifies a fuzzy truth value subexpression:

If the Temperature attribute of tank-1 has a value of 101.8, the subexpression causes the entire truth-value expression to produce the fuzzy truth value of +0.9 true. Because the value +0.9 true is greater than the value of Truth-threshold (that is, +0.8 true), G2 executes the action to conclude the truth-value (+0.9 true) into the Tank-is-boiling attribute of tank-1.

Using Logical Operators with Terms That Produce Fuzzy Truth Values

You can use logical operators to combine terms in a truth-value expression that produce fuzzy truth values. For example, you can specify a truth-value expression like this:

G2 evaluates these expressions as follows:

Given this truth-value expression that includes the and logical operator:

G2 evaluates this expression as follows:

  1. G2 finds values for X and for Y, then uses the fuzzy truth band ( +- 5 ) to calculate a truth-value for the subexpression X > Y.

  2. G2 finds values for Z and for W, then uses the fuzzy truth band ( +- 3 ) to calculate a truth-value for the subexpression Z > W.

  3. G2 produces the lesser of the two fuzzy truth values as the value of the entire expression. For example, if the value of the left side is +0.6 true and the value of the right side is +0.7 true, G2 produces the value +0.6 true for the entire expression.

Given this truth-value expression that includes the or logical operator:

G2 evaluates this expression as follows:

  1. G2 finds the value for X.

  2. G2 finds values for Y and for W, then uses the fuzzy truth band ( +- .3 ) to calculate a truth-value for the subexpression Y >= W.

  3. G2 produces the greater of the two fuzzy truth values as the value of the entire expression. For example, if the value of the left side is +0.4 true and the value of the right side is +0.9 true, G2 produces the value +0.9 true for the entire expression.

Given this truth-value expression that includes the not logical operator:

G2 evaluates this expression as follows: If the subexpression X > Y ( +- .2 ) evaluates to a fuzzy truth value of +0.3 true, G2 produces the value -0.3 true for the entire truth-value expression.

Fuzzy Truth Operators

You can specify a truth-value expression that compares one fuzzy truth value to another. This kind of expression uses one of these fuzzy truth operators:

A truth-value expression that specifies a fuzzy truth operator produces a truth-value of either true or false. It does not produce a fuzzy truth value.

The following truth-value expressions specify both fuzzy truth band subexpressions and fuzzy truth operators. The fuzzy truth operators appear in boldface:

In an expression that specifies a fuzzy truth operator, if either truth-value expression term being compared is more complex than a reference to a logical variable or logical parameter, enclose that term in parentheses, such as:

Using the Concatenation Operator

G2 provides a concatenation operator that you specify as the pair of characters [ and ]. Use this operator to insert, prepend, or append a text version of any value to a literal text value.

To concatenate by inserting, specify a text expression such as:

In this case, G2 converts the value produced by the expression the public-name of the water-pipe connected to tank-1 to a text value and inserts it into the literal text enclosed in double quotes. Likewise, G2 converts the value produced by the expression the direction-of-flow of the water-pipe connected to tank-1.

To concatenate by prepending, specify an expression such as:

To concatenate by appending, specify an expression such as:

You can also use the concatenation operator to produce an entire text value, as in this change the text of action:

G2 can convert to a text value any expression specified within the concatenation operator characters.

Formatting Using the Newline Character

You can include a newline character in a literal text value, by pressing Control + j within the two double quotes (") characters that enclose the value. However, including a newline character within the [ and ] concatenation operators only formats that line of code; G2 does not include the newline character in the literal text value.

For example, this literal text value includes a newline character after the word "ready":

For example, this literal text value does not include a newline character, because its author specified the newline character between the [ and ] operators:

Formatting Numeric Values

Within the concatenation operator, you can optionally specify how G2 formats a numeric value: its number of decimal digits left and/or right of a decimal point, as a time-stamp, or as a time interval.

For example, this inform action embeds a float value (the pressure of the tank) in a literal text value, with the float value formatted with four digits left and right of the decimal point:

Here is the syntax for a formatting expression:

The formatting alternatives in this expression are also those for formatting the display of a numeric value in a readout table. See Readout Tables.

For a float value, a ddd.dddd-format expression specifies the number of digits in the value to display left and right of the decimal point. For instance, the expression dd.ddd specifies to format a number with two digits left of the decimal point and three digits right of the decimal point.

For a float value expressed in exponential notation, a ddd.dddd-format expression adheres to the specified format and adds the exponential notation to the right. For example, an exponential value that adheres to the expression dd.dddddd is 23.2337182e3.

For an integer value, a ddd.dddd-format expression has no effect.

The next figure shows a workspace whose readout tables demonstrate how the same ddd.dddd-format expression affects differently the display of an integer value, a nonexponential float value, and an exponential float value.


You can also format time values using a formatting expression. For example, this inform action embeds a time value formatted as a time-stamp in a literal text value:

A time value is either an integer or float value. You can capture a time value by using one of the G2 time functions or one of these expressions:

The next figure shows a workspace whose readout tables demonstrate how the as time stamp and as interval formatting expressions affect differently the display of the current time and a literal float value.


Notice that when you format a number as a time-stamp, as in the third readout table in the figure above, the displayed value represents a time-stamp as of that number of seconds after the current KB was started, not after the current time.

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

Copyright © 1997 Gensym Corporation, Inc.