Search in sources :

Example 1 with ObjectSource

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

the class SegmentUtilities method getQueryOtn.

private static ObjectTypeNode getQueryOtn(LeftTupleSource lts) {
    while (!(lts instanceof LeftInputAdapterNode)) {
        lts = lts.getLeftTupleSource();
    }
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) lts;
    ObjectSource os = liaNode.getObjectSource();
    while (!(os instanceof EntryPointNode)) {
        os = os.getParentObjectSource();
    }
    return ((EntryPointNode) os).getQueryNode();
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) ObjectSource(org.drools.core.reteoo.ObjectSource) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 2 with ObjectSource

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

the class PhreakActivationIterator method processLeftTuples.

public static void processLeftTuples(LeftTupleSource node, List<AgendaItem> agendaItems, Set<RuleTerminalNode> nodeSet, InternalWorkingMemory wm) {
    LeftTupleSource node1 = node;
    while (NodeTypeEnums.LeftInputAdapterNode != node1.getType()) {
        node1 = node1.getLeftTupleSource();
    }
    int maxShareCount = node1.getAssociationsSize();
    while (NodeTypeEnums.LeftInputAdapterNode != node.getType()) {
        Memory memory = wm.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, wm);
                    }
                } else if (NodeTypeEnums.ExistsNode == node.getType()) {
                    bm = (BetaMemory) wm.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, wm);
                            }
                        }
                    }
                } else {
                    bm = (BetaMemory) wm.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, wm);
                        }
                    }
                }
                return;
            } else if (NodeTypeEnums.FromNode == node.getType()) {
                FromMemory fm = (FromMemory) wm.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, wm);
                    }
                }
                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;
    Memory memory = wm.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 = wm.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, wm);
            }
        });
    }
}
Also used : AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) 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) RightTuple(org.drools.core.reteoo.RightTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) 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) 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 3 with ObjectSource

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

the class BetaNodeBuilder method build.

public BetaNode build() {
    NodeFactory nFactory = buildContext.getComponentFactory().getNodeFactoryService();
    EntryPointNode epn = buildContext.getKnowledgeBase().getRete().getEntryPointNodes().values().iterator().next();
    ObjectTypeNode otn = nFactory.buildObjectTypeNode(buildContext.getNextId(), epn, new ClassObjectType(leftType), buildContext);
    LeftInputAdapterNode leftInput = nFactory.buildLeftInputAdapterNode(buildContext.getNextId(), otn, buildContext);
    ObjectSource rightInput = nFactory.buildObjectTypeNode(buildContext.getNextId(), epn, new ClassObjectType(rightType), buildContext);
    ReteTesterHelper reteTesterHelper = new ReteTesterHelper();
    Pattern pattern = new Pattern(0, new ClassObjectType(leftType));
    // BetaNodeFieldConstraint betaConstraint = null;
    BetaConstraints betaConstraints = null;
    if (constraintFieldName != null) {
        ClassFieldAccessorStore store = (ClassFieldAccessorStore) reteTesterHelper.getStore();
        InternalReadAccessor extractor = store.getReader(leftType, leftFieldName);
        Declaration declr = new Declaration(leftVariableName, extractor, pattern);
        betaConstraints = new SingleBetaConstraints(reteTesterHelper.getBoundVariableConstraint(rightType, constraintFieldName, declr, constraintOperator), buildContext.getKnowledgeBase().getConfiguration());
    } else {
        betaConstraints = new EmptyBetaConstraints();
    }
    switch(nodeType) {
        case NodeTypeEnums.JoinNode:
            return new JoinNode(0, leftInput, rightInput, betaConstraints, buildContext);
        case NodeTypeEnums.NotNode:
            return new NotNode(0, leftInput, rightInput, betaConstraints, buildContext);
        case NodeTypeEnums.ExistsNode:
            return new ExistsNode(0, leftInput, rightInput, betaConstraints, buildContext);
    }
    throw new IllegalStateException("Unable to build Node");
}
Also used : Pattern(org.drools.core.rule.Pattern) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) NotNode(org.drools.core.reteoo.NotNode) BetaConstraints(org.drools.core.common.BetaConstraints) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) ClassObjectType(org.drools.core.base.ClassObjectType) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) ExistsNode(org.drools.core.reteoo.ExistsNode) EntryPointNode(org.drools.core.reteoo.EntryPointNode) NodeFactory(org.drools.core.reteoo.builder.NodeFactory) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ObjectSource(org.drools.core.reteoo.ObjectSource) Declaration(org.drools.core.rule.Declaration) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 4 with ObjectSource

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

the class NodesPartitioningTest method checkNode.

private void checkNode(NetworkNode node) {
    if (node instanceof EntryPointNode) {
        assertSame(RuleBasePartitionId.MAIN_PARTITION, node.getPartitionId());
    } else if (node instanceof ObjectTypeNode) {
        assertSame(RuleBasePartitionId.MAIN_PARTITION, node.getPartitionId());
        checkPartitionedSinks((ObjectTypeNode) node);
    } else if (node instanceof ObjectSource) {
        ObjectSource source = ((ObjectSource) node).getParentObjectSource();
        if (!(source instanceof ObjectTypeNode)) {
            assertSame(source.getPartitionId(), node.getPartitionId());
        }
    } else if (node instanceof BetaNode) {
        ObjectSource rightInput = ((BetaNode) node).getRightInput();
        if (!(rightInput instanceof ObjectTypeNode)) {
            assertSame(rightInput.getPartitionId(), node.getPartitionId());
        }
        LeftTupleSource leftInput = ((BetaNode) node).getLeftTupleSource();
        assertSame(leftInput.getPartitionId(), node.getPartitionId());
    } else if (node instanceof TerminalNode) {
        LeftTupleSource leftInput = ((TerminalNode) node).getLeftTupleSource();
        assertSame(leftInput.getPartitionId(), node.getPartitionId());
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) BetaNode(org.drools.core.reteoo.BetaNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSource(org.drools.core.reteoo.ObjectSource) TerminalNode(org.drools.core.reteoo.TerminalNode)

Example 5 with ObjectSource

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

the class ReteDumper method getSinks.

public static Sink[] getSinks(BaseNode node) {
    Sink[] sinks = null;
    if (node instanceof EntryPointNode) {
        EntryPointNode source = (EntryPointNode) node;
        Collection<ObjectTypeNode> otns = source.getObjectTypeNodes().values();
        sinks = otns.toArray(new Sink[otns.size()]);
    } else if (node instanceof ObjectSource) {
        ObjectSource source = (ObjectSource) node;
        sinks = source.getObjectSinkPropagator().getSinks();
    } else if (node instanceof LeftTupleSource) {
        LeftTupleSource source = (LeftTupleSource) node;
        sinks = source.getSinkPropagator().getSinks();
    }
    return sinks;
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) Sink(org.drools.core.reteoo.Sink) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectSource(org.drools.core.reteoo.ObjectSource)

Aggregations

ObjectSource (org.drools.core.reteoo.ObjectSource)9 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)7 LeftTupleSource (org.drools.core.reteoo.LeftTupleSource)6 EntryPointNode (org.drools.core.reteoo.EntryPointNode)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)5 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)3 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)3 BetaMemory (org.drools.core.reteoo.BetaMemory)3 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)3 LeftTuple (org.drools.core.reteoo.LeftTuple)3 RightTuple (org.drools.core.reteoo.RightTuple)3 Tuple (org.drools.core.spi.Tuple)3 FastIterator (org.drools.core.util.FastIterator)3 BetaNode (org.drools.core.reteoo.BetaNode)2 ExistsNode (org.drools.core.reteoo.ExistsNode)2 JoinNode (org.drools.core.reteoo.JoinNode)2 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)2 NotNode (org.drools.core.reteoo.NotNode)2 ObjectTypeNodeMemory (org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)2 Sink (org.drools.core.reteoo.Sink)2