I have a similar problem and I solve it by having a pool
of threads that each have their own rete instance.
I instantiate an instance from a serialized version
on initialization. The rete object now has the rules
I need.
I then assert the first fact(s) that starts everything
going and then get the result. I then call (reset)
and reset storage (?) and as far as I know that sets
everything back to before the first fact was asserted.
This works well for me as I respond to web requests.
I have my own java web server that has a thread pool.
I give each of these threads a rete object on
initialization.
This might work for you.
John.
----Original Message Follows----
The best thing to do, if you can, is simply to write the rules so they
don't leave any extra facts behind; i.e., if one rule asserts some
fact, then some other rule matches it and deletes it. That's going to
be the easiest and most efficient solution in the long run.
Cloning a Rete object is always going to be slow, because it's not one
object -- it's a network of potentially thousands of interconnected
objects.
If, though, you absolutely can't get the rules to clean up after
themselves, I've got a nifty piece of code written by a user that
implements "unwind/protect." Unfortunately I can't give credit because
I don't remember where it came from anymore -- if the author is
listening, speak up, please. Anyway, here are the functions. There are
different versions for Jess 5 and Jess 6 (-5 and -6.) I haven't used
this in a while so it's possible it might need tweaking.
(deffunction protect ()
(bind ?p (assert (protect-to-here)))
(store PROTECT ?p))
(deffunction unwind-6 ()
(bind ?p (call (fetch PROTECT) getFactId))
(bind ?v (new java.util.Vector))
(bind ?facts (call (engine) listFacts))
(while (call ?facts hasMoreElements)
(bind ?fact (call ?facts nextElement))
(if (< ?p (call ?fact getFactId)) then
(?v addElement ?fact)))
(bind ?facts (call ?v elements))
(while (call ?facts hasMoreElements)
(bind ?fact (call ?facts nextElement))
(retract ?fact)))
(deffunction unwind-5 ()
(bind ?p (fetch PROTECT))
(bind ?v (new java.util.Vector))
(bind ?facts (call (engine) listFacts))
(while (call ?facts hasMoreElements)
(bind ?fact (call ?facts nextElement))
(if (< ?p (call ?fact getFactId)) then
(?v addElement ?fact)))
(bind ?facts (call ?v elements))
(while (call ?facts hasMoreElements)
(bind ?fact (call ?facts nextElement))
(retract (call ?fact getFactId))))
;; Example of using this
(assert (foo 1) (foo 2) (foo 3))
(protect)
;; This displays only the foo facts
(facts)
(assert (boo 1) (boo 2) (boo 3))
;; see six facts here
(facts)
(unwind-6)
;; see only the foo facts again.
(facts)
I think Joe Kopena wrote:
>
> I'm curious if anyone has any suggestions on handling the following
> situation:
>
> I have a knowledge base in my program, the Jess Rete object. The program
> receives a message and feeds it into the knowledge base so it can be
> interpreted properly. The program then looks for certain facts in the
> knowledge base (ie: a query defined by the message) and acts
> accordingly. Once the message is processed, I want to rollback the rete
> to the state it was before the message came in so the rete won't grow
> too big as I continue to receive messages. Complicating things, messages
> might be coming in concurrently. I understand that the rete's methods
> and parts are synchronized and thread safe (correct?). However, it does
> mean that I can't do something simple like count the number of facts
> before the message, count the number afterwards and subtract the
> difference from the tail of the facts list. I was thinking of cloning
> the rete for the message handler so it can't touch the original. This
> sounds potentially slow, but not nearly as slow as reading in all the
> facts to initialize the kb. It does sound difficult though. It's not
> clear to me just what that would entail, how many links the rete has and
> so forth. Would it be not worth the effort and easier to just assert a
> ton (couple hundred) facts into a new Rete everytime a message comes in?
>
>
> ---------------------------------------------------------------------
> 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]
> ---------------------------------------------------------------------
>
---------------------------------------------------------
Ernest Friedman-Hill
Distributed Systems Research Phone: (925) 294-2154
Sandia National Labs FAX: (925) 294-2234
Org. 8920, MS 9012 [EMAIL PROTECTED]
PO Box 969 http://herzberg.ca.sandia.gov
Livermore, CA 94550
---------------------------------------------------------------------
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]
---------------------------------------------------------------------
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com
---------------------------------------------------------------------
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]
---------------------------------------------------------------------