Types of loops

## Infinite Loops

In the example above, looping between rules 2, 3 and 4 continues indefinitely because there is nothing to stop the cycle. Some loops, especially those inadvertently introduced, are not self-terminating. Because these loops will not end by themselves, they are called infinite loops. Infinite loops can be especially vexing to a rule modeler because it isn't always apparent when a Rulesheet has entered one. A good indication, however, is that rule execution takes longer than expected to complete! A special control is provided to prevent infinite loops. This control is described in the Terminating Infinite Loops section, below.

## Trivial Loops

Single-rule loops, or loops caused by rules that depend logically on themselves, are also known as trivial loops. We consider single-rule loops to be a special kind of loop because they consist of just a single rule that successively revisits, or triggers, itself.
To enable the self-triggering mode of looping, we must select Rulesheet > Processing Modes > Advanced Inferencing with Self-Triggering from the Corticon Studio menubar, as shown in
Figure 143. Selecting Advanced Inferencing with Self-Triggering Processing Mode for a Rulesheet
Notice the icon to the left of the Conditions header - it contains the additional tiny arrow, which indicates self-triggering is active.
Here's an example of a loop created by a self-triggering rule:
Figure 144. Example of an Infinite Single-Rule Loop
Let's trace this rule to make sure we understand how it works.
When Cargo.weight has a value equal to or greater than 0, then rule 1 fires and the value of Cargo.weight is incremented by 1. Data state has now changed, in other words, the value of at least one of the attibutes has changed. In this case, it's the value of Cargo.weight which has changed.
Because it was rule 1 execution that caused the data state change, and since self-triggering is enabled, the same rule 1 will be re-evaluated. Now, if the value of Cargo.weight satisfied the rule initially, it certainly will do so again, so the rule fires again, and self-triggers again. And so on, and so on. This is also an example of an infinite loop, because no logic exists in this rule to prevent it from continuing to loop and fire.

## An Exception to Self-Triggering

Self-triggering logic can also be modeled in Column 0 of the Rulesheet, as shown:
Figure 145. Example of an Infinite Loop created by a Self-Triggering Rule
But this figure is also a good example of why it might be appropriate to disable self-triggering processing: we only want the weight to increment once, not enter into an infinite loop, which it would otherwise do, unconditionally! This is a special case where we have intentionally prevented this rule from iterating, even though self-triggering is enabled. This rule will execute only once, regardless of looping processing mode.
Another example of a loop caused by self-triggering rule, but one which is not infinite, is shown below. The behavior described only occurs when Rulesheet processing mode is set to Advanced Inferencing with Self-Triggers :
Figure 146. Example of a Finite Single-Rule Loop
In the figure above, the rule continues to fire until Cargo.weight reaches a value of 21, whereupon it fails to satisfy the Condition, and firing ceases. The loop terminates with Cargo.weight containing a final value of 21.
It's important to note that in all three examples, an initial Cargo.weight value of 0 or higher was necessary to activate the loop. A negative (or null) value, for example, would not have satisfied the rule's Condition and the loop would not have begun at all.

## Multi-rule Loops

As the name suggests, multi-rule loops exist when 2 or more rules are mutually dependent. As with single-rule loops, the Rulesheet containing the looping rules must be configured to process them. This is accomplished as before. Choose Rulesheet > Processing Mode > Advanced Inferencing from the Studio menubar, as shown previously in Selecting Advanced Inferencing Processing Mode for a Rulesheet.
Here's an example of a multi-rule logical loop:
Figure 147. Example of a Finite Multi-Rule Loop
In the figure above, rule 2 is dependent upon rule 1, and rule 1 is dependent upon rule 2. We've also added a rule 3, which does not participate in the 1—2 loop, but will generate a nice Violation message when the 1—2 loop finally terminates. Note, rule 3 does not cause the 1—2 loop to terminate, it just announces that the loop has terminated. Let's test these rules and see how they behave. In Ruletest for the Multi-rule Rulesheet, we see a simple Ruletest.
Figure 148. Ruletest for the Multi-rule Rulesheet
We're providing a starting value of Cargo.weight just to get the loop going. According to the Condition in rule 1, this value needs to be between 1 and 10 (inclusive).
Figure 149. Ruletest for the Multi-rule Rulesheet
When intentionally building looping rules, it is often helpful to post messages with embedded attribute values (as shown in the Rule Statements section of Figure 147) so we can trace the loop's operation and verify it is behaving as expected. It should be clear to the reader that the Ruletest shown in Ruletest for the Multi-rule Rulesheet contains the expected results.