I have a series of rules which call a Java user function when they
fire which among other things, sometimes needs to get the list of
facts which caused the rule to fire and then modify a certain slot in
those facts.
 
Perhaps I have a misunderstanding of how the current activation works
((engine) getThisAcivation), but I was under the impression that if
you get the Token from the Activation, you can get at the Facts which
contributed to the current rule firing.
 
I've simulated this with the following simple code, which produces an
exception: 

        (deftemplate Class1
            (slot A)
            (multislot B))
        
        (defrule ModClass1
            (Class1
                (A ?a)
                (B $? ?var1 $?))
        =>
            (bind ?token (((engine) getThisActivation) getToken))
            (bind ?fact1 (?token fact 0))
            (modify ?fact1 (A "123")))
        
        (reset)
        
        (assert
            (Class1
                (A "abc")
                (B "def")))
        
        (run)

When run this, I get the following exception: Fact object not in
working memory (MAIN::Class1 (A "abc") (B "def" )).
 
Evidently, this is because ?fact1 is not identical to the Fact
returned from:
 
        (bind ?fact2 ((engine) findFactByID (?fact1 getFactId)))
 
It turns out that if I modify ?fact2 instead of ?fact1, I'm good to
go.  Can someone please explain to me what is going on here?  Why is
fact2 different from fact1?
 
Here's a version of the code from above that works just fine:
 

        (deftemplate Class1
            (slot A)
            (multislot B))
        
        (defrule ModClass1
            (Class1
                (A ?a)
                (B $? ?var1 $?))
        =>
            (bind ?token (((engine) getThisActivation) getToken))
            (bind ?fact1 (?token fact 0))
            (bind ?fact2 ((engine) findFactByID (?fact1 getFactId)))
            (modify ?fact2 (A "123")))
        
        (reset)
        
        (assert
            (Class1
                (A "abc")
                (B "def")))
        
        (run)

 
Thanks very much,
 
Alan
 
 

Reply via email to