<HTML >
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">



<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7653.38">
<TITLE>Re: [rules-users] Update vs insert</TITLE>
</HEAD>
<BODY >
<DIV>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>WM uses IdentityHashMap to maintain asserted facts. This would mean that if you try to re-insert facts already present in working memory, the re-insert will fail silently.<BR>
<BR>
Drawing a corrollary to database, if you try to re-insert a row already present in the table you would get primary key violation exception.<BR>
<BR>
Please check Sun's API on java.util.IdentityHashMap for further details.<BR>
<BR>
<BR>
----- Original Message -----<BR>
From: rules-users-bounces@lists.jboss.org &lt;rules-users-bounces@lists.jboss.org&gt;<BR>
To: Rules Users List &lt;rules-users@lists.jboss.org&gt;<BR>
Sent: Thu Sep 11 20:53:42 2008<BR>
Subject: RE: [rules-users] Update vs insert<BR>
<BR>
I agree that update is the proper route to take. This question is more<BR>
about my understanding of drools. My theory was insert would work as<BR>
long as my hashcode and equals methods are overriden correctly, which I<BR>
believe I have done. Since that didn't work, I thought there was<BR>
something fundemental about drools that I didn't understand.<BR>
<BR>
My first assumption was something with shadow facts was causing my<BR>
problems. I am using Drools 5 and another user, Micheal, mentioned<BR>
Drools 5 does not use shadow facts. If that's the case I am further<BR>
confused as to why it didn't work.<BR>
<BR>
Below is the code I am using. It's a pretty simple use case. In the<BR>
code, I pull the fact out of working memory before I run the query, and<BR>
look at the class name. Its been updated properly(i.e it equals Math),<BR>
but the query still doesn't return the Person fact. Is there something<BR>
about the query that relies on a property change listener firing before<BR>
it will re-analyze working memory to get results.<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String name = &quot;Dan&quot;;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Person daniel = new Person(name, &quot;M&quot;, 29);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; daniel.setLikesMath(true);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Person Chris = new Person(&quot;Chris&quot;, &quot;M&quot;, 34);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Chris.setLikesMath(true);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Person Debbie = new Person(&quot;Deb&quot;, &quot;F&quot;, 34);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debbie.setLikesMath(false);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FactHandle danielHandle = DroolsUtil.insertOrUpdate(daniel);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DroolsUtil.insertOrUpdate(Chris);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DroolsUtil.insertOrUpdate(Debbie);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DroolsUtil.fireAllRules();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Person extractedDaniel =<BR>
(Person)DroolsUtil.getObject(danielHandle);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // This line prints out Math<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;Dans class: &quot; +<BR>
extractedDaniel.getClassName());<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;Person&gt; students =<BR>
<BR>
(List&lt;Person&gt;)DroolsUtil.getQueryResults(&quot;getPeopleForClass&quot;, &quot;Math&quot;);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DroolsUtil.fireAllRules();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;Number of students: &quot; + students.size());<BR>
<BR>
Thanks again for any help.<BR>
<BR>
Daniel Quinn<BR>
Fedex - Custom Critical<BR>
Software Specialist I<BR>
234.310.4090(x2586)<BR>
<BR>
-----Original Message-----<BR>
From: rules-users-bounces@lists.jboss.org<BR>
[<A HREF="mailto:rules-users-bounces@lists.jboss.org">mailto:rules-users-bounces@lists.jboss.org</A>] On Behalf Of Ingomar Otter<BR>
Sent: Thursday, September 11, 2008 6:53 AM<BR>
To: Rules Users List<BR>
Subject: Re: [rules-users] Update vs insert<BR>
<BR>
Ehm, I would think that unless person implements a<BR>
PropertyChangeListener, you need to tell the engine about the change of<BR>
the slot.<BR>
Update would be the proper way AFAIK.<BR>
<BR>
In this case, I don't see why one would&nbsp; want to insert an object into<BR>
WM - as it is already in WM.<BR>
I think the exact result of the insert depends on whether you have<BR>
implemented equals/hashCode properly.<BR>
But again, in that example I understand that you want to change the<BR>
property of an object you have matched&nbsp; in the LHS.<BR>
The way to do this is using update().<BR>
<BR>
-- Ingomar<BR>
<BR>
Am 11.09.2008 um 12:39 schrieb Shyam, Pallav (MSCIBARRA):<BR>
<BR>
&gt; Very strange. The facts are passed into the WM by-reference.&nbsp;<BR>
&gt; Therefore the query should work after calling the<BR>
&gt; person.setClassName(&quot;Math&quot;) on the fact. And this should work without<BR>
&gt; calling the insert or update.<BR>
&gt;<BR>
&gt; This leaves me guessing that the query does not work on the facts<BR>
&gt; directly, instead they work on shadow facts.<BR>
&gt;<BR>
&gt;<BR>
&gt;<BR>
&gt;<BR>
&gt; ________________________________<BR>
&gt;<BR>
&gt; From: rules-users-bounces@lists.jboss.org<BR>
&gt; To: Rules Users List<BR>
&gt; Sent: Thu Sep 11 04:41:37 2008<BR>
&gt; Subject: [rules-users] Update vs insert<BR>
&gt;<BR>
&gt;<BR>
&gt; Hello,<BR>
&gt;<BR>
&gt;&nbsp;&nbsp; What is the difference between calling insert and update in a rule<BR>
&gt; file? When I use insert my queries don't work, but if I use update<BR>
&gt; they seem to. I don't mind using update, but I would like to<BR>
&gt; understand the difference between the two. Here is my rule file.<BR>
&gt;<BR>
&gt;&nbsp;&nbsp;&nbsp; rule &quot;Add person to math class&quot;<BR>
&gt;&nbsp;&nbsp;&nbsp; no-loop<BR>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; when<BR>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; person : Person(likesMath == true)<BR>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; then<BR>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; person.setClassName(&quot;Math&quot;);<BR>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert(person);<BR>
&gt;&nbsp;&nbsp;&nbsp; end<BR>
&gt;<BR>
&gt;&nbsp;&nbsp; I also have a query:<BR>
&gt;<BR>
&gt;&nbsp;&nbsp; query getPeopleForClass(String _className)<BR>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Person(className == _className)<BR>
&gt;&nbsp;&nbsp; end<BR>
&gt;<BR>
&gt;&nbsp;&nbsp; When I add two facts to working memory, where the likesMath<BR>
&gt; attribute is true, then run my query I get zero results back. When I<BR>
&gt; change the rule, &quot;Add person to math class&quot;, to use update(person)<BR>
&gt; instead of insert(person) two students are returned.<BR>
&gt;<BR>
&gt;&nbsp; Any help would be appreciated.<BR>
&gt;<BR>
&gt; Thanks,<BR>
&gt; Dan<BR>
&gt;<BR>
&gt;<BR>
&gt; Daniel Quinn<BR>
&gt; Fedex - Custom Critical<BR>
&gt; Software Specialist I<BR>
&gt; 234.310.4090(x2586)<BR>
&gt;<BR>
&gt; -----Original Message-----<BR>
&gt; From: rules-users-bounces@lists.jboss.org<BR>
&gt; [<A HREF="mailto:rules-users-bounces@lists.jboss.org">mailto:rules-users-bounces@lists.jboss.org</A><BR>
&gt;&nbsp; &lt;<A HREF="mailto:rules-users-bounces@lists.jboss.org">mailto:rules-users-bounces@lists.jboss.org</A>&gt; ] On Behalf Of<BR>
&gt; thomaskukofka@web.de<BR>
&gt;<BR>
&gt; Sent: Wednesday, September 10, 2008 2:27 PM<BR>
&gt; To: rules-users@lists.jboss.org<BR>
&gt; Subject: [rules-users] Re: looping problem<BR>
&gt;<BR>
&gt; Hello,<BR>
&gt;<BR>
&gt; thanks that works. I just forgot to set the focus in the init rule..<BR>
&gt;<BR>
&gt; Regards<BR>
&gt; Thomas<BR>
&gt; _______________________________________________<BR>
&gt; rules-users mailing list<BR>
&gt; rules-users@lists.jboss.org<BR>
&gt; <A HREF="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</A><BR>
&gt; &lt;https://lists.jboss.org/mailman/listinfo/rules-users<BR>
&gt; &gt;<BR>
&gt; --------------------------------------------------------<BR>
&gt;<BR>
&gt; NOTICE: If received in error, please destroy and notify sender.&nbsp;<BR>
&gt; Sender does not intend to waive confidentiality or privilege. Use of<BR>
&gt; this email is prohibited when received in error.<BR>
&gt; _______________________________________________<BR>
&gt; rules-users mailing list<BR>
&gt; rules-users@lists.jboss.org<BR>
&gt; <A HREF="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</A><BR>
<BR>
<BR>
_______________________________________________<BR>
rules-users mailing list<BR>
rules-users@lists.jboss.org<BR>
<A HREF="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</A><BR>
<BR>
_______________________________________________<BR>
rules-users mailing list<BR>
rules-users@lists.jboss.org<BR>
<A HREF="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</A><BR>
</FONT>
</P>

</DIV>
<DIV>
<HR>
</DIV>
<P CLASS="BulletedList" STYLE="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0in; mso-list: none; tab-stops: .5in"><SPAN STYLE="FONT-SIZE: 8pt; COLOR: gray; mso-bidi-font-family: Arial"><FONT FACE="Arial" COLOR="gray" SIZE="1">NOTICE: If received in error, please destroy and notify sender. Sender does not intend to waive confidentiality or privilege. Use of this email is prohibited when received in error.</FONT></SPAN></P>
<DIV>
</DIV></BODY></HTML>