<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>