001: package org.drools.reteoo;
002:
003: import org.drools.common.InternalFactHandle;
004: import org.drools.common.InternalWorkingMemory;
005: import org.drools.spi.PropagationContext;
006:
007: public class CompositeTupleSinkAdapter implements TupleSinkPropagator {
008: private TupleSinkNodeList sinks;
009:
010: public CompositeTupleSinkAdapter() {
011: this .sinks = new TupleSinkNodeList();
012: }
013:
014: public void addTupleSink(final TupleSink sink) {
015: this .sinks.add((TupleSinkNode) sink);
016: }
017:
018: public void removeTupleSink(final TupleSink sink) {
019: this .sinks.remove((TupleSinkNode) sink);
020: }
021:
022: public void propagateAssertTuple(final ReteTuple tuple,
023: final InternalFactHandle handle,
024: final PropagationContext context,
025: final InternalWorkingMemory workingMemory) {
026:
027: for (TupleSinkNode sink = this .sinks.getFirst(); sink != null; sink = sink
028: .getNextTupleSinkNode()) {
029: sink.assertTuple(new ReteTuple(tuple, handle), context,
030: workingMemory);
031: }
032: }
033:
034: public void propagateAssertTuple(final ReteTuple tuple,
035: final PropagationContext context,
036: final InternalWorkingMemory workingMemory) {
037: for (TupleSinkNode sink = this .sinks.getFirst(); sink != null; sink = sink
038: .getNextTupleSinkNode()) {
039: sink.assertTuple(new ReteTuple(tuple), context,
040: workingMemory);
041: }
042: }
043:
044: public void propagateRetractTuple(final ReteTuple tuple,
045: final InternalFactHandle handle,
046: final PropagationContext context,
047: final InternalWorkingMemory workingMemory) {
048: for (TupleSinkNode sink = this .sinks.getFirst(); sink != null; sink = sink
049: .getNextTupleSinkNode()) {
050: sink.retractTuple(new ReteTuple(tuple, handle), context,
051: workingMemory);
052: }
053: }
054:
055: public void propagateRetractTuple(final ReteTuple tuple,
056: final PropagationContext context,
057: final InternalWorkingMemory workingMemory) {
058: for (TupleSinkNode sink = this .sinks.getFirst(); sink != null; sink = sink
059: .getNextTupleSinkNode()) {
060: sink.retractTuple(new ReteTuple(tuple), context,
061: workingMemory);
062: }
063: }
064:
065: public void createAndPropagateAssertTuple(
066: final InternalFactHandle handle,
067: final PropagationContext context,
068: final InternalWorkingMemory workingMemory) {
069: for (TupleSinkNode sink = this .sinks.getFirst(); sink != null; sink = sink
070: .getNextTupleSinkNode()) {
071: sink.assertTuple(new ReteTuple(handle), context,
072: workingMemory);
073: }
074: }
075:
076: public void createAndPropagateRetractTuple(
077: final InternalFactHandle handle,
078: final PropagationContext context,
079: final InternalWorkingMemory workingMemory) {
080: for (TupleSinkNode sink = this .sinks.getFirst(); sink != null; sink = sink
081: .getNextTupleSinkNode()) {
082: sink.retractTuple(new ReteTuple(handle), context,
083: workingMemory);
084: }
085: }
086:
087: public TupleSink[] getSinks() {
088: final TupleSink[] sinkArray = new TupleSink[this .sinks.size()];
089:
090: int i = 0;
091: for (TupleSinkNode sink = this .sinks.getFirst(); sink != null; sink = sink
092: .getNextTupleSinkNode()) {
093: sinkArray[i++] = sink;
094: }
095:
096: return sinkArray;
097: }
098:
099: // public void propagateNewTupleSink(TupleMatch tupleMatch,
100: // PropagationContext context,
101: // InternalWorkingMemory workingMemory) {
102: //
103: // final TupleSink sink = sinks.getLast();
104: // final ReteTuple tuple = new ReteTuple( tupleMatch.getTuple(),
105: // tupleMatch.getObjectMatches().getFactHandle(),
106: // sink );
107: // tupleMatch.addJoinedTuple( tuple );
108: // tuple.assertTuple( context,
109: // workingMemory );
110: // }
111: //
112: // public void propagateNewTupleSink(ReteTuple tuple,
113: // PropagationContext context,
114: // InternalWorkingMemory workingMemory) {
115: //
116: // final TupleSink sink = sinks.getLast();
117: // ReteTuple child = new ReteTuple( tuple,
118: // sink );
119: // tuple.addChildEntry( child );
120: // child.assertTuple( context,
121: // workingMemory );
122: // }
123: //
124: // public void propagateNewTupleSink(InternalFactHandle handle,
125: // LinkedList list,
126: // PropagationContext context,
127: // InternalWorkingMemory workingMemory) {
128: // TupleSink sink = this.sinks.getLast();
129: // ReteTuple tuple = new ReteTuple( handle,
130: // sink );
131: // list.add( new LinkedListEntry( tuple ) );
132: // tuple.assertTuple( context,
133: // workingMemory );
134: // }
135: //
136: // /**
137: // * @inheritDoc
138: // */
139: // public List getPropagatedTuples(final Map memory,
140: // final InternalWorkingMemory workingMemory,
141: // final TupleSink sink) {
142: // int index = 0;
143: // for ( TupleSinkNode node = this.sinks.getFirst(); node != null; node = node.getNextTupleSinkNode() ) {
144: // if ( node.equals( sink ) ) {
145: // break;
146: // }
147: // index++;
148: // }
149: //
150: // final List propagatedTuples = new ArrayList( memory.size() );
151: //
152: // for ( final Iterator it = memory.values().iterator(); it.hasNext(); ) {
153: // final LinkedList tuples = (LinkedList) it.next();
154: // LinkedListEntry wrapper = (LinkedListEntry) tuples.getFirst();
155: // for ( int i = 0; i < index; i++ ) {
156: // wrapper = (LinkedListEntry) wrapper.getNext();
157: // }
158: // propagatedTuples.add( wrapper.getObject() );
159: // }
160: //
161: // return propagatedTuples;
162: // }
163:
164: public int size() {
165: return this.sinks.size();
166: }
167: }
|