001/* An event that represents a token or tokens sent.
002
003 Copyright (c) 2006-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor;
029
030import ptolemy.data.Token;
031
032///////////////////////////////////////////////////////////////////
033//// TokenSentEvent
034
035/**
036 An event that is published by IOPorts whenever broadcast, send or
037 sendInside is called.  The appropriate event should be published
038 whenever a token is transferred from one port to another.  In
039 Kepler the provenance recorder uses these events to save
040 intermediate results of the workflow.  In the future these events
041 could be used for "smart" reruns or a fault tolerance mechanism.
042
043 @author  Oscar Barney
044 @version $Id$
045 @since Ptolemy II 5.2
046 @deprecated Use IOPortEvent instead.
047 @Pt.ProposedRating Red (barney)
048 @Pt.AcceptedRating Red (barney)
049 */
050@Deprecated
051public class TokenSentEvent {
052    /** Create a new token sent event with the given parameters.  This
053     *  constructor is used when an array of tokens is sent.
054     *  @param source The IOPort the token came from.
055     *  @param channel Channel the token was sent on.
056     *  @param tokens The token array used for the send.
057     *  @param vectorLength The number of tokens sent.
058     */
059    public TokenSentEvent(IOPort source, int channel, Token[] tokens,
060            int vectorLength) {
061        _port = source;
062        _channel = channel;
063        _tokenArray = tokens;
064        _token = null;
065        _vectorLength = vectorLength;
066    }
067
068    /** Create a new token sent event with the given parameters.  This
069     *  constructor is used when a token is sent or sent inside.
070     *  @param source The IOPort the token came from.
071     *  @param channel Channel the token was sent on.
072     *  @param token The token that was sent.
073     */
074    public TokenSentEvent(IOPort source, int channel, Token token) {
075        _port = source;
076        _channel = channel;
077        _tokenArray = null;
078        _token = token;
079        _vectorLength = -1;
080    }
081
082    /** Create a new token sent event with the given parameters.  This
083     *  constructor is used when a token is broadcast.
084     *  @param source The IOPort the token came from.
085     *  @param token The token that was sent.
086     */
087    public TokenSentEvent(IOPort source, Token token) {
088        _port = source;
089        _channel = -1;
090        _tokenArray = null;
091        _token = token;
092        _vectorLength = -1;
093    }
094
095    /** Create a new token sent event with the given parameters.  This
096     *  constructor is used when an array of tokens is broadcast.
097     *  @param source The IOPort the token came from.
098     *  @param tokens The token array used for the broadcast.
099     *  @param vectorLength The number of tokens sent.
100     */
101    public TokenSentEvent(IOPort source, Token[] tokens, int vectorLength) {
102        _port = source;
103        _channel = -1;
104        _tokenArray = tokens;
105        _token = null;
106        _vectorLength = vectorLength;
107    }
108
109    ///////////////////////////////////////////////////////////////////
110    ////                         public methods                    ////
111
112    /** Return the channel the token was sent on.
113     *  @return The channel number.
114     */
115    public int getChannel() {
116        return _channel;
117    }
118
119    /** Return the port that the token was sent from.
120     *  @return An instance of IOPort.
121     */
122    public IOPort getPort() {
123        return _port;
124    }
125
126    /** Return the token that was sent by the IOPort.  The variable
127     *  _token will be null if the event was for a sent array. To get
128     *  the tokens that were sent use getTokenArray() instead.
129     *  @return The token sent by the IOPort.
130     */
131    public Token getToken() {
132        return _token;
133    }
134
135    /** Return the array of tokens that the IOPort sent. The variable
136     *  _tokenArray will be null if an individual token was sent instead
137     *  of an array.
138     *  @return The array of tokens which were sent.
139     */
140    public Token[] getTokenArray() {
141        //may want to return the array shortened to be vector length long?
142        return _tokenArray;
143    }
144
145    /** Return the number of tokens in the array sent by the IOPort.
146     *  The variable _vectorLength will be -1 if an individual token
147     *  was sent instead of an array of tokens.
148     *  @return The number of tokens sent by the port.
149     */
150    public int getVectorLength() {
151        return _vectorLength;
152    }
153
154    /** Return a string representation of this event.
155     *  @return A user-readable string describing the event.
156     */
157    @Override
158    public String toString() {
159        StringBuffer buffer = new StringBuffer();
160        buffer.append("The port " + _port + " sent ");
161        if (_vectorLength != -1) {
162            buffer.append(_vectorLength);
163            buffer.append(" tokens ");
164        } else {
165            buffer.append(_token.toString());
166        }
167        if (_channel != -1) {
168            buffer.append(" to channel " + _channel + ".");
169        } else {
170            buffer.append(" to all channels");
171        }
172
173        return buffer.toString();
174    }
175
176    ///////////////////////////////////////////////////////////////////
177    ////                         private variables                 ////
178
179    //The channel the token was sent on.
180    private int _channel;
181
182    // The IOPort that was activated.
183    private IOPort _port;
184
185    //The token sent by the IOPort.
186    private Token _token;
187
188    //The array of tokens sent by the IOPort.
189    private Token[] _tokenArray;
190
191    //The number of tokens from the array that were sent by the IOPort.
192    private int _vectorLength;
193
194}