« Return to Thread: Re: JESS: getting a handle on fact in LHS

Re: JESS: getting a handle on fact in LHS

by Ernest Friedman-Hill :: Rate this Message:

Reply to Author | View in Thread

Using a query to find the existing fact is one way to do it; if you're  
not seeing the actual fact in the slot afterwards, it's probably just  
a coding error in using the query interface. For a query like

(defquery find-some-fact
     (declare (variables ?a ?b))
     ?f <- (some-fact (a ?a) (b ?b)))

Then you'd get the fact, if it exists, like

(bind ?result (run-query* find-some-fact A B))
(if (?result next) then
     (bind ?fact (?result get ?f))
  else
     (bind ?fact (assert (some-fact (a A) (b B)))))

You could put all that into a deffunction so it wouldn't detract from  
the flow of your other code.

An alternative way to do all this would be to have two versions of the  
original rule: one with a "not" pattern for the sub-fact and one with  
a positive pattern. The "not" one should create the subfact, while the  
other can directly use the matched fact.

On Jun 29, 2009, at 7:12 AM, axelrad wrote:

>
> Hello,
>
> I have a template that has a fact as a slot.
>
> ex)
>
> (deftemplate mainfact
> (slot subfact (type OBEJCT))
> (slot index))
>
> (deftemplate subfact
> (slot name)
> (slot content))
>
> I have a rule that instantiates (assert) the mainfact and subfact if  
> my
> condition is met. My problem is that if the subfact exists already  
> it gets
> asserted to false. What I need is to get a handle on the fact if it  
> exists
> and assert the mainfact with the existing subfact and a new current  
> index.
> What I do now is along the lines of:
>
> assert mainfact (subfact xxx) (index n)
>
> works great if the subfact doesn't exist yet: mainfact xxx n
> but if the subfact exists my result is: mainfact (false) (n)
>
> what i need is : mainfact (existing fact) (n)
>
> I need to compare the subfacts for when they are the same and keep  
> the one
> with the higher index value. I've tried doing a query and putting  
> that value
> in but i get a jess query object instead of a fact object, and later  
> when i
> need to compare the values they are not considered the same object,  
> even if
> the slot values are correct, so I don't get the correct results when  
> trying
> to keep the mainfact with higher index value.
>
> I can test if the fact exists already, but how can I get a handle on  
> it to
> place that value in the slot?
>
> Thanks in advance for any help!
>
> Monica
> --
> View this message in context: http://www.nabble.com/getting-a-handle-on-fact-in-LHS-tp24252267p24252267.html
> Sent from the Jess mailing list archive at Nabble.com.
>
>
>
> --------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users  
> you@...'
> in the BODY of a message to majordomo@..., NOT to the list
> (use your own address!) List problems? Notify owner-jess-users@...
> .
> --------------------------------------------------------------------

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences          Phone: (925) 294-2154
Sandia National Labs
PO Box 969, MS 9012                            ejfried@...
Livermore, CA 94550                             http://www.jessrules.com





--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users you@...'
in the BODY of a message to majordomo@..., NOT to the list
(use your own address!) List problems? Notify owner-jess-users@....
--------------------------------------------------------------------

 « Return to Thread: Re: JESS: getting a handle on fact in LHS