clipsexpert-systemjess

Increment or change variable each time Jess rule fires


Is there a way to increment or change value of some variable each time rule fires? I need that for every time rule fires introduce new value of the slot, since I will use this value for connectivity in the following example: I have an electric circuit and rule that transforms delta to star. For every transform product is additional node. For simple network I have named it T, but for more complicated network I end up with network that is full of T nodes, since every time rule is triggered for another delta, I get a T node.


Solution

  • Jess has a special kind of variables: global variable. They are visible in all parts of a program, including the RHSs of the rules. To create, use defglobal, e.g.

    (defglobal ?*firecount* = 0)
    

    Note that the asterisks are mandatory.

    You use them just like any other variable:

    (++ ?*firecount*)
    (printout t "fire counter = " ?*firecount* crlf)
    

    Note that not even reset clears a global. - Refer to the Jess manual for additional details.