javarulesjess

How to update facts in Jess Rules Engine?


In Jess, suppose my Factbase consists of several name-value pairs like

(node1 6.5) (node2 100) (node3 0.5)

How can I achieve to "update" the values for example the "node1" value from 6.5 to 100 without manually retracting it?

Also, is there a way to "reassert" all the facts, after a certain rule is fired?


Solution

  • You use Jess's (modify) function to modify facts. As explained in the manual, ordered facts like (node1 6.5) are internally represented as unordered facts with a single slot named __data, so you can exploit that knowledge to modify them:

    Jess> (assert (node1 6.5))
    <Fact-0>
    Jess> (modify 0 (__data 7.5))
    <Fact-0>
    Jess> (facts)
    f-0   (MAIN::node1 7.5)
    For a total of 1 facts in module MAIN.
    

    Now, given all that, for efficiency I'd still recommend that you use an unordered template like

    (node (slot index) (slot value))
    

    so that your facts look like

    (node (index 1) (value 6.5))
    (node (index 2) (value 100))
    

    As regards your last question: I'm not quite sure what you mean, but you might look up the (deffacts) construct and the (reset) function and see if together those will do what you need.