Search in sources :

Example 61 with TupleMemory

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

the class PhreakJoinNode method doRightDeletes.

public void doRightDeletes(BetaMemory bm, TupleSets<RightTuple> srcRightTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleMemory rtm = bm.getRightTupleMemory();
    for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; ) {
        RightTuple next = rightTuple.getStagedNext();
        if (rightTuple.getMemory() != null) {
            // it may have been staged and never actually added
            rtm.remove(rightTuple);
        }
        if (rightTuple.getFirstChild() != null) {
            LeftTuple childLeftTuple = rightTuple.getFirstChild();
            childLeftTuple.setPropagationContext(rightTuple.getPropagationContext());
            while (childLeftTuple != null) {
                LeftTuple nextChild = childLeftTuple.getRightParentNext();
                RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                childLeftTuple = nextChild;
            }
        }
        rightTuple.clearStaged();
        rightTuple = next;
    }
}
Also used : RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory)

Example 62 with TupleMemory

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

the class PhreakJoinNode method doLeftInserts.

public void doLeftInserts(JoinNode joinNode, LeftTupleSink sink, BetaMemory bm, ReteEvaluator reteEvaluator, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    TupleMemory ltm = bm.getLeftTupleMemory();
    TupleMemory rtm = bm.getRightTupleMemory();
    ContextEntry[] contextEntry = bm.getContext();
    BetaConstraints constraints = joinNode.getRawConstraints();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(joinNode, leftTuple);
        if (useLeftMemory) {
            ltm.add(leftTuple);
        }
        FastIterator it = joinNode.getRightIterator(rtm);
        constraints.updateFromTuple(contextEntry, reteEvaluator, leftTuple);
        for (RightTuple rightTuple = joinNode.getFirstRightTuple(leftTuple, rtm, it); rightTuple != null; rightTuple = (RightTuple) it.next(rightTuple)) {
            if (constraints.isAllowedCachedLeft(contextEntry, rightTuple.getFactHandle())) {
                insertChildLeftTuple(trgLeftTuples, leftTuple, rightTuple, null, null, sink, useLeftMemory);
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    constraints.resetTuple(contextEntry);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ContextEntry(org.drools.core.rule.ContextEntry)

Example 63 with TupleMemory

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

the class PhreakSubnetworkNotExistsNode method doSubNetworkNode.

public static void doSubNetworkNode(BetaNode node, LeftTupleSink sink, BetaMemory bm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TupleSets<RightTuple> srcRightTuples = bm.getStagedRightTuples().takeAll();
    TupleMemory ltm = bm.getLeftTupleMemory();
    boolean tupleMemoryEnabled = node.isLeftTupleMemoryEnabled();
    deleteLeft(srcLeftTuples, trgLeftTuples, stagedLeftTuples, ltm);
    insertRight(node, sink, trgLeftTuples, stagedLeftTuples, srcRightTuples, tupleMemoryEnabled);
    insertLeft(node, sink, srcLeftTuples, trgLeftTuples, ltm, tupleMemoryEnabled);
    updateRight(srcRightTuples);
    deleteRight(node, sink, trgLeftTuples, stagedLeftTuples, srcRightTuples);
    updateLeft(srcLeftTuples, trgLeftTuples, stagedLeftTuples, ltm);
    srcRightTuples.resetAll();
    srcLeftTuples.resetAll();
}
Also used : RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory)

Example 64 with TupleMemory

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

the class PhreakActivationIterator method processLeftTuples.

public static void processLeftTuples(LeftTupleSource node, List<AgendaItem> agendaItems, Set<RuleTerminalNode> nodeSet, ReteEvaluator reteEvaluator) {
    LeftTupleSource node1 = node;
    while (NodeTypeEnums.LeftInputAdapterNode != node1.getType()) {
        node1 = node1.getLeftTupleSource();
    }
    int maxShareCount = node1.getAssociationsSize();
    while (NodeTypeEnums.LeftInputAdapterNode != node.getType()) {
        Memory memory = reteEvaluator.getNodeMemory((MemoryFactory) node);
        if (memory.getSegmentMemory() == null) {
            // segment has never been initialized, which means the rule has never been linked.
            return;
        }
        if (node.getAssociationsSize() == maxShareCount) {
            // the recurse must start from the first split node, otherwise we get partial overlaps in propagations
            if (NodeTypeEnums.isBetaNode(node)) {
                BetaMemory bm;
                if (NodeTypeEnums.AccumulateNode == node.getType()) {
                    AccumulateMemory am = (AccumulateMemory) memory;
                    bm = am.getBetaMemory();
                    FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
                    Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
                    for (; lt != null; lt = (LeftTuple) it.next(lt)) {
                        AccumulateContext accctx = (AccumulateContext) lt.getContextObject();
                        collectFromPeers(accctx.getResultLeftTuple(), agendaItems, nodeSet, reteEvaluator);
                    }
                } else if (NodeTypeEnums.ExistsNode == node.getType()) {
                    bm = (BetaMemory) reteEvaluator.getNodeMemory((MemoryFactory) node);
                    // done off the RightTupleMemory, as exists only have unblocked tuples on the left side
                    FastIterator it = bm.getRightTupleMemory().fullFastIterator();
                    RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it);
                    for (; rt != null; rt = (RightTuple) it.next(rt)) {
                        for (LeftTuple lt = rt.getBlocked(); lt != null; lt = lt.getBlockedNext()) {
                            if (lt.getFirstChild() != null) {
                                collectFromPeers(lt.getFirstChild(), agendaItems, nodeSet, reteEvaluator);
                            }
                        }
                    }
                } else {
                    bm = (BetaMemory) reteEvaluator.getNodeMemory((MemoryFactory) node);
                    FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
                    Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
                    for (; lt != null; lt = (LeftTuple) it.next(lt)) {
                        if (lt.getFirstChild() != null) {
                            collectFromLeftInput(lt.getFirstChild(), agendaItems, nodeSet, reteEvaluator);
                        }
                    }
                }
                return;
            } else if (NodeTypeEnums.FromNode == node.getType()) {
                FromMemory fm = (FromMemory) reteEvaluator.getNodeMemory((MemoryFactory) node);
                TupleMemory ltm = fm.getBetaMemory().getLeftTupleMemory();
                FastIterator it = ltm.fullFastIterator();
                for (LeftTuple lt = (LeftTuple) ltm.getFirst(null); lt != null; lt = (LeftTuple) it.next(lt)) {
                    if (lt.getFirstChild() != null) {
                        collectFromLeftInput(lt.getFirstChild(), agendaItems, nodeSet, reteEvaluator);
                    }
                }
                return;
            }
        }
        node = node.getLeftTupleSource();
    }
    // No beta or from nodes, so must retrieve LeftTuples from the LiaNode.
    // This is done by scanning all the LeftTuples referenced from the FactHandles in the ObjectTypeNode
    LeftInputAdapterNode lian = (LeftInputAdapterNode) node;
    if (!lian.isTerminal()) {
        Memory memory = reteEvaluator.getNodeMemory((MemoryFactory) node);
        if (memory.getSegmentMemory() == null) {
            // segment has never been initialized, which means the rule has never been linked.
            return;
        }
    }
    ObjectSource os = lian.getObjectSource();
    while (os.getType() != NodeTypeEnums.ObjectTypeNode) {
        os = os.getParentObjectSource();
    }
    ObjectTypeNode otn = (ObjectTypeNode) os;
    final ObjectTypeNodeMemory omem = reteEvaluator.getNodeMemory(otn);
    LeftTupleSink firstLiaSink = lian.getSinkPropagator().getFirstLeftTupleSink();
    java.util.Iterator<InternalFactHandle> it = omem.iterator();
    while (it.hasNext()) {
        InternalFactHandle fh = it.next();
        fh.forEachLeftTuple(lt -> {
            if (lt.getTupleSink() == firstLiaSink) {
                collectFromLeftInput(lt, agendaItems, nodeSet, reteEvaluator);
            }
        });
    }
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) Memory(org.drools.core.common.Memory) BetaMemory(org.drools.core.reteoo.BetaMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) BetaMemory(org.drools.core.reteoo.BetaMemory) MemoryFactory(org.drools.core.common.MemoryFactory) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ObjectSource(org.drools.core.reteoo.ObjectSource) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle) 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 65 with TupleMemory

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

the class OOPathReactiveTest method testReactiveDeleteOnLia.

@Test
public void testReactiveDeleteOnLia() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + "  list.add( $toy.getName() );\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession();
    final EntryPointNode epn = ((InternalKnowledgeBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
    final ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Man.class));
    final LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
    final ReactiveFromNode from1 = (ReactiveFromNode) lian.getSinkPropagator().getSinks()[0];
    final ReactiveFromNode from2 = (ReactiveFromNode) from1.getSinkPropagator().getSinks()[0];
    final ReactiveFromNode from3 = (ReactiveFromNode) from2.getSinkPropagator().getSinks()[0];
    final BetaMemory betaMemory = ((InternalWorkingMemory) ksession).getNodeMemory(from3).getBetaMemory();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 12);
    final Child debbie = new Child("Debbie", 11);
    alice.addChild(charlie);
    alice.addChild(debbie);
    charlie.addToy(new Toy("car"));
    charlie.addToy(new Toy("ball"));
    debbie.addToy(new Toy("doll"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball", "doll");
    final TupleMemory tupleMemory = betaMemory.getLeftTupleMemory();
    Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(2);
    Iterator<LeftTuple> it = tupleMemory.iterator();
    for (LeftTuple next = it.next(); next != null; next = it.next()) {
        final Object obj = next.getFactHandle().getObject();
        Assertions.assertThat(obj == charlie || obj == debbie).isTrue();
    }
    list.clear();
    debbie.setAge(10);
    ksession.fireAllRules();
    Assertions.assertThat(list).hasSize(0);
    ;
    Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(1);
    it = tupleMemory.iterator();
    for (LeftTuple next = it.next(); next != null; next = it.next()) {
        final Object obj = next.getFactHandle().getObject();
        Assertions.assertThat(obj == charlie).isTrue();
    }
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.mvel.compiler.oopath.model.Man) Woman(org.drools.mvel.compiler.oopath.model.Woman) Child(org.drools.mvel.compiler.oopath.model.Child) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Toy(org.drools.mvel.compiler.oopath.model.Toy) Test(org.junit.Test)

Aggregations

TupleMemory (org.drools.core.reteoo.TupleMemory)66 LeftTuple (org.drools.core.reteoo.LeftTuple)59 RightTuple (org.drools.core.reteoo.RightTuple)52 FastIterator (org.drools.core.util.FastIterator)40 BetaConstraints (org.drools.core.common.BetaConstraints)36 ContextEntry (org.drools.core.rule.ContextEntry)36 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)23 BetaMemory (org.drools.core.reteoo.BetaMemory)23 Accumulate (org.drools.core.rule.Accumulate)12 Tuple (org.drools.core.spi.Tuple)9 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)8 ArrayList (java.util.ArrayList)6 BetaNode (org.drools.core.reteoo.BetaNode)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)5 BaseAccumulation (org.drools.core.reteoo.AccumulateNode.BaseAccumulation)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 PhreakPropagationContext (org.drools.core.common.PhreakPropagationContext)4 AccumulateContextEntry (org.drools.core.reteoo.AccumulateNode.AccumulateContextEntry)4 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)4