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);
}
}
}
}
}
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;
}
}
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();
}
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;
}
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);
}
Aggregations