Arbitrary is not the same as random. The algorithm is deterministic, but simple implementation changes such as whether an item is added to the beginning or the end of a list can change the results.
In the simple case you've described, once the pattern for A is matched three rules will be activated. Suppose when the rules are defined, they're added to the beginning of a list of rules activated by the pattern A. The final list would be D, C, B since B is added first and D is added last. For depth strategy, new rules are placed at the beginning of the agenda. So when A is asserted, the activations D, C, and B are added to the agenda in that order which results in an agenda of B, C, D. For breadth, new activations are placed at the end. So the activations are added in the same order of D, C, and B, but the final ordering is now D, C, B since they're added to the end one at a time.
If you were to change the internal implementation so that as rules were added to the end of the list of rules activated by pattern A, you'd get the exact opposite behavior with the final ordering for depth being D, C, and B and the final ordering for breadth being B, C, and D.
This is a rather simple case since these rules have the exact same conditions. Because rules can share only some of their patterns, it's difficult to easily determine the order in which multiple rules would be activated by the assertion of a single fact.
On Wednesday, January 30, 2013 6:52:52 AM UTC-6, Xuesong Liu wrote:
Dear all,
I have a question about how CLIPS determine the position of activation. If there are three rules:
Rule B: IF A -> B
Rule C: IF A -> C
Rule D: IF A -> D
If I define the three rules in the order of Rule B, Rule C, Rule D. Then after I assert fact (A) and set the strategy to depth, the agenda is:
Rule B; Rule C; Rule D
If I set the strategy to breadth, the agenda is:
Rule D; Rule C; Rule B
I know that in both case, the three rules should have equal priority and should be fired at an arbitrary order. But after several trials, it appears that CLIPS always choose to fire the most recently defined rule last in depth search, and the most recently defined rule first in the breadth search. I am wondering what logic CLIPS uses to determine this here.
I tried to understand it by assuming that the order of which rule is defined first may also impact the agenda. But if this is the case, then the result does not make sense since the breadth search place the most recently added rule to the first. Please let me know how I should understand the logic here. Thank you very much!
Best wishes,
Pine