Search in sources :

Example 71 with SegmentMemory

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

the class SegmentPropagator method processPeerInserts.

private static void processPeerInserts(TupleSets<LeftTuple> leftTuples, SegmentMemory firstSmem) {
    for (LeftTuple leftTuple = leftTuples.getInsertFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {
        SegmentMemory smem = firstSmem.getNext();
        if (smem != null) {
            // So we should check if the instances already exist and use them if they do.
            if (leftTuple.getPeer() == null) {
                LeftTuple peer = leftTuple;
                // peers do not exist, so create and add them.
                for (; smem != null; smem = smem.getNext()) {
                    LeftTupleSink sink = smem.getSinkFactory();
                    // pctx is set during peer cloning
                    peer = sink.createPeer(peer);
                    smem.getStagedLeftTuples().addInsert(peer);
                }
            } else {
                LeftTuple peer = leftTuple.getPeer();
                // peers exist, so update them as an insert, which also handles staged clashing.
                for (; smem != null; smem = smem.getNext()) {
                    peer.setPropagationContext(leftTuple.getPropagationContext());
                    // ... and update the staged LeftTupleSets according to its current staged state
                    updateChildLeftTupleDuringInsert(peer, smem.getStagedLeftTuples(), smem.getStagedLeftTuples());
                    peer = peer.getPeer();
                }
            }
        }
    }
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) AddRemoveRule.forceFlushLeftTuple(org.drools.core.phreak.AddRemoveRule.forceFlushLeftTuple) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 72 with SegmentMemory

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

the class SegmentPropagator method propagate.

public static void propagate(SegmentMemory sourceSegment, TupleSets<LeftTuple> leftTuples, ReteEvaluator reteEvaluator) {
    if (leftTuples.isEmpty()) {
        return;
    }
    LeftTupleSource source = (LeftTupleSource) sourceSegment.getTipNode();
    if (sourceSegment.isEmpty()) {
        SegmentUtilities.createChildSegments(reteEvaluator, sourceSegment, source.getSinkPropagator());
    }
    processPeers(sourceSegment, leftTuples);
    Iterator<SegmentMemory> peersIterator = sourceSegment.getPeersWithDataDrivenPathMemoriesIterator();
    while (peersIterator.hasNext()) {
        SegmentMemory smem = peersIterator.next();
        for (PathMemory dataDrivenPmem : smem.getDataDrivenPathMemories()) {
            if (smem.getStagedLeftTuples().getDeleteFirst() == null && smem.getStagedLeftTuples().getUpdateFirst() == null && !dataDrivenPmem.isRuleLinked()) {
                // skip flushing segments that have only inserts staged and the path is not linked
                continue;
            }
            forceFlushLeftTuple(dataDrivenPmem, smem, reteEvaluator, smem.getStagedLeftTuples());
            forceFlushWhenRiaNode(reteEvaluator, dataDrivenPmem);
        }
    }
}
Also used : LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) SegmentMemory(org.drools.core.reteoo.SegmentMemory) PathMemory(org.drools.core.reteoo.PathMemory)

Example 73 with SegmentMemory

use of org.drools.core.reteoo.SegmentMemory 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 74 with SegmentMemory

use of org.drools.core.reteoo.SegmentMemory 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 75 with SegmentMemory

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

the class SegmentCreationTest method tesShareInSubnetwork.

@Test
public void tesShareInSubnetwork() throws Exception {
    KieBase kbase = buildKnowledgeBase("   A() \n", "   A() B() C() \n", "   A()  not ( B() and C() ) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[1];
    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[2];
    RuleTerminalNode rtn3 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];
    wm.insert(new LinkingTest.A());
    wm.insert(new LinkingTest.B());
    wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    // LiaNode  is in it's own segment
    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(liaNode, smem.getRootNode());
    assertEquals(liaNode, smem.getTipNode());
    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());
    SegmentMemory bSmem = rtnSmem1.getNext();
    assertEquals(bNode, bSmem.getRootNode());
    assertEquals(cNode, bSmem.getTipNode());
    // segment is not initialized yet
    assertNull(bSmem.getFirst());
    wm.fireAllRules();
    SegmentMemory rtn2Smem = bSmem.getFirst();
    assertEquals(rtn2, rtn2Smem.getRootNode());
    assertEquals(rtn2, rtn2Smem.getTipNode());
    SegmentMemory riaSmem = rtn2Smem.getNext();
    assertEquals(riaNode, riaSmem.getRootNode());
    assertEquals(riaNode, riaSmem.getTipNode());
    SegmentMemory notSmem = bSmem.getNext();
    assertEquals(notNode, notSmem.getRootNode());
    assertEquals(rtn3, notSmem.getTipNode());
}
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) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

SegmentMemory (org.drools.core.reteoo.SegmentMemory)122 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)75 Test (org.junit.Test)60 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)50 PathMemory (org.drools.core.reteoo.PathMemory)49 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)44 BetaMemory (org.drools.core.reteoo.BetaMemory)42 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)42 JoinNode (org.drools.core.reteoo.JoinNode)37 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)36 ArrayList (java.util.ArrayList)31 List (java.util.List)29 KieBase (org.kie.api.KieBase)24 LeftTuple (org.drools.core.reteoo.LeftTuple)23 Memory (org.drools.core.common.Memory)22 Match (org.kie.api.runtime.rule.Match)22 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)18 Arrays.asList (java.util.Arrays.asList)17 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)17 QueryElementNodeMemory (org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory)14