Thank you, Jason! Yeap, an OnBeforeRuleFire event is exactly what I was
looking for. :) You can't hide 10+ years of procedural programming
experience can you? :) I had already started implementing my own suggestion
about asserting a "clean-up xyz" fact and its working perfectly. It's very
much in-line with your suggestions and your sample code. And it gives me a
lot more confidence in what I'm doing to see your sample code! 

I had read your "The Zen of Jess II" article before. But, I just read it
again, now that I've actually got a few rules under my belt. I definitely
got a lot more out of it this time. Thanks! Unfortunately, the paragraph
that starts with "If you are approaching Jess from a professional
context..." describes my situation exactly! :) While I'd love to have the
time to know exactly what I'm doing before I do it, I simply don't have that
luxury. But, I am convinced that JESS is the right tool for the problem I'm
trying to solve. While I can imagine writing some procedural code to solve
the problem that I've already solved with a few rules in JESS it would rely
on brute-force loops and comparisons on each and every iteration. And it
wouldn't be at all pretty. So, I'm convinced I'm using the right tool. But,
I'm just going to have to learn the new paradigms as I go. I'll try to keep
the newbie questions to a minimum! But, this list with the support of people
like you and Dr. Friedman-Hill is an invaluable resource! 

Thanks, again for the help!

Eric

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Jason Morris
Sent: Saturday, April 08, 2006 3:17 PM
To: Jess Users Group
Subject: Re: JESS: Firing a rule on a retract?

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

--------------------------------------------------------------------
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