001: package org.drools.reteoo;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.Serializable;
020:
021: import org.drools.common.BaseNode;
022: import org.drools.common.InternalWorkingMemory;
023: import org.drools.spi.PropagationContext;
024:
025: /**
026: * A source of <code>ReteTuple</code> s for a <code>TupleSink</code>.
027: *
028: * <p>
029: * Nodes that propagate <code>Tuples</code> extend this class.
030: * </p>
031: *
032: * @see TupleSource
033: * @see ReteTuple
034: *
035: * @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
036: * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
037: */
038: public abstract class TupleSource extends BaseNode implements
039: Serializable {
040: // ------------------------------------------------------------
041: // Instance members
042: // ------------------------------------------------------------
043:
044: /** The destination for <code>Tuples</code>. */
045: protected TupleSinkPropagator sink;
046:
047: // ------------------------------------------------------------
048: // Constructors
049: // ------------------------------------------------------------
050:
051: /**
052: * Single parameter constructor that specifies the unique id of the node.
053: *
054: * @param id
055: */
056: TupleSource(final int id) {
057: super (id);
058: this .sink = EmptyTupleSinkAdapter.getInstance();
059: }
060:
061: // ------------------------------------------------------------
062: // Instance methods
063: // ------------------------------------------------------------
064:
065: /**
066: * Adds the <code>TupleSink</code> so that it may receive
067: * <code>Tuples</code> propagated from this <code>TupleSource</code>.
068: *
069: * @param tupleSink
070: * The <code>TupleSink</code> to receive propagated
071: * <code>Tuples</code>.
072: */
073: protected void addTupleSink(final TupleSink tupleSink) {
074: if (this .sink == EmptyTupleSinkAdapter.getInstance()) {
075: this .sink = new SingleTupleSinkAdapter(tupleSink);
076: } else if (this .sink instanceof SingleTupleSinkAdapter) {
077: final CompositeTupleSinkAdapter sinkAdapter = new CompositeTupleSinkAdapter();
078: sinkAdapter.addTupleSink(this .sink.getSinks()[0]);
079: sinkAdapter.addTupleSink(tupleSink);
080: this .sink = sinkAdapter;
081: } else {
082: ((CompositeTupleSinkAdapter) this .sink)
083: .addTupleSink(tupleSink);
084: }
085: }
086:
087: /**
088: * Removes the <code>TupleSink</code>
089: *
090: * @param tupleSink
091: * The <code>TupleSink</code> to remove
092: */
093: protected void removeTupleSink(final TupleSink tupleSink) {
094: if (this .sink == EmptyTupleSinkAdapter.getInstance()) {
095: throw new IllegalArgumentException(
096: "Cannot remove a sink, when the list of sinks is null");
097: }
098:
099: if (this .sink instanceof SingleTupleSinkAdapter) {
100: this .sink = EmptyTupleSinkAdapter.getInstance();
101: } else {
102: final CompositeTupleSinkAdapter sinkAdapter = (CompositeTupleSinkAdapter) this .sink;
103: sinkAdapter.removeTupleSink(tupleSink);
104: if (sinkAdapter.size() == 1) {
105: this .sink = new SingleTupleSinkAdapter(sinkAdapter
106: .getSinks()[0]);
107: }
108: }
109: }
110:
111: public TupleSinkPropagator getSinkPropagator() {
112: return this .sink;
113: }
114:
115: public abstract void updateSink(TupleSink sink,
116: PropagationContext context,
117: InternalWorkingMemory workingMemory);
118:
119: }
|