Object tupleObject = leftTuple.get( declr ).getObject();
if ( tupleObject instanceof DroolsQuery ) {
ArrayElementReader reader = (ArrayElementReader) declr.getExtractor();
DroolsQuery q = (DroolsQuery) tupleObject;
Variable v = q.getVariables()[reader.getIndex()];
// is that parameter an output Variable
if ( v != null ) {
if ( !v.isSet() ) {
// it's not set yet, so we need to pass back any unified values
// If the declaration resolves to a variable being passed in, we need to add that to the variable indexes, so it's copied
if ( srcVarIndexes == null ) {
srcVarIndexes = new ArrayList<Integer>();
trgVars = new ArrayList<Variable>();
}
trgVars.add( v ); // this needs to be here, so we can pass the value back
srcVarIndexes.add( declIndexes[i] );
args[declIndexes[i]] = Variable.variable;
continue;
}
}
}
Object o = declr.getValue( workingMemory,
tupleObject );
args[declIndexes[i]] = o;
}
int[] varIndexes = this.queryElement.getVariableIndexes();
if ( srcVarIndexes != null ) {
// we have Variable inputs to handle
// now merge the two, by adding new onto the end of the old
int length = varIndexes.length;
varIndexes = new int[varIndexes.length + srcVarIndexes.size()];
System.arraycopy( this.queryElement.getVariableIndexes(),
0,
varIndexes,
0,
length );
for ( int i = 0; i < srcVarIndexes.size(); i++ ) {
varIndexes[i + length] = srcVarIndexes.get( i );
}
}
UnificationNodeViewChangedEventListener collector = new UnificationNodeViewChangedEventListener( leftTuple,
varIndexes,
this.sink,
this.tupleMemoryEnabled );
DroolsQuery queryObject = new DroolsQuery( this.queryElement.getQueryName(),
args,
collector,
false );
collector.setDroolsQuery( queryObject );
InternalFactHandle handle = workingMemory.getFactHandleFactory().newFactHandle( queryObject,
workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf( EntryPoint.DEFAULT,
queryObject ),
workingMemory,
null );
workingMemory.insert( handle,
queryObject,
null,
null,
workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf( workingMemory.getEntryPoint(),
queryObject ) );
workingMemory.getFactHandleFactory().destroyFactHandle( handle );
LeftTuple childLeftTuple = leftTuple.firstChild;
LeftTuple temp = null;
while ( childLeftTuple != null ) {
int varsLength = this.queryElement.getVariableIndexes().length;
if ( srcVarIndexes != null ) {
QueryElementFactHandle qeh = (QueryElementFactHandle) childLeftTuple.getLastHandle();
Object[] resultObjects = (Object[]) qeh.getObject();
for ( int j = 0, i = varsLength; i < varIndexes.length; i++, j++ ) {
Variable v = trgVars.get( j );
v.setValue( resultObjects[i] );
}
}
temp = childLeftTuple;
this.sink.doPropagateAssertLeftTuple( context,
workingMemory,
childLeftTuple,
childLeftTuple.getLeftTupleSink() );
if ( srcVarIndexes != null ) {
QueryElementFactHandle qeh = (QueryElementFactHandle) childLeftTuple.getLastHandle();
for ( int i = 0; i < trgVars.size(); i++ ) {
Variable v = trgVars.get( i );
v.unSet();
}
}
childLeftTuple = childLeftTuple.getLeftParentNext();
temp.setLeftParentNext( null );