[rules-users] Truth Maintenance, equals, and hashCode
Mark Proctor
mproctor at codehaus.org
Tue Aug 5 18:44:24 EDT 2008
ringsah at comcast.net wrote:
> I have a question about logical assertions and truth maintenance.
> First of all, the Drools manual states:
>
>
>
> 2.5.8.2. Important note: Equality for Java objects
>
> It is important to note that for Truth Maintenance (and logical
> assertions) to work at all, your Fact objects (which may be Javabeans)
> override equals and hashCode methods (from java.lang.Object)
> correctly. As the truth maintenance system needs to know when 2
> different physical objects are equal in value, BOTH equals and
> hashCode must be overridden correctly, as per the Java standard.
>
> Two objects are equal if and only if their equals methods return true
> for each other and if their hashCode methods return the same values.
> See the Java API for more details (but do keep in mind you MUST
> override both equals and hashCode).
>
> Does this mean that in order to use logical assertions that I /have
> /to override equals and hashCode?
>
TMS will work if you don't override logical/hashcode but it just means
it will always created a new logical relationship relationship. The nice
thing about impl equals and hashcode is it means if you try and logical
assert an object which is equal, it'll ignore it.
>
> What if I have a class that I would like to use as fact in a logical
> assertions, but for this class equality should be the same as
> identity. In other words, I don't want to override equals and
> hashCode, because the versions of these methods inherited from Object
> have the correct behavior for my application. Will this class still be
> okay to use as facts in logical assertions?
>
It will work ok, but you need to make sure you understand the role of
equals/hashcode to stop fact duplication - it's pretty much like the
difference between an IdentityMap and HashMap.
>
> Thanks,
> -Hans
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080805/42e17126/attachment.html
More information about the rules-users
mailing list