Search in sources :

Example 1 with QueryElementNodeMemory

use of org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory in project drools by kiegroup.

the class PhreakQueryTerminalNode method checkAndTriggerQueryReevaluation.

public static void checkAndTriggerQueryReevaluation(InternalAgenda agenda, LinkedList<StackEntry> stack, Tuple rootEntry, DroolsQuery dquery) {
    StackEntry stackEntry = dquery.getStackEntry();
    if (!isAdded(stack, stackEntry)) {
        // Ignore unless stackEntry is not added to stack
        // node must be marked as dirty
        ((QueryElementNodeMemory) stackEntry.getNodeMem()).setNodeDirtyWithoutNotify();
        if (stackEntry.getRmem().getPathEndNode().getPathNodes()[0] == ((LeftTupleSink) rootEntry.getTupleSink()).getLeftTupleSource()) {
            // query is recursive, so just re-add the stack entry to the current stack. This happens for reactive queries, triggered by a beta node right input
            stack.add(stackEntry);
        } else {
            // parents is anther rule/query need to notify for agenda to schedule. query is reactive, triggered by right input,
            List<PathMemory> pmems = dquery.getRuleMemories();
            if (pmems != null) {
                // reactivity comes form within the query, so need to notify parent rules to evaluate the results
                for (int i = 0, length = pmems.size(); i < length; i++) {
                    PathMemory pmem = pmems.get(i);
                    // method already ignores is rule is activated and on agenda
                    pmem.doLinkRule(agenda);
                }
            }
        }
    }
}
Also used : QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) PathMemory(org.drools.core.reteoo.PathMemory)

Example 2 with QueryElementNodeMemory

use of org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory in project drools by kiegroup.

the class RuleNetworkEvaluator method evalQueryNode.

private boolean evalQueryNode(PathMemory pmem, NetworkNode node, long bit, Memory nodeMem, SegmentMemory[] smems, int smemIndex, TupleSets<LeftTuple> trgTuples, InternalWorkingMemory wm, LinkedList<StackEntry> stack, TupleSets<LeftTuple> srcTuples, LeftTupleSinkNode sink, TupleSets<LeftTuple> stagedLeftTuples) {
    QueryElementNodeMemory qmem = (QueryElementNodeMemory) nodeMem;
    if (srcTuples.isEmpty() && qmem.getResultLeftTuples().isEmpty()) {
        // no point in evaluating query element, and setting up stack, if there is nothing to process
        return false;
    }
    QueryElementNode qnode = (QueryElementNode) node;
    if (log.isTraceEnabled()) {
        int offset = getOffset(node);
        log.trace("{} query result tuples {}", indent(offset), qmem.getResultLeftTuples().toStringSizes());
    }
    // result tuples can happen when reactivity occurs inside of the query, prior to evaluation
    // we will need special behaviour to add the results again, when this query result resumes
    qmem.getResultLeftTuples().addTo(trgTuples);
    qmem.setNodeCleanWithoutNotify();
    if (!srcTuples.isEmpty()) {
        // only process the Query Node if there are src tuples
        StackEntry stackEntry = new StackEntry(node, bit, sink, pmem, nodeMem, smems, smemIndex, trgTuples, true, true);
        stack.add(stackEntry);
        pQueryNode.doNode(qnode, (QueryElementNodeMemory) nodeMem, stackEntry, wm, srcTuples, trgTuples, stagedLeftTuples);
        SegmentMemory qsmem = ((QueryElementNodeMemory) nodeMem).getQuerySegmentMemory();
        List<PathMemory> qpmems = qsmem.getPathMemories();
        // Build the evaluation information for each 'or' branch
        for (int i = 0; i < qpmems.size(); i++) {
            PathMemory qpmem = qpmems.get(i);
            pmem = qpmem;
            smems = qpmem.getSegmentMemories();
            smemIndex = 0;
            // 0
            SegmentMemory smem = smems[smemIndex];
            LeftTupleNode liaNode = (LeftInputAdapterNode) qpmem.getPathEndNode().getPathNodes()[0];
            if (liaNode == smem.getTipNode()) {
                // segment only has liaNode in it
                // nothing is staged in the liaNode, so skip to next segment
                // 1
                smem = smems[++smemIndex];
                node = smem.getRootNode();
                nodeMem = smem.getNodeMemories().getFirst();
                bit = 1;
            } else {
                // lia is in shared segment, so point to next node
                node = liaNode.getSinkPropagator().getFirstLeftTupleSink();
                // skip the liaNode memory
                nodeMem = smem.getNodeMemories().getFirst().getNext();
                bit = 2;
            }
            trgTuples = smem.getStagedLeftTuples().takeAll();
            stackEntry = new StackEntry(node, bit, null, pmem, nodeMem, smems, smemIndex, trgTuples, false, true);
            if (log.isTraceEnabled()) {
                int offset = getOffset(stackEntry.getNode());
                log.trace("{} ORQueue branch={} {} {}", indent(offset), i, stackEntry.getNode().toString(), trgTuples.toStringSizes());
            }
            stack.add(stackEntry);
        }
        return true;
    } else {
        return false;
    }
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) QueryElementNode(org.drools.core.reteoo.QueryElementNode) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) PathMemory(org.drools.core.reteoo.PathMemory) RiaPathMemory(org.drools.core.reteoo.RiaPathMemory) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 3 with QueryElementNodeMemory

use of org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory in project drools by kiegroup.

the class SegmentUtilities method processQueryNode.

private static boolean processQueryNode(QueryElementNode queryNode, InternalWorkingMemory wm, LeftTupleSource segmentRoot, SegmentMemory smem, long nodePosMask) {
    // Initialize the QueryElementNode and have it's memory reference the actual query SegmentMemory
    SegmentMemory querySmem = getQuerySegmentMemory(wm, segmentRoot, queryNode);
    QueryElementNodeMemory queryNodeMem = smem.createNodeMemory(queryNode, wm);
    queryNodeMem.setNodePosMaskBit(nodePosMask);
    queryNodeMem.setQuerySegmentMemory(querySmem);
    queryNodeMem.setSegmentMemory(smem);
    return !queryNode.getQueryElement().isAbductive();
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory)

Example 4 with QueryElementNodeMemory

use of org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeQueryElementNodeMemory.

private static ProtobufMessages.NodeMemory writeQueryElementNodeMemory(final int nodeId, final Memory memory, final InternalWorkingMemory wm) {
    org.drools.core.util.Iterator<LeftTuple> it = LeftTupleIterator.iterator(wm, ((QueryElementNodeMemory) memory).getNode());
    ProtobufMessages.NodeMemory.QueryElementNodeMemory.Builder _query = ProtobufMessages.NodeMemory.QueryElementNodeMemory.newBuilder();
    for (LeftTuple leftTuple = it.next(); leftTuple != null; leftTuple = it.next()) {
        InternalFactHandle handle = (InternalFactHandle) leftTuple.getContextObject();
        FactHandle _handle = ProtobufMessages.FactHandle.newBuilder().setId(handle.getId()).setRecency(handle.getRecency()).build();
        ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext.Builder _context = ProtobufMessages.NodeMemory.QueryElementNodeMemory.QueryContext.newBuilder().setTuple(PersisterHelper.createTuple(leftTuple)).setHandle(_handle);
        LeftTuple childLeftTuple = leftTuple.getFirstChild();
        while (childLeftTuple != null) {
            RightTuple rightParent = childLeftTuple.getRightParent();
            _context.addResult(ProtobufMessages.FactHandle.newBuilder().setId(rightParent.getFactHandle().getId()).setRecency(rightParent.getFactHandle().getRecency()).build());
            while (childLeftTuple != null && childLeftTuple.getRightParent() == rightParent) {
                // skip to the next child that has a different right parent
                childLeftTuple = childLeftTuple.getHandleNext();
            }
        }
        _query.addContext(_context.build());
    }
    return _query.getContextCount() > 0 ? ProtobufMessages.NodeMemory.newBuilder().setNodeId(nodeId).setNodeType(ProtobufMessages.NodeMemory.NodeType.QUERY_ELEMENT).setQueryElement(_query.build()).build() : null;
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) InternalFactHandle(org.drools.core.common.InternalFactHandle) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)

Example 5 with QueryElementNodeMemory

use of org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory in project drools by kiegroup.

the class RuleNetworkEvaluator method evalStackEntry.

public void evalStackEntry(StackEntry entry, LinkedList<StackEntry> stack, RuleExecutor executor, InternalAgenda agenda) {
    NetworkNode node = entry.getNode();
    Memory nodeMem = entry.getNodeMem();
    TupleSets<LeftTuple> trgTuples = entry.getTrgTuples();
    if (node.getType() == NodeTypeEnums.QueryElementNode) {
        // copy across the results, if any from the query node memory
        QueryElementNodeMemory qmem = (QueryElementNodeMemory) nodeMem;
        qmem.setNodeCleanWithoutNotify();
        qmem.getResultLeftTuples().addTo(trgTuples);
    }
    LeftTupleSinkNode sink = entry.getSink();
    PathMemory pmem = entry.getRmem();
    SegmentMemory[] smems = entry.getSmems();
    int smemIndex = entry.getSmemIndex();
    boolean processRian = entry.isProcessRian();
    long bit = entry.getBit();
    if (entry.isResumeFromNextNode()) {
        SegmentMemory smem = smems[smemIndex];
        if (node != smem.getTipNode()) {
            // get next node and node memory in the segment
            LeftTupleSink nextSink = sink.getNextLeftTupleSinkNode();
            if (nextSink == null) {
                node = sink;
            } else {
                // there is a nested subnetwork, take out path
                node = nextSink;
            }
            nodeMem = nodeMem.getNext();
            // update bit to new node
            bit = bit << 1;
        } else {
            // Reached end of segment, start on new segment.
            SegmentPropagator.propagate(smem, trgTuples, agenda.getWorkingMemory());
            smem = smems[++smemIndex];
            trgTuples = smem.getStagedLeftTuples().takeAll();
            node = smem.getRootNode();
            nodeMem = smem.getNodeMemories().getFirst();
            // update bit to start of new segment
            bit = 1;
        }
    }
    if (log.isTraceEnabled()) {
        int offset = getOffset(node);
        log.trace("{} Resume {} {}", indent(offset), node.toString(), trgTuples.toStringSizes());
    }
    innerEval(pmem, node, bit, nodeMem, smems, smemIndex, trgTuples, agenda, stack, processRian, executor);
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ConditionalBranchMemory(org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) TimerNodeMemory(org.drools.core.reteoo.TimerNode.TimerNodeMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) EvalMemory(org.drools.core.reteoo.EvalConditionNode.EvalMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) RiaPathMemory(org.drools.core.reteoo.RiaPathMemory) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) NetworkNode(org.drools.core.common.NetworkNode) LeftTuple(org.drools.core.reteoo.LeftTuple) LeftTupleSinkNode(org.drools.core.reteoo.LeftTupleSinkNode) PathMemory(org.drools.core.reteoo.PathMemory) RiaPathMemory(org.drools.core.reteoo.RiaPathMemory)

Aggregations

QueryElementNodeMemory (org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory)5 PathMemory (org.drools.core.reteoo.PathMemory)3 SegmentMemory (org.drools.core.reteoo.SegmentMemory)3 LeftTuple (org.drools.core.reteoo.LeftTuple)2 RiaPathMemory (org.drools.core.reteoo.RiaPathMemory)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)1 EventFactHandle (org.drools.core.common.EventFactHandle)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 Memory (org.drools.core.common.Memory)1 NetworkNode (org.drools.core.common.NetworkNode)1 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)1 FactHandle (org.drools.core.marshalling.impl.ProtobufMessages.FactHandle)1 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1 ConditionalBranchMemory (org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory)1 EvalMemory (org.drools.core.reteoo.EvalConditionNode.EvalMemory)1 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)1 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)1 LeftTupleNode (org.drools.core.reteoo.LeftTupleNode)1