JESS: Java object inside slots -- checking matches

classic Classic list List threaded Threaded
3 messages Options
hlc
Reply | Threaded
Open this post in threaded view
|

JESS: Java object inside slots -- checking matches

hlc
Hi,

I have 2 deftemplates, each of which includes a slot with a Java object.
I want to have a rule that fires when the objects inside those slots are
equal. This is my implementation:

(deftemplate t1 (slot o))
(deftemplate t2 (slot o))

(defrule r1
    (t1 (o ?obj))
    (t2 (o ?obj))
    =>
    (printout t "--r1--->" (call ?obj getX) crlf) )

(defrule r2
    (t1 (o ?obj1))
    (t2 (o ?obj2))
    (test (= (call ?obj1 equals ?obj2) TRUE))
    =>
    (printout t "--r2--->" (call ?obj1 getX) crlf) )

(reset)
(assert (t1 (o (new Fact 1))))
(assert (t2 (o (new Fact 1))))
(facts)
(run)

As can be easily seen, my Fact class has an x attribute accessible via
getX(). I have also implemented equals().
Rule r2 works fine.

I was just trying to better understand the contents of section 8.4 of
the manual, where it is said that "Jess may call the equals and hashCode
methods of any objects in working memory". This does not mean that rule
r1 should fire, right? Rule r1 really demands for the references to
refer to the same object, not equal objects, am I right?
So, in this case should I implement hashCode()? It does not seem to be
necessary. Is it crucial only when using shadow facts?

Thanks.

Henrique



--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: JESS: Java object inside slots -- checking matches

Friedman-Hill, Ernest
Jess will call Fact.equals() to evaluate the first rule -- if Fact  
doesn't override equals(), then of course Object's version is used,  
which is based on identity. Jess may also call Fact.hashCode() as part  
of computing a hashcode for the jess.Fact objects.


On Jul 12, 2010, at 4:56 AM, Henrique Lopes Cardoso wrote:

> Hi,
>
> I have 2 deftemplates, each of which includes a slot with a Java  
> object.
> I want to have a rule that fires when the objects inside those slots  
> are
> equal. This is my implementation:
>
> (deftemplate t1 (slot o))
> (deftemplate t2 (slot o))
>
> (defrule r1
>    (t1 (o ?obj))
>    (t2 (o ?obj))
>    =>
>    (printout t "--r1--->" (call ?obj getX) crlf) )
>
> (defrule r2
>    (t1 (o ?obj1))
>    (t2 (o ?obj2))
>    (test (= (call ?obj1 equals ?obj2) TRUE))
>    =>
>    (printout t "--r2--->" (call ?obj1 getX) crlf) )
>
> (reset)
> (assert (t1 (o (new Fact 1))))
> (assert (t2 (o (new Fact 1))))
> (facts)
> (run)
>
> As can be easily seen, my Fact class has an x attribute accessible via
> getX(). I have also implemented equals().
> Rule r2 works fine.
>
> I was just trying to better understand the contents of section 8.4 of
> the manual, where it is said that "Jess may call the equals and  
> hashCode
> methods of any objects in working memory". This does not mean that  
> rule
> r1 should fire, right? Rule r1 really demands for the references to
> refer to the same object, not equal objects, am I right?
> So, in this case should I implement hashCode()? It does not seem to be
> necessary. Is it crucial only when using shadow facts?
>
> Thanks.
>
> Henrique
>
>
>
> --------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users  
> [hidden email]'
> in the BODY of a message to [hidden email], NOT to the list
> (use your own address!) List problems? Notify [hidden email]
> .
> --------------------------------------------------------------------

---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences, Sandia National Laboratories
PO Box 969, MS 9012, Livermore, CA 94550
http://www.jessrules.com







--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------

hlc
Reply | Threaded
Open this post in threaded view
|

Re: JESS: Java object inside slots -- checking matches

hlc
Thanks!
Actually there was a bug in my Fact.equals() method, as it was receiving
a Fact instead of an Object :p
I also confirmed that I must implement Fact.hashCode() as well, so that
rule r1 works properly.

Henrique


Ernest Friedman-Hill wrote:

> Jess will call Fact.equals() to evaluate the first rule -- if Fact
> doesn't override equals(), then of course Object's version is used,
> which is based on identity. Jess may also call Fact.hashCode() as part
> of computing a hashcode for the jess.Fact objects.
>
>
> On Jul 12, 2010, at 4:56 AM, Henrique Lopes Cardoso wrote:
>
>> Hi,
>>
>> I have 2 deftemplates, each of which includes a slot with a Java object.
>> I want to have a rule that fires when the objects inside those slots are
>> equal. This is my implementation:
>>
>> (deftemplate t1 (slot o))
>> (deftemplate t2 (slot o))
>>
>> (defrule r1
>>    (t1 (o ?obj))
>>    (t2 (o ?obj))
>>    =>
>>    (printout t "--r1--->" (call ?obj getX) crlf) )
>>
>> (defrule r2
>>    (t1 (o ?obj1))
>>    (t2 (o ?obj2))
>>    (test (= (call ?obj1 equals ?obj2) TRUE))
>>    =>
>>    (printout t "--r2--->" (call ?obj1 getX) crlf) )
>>
>> (reset)
>> (assert (t1 (o (new Fact 1))))
>> (assert (t2 (o (new Fact 1))))
>> (facts)
>> (run)
>>
>> As can be easily seen, my Fact class has an x attribute accessible via
>> getX(). I have also implemented equals().
>> Rule r2 works fine.
>>
>> I was just trying to better understand the contents of section 8.4 of
>> the manual, where it is said that "Jess may call the equals and hashCode
>> methods of any objects in working memory". This does not mean that rule
>> r1 should fire, right? Rule r1 really demands for the references to
>> refer to the same object, not equal objects, am I right?
>> So, in this case should I implement hashCode()? It does not seem to be
>> necessary. Is it crucial only when using shadow facts?
>>
>> Thanks.
>>
>> Henrique



--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------