Search in sources :

Example 1 with AFact

use of org.drools.compiler.integrationtests.facts.AFact in project drools by kiegroup.

the class UpdateTest method testNotIterativeModifyBug.

@Test
public void testNotIterativeModifyBug() {
    // JBRULES-2809
    // This bug occurs when a tuple is modified, the remove/add puts it onto the memory end
    // However before this was done it would attempt to find the next tuple, starting from itself
    // This meant it would just re-add itself as the blocker, but then be moved to end of the memory
    // If this tuple was then removed or changed, the blocked was unable to check previous tuples.
    String str = "";
    str += "package org.simple \n";
    str += "import " + AFact.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule xxx \n";
    str += "when \n";
    str += "  $f1 : AFact() \n";
    str += "    not AFact(this != $f1,  eval(field2 == $f1.getField2())) \n";
    str += "    eval( !$f1.getField1().equals(\"1\") ) \n";
    str += "then \n";
    str += "  list.add($f1); \n";
    str += "end  \n";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final AFact a1 = new AFact("2", "2");
    final AFact a2 = new AFact("1", "2");
    final AFact a3 = new AFact("1", "2");
    final FactHandle fa1 = ksession.insert(a1);
    final FactHandle fa2 = ksession.insert(a2);
    final FactHandle fa3 = ksession.insert(a3);
    ksession.fireAllRules();
    // a1 is blocked by a2
    assertEquals(0, list.size());
    // modify a2, so that a1 is now blocked by a3
    // Do
    a2.setField2("1");
    ksession.update(fa2, a2);
    // Undo
    a2.setField2("2");
    ksession.update(fa2, a2);
    // modify a3 to cycle, so that it goes on the memory end, but in a previous bug still blocked a1
    ksession.update(fa3, a3);
    // Do
    a3.setField2("1");
    ksession.update(fa3, a3);
    ksession.fireAllRules();
    // this should still now blocked by a2, but bug from previous update hanging onto blocked
    assertEquals(0, list.size());
    ksession.dispose();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) AFact(org.drools.compiler.integrationtests.facts.AFact) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 AFact (org.drools.compiler.integrationtests.facts.AFact)1 Test (org.junit.Test)1 KieBase (org.kie.api.KieBase)1 KieSession (org.kie.api.runtime.KieSession)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1