Expanded view of rule processing
While the previous top-to-bottom rule processing occurs in the majority of scenarios, the full picture of how rules are processed is more elaborate.
In practice, when execution enters a state, the rules of that state are placed on a queue in the order shown in the Rules panel — first global rules and then local rules. This queue is known as the rule queue. Rules are taken off the head of this queue and processed.
The sequence can differ if any of the action statements modify a scenario variable (or block parameter or block field) that is referenced by the condition of any rule within that state.
In that case, all rules whose condition references that variable, and that are no longer on the queue, will be added to the end of the queue. If those rules had already been on the queue waiting to be processed, then they would not be added again. For example, consider the following rules:
R1: f(b): continue;
R2: f(a): continue;
R3: f(c): a=7; continue;
R4: f(d): b=0; continue;
R5: f(a,b): continue;
Suppose that a, b, c, and d are variables and f(a) means “some function of 'a'”. Assume that f(c) and f(d) are both true. Event Modeler places the rules on the queue as follows:
R1 R2 R3 R4 R5 R2 R1
As you can see, when Event Modeler adds a rule to the queue, it always adds it to the end of the queue.
Consider the set of rules shown in the next screen:
Rule processing example two
1. When execution enters NewState 3 any rules of the previous state are removed from the rule queue, and the following rules will be placed on it, in this order: New Rule 1, New Rule 2, New Rule 3, New Rule 4 and New Rule 5.
2. New Rule 1 will be taken off the queue and its condition examined. If Variable3 is true, then the scenario will move to NewState1. The rule queue will be emptied of all New State 3 rules, and no further steps apply.
3. However, if Variable3 is false, then New Rule 2 is taken off the queue and its condition checked. Note that at this point the rule queue would contain New Rule 3, New Rule 4 and New Rule 5. New Rule 2’s condition states that if Variable1 is not equal to 15 its action part must be processed. Let us assume that Variable1 is indeed not equal to 15 and its single action statement changes Variable2 to the value “A new value”.
4. What happens next in this case depends on the state transition statement of New Rule 2. If it had caused a transition to another state, then the scenario would have emptied the rule queue, moved to that state, and then repopulated the queue with the rules from the new state. However, in this case the state transition statement is continue. Note that Variable2 is referred to in the condition part of New Rule 3 and New Rule 5, and that it has now been changed. Therefore, New Rule 3 and New Rule 5 must be added to the rule queue. However, they are already on the queue, so nothing happens. If either of these two rules had not been on the queue, they would have been added to the end of the queue.
Now, consider this slightly changed set of rules, specifically New Rule 2.
Rule processing example three
New Rule 2 is now also changing Variable3. This time, starting with step 3 from the previous sequence, the following is what happens:
1. If Variable3 is false, then New Rule 2 is taken off the queue and its condition checked. Note that at this point the rule queue would contain New Rule 3, New Rule 4 and New Rule 5. New Rule 2’s condition states that if Variable1 is not equal to 15 its action part must be processed. Let us assume that Variable1 is indeed not equal to 15 and action statements change Variable2 to the value “A new value”, and Variable3 to true.
2. What happens next in this case depends on the state transition statement of New Rule2. If it had caused a transition to another state, then the scenario would have emptied the rule queue, moved to that state, and then repopulated the queue with the rules from the new state. However, in this case the state transition statement is continue. Note that Variable2 is referred to in the condition part of New Rule 3 and New Rule 5, and that it has now been changed. Therefore, New Rule 3 and New Rule 5 must be added to the rule queue. Also, Variable3 is referred to in the condition part of New Rule 1, and it has also now been changed. Therefore, New Rule 1 must be added to the rule queue. Now, New Rule 3 and New Rule 5 are already on the queue, so they are not added. New Rule 1 is no longer on the queue, so it is added. Therefore, at the end of processing New Rule 2’s action part, the rule queue will now be: New Rule 3, New Rule 4, New Rule 5 and New Rule 1.