Search in sources :

Example 21 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class MemoryLeakTest method testStagedTupleLeak.

@Test
public void testStagedTupleLeak() throws Exception {
    // BZ-1056599
    String str = "rule R1 when\n" + "    $i : Integer()\n" + "then\n" + "    insertLogical( $i.toString() );\n" + "end\n" + "\n" + "rule R2 when\n" + "    $i : Integer()\n" + "then\n" + "    delete( $i );\n" + "end\n" + "\n" + "rule R3 when\n" + "    $l : Long()\n" + "    $s : String( this == $l.toString() )\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    for (int i = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.fireAllRules();
    }
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    JoinNode joinNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        if (String.class == otn.getObjectType().getValueType().getClassType()) {
            joinNode = (JoinNode) otn.getObjectSinkPropagator().getSinks()[0];
            break;
        }
    }
    assertNotNull(joinNode);
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    BetaMemory memory = (BetaMemory) wm.getNodeMemory(joinNode);
    TupleSets<RightTuple> stagedRightTuples = memory.getStagedRightTuples();
    assertNull(stagedRightTuples.getDeleteFirst());
    assertNull(stagedRightTuples.getInsertFirst());
}
Also used : Rete(org.drools.core.reteoo.Rete) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 22 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class AccumulateTest method testAccumulateWithSameSubnetwork.

// (timeout = 10000)
@Test
public void testAccumulateWithSameSubnetwork() throws Exception {
    String rule = "package org.drools.compiler.test;\n" + "import org.drools.compiler.Cheese;\n" + "import org.drools.compiler.Person;\n" + "global java.util.List list; \n" + "rule r1 salience 100 \n" + "    when\n" + "        $person      : Person( name == 'Alice', $likes : likes )\n" + "        $total       : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + "                                                min($c.getPrice()) )\n" + "    then\n" + "        list.add( 'r1' + ':' + $total);\n" + "end\n" + "rule r2 \n" + "    when\n" + "        $person      : Person( name == 'Alice', $likes : likes )\n" + "        $total       : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + "                                                max($c.getPrice()) )\n" + "    then\n" + "        list.add( 'r2' + ':' + $total);\n" + "end\n" + "";
    // read in the source
    KieBase kbase = loadKnowledgeBaseFromString(rule);
    KieSession wm = createKnowledgeSession(kbase);
    List list = new ArrayList();
    wm.setGlobal("list", list);
    // Check the network formation, to ensure the RiaNode is shared.
    ObjectTypeNode cheeseOtn = LinkingTest.getObjectTypeNode(kbase, Cheese.class);
    ObjectSink[] oSinks = cheeseOtn.getObjectSinkPropagator().getSinks();
    assertEquals(1, oSinks.length);
    JoinNode cheeseJoin = (JoinNode) oSinks[0];
    LeftTupleSink[] ltSinks = cheeseJoin.getSinkPropagator().getSinks();
    assertEquals(1, ltSinks.length);
    RightInputAdapterNode rian = (RightInputAdapterNode) ltSinks[0];
    // RiaNode is shared, if this has two outputs
    assertEquals(2, rian.getObjectSinkPropagator().size());
    wm.insert(new Cheese("stilton", 10));
    wm.insert(new Person("Alice", "brie"));
    wm.insert(new Person("Bob", "stilton"));
    wm.fireAllRules();
    assertEquals(2, list.size());
    assertEquals("r1:10", list.get(0));
    assertEquals("r2:10", list.get(1));
}
Also used : JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) Cheese(org.drools.compiler.Cheese) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) ObjectSink(org.drools.core.reteoo.ObjectSink) Person(org.drools.compiler.Person) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) Test(org.junit.Test)

Example 23 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class LinkingTest method testNotNodeMasksWithoutConstraints.

@Test
public void testNotNodeMasksWithoutConstraints() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   not( B( ) ) \n";
    str += "   C() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(1, liaNode.getSinkPropagator().size());
    wm.insert(new A());
    wm.flushPropagations();
    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(1, pmem.getSegmentMemories().length);
    assertEquals(1, pmem.getAllLinkedMaskTest());
    SegmentMemory sm = pmem.getSegmentMemories()[0];
    assertEquals(7, sm.getAllLinkedMaskTest());
    assertEquals(3, sm.getLinkedNodeMask());
    assertFalse(sm.isSegmentLinked());
    assertFalse(pmem.isRuleLinked());
    wm.insert(new C());
    wm.flushPropagations();
    assertEquals(7, sm.getLinkedNodeMask());
    assertTrue(sm.isSegmentLinked());
    assertTrue(pmem.isRuleLinked());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 24 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class LinkingTest method testNotNodeMasksWithConstraints.

@Test
public void testNotNodeMasksWithConstraints() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   not( B( value == $a.value ) ) \n";
    str += "   C() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(1, liaNode.getSinkPropagator().size());
    wm.insert(new A());
    wm.flushPropagations();
    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(1, pmem.getSegmentMemories().length);
    assertEquals(1, pmem.getAllLinkedMaskTest());
    SegmentMemory sm = pmem.getSegmentMemories()[0];
    assertEquals(5, sm.getAllLinkedMaskTest());
    assertEquals(3, sm.getLinkedNodeMask());
    assertFalse(sm.isSegmentLinked());
    assertFalse(pmem.isRuleLinked());
    wm.insert(new C());
    wm.flushPropagations();
    // only 5 is needed to link, the 'not' turns on but it has no unfleunce either way
    assertEquals(7, sm.getLinkedNodeMask());
    assertTrue(sm.isSegmentLinked());
    assertTrue(pmem.isRuleLinked());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 25 with JoinNode

use of org.drools.core.reteoo.JoinNode in project drools by kiegroup.

the class LinkingTest method testNonReactiveSubNetworkOwnSegmentMasks.

@Test
public void testNonReactiveSubNetworkOwnSegmentMasks() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() ) \n";
    str += "   exists( eval(1==1) ) \n";
    str += "   D() \n";
    str += "then \n";
    str += "end \n";
    str += "rule rule2 when \n";
    str += "   A() \n";
    str += "   exists( B() and C() ) \n";
    str += "   exists( eval(1==1) ) \n";
    str += "   E() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(2, liaNode.getSinkPropagator().size());
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    ExistsNode exists1n = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
    EvalConditionNode evalNode = (EvalConditionNode) exists1n.getSinkPropagator().getSinks()[0];
    ExistsNode exists2n = (ExistsNode) exists1n.getSinkPropagator().getSinks()[1];
    JoinNode dNode = (JoinNode) exists2n.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) dNode.getSinkPropagator().getSinks()[0];
    wm.insert(new A());
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(4, pmem.getSegmentMemories().length);
    // the exists eval segment does not need to be linked in
    assertEquals(11, pmem.getAllLinkedMaskTest());
    RightInputAdapterNode.RiaNodeMemory riaMem = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory((MemoryFactory) exists1n.getRightInput());
    // second segment must be linked in
    assertEquals(2, riaMem.getRiaPathMemory().getAllLinkedMaskTest());
    wm.insert(new B());
    wm.insert(new C());
    assertEquals(2, riaMem.getRiaPathMemory().getSegmentMemories().length);
    riaMem = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory((MemoryFactory) exists2n.getRightInput());
    // no segments to be linked in
    assertEquals(0, riaMem.getRiaPathMemory().getAllLinkedMaskTest());
}
Also used : EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MemoryFactory(org.drools.core.common.MemoryFactory) ExistsNode(org.drools.core.reteoo.ExistsNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

JoinNode (org.drools.core.reteoo.JoinNode)35 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)32 Test (org.junit.Test)32 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)30 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)29 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)20 BetaMemory (org.drools.core.reteoo.BetaMemory)19 SegmentMemory (org.drools.core.reteoo.SegmentMemory)18 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)16 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)15 ArrayList (java.util.ArrayList)12 List (java.util.List)12 PathMemory (org.drools.core.reteoo.PathMemory)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)12 ExistsNode (org.drools.core.reteoo.ExistsNode)10 NotNode (org.drools.core.reteoo.NotNode)10 KieBase (org.kie.api.KieBase)10 FactHandle (org.kie.api.runtime.rule.FactHandle)9 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)8 Match (org.kie.api.runtime.rule.Match)8