<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">&lt;<a href="mailto:dsotty@gmail.com" target="_blank">dsotty@gmail.com</a>&gt;</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 &quot;Insert Children&quot;</tt><tt><br>
      </tt><tt>when</tt><tt><br>
      </tt></div><tt>then</tt><tt><br>
      </tt><tt>    Parent p1 = new Parent( &quot;a1&quot; );</tt><tt><br>
      </tt><tt>    Child c1p1 = new Child( &quot;1&quot; );</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 &quot;Rule A&quot;</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(&quot;FOUND: &quot;+ $child + &quot; &gt;&gt; &quot; +
        $child.getParentId() + &quot; == &quot; + $p.getAttribute2() + &quot; ??&quot; );</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&#39;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&lt;Child&gt; 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&#39;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(&quot;1&quot;);</div>
          <div>        p1.setAttribute1(&quot;a1&quot;);</div>
          <div>        p1.setAttribute2(null);</div>
          <div>        </div>
          <div>
                    Child c1p1 = new Child();</div>
          <div>        c1p1.setName(&quot;n1&quot;);</div>
          <div>        c1p1.setParentId(p1.getId());</div>
          <div>        c1p1.setValue(&quot;v1.1&quot;);</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 &quot;Insert Children&quot;</div>
          <div>when</div>
          <div>    $p: Parent()    @watch(!*)</div>
          <div>    $c: Child() from $p.children</div>
          <div>then</div>
          <div>    System.out.println(&quot;Inserting child &quot;+$c);</div>
          <div>    insert($c);</div>
          <div>end</div>
          <div><br>
          </div>
          <div>rule &quot;Rule A&quot;</div>
          <div>when</div>
          <div>    $p: Parent(attribute2 != null)</div>
          <div>    ?getChildWithName($p.attribute2, &quot;n1&quot;, $child;)</div>
          <div>then</div>
          <div>    System.out.println(&quot;FOUND: &quot;+$child+&quot;. The attribute
            &#39;parentId&#39; of this object must have the value
            &#39;&quot;+$p.getAttribute2()+&quot;&#39;. Does it? &quot;+$child.getParentId()+&quot;
            == &quot;+$p.getAttribute2()+&quot; ??&quot;);</div>
          <div>    globalList.add($child);</div>
          <div>end</div>
          <div><br>
          </div>
          <div><br>
          </div>
          <div>rule &quot;Copy Attribute1 into Attribute2&quot;</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 &#39;Rule A&#39; and &#39;Copy Attribute1
          into Attribute2&#39;. The latter copies the value of attribute1 to
          attribute2 for any Parent object present in the session. (Note
          that the parent I&#39;m inserting has attribute2 = null). &#39;Rule A&#39;
          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 &#39;1&#39; I don&#39;t expect &#39;Rule A&#39; to be activated/executed.
          When I modify parent.attribute2 in &#39;Copy Attribute1 into
          Attribute2&#39; I&#39;m setting its value to &#39;a1&#39;. &#39;Rule A&#39; (via the
          query) should then look for a Child with parentid = &#39;a1&#39; and
          It shouldn&#39;t find anything. </div>
        <div>Funny thing is that &#39;Rule A&#39; activates and fires. The child
          object that is &#39;returned&#39; by the query has a parentId of &#39;1&#39;
          so I don&#39;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>