Implementations of equals() and hashCode() should always agree: http://www.javapractices.com/topic/TopicAction.do?Id=28
--- On Tue, 2/22/11, Simon Chen <[email protected]> wrote: > From: Simon Chen <[email protected]> > Subject: Re: [rules-users] unsolved myth regarding transitive closure using > insertlogical... > To: "Rules Users List" <[email protected]> > Date: Tuesday, February 22, 2011, 2:24 PM > On Sun, Feb 20, 2011 at 10:11 PM, > Mark Proctor <[email protected]> > wrote: > > On 21/02/2011 03:03, Simon Chen wrote: > >> On Sun, Feb 20, 2011 at 8:20 PM, Ansgar Konermann > >> <[email protected]> > wrote: > >>> On 19.02.2011 16:01, Simon Chen wrote: > >>>> The example you gave seems to be the > one-hop case. For the two-hop > >>>> case, we need something like this > >>>> > >>>> when > >>>> edge(a, b), reach(b, c), not > exists reach(a, c) > >>>> then > >>>> insertLogical( reach(a,c) ) > >>>> > >>>> So, where do you put your logical around? > It should include both > >>>> edge(a,b) and reach(b,c), right? > >>>> > >>>> Another thought, can we have something > like > >>>> testExistsAndInsertLogical() to replace > insertLogical()? But this may > >>>> be buggy, as the conditions are all met, > so the rule actually fired... > >>>> > >>> Hi, > >>> > >>> from my experience, insertLogical does exactly > what > >>> testExistsAndInsertLogical would suggest. If > the same object is already > >>> in the working memory, it keeps this object > and does not insert another > >>> instance. This behaviour is not stated > explicitly in the documentation, > >>> but I did a learning test a few weeks ago and > IIRC it clearly showed > >>> this behaviour (at least for 5.0.1). -- I > consider this behaviour a > >>> feature and would like it to be kept this > way. > >> I am using Drools 5.1.1, and I don't think > insertLogical prevents > >> duplicates automatically. This also boils down to > the question of how > >> Drools decides whether two objects are indeed the > same. For strings > >> and integers, it is straightforward, but not much > so for complex > >> objects. Is there a way to pass in a comparison > function? > > InsertLogical operates on equality mode, that is > determined by the > > pojo's equals() method implementation. If an object > already exists that > > is equal, it will use that and the justification > counter for that > > existing object is increased. > > Mark, > > I did a bit debugging using the drools source code, which > is neatly > written btw. I now know what caused my problem. When > logically > inserting an object, the current implementation would > compare the > object with saved EqualityKeys in the > TruthMaintenanceSystem. The > comparison is done through hashCode(), not equals(). So, a > duplicate > (by value) object can be inserted, because the hashcode is > different. > > I am not sure if this is a design decision, but I would > rather using > equals() here. I overrode the hashCode() implementation of > my objects, > and it is working now... > > Thanks. > -Simon > > > > > Mark > >>> With this, all which is necessary to implement > transitive closure is to > >>> remove the contradicting part of the > precondition to avoid oscillation. > >>> If it turns out that insertLogical does not > perform a "does fact already > >>> exist" check and thus might potentially insert > duplicates, put exists( ) > >>> around the two preconditions and also use > "exists( reach(x,y) )" to > >>> check whether y is reachable from x. > >> I don't quite follow. Can you elaborate with an > actual rule? > >> > >> Thanks! > >> -Simon > >> > >>> Kind regards > >>> > >>> Ansgar > >>> > _______________________________________________ > >>> rules-users mailing list > >>> [email protected] > >>> https://lists.jboss.org/mailman/listinfo/rules-users > >>> > >> _______________________________________________ > >> rules-users mailing list > >> [email protected] > >> https://lists.jboss.org/mailman/listinfo/rules-users > >> > >> > > > > > > _______________________________________________ > > rules-users mailing list > > [email protected] > > https://lists.jboss.org/mailman/listinfo/rules-users > > > > _______________________________________________ > rules-users mailing list > [email protected] > https://lists.jboss.org/mailman/listinfo/rules-users > _______________________________________________ rules-users mailing list [email protected] https://lists.jboss.org/mailman/listinfo/rules-users
