Reputation: 35
For simplicity suppose I have the following two templates:
(deftemplate JessObjA (declare (from-class JessObjA)) )
(deftemplate JessObjB (declare (from-class JessObjB)) )
and a rule which adds a JessObjB into memory whenever a match on a JessObjA happens. In addition to this, I have a list-like object in my Java program which stores these JessObjB's (either the fact-id or reference. I am undecided on this since I do not know which is better for now):
(defrule one-for-one
(logical
?x <- (JessObjA)
)
=>
(bind ?p (new JessObjB ))
(printout t "made a new JessObjB" ?p crlf)
(add ?p)
(bind ?list (fetch someList))
(?list add ?p)
)
I want to keep the contents of someList up to date. I know that if I retract my JessObjA, the fact associated with JessObjB will also be retracted, but both the Fact and the JessObjB objects still remain in Java's memory (if my list holds the references).
What I want to do is to periodically check within my list if any values reference fact identifiers that are no longer in memory. My first thought is to use (fact-id <id>)
, but that actually throws an exception if the fact no longer is in memory. On top of this, it is a slow function, which I would want to avoid.
My only working solution so far is ugly: my list holds the JessObjB
objects, and to check whether it exists as a fact or not, I call Rete.getShadowFactForObject on it, and if I encounter a JessException, then I know it is no longer in memory, so I can remove it from the list.
Is there a more elegant way to do this?
Upvotes: 0
Views: 80
Reputation: 31290
If you need to do an action when some condition is met it's best to write a rule to match the condition. If you have an A but no B, the rule must: (a1) create B, (a2) insert B into the List<>. If you have a B but no A, another rule must: (b1) retract B, (b2) remove B from the List<>. As your rule "one-for-one" doesn't permit the coding of (b2), you cannot really use the "logical" CE.
Keeping the Java list up-to-date is much preferable compared to a solution using two rules.
Upvotes: 0