clipsexpert-systemclipspy

How can I find out at which step some rules have fired?


I need to know which rules and facts were fired in which inference step.

I used watch rules to print out which rules have fired. But I cannot figure out how to output at which steps the rules have fired.

Can anyone help?


Solution

  • From section 2.5.1 of the Basic Programming Guide

    The actions of applicable rules are executed when the CLIPS inference engine is instructed to begin execution of applicable rules. If more than one rule is applicable, the inference engine uses a conflict resolution strategy to select which rule should have its actions executed. The actions of the selected rule are executed (which may affect the list of applicable rules) and then the inference engine selects another rule and executes its actions. This process continues until no applicable rules remain.

    Or more succinctly:

    1. Pick a rule to fire.
    2. Execute the actions of that rule.
    3. Repeat.

    Pattern matching and conflict resolution (the maintenance of the rules on the agenda) occur as facts are being asserted and retracted in the actions of the rule, so step 1 is really just select the rule at the top of the agenda. There is no gap between when an assert/retract action is processed and the fact is finally asserted/retracted. You can observe this by watching facts and activations:

             CLIPS (6.4 2/9/21)
    CLIPS> 
    (defrule start
       =>
       (assert (a) (b)))
    CLIPS> 
    (defrule have-a
       (a)
       =>
       (assert (d)))
    CLIPS> 
    (defrule have-b
       (b)
       =>
       (assert (e)))
    CLIPS> (watch rules)
    CLIPS> (watch facts)
    CLIPS> (watch activations)
    CLIPS> (run)
    FIRE    1 start: *
    ==> f-1     (a)
    ==> Activation 0      have-a: f-1
    ==> f-2     (b)
    ==> Activation 0      have-b: f-2
    FIRE    2 have-b: f-2
    ==> f-3     (e)
    FIRE    3 have-a: f-1
    ==> f-4     (d)
    CLIPS>