<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 <rules-users-bounces@lists.jboss.org><BR>
To: Rules Users List <rules-users@lists.jboss.org><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>
String name = "Dan";<BR>
Person daniel = new Person(name, "M", 29);<BR>
daniel.setLikesMath(true);<BR>
<BR>
Person Chris = new Person("Chris", "M", 34);<BR>
Chris.setLikesMath(true);<BR>
<BR>
Person Debbie = new Person("Deb", "F", 34);<BR>
Debbie.setLikesMath(false);<BR>
<BR>
FactHandle danielHandle = DroolsUtil.insertOrUpdate(daniel);<BR>
DroolsUtil.insertOrUpdate(Chris);<BR>
DroolsUtil.insertOrUpdate(Debbie);<BR>
<BR>
DroolsUtil.fireAllRules();<BR>
<BR>
Person extractedDaniel =<BR>
(Person)DroolsUtil.getObject(danielHandle);<BR>
<BR>
// This line prints out Math<BR>
System.out.println("Dans class: " +<BR>
extractedDaniel.getClassName());<BR>
<BR>
List<Person> students =<BR>
<BR>
(List<Person>)DroolsUtil.getQueryResults("getPeopleForClass", "Math");<BR>
DroolsUtil.fireAllRules();<BR>
<BR>
System.out.println("Number of students: " + 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 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 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>
> Very strange. The facts are passed into the WM by-reference. <BR>
> Therefore the query should work after calling the<BR>
> person.setClassName("Math") on the fact. And this should work without<BR>
> calling the insert or update.<BR>
><BR>
> This leaves me guessing that the query does not work on the facts<BR>
> directly, instead they work on shadow facts.<BR>
><BR>
><BR>
><BR>
><BR>
> ________________________________<BR>
><BR>
> From: rules-users-bounces@lists.jboss.org<BR>
> To: Rules Users List<BR>
> Sent: Thu Sep 11 04:41:37 2008<BR>
> Subject: [rules-users] Update vs insert<BR>
><BR>
><BR>
> Hello,<BR>
><BR>
> What is the difference between calling insert and update in a rule<BR>
> file? When I use insert my queries don't work, but if I use update<BR>
> they seem to. I don't mind using update, but I would like to<BR>
> understand the difference between the two. Here is my rule file.<BR>
><BR>
> rule "Add person to math class"<BR>
> no-loop<BR>
> when<BR>
> person : Person(likesMath == true)<BR>
> then<BR>
> person.setClassName("Math");<BR>
> insert(person);<BR>
> end<BR>
><BR>
> I also have a query:<BR>
><BR>
> query getPeopleForClass(String _className)<BR>
> Person(className == _className)<BR>
> end<BR>
><BR>
> When I add two facts to working memory, where the likesMath<BR>
> attribute is true, then run my query I get zero results back. When I<BR>
> change the rule, "Add person to math class", to use update(person)<BR>
> instead of insert(person) two students are returned.<BR>
><BR>
> Any help would be appreciated.<BR>
><BR>
> Thanks,<BR>
> Dan<BR>
><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><BR>
> <<A HREF="mailto:rules-users-bounces@lists.jboss.org">mailto:rules-users-bounces@lists.jboss.org</A>> ] On Behalf Of<BR>
> thomaskukofka@web.de<BR>
><BR>
> Sent: Wednesday, September 10, 2008 2:27 PM<BR>
> To: rules-users@lists.jboss.org<BR>
> Subject: [rules-users] Re: looping problem<BR>
><BR>
> Hello,<BR>
><BR>
> thanks that works. I just forgot to set the focus in the init rule..<BR>
><BR>
> Regards<BR>
> Thomas<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>
> <https://lists.jboss.org/mailman/listinfo/rules-users<BR>
> ><BR>
> --------------------------------------------------------<BR>
><BR>
> NOTICE: If received in error, please destroy and notify sender. <BR>
> Sender does not intend to waive confidentiality or privilege. Use of<BR>
> this email is prohibited when received in error.<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>
_______________________________________________<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>