<div dir="ltr">Good catch Davide. I can confirm that not using nested accessors the query works as expected.<div><br></div><div>Regards,</div></div><div class="gmail_extra"><br clear="all"><div><br>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>
<br>Esteban Aliverti<br>- Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a></div>
<br><br><div class="gmail_quote">On Wed, Jan 22, 2014 at 6:57 PM, Davide Sottara <span dir="ltr"><<a href="mailto:dsotty@gmail.com" target="_blank">dsotty@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>Indeed, the use case can be simplified
further:<br>
<br>
<tt>package org.drools.test.bc;</tt><tt><br>
</tt><tt><br>
</tt><tt>declare Parent</tt><tt><br>
</tt><tt> attribute2 : String</tt><tt><br>
</tt><tt>end</tt><tt><br>
</tt><tt><br>
</tt><tt>declare Child</tt><tt><br>
</tt><tt> parentId : String</tt><tt><br>
</tt><tt>end</tt><tt><br>
</tt><tt><br>
</tt><tt>query getChildWithName( String $parentId, Child $child )</tt><tt><br>
</tt><tt> $child:= Child( parentId == $parentId )</tt><div class="im"><tt><br>
</tt><tt>end</tt><tt><br>
</tt><tt><br>
</tt><tt>rule "Insert Children"</tt><tt><br>
</tt><tt>when</tt><tt><br>
</tt></div><tt>then</tt><tt><br>
</tt><tt> Parent p1 = new Parent( "a1" );</tt><tt><br>
</tt><tt> Child c1p1 = new Child( "1" );</tt><tt><br>
</tt><tt><br>
</tt><tt> insert( c1p1 );</tt><tt><br>
</tt><tt> insert( p1 );</tt><div class="im"><tt><br>
</tt><tt>end</tt><tt><br>
</tt><tt><br>
</tt><tt>rule "Rule A"</tt><tt><br>
</tt><tt>when</tt><tt><br>
</tt></div><tt> $p: Parent( $a2 : attribute2 )</tt><tt><br>
</tt><tt> ?getChildWithName( $p.attribute2, $child;)</tt><tt><br>
</tt><tt>then</tt><tt><br>
</tt><tt> System.out.println("FOUND: "+ $child + " >> " +
$child.getParentId() + " == " + $p.getAttribute2() + " ??" );</tt><tt><br>
</tt><tt>end</tt><tt><br>
</tt><tt><br>
</tt>It has nothing to do with nested objects, @PR or
modifications. <br>
It seems that the culprit is the chained property accessor.<div class="im"><br>
<br>
Davide<br>
<br>
<br>
<br>
On 01/22/2014 02:22 PM, Esteban Aliverti wrote:<br>
</div></div>
<blockquote type="cite"><div><div class="h5">
<div dir="ltr">Hi guys,
<div>I was writing some rules using backward chaining and found
a strange behavior. </div>
<div>I managed to isolate the error in the test project I'm
attaching. </div>
<div><br>
</div>
<div>In the project I have 2 classes: Parent and Child</div>
<div><br>
</div>
<div>Parent:</div>
<div> * String id</div>
<div> * String attribute1</div>
<div> * String attribute2</div>
<div> * List<Child> children</div>
<div><br>
</div>
<div>Child:</div>
<div> * String parentId;</div>
<div> * String name;</div>
<div> * String value;</div>
<div><br>
</div>
<div><br>
</div>
<div>In the test I'm creating 1 Parent object with just 1 child
and then inserting the Parent object into a drools session:</div>
<div><br>
</div>
<div>
<div> Parent p1 = new Parent();</div>
<div> p1.setId("1");</div>
<div> p1.setAttribute1("a1");</div>
<div> p1.setAttribute2(null);</div>
<div> </div>
<div>
Child c1p1 = new Child();</div>
<div> c1p1.setName("n1");</div>
<div> c1p1.setParentId(p1.getId());</div>
<div> c1p1.setValue("v1.1");</div>
<div> p1.addChild(c1p1);</div>
<div> </div>
<div> kSession.insert(p1);</div>
<div> kSession.fireAllRules();</div>
</div>
<div><br>
</div>
<div><br>
</div>
<div>So far so good. </div>
<div>The rules I have in my session are the following:</div>
<div><br>
</div>
<div>
<div>declare Parent</div>
<div> @propertyReactive</div>
<div>end</div>
<div><br>
</div>
<div>query getChildWithName(String $parentId, String $name,
Child $child)</div>
<div> $child:= Child(parentId == $parentId, name == $name)</div>
<div>end</div>
<div><br>
</div>
<div>rule "Insert Children"</div>
<div>when</div>
<div> $p: Parent() @watch(!*)</div>
<div> $c: Child() from $p.children</div>
<div>then</div>
<div> System.out.println("Inserting child "+$c);</div>
<div> insert($c);</div>
<div>end</div>
<div><br>
</div>
<div>rule "Rule A"</div>
<div>when</div>
<div> $p: Parent(attribute2 != null)</div>
<div> ?getChildWithName($p.attribute2, "n1", $child;)</div>
<div>then</div>
<div> System.out.println("FOUND: "+$child+". The attribute
'parentId' of this object must have the value
'"+$p.getAttribute2()+"'. Does it? "+$child.getParentId()+"
== "+$p.getAttribute2()+" ??");</div>
<div> globalList.add($child);</div>
<div>end</div>
<div><br>
</div>
<div><br>
</div>
<div>rule "Copy Attribute1 into Attribute2"</div>
<div>when</div>
<div> $p: Parent(attribute1 != null, attribute2 == null)</div>
<div>then</div>
<div> modify($p){</div>
<div> setAttribute2($p.getAttribute1())</div>
<div> }</div>
<div>end</div>
</div>
<div><br>
</div>
<div>The important part here is 'Rule A' and 'Copy Attribute1
into Attribute2'. The latter copies the value of attribute1 to
attribute2 for any Parent object present in the session. (Note
that the parent I'm inserting has attribute2 = null). 'Rule A'
is then using a query to get a Child object that has a parent
with id = $p.attribute2 (where $p is a previously matched
Parent).</div>
<div>Given that I only have 1 Parent in my session and that its
id is '1' I don't expect 'Rule A' to be activated/executed.
When I modify parent.attribute2 in 'Copy Attribute1 into
Attribute2' I'm setting its value to 'a1'. 'Rule A' (via the
query) should then look for a Child with parentid = 'a1' and
It shouldn't find anything. </div>
<div>Funny thing is that 'Rule A' activates and fires. The child
object that is 'returned' by the query has a parentId of '1'
so I don't know how this behavior is possible. If you take a
look at the System.out output you will see that the activation
makes no sense at all.</div>
<div><br>
</div>
<div>Am I doing something wrong in my project? Is this a bug? Is
this the expected behavior?</div>
<div><br>
</div>
<div>Regards,</div>
<div><br>
</div>
<div><br clear="all">
<div><br>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>
<br>
Esteban Aliverti<br>
- Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a></div>
</div>
</div>
<br>
<fieldset></fieldset>
<br>
</div></div><div class="im"><pre>_______________________________________________
rules-users mailing list
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a></pre>
</div></blockquote>
<br>
</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></blockquote></div><br></div>