Skip to content

Instantly share code, notes, and snippets.

@leogomes
Created November 14, 2010 12:59
Show Gist options
  • Save leogomes/676142 to your computer and use it in GitHub Desktop.
Save leogomes/676142 to your computer and use it in GitHub Desktop.
JoinNodeWithCollections
public LeftTupleCollection assertLeftTuple(final LeftTupleCollection leftTupleCollection,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
boolean useLeftMemory = true;
LeftTuple leftTuple = leftTupleCollection.getInsert();
LeftTuple rootLeftTuple = null;
LeftTuple currentLeftTuple = null;
while ( leftTuple != null ) {
if ( this.tupleMemoryEnabled ) {
memory.getLeftTupleMemory().add( leftTuple );
} else {
// This is a hack, to not add closed DroolsQuery objects
Object object = ((InternalFactHandle) context.getFactHandle()).getObject();
if ( object instanceof DroolsQuery && !((DroolsQuery) object).isOpen() ) {
useLeftMemory = false;
} else if ( memory.getLeftTupleMemory() != null ) {
// LeftMemory will be null for sequential (still created for queries).
memory.getLeftTupleMemory().add( leftTuple );
}
}
this.constraints.updateFromTuple( memory.getContext(),
workingMemory,
leftTuple );
// first find the root
LeftTupleSinkNode sink;
RightTuple rightTuple = memory.getRightTupleMemory().getFirst( leftTuple,
(InternalFactHandle) context.getFactHandle() );
for (; rightTuple != null; rightTuple = (RightTuple) rightTuple.getNext() ) {
final InternalFactHandle handle = rightTuple.getFactHandle();
if ( this.constraints.isAllowedCachedLeft( memory.getContext(),
handle ) ) {
sink = this.sink.getFirstSink();
if ( rootLeftTuple == null ) {
currentLeftTuple = new LeftTuple( leftTuple,
rightTuple,
sink,
useLeftMemory );
// record the rootLeftTuple when it's recreated
rootLeftTuple = currentLeftTuple;
} else {
currentLeftTuple = currentLeftTuple.setCollectionNext( new LeftTuple( leftTuple,
rightTuple,
sink,
useLeftMemory ) );
}
sink = sink.getNextLeftTupleSinkNode();
if ( sink != null ) {
// now make a root for each sink, if they exist
LeftTuple peerLeftTuple = currentLeftTuple;
for (sink = sink.getNextLeftTupleSinkNode(); sink != null; sink = sink.getNextLeftTupleSinkNode()) {
peerLeftTuple = new LeftTuple(leftTuple,
peerLeftTuple,
rightTuple,
sink,
useLeftMemory);
}
break;
}
}
}
// The previous LeftTuple that a sink peer links to
LeftTuple prevLeftTuple = currentLeftTuple.getSinkPeerNext();
InternalFactHandle handle = null;
LeftTuple peerLeftTuple = null;
// now create all the other child LeftTuples
for (rightTuple = (RightTuple) rightTuple.getNext(); rightTuple != null; rightTuple = (RightTuple) rightTuple.getNext() ) {
handle = rightTuple.getFactHandle();
if ( this.constraints.isAllowedCachedLeft( memory.getContext(),
handle ) ) {
sink = this.sink.getFirstSink();
peerLeftTuple = new LeftTuple( leftTuple,
rightTuple,
sink,
useLeftMemory );
sink = sink.getNextLeftTupleSinkNode();
if ( sink != null ) {
prevLeftTuple = peerLeftTuple.getLeftParentPrevious().getSinkPeerNext(); // navigate to previous first peer
for (; sink != null; sink = sink.getNextLeftTupleSinkNode()) {
peerLeftTuple = new LeftTuple(leftTuple,
prevLeftTuple,
peerLeftTuple,
rightTuple,
sink,
useLeftMemory);
prevLeftTuple = prevLeftTuple.getSinkPeerNext();
}
}
}
}
leftTuple = leftTuple.getCollectionNext();
}
this.constraints.resetTuple( memory.getContext() );
LeftTupleCollection lc = new LeftTupleCollection();
lc.setInsert( rootLeftTuple );
return lc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment