<br> Regis,<br><br> It is well known that from a performance perspective, rules engines work more efficiently with flat models than with deeply nested models. That is because rules engines usually implement relational-like algorithms (you can make an analogy to relational database technology). <br>
It does not mean that you can't use pure OO models in Drools. In fact, drools is one of the engines that better handles OO models (if it is not the best). The example I gave you using "from" instead of the nested accessors should work for your case just fine.<br>
<br> Now, the immutability of the predicates is a different matter. To keep working memory consistency, drools 4 makes use of shadow facts, that are lazy cache objects implemented as transparent proxies for your facts. Of course, it would not be feasible to cache everything in an object graph, and so, drools caches only the root objects you assert as facts into the working memory. When you use nested accessors, you are obviously accessing non-cached data and for that reason they must be immutable or you will have problems.<br>
Drools 5 is making the use of shadow proxies not required by implementing a different strategy of constraint evaluation. But that is only in trunk, not in 4.0.x branch.<br><br> []s<br> Edson<br><br><br><div class="gmail_quote">
2008/4/15 Piccand, Regis <<a href="mailto:rpiccand@verisign.com">rpiccand@verisign.com</a>>:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
<div><span><font color="#0000ff" face="Arial" size="2">Hi
Edson,</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Thanks
for your help. I restructured my biz objects to avoid nested structures, which
makes it work now (I have new objects to hold allocatedHour per
subject).</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">However, I have the feeling that this constraint has a certain
impact on o-o design (more flat objects vs composite object). The documentation
says that nested values should be considered immutable, unless using the modify
block (as you suggested) and retract/re-assert the objects in the workingMemory
; it also has a much greater performance cost.</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Am I
getting it right when saying that the object design should really be like 'many
flat-type objects avoiding composite approach' to have Drools perform well or is
it more sensible to have a proper o-o design and tweak Drools (using modify
block, disabling cache, etc.)</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Thanks
in advance for sharing your experience</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Regis</font></span></div><div class="Ih2E3d">
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div><b><font color="#960000" size="2">
<p align="left">when</p></font></b><font size="2">
<p align="left">$lesson : Lesson(allocated == </p></font><b><font color="#960000" size="2">false</font></b></div><font size="2">, session.type == Session.DISTINCT)
<p align="left">$subjectAllocation : SubjectAllocation( subject ==
$lesson.subject, $allocatedDays : allocatedDays )</p>
<p align="left">$availableHour : Hour(allocated == </p></font><div class="Ih2E3d"><b><font color="#960000" size="2">false</font></b><font size="2">, day </font><b><font color="#960000" size="2">not</font></b><font size="2"> </font><b><font color="#960000" size="2">memberOf</font></b><font size="2"> $allocatedDays)
<p align="left"></p>
<p align="left"></p></font><b><font color="#960000" size="2">then</font></b><font size="2">
<p align="left">$lesson.setHour($availableHour ) ;</p>
<p align="left">$lesson.setAllocated(</p></font><b><font color="#960000" size="2">true</font></b><font size="2"> ) ;
<p align="left"></p></font><b><font color="#960000" size="2">update</font></b><font size="2">($lesson) ;
<p align="left"></p>
<p align="left">$availableHour.setSubject($lesson.getSubject() ) ;</p>
<p align="left">$availableHour.setAllocated(</p></font><b><font color="#960000" size="2">true</font></b><font size="2"> ) ;
<p align="left"></p></font><b><font color="#960000" size="2">update</font></b></div><font size="2">($availableHour) ;
<p align="left"></p>
<p align="left">$subjectAllocation.addAllocation($availableHour ) ;</p>
<p></p></font><b><font color="#960000" size="2">update</font></b><font size="2">($subjectAllocation) ;</font>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div><font size="2"></font><br>
<div dir="ltr" align="left" lang="en-us">
<hr>
<font face="Tahoma" size="2"><b>From:</b> <a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>
[mailto:<a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>] <b>On Behalf Of </b>Edson
Tirelli<br><b>Sent:</b> Monday, April 14, 2008 4:55 PM<br><b>To:</b> Rules Users
List<br><b>Subject:</b> Re: [rules-users] newbie: update and
shallowCache<br></font><br></div><div><div></div><div class="Wj3C7c">
<div></div><font color="#0000ff" face="Arial" size="2"></font><br>
Regis,<br><br> You are probably having problems with mutable
predicates, when using "<span><font face="Arial" size="2">subject.allocatedDays</font></span>". As a first step to understand your
problem, I strongly encourage you to make constraints flat, i.e., avoid using
nested accessors. After you get used to what can and what can't be done, you can
go back to use nested accessors. <br> Also, I suggest you use
modify() block instead of update():<br><br><span><b><font color="#960000">
<p align="left"><font face="Arial" size="2">rule <br></font></p></font></b><font face="Arial"><font size="2"><font color="#008000">"allocate"</font></font></font>
<p align="left"><b><font color="#960000"><font face="Arial" size="2">when</font></font></b></p>
<p align="left"><font size="2"><font face="Arial">$lesson : Lesson(allocated ==
</font><b><font color="#960000"><font face="Arial">false</font></font></b></font><font face="Arial" size="2">, session.type
== Session.DISTINCT )</font></p>
<p align="left"><font face="Arial" size="2">$subject : Subject( $allocatedDays :
allocatedDays ) from $lesson.subject<br></font></p>
<p align="left"><font size="2"><font face="Arial">$availableHour : Hour(allocated ==
</font><b><font color="#960000"><font face="Arial">false</font></font></b></font><font face="Arial"><font size="2">, day
<b><font color="#960000">not</font></b></font></font><font face="Arial"><font size="2"> <b><font color="#960000">memberOf</font></b></font></font><font face="Arial" size="2"> $allocatedDays)</font></p>
<p align="left"><b><font color="#960000"><font face="Arial" size="2">then</font></font></b></p>
<p align="left"><font face="Arial" size="2">modify( $lesson) {</font></p>
<p align="left"><font face="Arial" size="2"> setHour($availableHour
),<br></font></p>
<p align="left"><font size="2"><font face="Arial">
setAllocated(</font><b><font color="#960000"><font face="Arial">true</font></font></b></font><font face="Arial" size="2">)</font></p>
<p align="left"><font face="Arial" size="2">}<br></font></p>
<p align="left"><font face="Arial" size="2">modify( $availableHour )</font></p>
<p align="left"><font face="Arial" size="2">
setSubject($lesson.getSubject()),<br></font></p>
<p align="left"><font size="2"><font face="Arial">
setAllocated(</font><b><font color="#960000"><font face="Arial">true</font></font></b></font><font face="Arial" size="2"> )</font></p>
<p align="left"><font face="Arial" size="2">}<br></font></p><font color="#960000"><font size="2"><font face="Arial"><b></b></font></font></font><font color="#960000">
<p><font face="Arial" size="2"><b>end</b></font><br></p>
<div><span><span><br></span></span></div></font></span>
<div class="gmail_quote"> Hope it
helps.<br><br> []s<br> Edson<br>
<br>2008/4/14 Piccand, Regis <<a href="mailto:rpiccand@verisign.com" target="_blank">rpiccand@verisign.com</a>>:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
<div><span><font face="Arial" size="2">Hi all,</font></span></div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2">I am evaluating Drools and am stuck
with a simple rule that selects elements which do not correspond to the
condition... Since I am updating objects in the RHD, I guess it has to do with
my wrong understanding (and the cache...?)</font></span></div>
<div><span><span><font color="#000000" face="Arial" size="2"></font></span></span> </div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2">I am trying to allocate lessons to periods
(hour). My object model can be summarize is as follows :</font></span></div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2">Lesson </font></span></div>
<div><span> <font face="Arial" size="2">boolean :
allocated // true if the lesson has been allocated to an
hour</font></span></div>
<div><span> <font face="Arial" size="2">int :
session
// a session is a bunch of lessons</font></span></div>
<div><span><font face="Arial" size="2"> Subject :
subject // an allocated lesson is assigned a
subject</font></span></div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2">Subject</font></span></div>
<div><span> <font face="Arial" size="2">Set<Day> :
allocatedDays // days in which this subject is taught - checked before
allocation, as a subject can only be taught once a day</font></span></div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2">Hour</font></span></div>
<div><span> <font face="Arial" size="2">boolean : allocated //
true is this hour has been allocated with a lesson</font></span></div>
<div><span> D<font face="Arial" size="2">ay : day // the day
this hour belongs to - required to check with
subject.allocatedDays</font></span></div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2">I put in the workingMemory a bunch of
Lesson objects, which have been set with Subject and Session id. I also
put a bunch of Hour object, set with a Day.</font></span></div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><font face="Arial" size="2">My rule is as follows:</font></span></div>
<div><span><font face="Arial" size="2"></font></span> </div>
<div><span><b><font color="#960000">
<p align="left"><font face="Arial" size="2">rule</font></p></font></b><font face="Arial"><font size="2"><font color="#008000">"allocate"</font></font></font>
<p align="left"><font face="Arial" size="2"></font></p>
<p align="left"><b><font color="#960000"><font face="Arial" size="2">when</font></font></b></p>
<p align="left"><font size="2"><font face="Arial">$lesson : Lesson(allocated ==
</font><b><font color="#960000"><font face="Arial">false</font></font></b></font><font face="Arial" size="2">,
session.type == Session.DISTINCT, $allocatedDays :
subject.allocatedDays)</font></p>
<p align="left"><font size="2"><font face="Arial">$availableHour : Hour(allocated ==
</font><b><font color="#960000"><font face="Arial">false</font></font></b></font><font face="Arial"><font size="2">, day
<b><font color="#960000">not</font></b></font></font><font face="Arial"><font size="2"> <b><font color="#960000">memberOf</font></b></font></font><font face="Arial" size="2"> $allocatedDays)</font></p>
<p align="left"><font face="Arial" size="2"></font></p>
<p align="left"><b><font color="#960000"><font face="Arial" size="2">then</font></font></b></p>
<p align="left"><font face="Arial" size="2">$lesson.setHour($availableHour )
;</font></p>
<p align="left"><font size="2"><font face="Arial">$lesson.setAllocated(</font><b><font color="#960000"><font face="Arial">true</font></font></b></font><font face="Arial" size="2">) ;</font></p>
<p align="left"><b><font color="#960000"><font face="Arial" size="2">update</font></font></b><font face="Arial" size="2">($lesson);</font></p>
<p align="left"><font face="Arial" size="2"></font></p>
<p align="left"><font face="Arial" size="2">$availableHour.setSubject($lesson.getSubject()) ;</font></p>
<p align="left"><font size="2"><font face="Arial">$availableHour.setAllocated(</font><b><font color="#960000"><font face="Arial">true</font></font></b></font><font face="Arial" size="2"> );</font></p>
<p align="left"><b><font color="#960000"><font face="Arial" size="2">update</font></font></b><font face="Arial" size="2">($availableHour);</font></p>
<p align="left"><font face="Arial" size="2"></font></p><font color="#960000">
<p><font face="Arial" size="2"><b>end</b></font></p>
<div><span><span><font color="#000000" face="Arial" size="2">When I set a debug on
the first line of the THEN section, I can see that sometimes
$availableHour.allocated == true ... sometimes $lesson.allocated == true
(although it's an implicit And statement ...)</font></span></span></div>
<div>
<p><font face="Arial"><font color="#000000"><font size="2"><span>Therefore, I can
see that the update() works, but it seems that the rule does not use the
updated object (due to shallow copy ??)</span></font></font></font></p>
<p><font face="Arial"><font color="#000000"><font size="2"><span></span>A<span>ny
help more than
welcome</span></font></font></font></p></div></font></span></div>
<p><span><font color="#000000" face="Arial" size="2">Regis</font></span></p>
<p><span><font color="#000000" face="Arial" size="2"></font></span> </p></div><br>_______________________________________________<br>rules-users
mailing list<br><a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>Edson Tirelli<br>JBoss Drools Core Development<br>Office:
+55 11 3529-6000<br>Mobile: +55 11 9287-5646<br>JBoss, a division of Red Hat @
<a href="http://www.jboss.com" target="_blank">www.jboss.com</a> </div></div></div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> JBoss Drools Core Development<br> Office: +55 11 3529-6000<br> Mobile: +55 11 9287-5646<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a>