LeftTupleSets srcLeftTuples) {
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
InternalFactHandle fh = (InternalFactHandle) leftTuple.getObject();
DroolsQuery dquery = (DroolsQuery) fh.getObject();
Object[] argTemplate = queryNode.getQueryElement().getArgTemplate(); // an array of declr, variable and literals
Object[] args = new Object[argTemplate.length]; // the actual args, to be created from the template
// first copy everything, so that we get the literals. We will rewrite the declarations and variables next
System.arraycopy(argTemplate,
0,
args,
0,
args.length);
int[] declIndexes = queryNode.getQueryElement().getDeclIndexes();
for (int i = 0, length = declIndexes.length; i < length; i++) {
Declaration declr = (Declaration) argTemplate[declIndexes[i]];
Object tupleObject = leftTuple.get(declr).getObject();
Object o;
if (tupleObject instanceof DroolsQuery) {
// If the query passed in a Variable, we need to use it
ArrayElementReader arrayReader = (ArrayElementReader) declr.getExtractor();
if (((DroolsQuery) tupleObject).getVariables()[arrayReader.getIndex()] != null) {
o = Variable.v;
} else {
o = declr.getValue(wm,
tupleObject);
}
} else {
o = declr.getValue(wm,
tupleObject);
}
args[declIndexes[i]] = o;
}
int[] varIndexes = queryNode.getQueryElement().getVariableIndexes();
for (int i = 0, length = varIndexes.length; i < length; i++) {
if (argTemplate[varIndexes[i]] == Variable.v) {
// Need to check against the arg template, as the varIndexes also includes re-declared declarations
args[varIndexes[i]] = Variable.v;
}
}
dquery.setParameters(args);
((UnificationNodeViewChangedEventListener) dquery.getQueryResultCollector()).setVariables(varIndexes);
SegmentMemory qsmem = qmem.getQuerySegmentMemory();
LeftInputAdapterNode lian = (LeftInputAdapterNode) qsmem.getRootNode();
LiaNodeMemory lmem = (LiaNodeMemory) qsmem.getNodeMemories().getFirst();
if (dquery.isOpen()) {
LeftTuple childLeftTuple = fh.getFirstLeftTuple(); // there is only one, all other LTs are peers
LeftInputAdapterNode.doUpdateObject(childLeftTuple, childLeftTuple.getPropagationContext(), wm, lian, false, lmem, qmem.getQuerySegmentMemory());
flushTupleQuery( lmem, wm );
} else {
if (fh.getFirstLeftTuple() != null) {
throw new RuntimeException("defensive programming while testing"); // @TODO remove later (mdp)
}
LiaNodeMemory lm = (LiaNodeMemory) qmem.getQuerySegmentMemory().getNodeMemories().get(0);
LeftInputAdapterNode.doInsertObject(fh, leftTuple.getPropagationContext(), lian, wm, lm, false, dquery.isOpen());
flushTupleQuery( lm, wm );
}
leftTuple.clearStaged();