Aspen SCM‎ > ‎Expert System‎ > ‎2. Flow of Control‎ > ‎

2.6 How a Rule is Evaluated

Execution of Rules

A rule is executed either when it is fired, e.g. >MYRULE or when its predicate is asserted, e.g. SUM_OF 3 4 ?X. In either case execution starts at the IF statement and attempts to establish whether the rule is TRUE or not. With the exception of IN and WHILE loops (see section 2.b), a rule is no more and no less than a series of logical conjunctions and disjunctions which is evaluated in a specific order.

From a theoretical point of view, the sequence in which the component statements of a rule are evaluated is irrelevant: the result will be the same in all cases. In practice, as the Aspen SCM Expert System is the most powerful and efficient programming language in Aspen SCM, it is used to implement procedural algorithms. It thus becomes important to understand the sequence in which statements are evaluated. This applies especially where it is the work undertaken in a rule which is important and the predicate is no more than the mechanism used to call the rule and pass it arguments.

The result of running a rule can be seen in its trace. To see the trace, set CNTLE(TRACE,1) = YES before running the rule. The trace is stored in the table ETRACE (or whatever is specified in CNTLE(TRTAB,1)). By inspecting this, you can see what has been evaluated at each stage, the values which variables took and the status (TRUE, FALSE or UNKNOWN) of the component statement. However, it can often be hard to identify precisely which statement was being evaluated and to understand the flow of control.

SYN for Aspen SCM Expert System

Eudoxus Systems has developed a pretty printer, SYN for Aspen SCM Expert System, which reformats rules so that their logical structure is displayed. It also checks the syntax for common errors and highlights structural keywords. SYN consists of sets and tables which you add to your case. Once you have done this, you can run it on any rule set by executing a command such as SYN MYRULE. It generates the reformatted rule in two forms:

  • as a panel table, in this case MYRULE.L;

  • as a .HTM file for viewing in a web browser and printing to a colour printer.

For the rule set MAXOF1.R

MAXOF1  Returns ?A as greatest of ?X ?Y ?Z

IF      ?A = ?X + 0
AND     ?A GE ?Y OR ?A = ?Y + 0
AND     ?A GE ?Z OR ?A = ?Z + 0
THEN    MAXOF1 ?X ?Y ?Z ?A

SYN generates the panel table:

It uses the following colour-coding conventions:

  • blue is used for structural keywords and brackets;

  • green is used for comments;

  • red is used for warnings, messages and unrecognised tokens;

  • black is used for other parts of the rule.

As well as formatting the code and colouring it, the .HTM file also hyperlinks the predicates to the rules which prove them. They are shown by a pale blue background which turns orange when the mouse hovers over it and violet when the link has been followed. The rule name at the start of the rule has a light grey background:

MAXOF1.R checked. 0 errors and 0 warnings.

1 MAXOF1   Returns ?A as greatest of ?X ?Y ?Z
3 IF       ?A = ?X + 0
4     AND  ?A GE ?Y
            OR ?A = ?Y + 0
5     AND  ?A GE ?Z
            OR ?A = ?Z + 0
6 THEN     MAXOF1 ?X ?Y ?Z ?A

Translation from SYN to Procedural Code

To translate SYN’s output to procedural code we:

  • remove ANDs (they cause SYN to start a new line for a new statement)

  • replace each OR by ELSE;

  • insert an IF immediately before a logical test;

  • complete Block IFs with END IF

  • move the THEN clause to the top as the declaration of the procedure

Using these rules we get


?A = ?X + 0

IF    ?A GE ?Y
ELSE  ?A = ?Y + 0

IF    ?A GE ?Z
ELSE  ?A = ?Z + 0


Note that if the IF tests are satisfied, no action is taken as their ELSE clauses appear immediately after the tests.

Taking Action After a Test is Satisfied

In the rule MAXOF1 we start by assigning ?A = ?X + 0 and then reassign ?A = ?Y + 0 if ?Y > ?X. It would be neater if we started by testing whether ?X ≥ ?Y and assigned ?A = ?X + 0 or ?A = ?Y + 0 accordingly. Our rule for this is:

MAXOF2  Returns ?A as greatest of ?X ?Y ?Z

IF      ?X GE ?Y AND ?A = ?X + 0 OR ?A = ?Y + 0
AND     ?A GE ?Z OR ?A = ?Z + 0
THEN    MAXOF2 ?X ?Y ?Z ?A

SYN reformats this as:

1 MAXOF2    Returns ?A as greatest of ?X ?Y ?Z
3 IF        ?X GE ?Y
       AND  ?A = ?X + 0
             OR ?A = ?Y + 0
4      AND  ?A GE ?Z
             OR ?A = ?Z + 0
5 THEN      MAXOF2 ?X ?Y ?Z ?A

This translates as


IF    ?X GE ?Y
         ?A = ?X + 0
ELSE  ?A = ?Y + 0

IF    ?A GE ?Z
ELSE  ?A = ?Z + 0


Here the first IF test has a non-null action clause as well as a non-null ELSE clause. In its reformatted form, this rule looks and behaves like its procedural equivalent.

Back                                Next