There is a bug here though if the hashcodes are equals.

You need to make it 
 $w : Widget( $name : name )
     Widget( this != $w, hashCode <= $w.hashCode, name == $name )

And live with the fact there is a chance that the rule does fire twice - which 
is probably a better scenario that it failing to fire at all.  Be particularly 
careful if your widget overrides the equals() method as it should also override 
the hashcode operator to make it the case that if a.equals(b) then 
a.hashcode()==b.hashcode().

Thomas
> -----Original Message-----
> From: [email protected] [mailto:rules-users-
> [email protected]] On Behalf Of D Brock
> Sent: 07 November 2009 16:04
> To: [email protected]
> Subject: Re: [rules-users] Suppressing reciprocal matches
> 
> 
> Thanks Mark, sounds good.  And the hashCode trick works.  Wow, I would
> have
> never thought of that!
> 
> 
> Mark Proctor wrote:
> >
> > D Brock wrote:
> >> I have defined the following rule to detect when duplicate objects
> (in
> >> this
> >> example, Widgets) exist.  This always produces two matches, and
> therefore
> >> two messages.  I understand why this occurs, but I would like for
> only
> >> one
> >> message to be reported.  I cannot retract one of the widgets after a
> >> match
> >> b/c all are needed for additional rules matching.  Is the a
> conventional
> >> way
> >> for accomplishing the elimination of the reciprocal match?
> >>
> >> rule "Do not allow duplicate widgets"
> >>    when
> >>            $w : Widget( $name : name )
> >>            Widget( this != $w, name == $name )
> >>    then
> >>            System.out.println( "Duplicate widgets found with name =  "
> + $name );
> >> end
> >>
> > We are looking into a new conditional element that can achieve this.
> The
> > idea is when you use this CE, it ensures only a single permutation
> will
> > be propagated. In the meantime you can probably try something like:
> >
> > $w : Widget( $name : name )
> >      Widget( this != $w, hashCode < $w.hashCode, name == $name )
> >
> >
> >
> >
> > _______________________________________________
> > rules-users mailing list
> > [email protected]
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
> 
> --
> View this message in context: http://old.nabble.com/Suppressing-
> reciprocal-matches-tp26230834p26241896.html
> Sent from the drools - user mailing list archive at Nabble.com.
> 
> _______________________________________________
> rules-users mailing list
> [email protected]
> https://lists.jboss.org/mailman/listinfo/rules-users

**************************************************************************************
This message is confidential and intended only for the addressee. If you have 
received this message in error, please immediately notify the 
[email protected] and delete it from your system as well as any copies. The 
content of e-mails as well as traffic data may be monitored by NDS for 
employment and security purposes. To protect the environment please do not 
print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, 
United Kingdom. A company registered in England and Wales. Registered no. 
3080780. VAT no. GB 603 8808 40-00
**************************************************************************************

_______________________________________________
rules-users mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to