Search in sources :

Example 46 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testLiaNodeLinking.

@Test
public void testLiaNodeLinking() {
    setUp(JOIN_NODE);
    // Initialise from lian
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    SegmentUtilities.createSegmentMemory(liaNode, ksession);
    InternalFactHandle fh1 = (InternalFactHandle) ksession.insert("str1");
    n1.assertObject(fh1, context, ksession);
    LiaNodeMemory liaMem = (LiaNodeMemory) ksession.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
    BetaMemory bm1 = (BetaMemory) ksession.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
    // still unlinked
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
    // now linked
    InternalFactHandle fh2 = (InternalFactHandle) ksession.insert("str2");
    liaNode.assertObject(fh2, context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    // test unlink after one retract
    liaNode.retractLeftTuple(fh2.getFirstLeftTuple(), context, ksession);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
    // check counter, after multiple asserts
    InternalFactHandle fh3 = (InternalFactHandle) ksession.insert("str3");
    InternalFactHandle fh4 = (InternalFactHandle) ksession.insert("str4");
    liaNode.assertObject(fh3, context, ksession);
    liaNode.assertObject(fh4, context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    liaNode.retractLeftTuple(fh3.getFirstLeftTuple(), context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    liaNode.retractLeftTuple(fh4.getFirstLeftTuple(), context, ksession);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
}
Also used : StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Test(org.junit.Test)

Example 47 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class PhreakQueryNode method doLeftDeletes.

public void doLeftDeletes(QueryElementNodeMemory qmem, ReteEvaluator reteEvaluator, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        InternalFactHandle fh = (InternalFactHandle) leftTuple.getContextObject();
        DroolsQuery dquery = (DroolsQuery) fh.getObject();
        if (dquery.isOpen()) {
            LeftInputAdapterNode lian = (LeftInputAdapterNode) qmem.getQuerySegmentMemory().getRootNode();
            LiaNodeMemory lm = (LiaNodeMemory) qmem.getQuerySegmentMemory().getNodeMemories().get(0);
            // there is only one, all other LTs are peers
            LeftTuple childLeftTuple = fh.getFirstLeftTuple();
            LeftInputAdapterNode.doDeleteObject(childLeftTuple, childLeftTuple.getPropagationContext(), qmem.getQuerySegmentMemory(), reteEvaluator, lian, false, lm);
        } else {
            LeftTuple childLeftTuple = leftTuple.getFirstChild();
            while (childLeftTuple != null) {
                LeftTuple nextChild = childLeftTuple.getHandleNext();
                RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                childLeftTuple = nextChild;
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) DroolsQuery(org.drools.core.base.DroolsQuery) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 48 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedRuleMidwayShare.

@Test
public void testPopulatedRuleMidwayShare() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B() C(1;) X() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new X(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B() C(2;) X() E()\n"));
    wm.fireAllRules();
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) bNode.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals(3, c1Mem.getLeftTupleMemory().size());
    assertEquals(1, c1Mem.getRightTupleMemory().size());
    BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem = sm.getFirst().getNext();
    assertSame(c2Smem, c2Mem.getSegmentMemory());
    assertEquals(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertEquals(6, list.size());
    kbase1.removeRule("org.kie", "r2");
    assertEquals(10, countNodeMemories(wm.getNodeMemories()));
    assertNull(sm.getFirst());
    // c1SMem repoints back to original Smem
    assertSame(sm, c1Mem.getSegmentMemory());
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(6)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(7, list.size());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 49 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedSharedLiaNode.

@Test
public void testPopulatedSharedLiaNode() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B(1;) C() X() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new B(2));
    wm.insert(new C(1));
    wm.insert(new X(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(3, list.size());
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B(2;) C() X() E()\n"));
    wm.fireAllRules();
    assertEquals(17, countNodeMemories(wm.getNodeMemories()));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode b1Node = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode b2Node = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink();
    JoinNode c1Node = (JoinNode) b1Node.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory b1Mem = (BetaMemory) wm.getNodeMemory(b1Node);
    assertSame(sm.getFirst(), b1Mem.getSegmentMemory());
    assertEquals(3, b1Mem.getLeftTupleMemory().size());
    assertEquals(1, b1Mem.getRightTupleMemory().size());
    BetaMemory b2Mem = (BetaMemory) wm.getNodeMemory(b2Node);
    SegmentMemory b2Smem = sm.getFirst().getNext();
    assertSame(b2Smem, b2Mem.getSegmentMemory());
    assertEquals(3, b2Mem.getLeftTupleMemory().size());
    assertEquals(1, b2Mem.getRightTupleMemory().size());
    assertEquals(6, list.size());
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(b1Mem.getSegmentMemory(), c1Mem.getSegmentMemory());
    assertNotSame(b1Mem.getSegmentMemory(), b2Mem.getSegmentMemory());
    wm.fireAllRules();
    assertEquals(6, list.size());
    assertEquals(17, countNodeMemories(wm.getNodeMemories()));
    kbase1.removeRule("org.kie", "r2");
    assertEquals(12, countNodeMemories(wm.getNodeMemories()));
    assertSame(sm, b1Mem.getSegmentMemory());
    assertSame(sm, c1Mem.getSegmentMemory());
    assertNull(sm.getFirst());
    assertEquals(3, b1Mem.getLeftTupleMemory().size());
    assertEquals(1, b1Mem.getRightTupleMemory().size());
    // SegmentMemory b2Smem =  sm.getFirst().remove();
    assertSame(b2Smem, b2Mem.getSegmentMemory());
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(6)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(7, list.size());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 50 with LiaNodeMemory

use of org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory in project drools by kiegroup.

the class AddRuleTest method testPopulatedRuleWithEvals.

@Test
public void testPopulatedRuleWithEvals() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a:A() B() eval(1==1) eval(1==1) C(1;) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.fireAllRules();
    assertEquals(3, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   a:A() B() eval(1==1) eval(1==1) C(2;) \n"));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) e2.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals(3, c1Mem.getLeftTupleMemory().size());
    assertEquals(1, c1Mem.getRightTupleMemory().size());
    BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem = sm.getFirst().getNext();
    assertSame(c2Smem, c2Mem.getSegmentMemory());
    assertEquals(0, c2Mem.getLeftTupleMemory().size());
    assertEquals(0, c2Mem.getRightTupleMemory().size());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext());
    assertNotNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst().getStagedNext().getStagedNext().getStagedNext());
    wm.fireAllRules();
    assertEquals(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertNull(c2Smem.getStagedLeftTuples().getInsertFirst());
    assertEquals(6, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    assertEquals("r1", ((Match) list.get(1)).getRule().getName());
    assertEquals("r1", ((Match) list.get(2)).getRule().getName());
    assertEquals("r2", ((Match) list.get(3)).getRule().getName());
    assertEquals(3, ((A) ((Match) list.get(3)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(4)).getRule().getName());
    assertEquals(2, ((A) ((Match) list.get(4)).getDeclarationValue("a")).getObject());
    assertEquals("r2", ((Match) list.get(5)).getRule().getName());
    assertEquals(1, ((A) ((Match) list.get(5)).getDeclarationValue("a")).getObject());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)56 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)50 Test (org.junit.Test)46 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)42 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)42 SegmentMemory (org.drools.core.reteoo.SegmentMemory)36 JoinNode (org.drools.core.reteoo.JoinNode)29 ArrayList (java.util.ArrayList)22 BetaMemory (org.drools.core.reteoo.BetaMemory)22 List (java.util.List)21 KieBase (org.kie.api.KieBase)21 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)20 Match (org.kie.api.runtime.rule.Match)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)15 FactHandle (org.kie.api.runtime.rule.FactHandle)10 NotNode (org.drools.core.reteoo.NotNode)9 InternalFactHandle (org.drools.core.common.InternalFactHandle)8 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)8 Arrays.asList (java.util.Arrays.asList)7 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)7