Aspen SCM‎ > ‎Expert System‎ > ‎1. Elements‎ > ‎

1.4 Instantiation and Assignment of Variables

Instantiation of Rule Variables

Aspen SCM Expert System uses the concept of instantiation for rule variables (but not for other types of variable). Rule variables do not have a default value (e.g. zero) but start as FREE. When they are given a value they are said to be instantiated. If a rule attempts to use the value of a variable which is FREE this causes an error and stops the program. This is a boon which goes some way to compensating for the inability to require variables to be declared before they can be used.

Assignment of Rule Variables and Overloading of “=”

There is no requirement to free a variable before it can be given another value. This is, however, often done because the equals sign is overloaded and becomes a test in some circumstances. In most cases an equals sign is an assignment and == or EQ is used as a test for equality.

Problems arise where the item on the left is a rule variable and the item on the right is also a rule variable which is on its own. In this case the statement may be interpreted either as an assignment or a test for equality, depending on whether the variables are FREE or not.

The statement ?A = ?B:

  • assigns the value of ?B into ?A if ?A is FREE and ?B is instantiated;

  • acts as a test that ?A equals ?B if both ?A and ?B are instantiated;

  • evaluates as FALSE if both ?A and ?B are FREE;

  • assigns the value of ?A into ?B if ?B is FREE and ?A is instantiated.

Ensuring that Statements are Assignments

Because statements which are intended as assignments can become tests, it is much safer not to write statements of the form ?A = ?B. Instead such statements can be replaced by ?A = ?B + 0 if ?B is numeric or ?A = '?B' if ?B is character or large integer. These statements have the advantage that they continue to work as assignments irrespective of whether ?A is FREE or not and that if ?B is FREE they cause an error and stop the program. If a statement ?A = ?B is to be used, it should be preceded immediately by the statement FREE ?A, preferably on a single line as

    FREE ?A AND ?A = ?B

Statements in which a rule variable is set equal to an element in a table or vice versa always work as assignments so there is no need to add 0 or quote the variable.

Amper Variables in Rules

Amper variables cannot be used directly in rules and have to have their values loaded from and unloaded to rule variables. Thus $5 = ?A loads the value from ?A into $5 while ?B = $1 unloads the value from $1 into ?B. Note that such statements are never interpreted as tests and there is no need to add 0 to the rule variable or quote it.

Setting Rule Variables to Literal Values

To set a rule variable to a literal value, e.g. 1.5, “Hello, world”, just write it normally, e.g. ?A = 1.5, ?B = "Hello, world". When you do this, the literal value is interpreted as a character string; thus ?A becomes the 3-character string “1.5” rather than the number. In most cases you don’t need to worry about this as the value will be converted automatically to a number if the context requires it. To force the value to be numeric immediately, write it as ?A = 1.5 + 0 or ?C = INT 35.

If you are trying to give a variable a value which is a character string, you are recommended to enclose it in quotes (either double or single – they have different meanings; see below). These are required if the string includes embedded blanks or if the string is one of the structural keywords. In some circumstances the use of a character string not enclosed in quotes (especially a single character) can cause the interpreter’s internal representation to be corrupted. This can lead it to fail to execute subsequent statements which begin with the same character string. As such statements may be separated by a considerable distance from the statement which caused the problem, it can be extremely difficult to identify the cause of the problem.

Single and Double Quotes

Aspen SCM Expert System uses both double quotes and single quotes. Double quotes are used to define a character string which is to be interpreted literally. Single quotes are used to define a string in which substitution of rule variables is to occur. Thus ?A = "?B" sets the value of ?A to the string consisting of a question mark followed by a capital B.

If the value of the variable ?B is “Hello, world” the statement ?A = '?B' sets the value of ?A to the contents of ?B, i.e. to “Hello, world”. Characters which are not part of the name of a rule variable are left as they are found, so ?A = 'A?BC' sets ?A to “AHello, worldC”. Clearly there is scope for confusion if there is a variable ?BC as well as ?B. If there is no rule variable in a single-quoted string it behaves as a double-quoted string (see section 4.4).

Mixing single and double quotes is very dangerous and often causes the interpreter to branch to an unrelated rule.


Back                                Next
Comments