Ernest,

a small observation. This test example does not work because hashCode has been reimplemented inTest class, not because hash code for Test object (used in HashMap) has been changed. If method hashCode is deleted (or commented out, then everything is OK).
So HashMap uses hashCode() method to retrieve the key. There is no method setHashCode () in Object class.

Dusan Sormaz

At 01:19 PM 1/31/02 -0800, you wrote:

Jess assumes (and in this case, the assumption is unwarranted) that
any objects in the slots of a fact will have constant hashCode values
-- i.e., the hashCode of the contents of a slot will not change while
the object is in a slot. This is true for "value" objects like Strings
and Integers, and, actually, most objects. Some classes work
differently, however. HashMap, like many containers, defines equals()
and hashCode() to both change their results depending on the contents
of the container. This drives Jess nuts, as Jess goes to great lengths
to index things according to their identity and hashCode, and if these
things change unexpectedly, chaos ensues.

You have to read between the lines of manual sections 3.4 and the
second paragraph of 4.7 to see some hint of this; I should probably
spell this out more clearly.

Before you start to think there's something "wrong" with Jess for
behaving this way, note that HashMap itself gets just as upset if you
meddle with its contents. The following program puts some objects into
a HashMap, then changes the hashCode of the keys. When you try to
retrieve those objects using the modified keys, HashMap can't find
them! Once you use an object as a key in a HashMap, you must treat it
as immutable.

You could perhaps make Jess work properly by using a trivial "wrapper"
for the HashMap before putting it into a slot, as long as that wrapper
had a constant hashCode.


----------------------Example program --------------------------
import java.util.HashMap;

public class Test {
    private int m_i;
    public Test(int i) {
        m_i = i;
    }

    public void setHashCode(int i) {
        m_i = i;
    }

    public int hashCode() {
        return m_i;
    }

    public boolean equals(Object o) {
        if (o instanceof Test) {
            return m_i == ((Test) o).m_i;
        } else
            return false;
    }

    public static void main(String[] argv) {

        HashMap map = new HashMap();
        Test t1 = new Test(1);
        Test t2 = new Test(2);
       
        map.put(t1, "tee-one");
        map.put(t2, "tee-two");

        System.out.println(map.get(t1));
        System.out.println(map.get(t2));
       
        t1.setHashCode(9);
        t2.setHashCode(23);
       
        System.out.println(map.get(t1));
        System.out.println(map.get(t2));
    }
}
----------------------------------------------------------------
I think Marc Shlomo L. Hronec wrote:
> Hi,
>
> I've experienced problems with retracting facts under the following
> circumstances: a) when it contained a reference to a java object and b) when
> I accessed the member methods of that object. It can't be retracted but it
> can be modified: that produces two modified copies (!). The same problem
> appears to exist with definstanced facts.
>
> See the sample code and output below:
>
> (clear)
> (reset)
> (defclass map java.util.HashMap)
> (watch all)
> (definstance map (new java.util.HashMap) static)
> (retract 1)
> (facts)
>
> (definstance map (new java.util.HashMap) static)
> (call (fact-slot-value (fact-id 2) OBJECT) put "key1" "val1")
> (call (fact-slot-value (fact-id 2) OBJECT) get "key1")
> (retract 2)
> (facts)
>
> (clear)
> (reset)
> (deftemplate map (slot object))
> (watch all)
> (assert (map (object (new java.util.HashMap))))
> (retract 1)
> (facts)
>
> (assert (map (object (new java.util.HashMap))))
> (call (fact-slot-value (fact-id 2) object) put "key1" "val1")
> (call (fact-slot-value (fact-id 2) object) get "key1")
> (retract 2)
> (facts)
>
> (modify 2 (object nil))
> (facts)
>
>
> ;******** OUTPUT ************
>
> Jess> TRUE
> Jess> java.util.HashMap
> Jess> TRUE
> Jess>  ==> f-1 (MAIN::map (class <External-Address:java.lang.Class>) (empty
> TRUE) (OBJECT <External-Address:java.util.HashMap>))
> <Fact-1>
> Jess>  <== f-1 (MAIN::map (class <External-Address:java.lang.Class>) (empty
> TRUE) (OBJECT <External-Address:java.util.HashMap>))
> TRUE
> Jess> f-0   (MAIN::initial-fact)
> For a total of 1 facts.
> Jess>  ==> f-2 (MAIN::map (class <External-Address:java.lang.Class>) (empty
> TRUE) (OBJECT <External-Address:java.util.HashMap>))
> <Fact-2>
> Jess> Jess> "val1"
> Jess> TRUE
> Jess> f-0   (MAIN::initial-fact)
> f-2   (MAIN::map (class <External-Address:java.lang.Class>) (empty TRUE)
> (OBJECT <External-Address:java.util.HashMap>))
> For a total of 2 facts.
> Jess> TRUE
> Jess> TRUE
> Jess> TRUE
> Jess> TRUE
> Jess>  ==> f-1 (MAIN::map (object <External-Address:java.util.HashMap>))
> <Fact-1>
> Jess>  <== f-1 (MAIN::map (object <External-Address:java.util.HashMap>))
> TRUE
> Jess> f-0   (MAIN::initial-fact)
> For a total of 1 facts.
> Jess>  ==> f-2 (MAIN::map (object <External-Address:java.util.HashMap>))
> <Fact-2>
> Jess> Jess> "val1"
> Jess> TRUE
> Jess> f-0   (MAIN::initial-fact)
> f-2   (MAIN::map (object <External-Address:java.util.HashMap>))
> For a total of 2 facts.
> Jess> <Fact-2>
> Jess> f-0   (MAIN::initial-fact)
> f-2   (MAIN::map (object nil))
> f-2   (MAIN::map (object nil))
> For a total of 3 facts.
> Jess>
>
> --------------------------------------------------------------------
> 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]
--------------------------------------------------------------------

***************************************************************************
* Dusan Sormaz, PhD, Assistant Professor                     
* Ohio University
* Industrial and Manufacturing Systems Engineering Department
* 280 Stocker Center, Athens, OH 45701-2979
* phone: (740) 593-1548
* fax:   (740) 593-0778 
* e-mail: [EMAIL PROTECTED]
* url: http://www.ent.ohiou.edu/~sormaz
***************************************************************************

Reply via email to