Hello,

I tried this but it still loops:


    public boolean equals(Object o) {
        if(o instanceof OutputObject) {
            OutputObject other = (OutputObject)o;
            if (this.type != other.type) {
                return false;
            }
            if (this.type == OutputObject.Type.LEVEL) {
                if (this.getStringPropertyValue(OutputObject.SEGMENTNAME) != other.getStringPropertyValue(OutputObject.SEGMENTNAME)) {
                    return false;
                }
                return true;
            }
            return true;
        }
        else {
            return false;
        }
    }

    public int hashCode() {
        int hash = 37;
        hash ^= type.hashCode();
        return hash;
    }

If I understand it right I have to overwrite check the equality of all fields which must be identical to for two objects which are seen equal. Fields which can have different values for equal objects don't have to be checked in the equals()-method. In my rule there is an update of the OutputObject:

rule "Assess Level"
  dialect "java"
  no-loop
    when
        io: InputObject(type == InputObject.Type.OBSERVATION)
        ewhsMap: HashMap() from io.getMapParameters()
        ewhs: HashMap() from ewhsMap.get("EWHS")
        segment: String() from ewhs.keySet()
        oo: OutputObject (type == OutputObject.Type.LEVEL, stringParameters["SEGMENTNAME"] == segment)
        ewh: Number() from ewhs.get(segment)       
        eval(ewh.doubleValue() >= 0.5 && ewh.doubleValue() < 3.0)                       
    then
        oo.setIntPropertyValue(OutputObject.SEGMENTLEVEL, "WARNING");
        update(oo);
        System.out.println( "Warning Segment " + segment + " has warning level WARNING");
           
end

Thomas

Greg Barton greg_barton at yahoo.com
Fri Aug 1 12:03:55 EDT 2008

Did you implement equals() and hashCode()?

Implementing them isn't hard.  Implementing them well
can be tricky, but for these purposes a simple
implementation will do.  Basically, with equals() you
want to compare the stuff contained in the objects.
With hashCode() you want to produce an integer that
can be used in HashMaps.  The only restriction is
that, if o1.equals(o2) == true, then o1.hashCode() ==
o2.hashCode().  (The reverse is not necessarily true.)

The easiest way to implement these is to just compare
the fields that define the state of your object:

public class Foo {

  int bar;
  String bas;

  public boolean equals(Object o) {
    if(o instanceof Foo) {
      Foo other = (Foo)o;
      return bar == other.bar && bas != null &&
bas.equals(other.bas);
    } else {
      return false;
    }
  }

  public int hashCode() {
    int hash = 37;
    hash ^= bar;
    if(bas != null) {
      hash ^= bas.hashCode();
    }
    return hash;
  }
}

One good way to make this easier is to use the jakarta
commons EqualsBuilder and HashCodeBuilder classes.
They also help you implement them "well," especially
hashCode.

GreG