On 4/7/06, Eric W. Bonnett <[EMAIL PROTECTED]> wrote:
> Is there a simple way to fire a rule when a fact is about to be retracted?
I
> have a case where references to a fact are stored in other facts. So, when
> the first fact is retracted, I'd like to be able to fire a rule to go
> clean-up the references to it. Is there a simple way to do that? I tried
> putting a not statement around a match for the fact, but it doesn't seem
to
> fire. Plus, I'll need to access the fact itself before it's actually gone.
I
> need a trigger that it's about to be removed, so I can access its fields.


Hi Eric,

Generally you want to refrain from trying to force Jess (or any rule engine)
to fire a particular rule at a particular time.  The best approach is to
assert a fact that initiates a state change, and then let the rule engine
process it in due course.

I suspect that you are looking for a JavaScript-like OnBeforeRuleFire event
or something, but there isn't one for a good reason.  Activated rules can be
removed from the agenda (deactivated) by new fact assertions.  It would be
unwise to initiate any other actions based on a particular rule firing until
it actually fires.

One way to get around this is to do your cascading delete in a few stages,
not unlike how databases maintain referential integrity among linked tables:

1.  Add a fact template that supplies trigger facts for the delete process
you want.
2.  Add a rule that handles the cascading delete (see example below).
3.  Assert a fact that triggers a state change.  In this case, you want to
delete a particular fact that might be referenced by other facts, so you
include the fact reference in the trigger fact.
4. Run the rule engine on the new (dirty) state to clean it up.

A sample program is attached (zapfacts.clp)
I hope this helps.... it's just the first approach that came to mind.
Others might have more elegant solutions.

BTW - you might find some useful hints here
http://www.jessrules.com/jess/zen.shtml

Cheers,
Jason

-----------------------------------------------------
Morris Technical Solutions LLC
www.morristechnicalsolutions.com
[EMAIL PROTECTED]
phone/fax: 503.692.1088

Attachment: zapfacts.clp
Description: Binary data

Reply via email to