[rules-users] Re.Problems with looping

thomas kukofka thomaskukofka at web.de
Mon Aug 4 13:15:22 EDT 2008


*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
<rules-users%40lists.jboss.org?Subject=%5Brules-users%5D%20Problems%20with%20looping&In-Reply-To=d8ac9d7f0808010810x45d911d7x98f70b7cd7ab9b6%40mail.gmail.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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080804/6450358d/attachment.html 


More information about the rules-users mailing list