I want to have a rule that fires for a definstance when it is created and other rules from when it is subsequently updated. The update rules should not fire when the definstance is created, only when it is updated. I can get a rule to first just once for a definstance but the only way I could get a rule to fire only on updates was to use a defquery on the RHS. Is there a better way to do this?

MyObj is the definstance for a bean with id as a property.

; Rule that runs once for a definstance.
; Assert a fact so we never do it twice.
(defrule rule-1
        (MyObj
                (id ?id)
                (OBJECT ?obj)
        )
        (not (id ?id))
=>
        (assert (id ?id))
        (myfunction1 ?obj)      
)

; Rule for updates.
(defrule rule-2
        (MyObj
                (id ?id)
                (OBJECT ?obj)
        )
        (id ?id)
=>
        (bind ?i (count-query-results find-first-id ?id))
        (if (> ?i 0) then
                (myfunction2 ?obj)
                else
                (assert (first-id ?id))
        )
)

(defquery find-first-id
        "Look for first-id fact for id"
        (declare (variables ?id))
        (first-id ?id)
)

--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------



Reply via email to