Search in sources :

Example 1 with AccumulateNode

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

the class LeftTupleIterator method getNextLeftTuple.

public LeftTuple getNextLeftTuple(LeftTupleSource source, LeftTupleSink sink, LeftTuple leftTuple, InternalWorkingMemory wm) {
    if (otnIterator != null) {
        LeftTuple leftParent = leftTuple.getLeftParent();
        while (leftTuple != null) {
            leftTuple = leftTuple.getHandleNext();
            for (; leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
                // Iterate to find the next left tuple for this sink, skip tuples for other sinks due to sharing split
                if (leftTuple.getTupleSink() == sink) {
                    return leftTuple;
                }
            }
        }
        // We have a parent LeftTuple so try there next
        if (leftParent != null) {
            // we know it has to be evalNode query element node
            while (leftParent != null) {
                leftParent = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftParent, wm);
                if (leftParent != null) {
                    for (leftTuple = leftParent.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
                        if (leftTuple.getTupleSink() == sink) {
                            return leftTuple;
                        }
                    }
                }
            }
            return null;
        }
        // We have exhausted the current FactHandle, now try the next
        while (otnIterator.hasNext()) {
            InternalFactHandle handle = otnIterator.next();
            leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
            if (leftTuple != null) {
                return leftTuple;
            }
        }
        // We've exhausted this OTN so set the iterator to null
        otnIterator = null;
    } else if (source instanceof AccumulateNode) {
        // when using phreak, accumulate result tuples will not link to leftParent, but to parent instead
        BetaMemory memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
        FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator(leftTuple.getParent());
        LeftTuple childLeftTuple = leftTuple;
        leftTuple = childLeftTuple.getParent();
        while (leftTuple != null) {
            if (childLeftTuple == null) {
                childLeftTuple = leftTuple.getFirstChild();
            } else {
                childLeftTuple = childLeftTuple.getHandleNext();
            }
            for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
                if (childLeftTuple.getTupleSink() == sink) {
                    return childLeftTuple;
                }
            }
            leftTuple = (LeftTuple) localIt.next(leftTuple);
        }
    } else 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(leftTuple.getLeftParent());
        LeftTuple childLeftTuple = leftTuple;
        leftTuple = childLeftTuple.getLeftParent();
        while (leftTuple != null) {
            if (childLeftTuple == null) {
                childLeftTuple = leftTuple.getFirstChild();
            } else {
                childLeftTuple = childLeftTuple.getHandleNext();
            }
            for (; 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);
        RightTuple rightTuple = leftTuple.getLeftParent().getBlocker();
        FastIterator localIt = memory.getRightTupleMemory().fullFastIterator(rightTuple);
        for (LeftTuple childleftTuple = leftTuple.getHandleNext(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
            if (childleftTuple.getTupleSink() == sink) {
                return childleftTuple;
            }
        }
        leftTuple = leftTuple.getLeftParent();
        // now move onto next RightTuple
        while (rightTuple != null) {
            if (rightTuple.getBlocked() != null) {
                if (leftTuple != null) {
                    leftTuple = leftTuple.getBlockedNext();
                } else {
                    leftTuple = rightTuple.getBlocked();
                }
                for (; 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 EvalConditionNode || source instanceof QueryElementNode) {
        LeftTuple childLeftTuple = leftTuple;
        if (leftTuple != null) {
            leftTuple = leftTuple.getLeftParent();
            while (leftTuple != null) {
                if (childLeftTuple != null) {
                    childLeftTuple = childLeftTuple.getHandleNext();
                } else {
                    childLeftTuple = leftTuple.getFirstChild();
                }
                for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
                    if (childLeftTuple.getTupleSink() == sink) {
                        return childLeftTuple;
                    }
                }
                if (source instanceof EvalConditionNode) {
                    leftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftTuple, wm);
                } else {
                    leftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftTuple, 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) 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) FastIterator(org.drools.core.util.FastIterator)

Example 2 with AccumulateNode

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

the class CollectBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    boolean existSubNetwort = false;
    final Collect collect = (Collect) rce;
    context.pushRuleComponent(collect);
    final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
    final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
    final Pattern sourcePattern = collect.getSourcePattern();
    // get builder for the pattern
    final ReteooComponentBuilder builder = utils.getBuilderFor(sourcePattern);
    // save tuple source and pattern offset for later if needed
    final LeftTupleSource tupleSource = context.getTupleSource();
    final int currentPatternIndex = context.getCurrentPatternOffset();
    // builds the source pattern
    builder.build(context, utils, sourcePattern);
    // if object source is null, then we need to adapt tuple source into a subnetwork
    if (context.getObjectSource() == null) {
        RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), context.getTupleSource(), tupleSource, context);
        // attach right input adapter node to convert tuple source into an object source
        context.setObjectSource(utils.attachNode(context, riaNode));
        // restore tuple source from before the start of the sub network
        context.setTupleSource(tupleSource);
        // create a tuple start equals constraint and set it in the context
        final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
        final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
        betaConstraints.add(constraint);
        context.setBetaconstraints(betaConstraints);
        existSubNetwort = true;
    }
    BetaConstraints binder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
    // indexing for the results should be always disabled
    BetaConstraints resultBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
    CollectAccumulator accumulator = new CollectAccumulator(collect, existSubNetwort);
    Accumulate accumulate = new SingleAccumulate(sourcePattern, sourcePattern.getRequiredDeclarations(), accumulator);
    AccumulateNode accNode = context.getComponentFactory().getNodeFactoryService().buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), // source binder
    binder, resultBinder, accumulate, existSubNetwort, context);
    context.setTupleSource(utils.attachNode(context, accNode));
    // source pattern was bound, so nulling context
    context.setObjectSource(null);
    context.setCurrentPatternOffset(currentPatternIndex);
    context.popRuleComponent();
}
Also used : Pattern(org.drools.core.rule.Pattern) BetaConstraints(org.drools.core.common.BetaConstraints) Collect(org.drools.core.rule.Collect) AccumulateNode(org.drools.core.reteoo.AccumulateNode) ArrayList(java.util.ArrayList) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) SingleAccumulate(org.drools.core.rule.SingleAccumulate) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) CollectAccumulator(org.drools.core.base.accumulators.CollectAccumulator) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 3 with AccumulateNode

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

the class AccumulateNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    AccumulateNode an = (AccumulateNode) node;
    DefaultNodeInfo ni = info.getNodeInfo(node);
    final AccumulateMemory memory = (AccumulateMemory) info.getSession().getNodeMemory(an);
    ni.setMemoryEnabled(true);
    if (an.isObjectMemoryEnabled()) {
        ni.setFactMemorySize(memory.getBetaMemory().getRightTupleMemory().size());
    }
    if (an.isLeftTupleMemoryEnabled()) {
        ni.setTupleMemorySize(memory.getBetaMemory().getLeftTupleMemory().size());
        FastIterator it = memory.getBetaMemory().getLeftTupleMemory().fullFastIterator();
        int i = 0;
        for (Tuple leftTuple = BetaNode.getFirstTuple(memory.getBetaMemory().getLeftTupleMemory(), it); leftTuple != null; leftTuple = (Tuple) it.next(leftTuple)) {
            AccumulateContext ctx = (AccumulateContext) leftTuple.getContextObject();
            if (ctx != null && ctx.result != null) {
                i++;
            }
        }
        ni.setCreatedFactHandles(i);
    }
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) AccumulateNode(org.drools.core.reteoo.AccumulateNode) FastIterator(org.drools.core.util.FastIterator) Tuple(org.drools.core.spi.Tuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext)

Example 4 with AccumulateNode

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

the class BackwardChainingTest method testSubNetworksAndQueries.

@Test(timeout = 10000)
public void testSubNetworksAndQueries() throws Exception {
    String str = "" + "package org.drools.compiler.test  \n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import java.util.Map\n" + "import java.util.HashMap\n" + "global List list\n" + "dialect \"mvel\"\n" + "\n" + "declare Location\n" + "    thing : String \n" + "    location : String \n" + "end" + "\n" + "declare Edible\n" + "   thing : String\n" + "end" + "\n" + "query whereFood( String x, String y ) \n" + "    Location(x, y;) Edible(x;) \n" + "end\n" + "\n" + "query look(String place, List food ) \n" + "    $s : String() // just here to give a OTN lookup point\n" + "    food := List() from accumulate( whereFood(thing, place;) ," + "                                    collectList( thing ) )\n" + "    exists( whereFood(thing, place;) )\n" + "    not( whereFood(thing, place;) and\n " + "         String( this == $s ) from thing )\n" + "end\n" + "\n" + "rule init when\n" + "then\n" + "        \n" + "        insert( new Location(\"apple\", \"kitchen\") );\n" + "        insert( new Location(\"crackers\", \"kitchen\") );\n" + "        insert( new Location(\"broccoli\", \"kitchen\") );\n" + "        insert( new Location(\"computer\", \"office\") );\n" + "        insert( new Edible(\"apple\") );\n" + "        insert( new Edible(\"crackers\") );\n" + "end\n" + "";
    logger.debug(str);
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    // Get the accumulate node, so we can test it's memory later
    // now check beta memory was correctly cleared
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == String.class) {
            node = n;
            break;
        }
    }
    BetaNode stringBetaNode = (BetaNode) node.getObjectSinkPropagator().getSinks()[0];
    QueryElementNode queryElementNode1 = (QueryElementNode) stringBetaNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode1 = (RightInputAdapterNode) queryElementNode1.getSinkPropagator().getSinks()[0];
    AccumulateNode accNode = (AccumulateNode) riaNode1.getObjectSinkPropagator().getSinks()[0];
    QueryElementNode queryElementNode2 = (QueryElementNode) accNode.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode2 = (RightInputAdapterNode) queryElementNode2.getSinkPropagator().getSinks()[0];
    ExistsNode existsNode = (ExistsNode) riaNode2.getObjectSinkPropagator().getSinks()[0];
    QueryElementNode queryElementNode3 = (QueryElementNode) existsNode.getSinkPropagator().getSinks()[0];
    FromNode fromNode = (FromNode) queryElementNode3.getSinkPropagator().getSinks()[0];
    RightInputAdapterNode riaNode3 = (RightInputAdapterNode) fromNode.getSinkPropagator().getSinks()[0];
    NotNode notNode = (NotNode) riaNode3.getObjectSinkPropagator().getSinks()[0];
    KieSession ksession = createKnowledgeSession(kbase);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) ksession);
    AccumulateMemory accMemory = (AccumulateMemory) wm.getNodeMemory(accNode);
    BetaMemory existsMemory = (BetaMemory) wm.getNodeMemory(existsNode);
    FromMemory fromMemory = (FromMemory) wm.getNodeMemory(fromNode);
    BetaMemory notMemory = (BetaMemory) wm.getNodeMemory(notNode);
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    ksession.setGlobal("list", list);
    FactHandle fh = ksession.insert("bread");
    ksession.fireAllRules();
    final List food = new ArrayList();
    QueryResults results = null;
    // Execute normal query and check no subnetwork tuples are left behind
    results = ksession.getQueryResults("look", new Object[] { "kitchen", Variable.v });
    assertEquals(1, results.size());
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        food.addAll((Collection) row.get("food"));
        logger.debug(row.get("food").toString());
    }
    assertEquals(2, food.size());
    assertContains(new String[] { "crackers", "apple" }, food);
    assertEquals(0, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(0, existsMemory.getRightTupleMemory().size());
    assertEquals(0, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
    // Now execute an open query and ensure the memory is left populated
    food.clear();
    final List foodUpdated = new ArrayList();
    LiveQuery query = ksession.openLiveQuery("look", new Object[] { "kitchen", Variable.v }, new ViewChangedEventListener() {

        public void rowUpdated(Row row) {
            foodUpdated.addAll((Collection) row.get("food"));
        }

        public void rowDeleted(Row row) {
        }

        public void rowInserted(Row row) {
            food.addAll((Collection) row.get("food"));
        }
    });
    assertEquals(2, food.size());
    assertContains(new String[] { "crackers", "apple" }, food);
    assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(2, existsMemory.getRightTupleMemory().size());
    assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
    food.clear();
    // Now try again, make sure it only delete's it's own tuples
    results = ksession.getQueryResults("look", new Object[] { "kitchen", Variable.v });
    assertEquals(1, results.size());
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        food.addAll((Collection) row.get("food"));
        logger.debug(row.get("food").toString());
    }
    assertEquals(2, food.size());
    assertContains(new String[] { "crackers", "apple" }, food);
    assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(2, existsMemory.getRightTupleMemory().size());
    assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
    food.clear();
    // do an update and check it's  still memory size 2
    // however this time the food should be empty, as 'crackers' now blocks the not.
    ksession.update(fh, "crackers");
    ksession.fireAllRules();
    assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(2, existsMemory.getRightTupleMemory().size());
    assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(1, notMemory.getRightTupleMemory().size());
    assertEquals(0, foodUpdated.size());
    // do an update and check it's  still memory size 2
    // this time
    ksession.update(fh, "oranges");
    ksession.fireAllRules();
    assertEquals(2, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(2, existsMemory.getRightTupleMemory().size());
    assertEquals(2, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
    assertEquals(2, food.size());
    assertContains(new String[] { "crackers", "apple" }, food);
    // Close the open
    query.close();
    assertEquals(0, accMemory.getBetaMemory().getRightTupleMemory().size());
    assertEquals(0, existsMemory.getRightTupleMemory().size());
    assertEquals(0, fromMemory.getBetaMemory().getLeftTupleMemory().size());
    assertEquals(0, notMemory.getRightTupleMemory().size());
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) NotNode(org.drools.core.reteoo.NotNode) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) AccumulateNode(org.drools.core.reteoo.AccumulateNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) FromNode(org.drools.core.reteoo.FromNode) ExistsNode(org.drools.core.reteoo.ExistsNode) LiveQuery(org.kie.api.runtime.rule.LiveQuery) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) BetaNode(org.drools.core.reteoo.BetaNode) BetaMemory(org.drools.core.reteoo.BetaMemory) QueryResults(org.kie.api.runtime.rule.QueryResults) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) Collection(java.util.Collection) Row(org.kie.api.runtime.rule.Row) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with AccumulateNode

use of org.drools.core.reteoo.AccumulateNode 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)

Aggregations

AccumulateNode (org.drools.core.reteoo.AccumulateNode)6 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)4 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)4 ArrayList (java.util.ArrayList)3 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)3 BetaMemory (org.drools.core.reteoo.BetaMemory)3 BetaNode (org.drools.core.reteoo.BetaNode)3 ExistsNode (org.drools.core.reteoo.ExistsNode)3 FromNode (org.drools.core.reteoo.FromNode)3 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)3 NotNode (org.drools.core.reteoo.NotNode)3 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)3 QueryElementNode (org.drools.core.reteoo.QueryElementNode)3 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)3 Tuple (org.drools.core.spi.Tuple)3 FastIterator (org.drools.core.util.FastIterator)3 BetaConstraints (org.drools.core.common.BetaConstraints)2 TupleStartEqualsConstraint (org.drools.core.common.TupleStartEqualsConstraint)2 EvalConditionNode (org.drools.core.reteoo.EvalConditionNode)2 JoinNode (org.drools.core.reteoo.JoinNode)2