InternalWorkingMemory workingMemory) {
QueryTerminalNode node = (QueryTerminalNode) resultLeftTuple.getLeftTupleSink();
QueryImpl query = node.getQuery();
Declaration[] decls = node.getDeclarations();
DroolsQuery dquery = (DroolsQuery) this.factHandle.getObject();
Object[] objects = new Object[ determineResultSize( query, dquery ) ];
Declaration decl;
for (int variable : this.variables) {
decl = decls[variable];
objects[variable] = decl.getValue(workingMemory,
resultLeftTuple.get(decl).getObject());
}
QueryElementFactHandle resultHandle = createQueryResultHandle(context,
workingMemory,
objects);
RightTuple rightTuple = createResultRightTuple(resultHandle, resultLeftTuple, dquery.isOpen());
boolean pass = true;
if ( query.isAbductive() ) {
int numArgs = dquery.getElements().length;
AbductiveQuery aq = (( AbductiveQuery) query );
for ( int j = 0; j < numArgs; j++ ) {
if ( dquery.getElements()[ j ] != null ) {
objects[ j ] = dquery.getElements()[ j ];
}
}
Object abduced = aq.abduce( Arrays.copyOfRange( objects, 0, numArgs - 1 ) );
if ( abduced != null ) {
EqualityKey key = ( (NamedEntryPoint) workingMemory.getEntryPoint( workingMemory.getEntryPointId() ) ).getTruthMaintenanceSystem().get( abduced );
InternalFactHandle handle;
if ( key != null ) {
handle = key.getFactHandle();
abduced = handle.getObject();
} else {
handle = (InternalFactHandle) ((InternalWorkingMemoryActions) workingMemory).insert( abduced,
MODE.POSITIVE.getId(),
false,
true,
query,
(RuleTerminalNodeLeftTuple) resultLeftTuple );
}
BeliefSet bs = handle.getEqualityKey().getBeliefSet();
if ( bs == null ) {
abduced = handle.getObject();
} else {
if ( ! bs.isPositive() ) {
pass = false;
}
}
} else {
// query was successful, but nothing could be abduced.
}
objects[ objects.length - 1 ] = abduced;
}
if ( pass ) {
LeftTupleSink sink = dquery.getLeftTupleSink();
LeftTuple childLeftTuple = sink.createLeftTuple( this.leftTuple, rightTuple, sink );
boolean stagedInsertWasEmpty = dquery.getResultLeftTupleSets().addInsert(childLeftTuple);
if ( stagedInsertWasEmpty ) {
dquery.getQueryNodeMemory().setNodeDirtyWithoutNotify();
}
}
}