[rules-users] Drools' use of hashCode

Wolfgang Laun wolfgang.laun at gmail.com
Tue Jun 21 08:58:28 EDT 2011


Assert behaviour merely defines the engine's reaction w.r.t. to the
insertion of another fact that might be considered being "the same as" one
that's already in the WM. This is due to the simple truth that facts
represent true propositions (and that you aren't the
Bellman<http://www.theotherpages.org/poems/carrol03.html>.)


For hashCode() and equals() you must follow the usual Java rules.

-W


On 21 June 2011 14:29, M. H. <hugues_81 at hotmail.com> wrote:
> Hi all,
>
> I have encountered a situation I don't quite understand, I would be happy
to
> have some explanations about it. This is my first post here and I'm quite
> new to all this so don't be afraid to go back to basics if I'm missing
> something:
>
> I have 3 classes :
>
> public class Child
> {
>        private Date time;
> +get
> +set
> +constructor
> }
>
> public class Father
> {
>        private Child child;
>        private int value;
> +get
> +set
> +constructor
>        public void setTime(Date d)
>        {
>                child.setTime(d);
>        }
> }
>
> public class GdFather
> {
>        private Father father;
> +get
> +set
> +constructor
> }
>
> then 2 rules :
>
> rule "1"
>        when
>                $father : Father(value==1)
>        then
>                $father.setTime(new Date(2));
>                $father.setValue(0);
>                update($father);
> end
>
> rule "2"
>        when
>                $father : Father()
>                $GdFather : GdFather(father==$father)
>        then
>                System.out.println($father);
> end
>
> ////////////////////////////////////////
>
> I didn't change the identity assert behavior, I insert a father and the
> corresponding GdFather in the workingMemory, and all seems to work OK.
> The issue occurs when I override the hashCode function in the Father class
:
> if I use this definition :
> @Override
>        public int hashCode()
>        {
>                return child.getTime().hashCode();
>        }
> then rule 2 is not fired after modification of the father, and is not
either
> if I change the function to return child.hashCode() with a Child hashCode
> returning time.hashCode().
> This also happens with a properly overriden equals function, and both
Drools
> 5.1.1 and 5.2.0.CR1.
> What I don't understand is why is it using the overriden hashCode function
> with an identity assert behavior? What is it used for? And it looks like
it
> is using the overriden hashCode, but not equals, is that correct, and if
so,
> why?
> How can I get the second rule to fire and have a custom hashCode() ?
>
> Thank you in advance for your answers,
>
>
> M.H.
>
> --
> View this message in context:
http://drools.46999.n3.nabble.com/Drools-use-of-hashCode-tp3090419p3090419.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> 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/20110621/8f7b3c17/attachment.html 


More information about the rules-users mailing list