Search in sources :

Example 76 with LeftTuple

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

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasLastWithMultipleData.

@Test
public void testRemoveChildLeftTupleThatWasLastWithMultipleData() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3, 4, 5);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE2_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final InternalFactHandle fh2 = (InternalFactHandle) kieSession.getFactHandle(4);
        final InternalFactHandle fh3 = (InternalFactHandle) kieSession.getFactHandle(5);
        final LeftTuple lt1 = fh1.getFirstLeftTuple();
        final LeftTuple lt1_1 = lt1.getFirstChild();
        final LeftTuple lt1_2 = lt1_1.getHandleNext();
        final LeftTuple lt1_3 = lt1_2.getHandleNext();
        assertNotNull(lt1_1);
        assertNotNull(lt1_2);
        assertNotNull(lt1_3);
        assertSame(lt1_3, lt1.getLastChild());
        assertSame(lt1_2, lt1_3.getHandlePrevious());
        assertSame(lt1_1, lt1_2.getHandlePrevious());
        assertEquals(1, lt1_1.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        assertNull(lt1_1.getPeer());
        assertEquals(1, lt1_2.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        assertNull(lt1_2.getPeer());
        assertEquals(1, lt1_3.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        assertNull(lt1_3.getPeer());
        final RightTuple rt1 = fh3.getFirstRightTuple();
        final LeftTuple rt1_1 = rt1.getLastChild();
        assertSame(lt1_1, rt1_1);
        final LeftTuple rt1_2 = rt1_1.getRightParentPrevious();
        final LeftTuple rt1_3 = rt1_2.getRightParentPrevious();
        assertNotNull(rt1_1);
        assertNotNull(rt1_2);
        assertNotNull(rt1_3);
        assertSame(rt1_2, rt1_3.getRightParentNext());
        assertSame(rt1_1, rt1_2.getRightParentNext());
        assertEquals(1, rt1_1.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        assertNull(rt1_1.getPeer());
        assertEquals(1, rt1_2.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        assertNull(rt1_2.getPeer());
        assertEquals(1, rt1_3.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        assertNull(rt1_3.getPeer());
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Test(org.junit.Test)

Example 77 with LeftTuple

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

the class MemoryLeakTest method testStagedLeftTupleLeak.

@Test
public void testStagedLeftTupleLeak() throws Exception {
    // BZ-1058874
    String str = "rule R1 when\n" + "    String( this == \"this\" )\n" + "    String( this == \"that\" )\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    ksession.fireAllRules();
    for (int i = 0; i < 10; i++) {
        FactHandle fh = ksession.insert("this");
        ksession.fireAllRules();
        ksession.delete(fh);
        ksession.fireAllRules();
    }
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        if (String.class == otn.getObjectType().getValueType().getClassType()) {
            AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
            liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
            break;
        }
    }
    assertNotNull(liaNode);
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    LeftInputAdapterNode.LiaNodeMemory memory = (LeftInputAdapterNode.LiaNodeMemory) wm.getNodeMemory(liaNode);
    TupleSets<LeftTuple> stagedLeftTuples = memory.getSegmentMemory().getStagedLeftTuples();
    assertNull(stagedLeftTuples.getDeleteFirst());
    assertNull(stagedLeftTuples.getInsertFirst());
}
Also used : Rete(org.drools.core.reteoo.Rete) FactHandle(org.kie.api.runtime.rule.FactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) AlphaNode(org.drools.core.reteoo.AlphaNode) LeftTuple(org.drools.core.reteoo.LeftTuple) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 78 with LeftTuple

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

the class MemoryLeakTest method testBetaMemoryLeakOnFactDelete.

@Test
public void testBetaMemoryLeakOnFactDelete() {
    // DROOLS-913
    String drl = "rule R1 when\n" + "    $a : Integer(this == 1)\n" + "    $b : String()\n" + "    $c : Integer(this == 2)\n" + "then \n" + "end\n" + "rule R2 when\n" + "    $a : Integer(this == 1)\n" + "    $b : String()\n" + "    $c : Integer(this == 3)\n" + "then \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    FactHandle fh1 = ksession.insert(1);
    FactHandle fh2 = ksession.insert(3);
    FactHandle fh3 = ksession.insert("test");
    ksession.fireAllRules();
    ksession.delete(fh1);
    ksession.delete(fh2);
    ksession.delete(fh3);
    ksession.fireAllRules();
    NodeMemories nodeMemories = ((InternalWorkingMemory) ksession).getNodeMemories();
    for (int i = 0; i < nodeMemories.length(); i++) {
        Memory memory = nodeMemories.peekNodeMemory(i);
        if (memory != null && memory.getSegmentMemory() != null) {
            SegmentMemory segmentMemory = memory.getSegmentMemory();
            System.out.println(memory);
            LeftTuple deleteFirst = memory.getSegmentMemory().getStagedLeftTuples().getDeleteFirst();
            if (segmentMemory.getRootNode() instanceof JoinNode) {
                BetaMemory bm = (BetaMemory) segmentMemory.getNodeMemories().getFirst();
                assertEquals(0, bm.getLeftTupleMemory().size());
            }
            System.out.println(deleteFirst);
            assertNull(deleteFirst);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FactHandle(org.kie.api.runtime.rule.FactHandle) Memory(org.drools.core.common.Memory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) JoinNode(org.drools.core.reteoo.JoinNode) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) NodeMemories(org.drools.core.common.NodeMemories) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 79 with LeftTuple

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

the class LeftTupleIterator method getFirstLeftTuple.

public LeftTuple getFirstLeftTuple(LeftTupleSource source, LeftTupleSink sink, InternalWorkingMemory wm) {
    if (source instanceof AccumulateNode) {
        AccumulateMemory accmem = (AccumulateMemory) wm.getNodeMemory((MemoryFactory) source);
        BetaMemory memory = accmem.getBetaMemory();
        FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator();
        Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
        if (leftTuple != null) {
            AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
            return accctx.getResultLeftTuple();
        }
        return null;
    }
    if (source instanceof JoinNode || source instanceof NotNode || source instanceof FromNode || source instanceof AccumulateNode) {
        BetaMemory memory;
        FastIterator localIt;
        if (source instanceof FromNode) {
            memory = ((FromMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
        } else if (source instanceof AccumulateNode) {
            memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
        } else {
            memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
        }
        localIt = memory.getLeftTupleMemory().fullFastIterator();
        Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
        while (leftTuple != null) {
            for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
                if (childleftTuple.getTupleSink() == sink) {
                    return childleftTuple;
                }
            }
            leftTuple = (LeftTuple) localIt.next(leftTuple);
        }
    }
    if (source instanceof ExistsNode) {
        BetaMemory memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
        FastIterator localIt = memory.getRightTupleMemory().fullFastIterator();
        RightTuple rightTuple = (RightTuple) BetaNode.getFirstTuple(memory.getRightTupleMemory(), localIt);
        while (rightTuple != null) {
            if (rightTuple.getBlocked() != null) {
                for (LeftTuple leftTuple = rightTuple.getBlocked(); leftTuple != null; leftTuple = leftTuple.getBlockedNext()) {
                    for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
                        if (childleftTuple.getTupleSink() == sink) {
                            return childleftTuple;
                        }
                    }
                }
            }
            rightTuple = (RightTuple) localIt.next(rightTuple);
        }
    } else if (source instanceof LeftInputAdapterNode) {
        ObjectSource os = ((LeftInputAdapterNode) source).getParentObjectSource();
        while (!(os instanceof ObjectTypeNode)) {
            os = os.getParentObjectSource();
        }
        ObjectTypeNode otn = (ObjectTypeNode) os;
        otnIterator = wm.getNodeMemory(otn).iterator();
        while (otnIterator.hasNext()) {
            InternalFactHandle handle = otnIterator.next();
            LeftTuple leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
            if (leftTuple != null) {
                return leftTuple;
            }
        }
    } else if (source instanceof EvalConditionNode || source instanceof QueryElementNode) {
        LeftTuple parentLeftTuple = getFirstLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, wm);
        while (parentLeftTuple != null) {
            for (LeftTuple leftTuple = parentLeftTuple.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
                if (leftTuple.getTupleSink() == sink) {
                    return leftTuple;
                }
            }
            parentLeftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, parentLeftTuple, wm);
        }
    }
    return null;
}
Also used : NotNode(org.drools.core.reteoo.NotNode) ExistsNode(org.drools.core.reteoo.ExistsNode) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) FastIterator(org.drools.core.util.FastIterator) AccumulateNode(org.drools.core.reteoo.AccumulateNode) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) BetaMemory(org.drools.core.reteoo.BetaMemory) JoinNode(org.drools.core.reteoo.JoinNode) FromNode(org.drools.core.reteoo.FromNode) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) Tuple(org.drools.core.spi.Tuple) BetaNode(org.drools.core.reteoo.BetaNode) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ObjectSource(org.drools.core.reteoo.ObjectSource) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Iterator(org.drools.core.util.Iterator) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) NotNode(org.drools.core.reteoo.NotNode) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) AccumulateNode(org.drools.core.reteoo.AccumulateNode) JoinNode(org.drools.core.reteoo.JoinNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) BetaMemory(org.drools.core.reteoo.BetaMemory) FromNode(org.drools.core.reteoo.FromNode) LeftTuple(org.drools.core.reteoo.LeftTuple) ExistsNode(org.drools.core.reteoo.ExistsNode) RightTuple(org.drools.core.reteoo.RightTuple) ObjectSource(org.drools.core.reteoo.ObjectSource) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 80 with LeftTuple

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

the class LeftTupleIterator method next.

public LeftTuple next() {
    LeftTuple leftTuple = null;
    if (this.currentLeftTuple != null) {
        leftTuple = currentLeftTuple;
        setNextLeftTuple();
    }
    return leftTuple;
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple)

Aggregations

LeftTuple (org.drools.core.reteoo.LeftTuple)125 RightTuple (org.drools.core.reteoo.RightTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)37 InternalFactHandle (org.drools.core.common.InternalFactHandle)34 FastIterator (org.drools.core.util.FastIterator)22 BetaConstraints (org.drools.core.common.BetaConstraints)21 BetaMemory (org.drools.core.reteoo.BetaMemory)21 ContextEntry (org.drools.core.rule.ContextEntry)20 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)16 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)13 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 Test (org.junit.Test)13 KieSession (org.kie.api.runtime.KieSession)13 Tuple (org.drools.core.spi.Tuple)12 WorkingMemory (org.drools.core.WorkingMemory)11 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)11 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)10 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 ArrayList (java.util.ArrayList)8