001/* An actor that outputs doubles read from a URL.
002
003 @Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the
009 above copyright notice and the following two paragraphs appear in all
010 copies of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION 2
026 COPYRIGHTENDKEY
027 */
028package ptolemy.actor.lib;
029
030import java.io.IOException;
031import java.util.StringTokenizer;
032
033import ptolemy.data.DoubleToken;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037
038///////////////////////////////////////////////////////////////////
039//// DoubleReader
040
041/**
042 This actor reads tokens from an URL, and output them. Each entry in
043 the file corresponds to one iteration. If there are multiple fires in
044 the iteration, the same token will be repeated.  This actor has a
045 multiport, where each port corresponds to one column in the data file.
046
047 <p> The file format at the URL is assumed as the following.  A newline
048 character separates the rows, and a tab or a space character separates
049 the columns.
050
051 <p> The <i>sourceURL</i> parameter should be set to the name of the
052 file, specified as a fully qualified URL.  If the <i>sourceURL</i>
053 parameter is an empty string, then the System.in is used for input.
054
055 It is possible to load a file from the local file system by using the
056 prefix "file://" instead of "http://". Relative file paths are
057 allowed. To specify a file relative to the current directory, use
058 "../" or "./". For example, if the current directory contains a file
059 called "test.txt", then <i>sourceURL</i> should be set to
060 "file:./test.txt". If the parent directory contains a file called
061 "test.txt", then <i>sourceURL</i> should be set to
062 "file:../test.txt". To reference the file test.txt, located at
063 "/tmp/test.txt", <i>sourceURL</i> should be set to
064 "file:///tmp/test.txt" The default value is "file:///tmp/test.txt".
065
066 <p>FIXME: The type of the output ports is set to Double for now.
067 It should read a line in the prefire() and refer the type
068 from there.
069 <p>FIXME: Reader should read in expressions and serialized tokens
070
071 @author  Jie Liu, Christopher Hylands
072 @version $Id$
073 @since Ptolemy II 2.0
074 @Pt.ProposedRating Red (liuj)
075 @Pt.AcceptedRating Red (liuj)
076 @deprecated Use ExpressionReader instead.
077 */
078@Deprecated
079public class DoubleReader extends URLReader {
080    /** Construct an actor with the given container and name.
081     *  @param container The container.
082     *  @param name The name of this actor.
083     *  @exception IllegalActionException If the actor cannot be contained
084     *   by the proposed container.
085     *  @exception NameDuplicationException If the container already has an
086     *   actor with this name.
087     */
088    public DoubleReader(CompositeEntity container, String name)
089            throws IllegalActionException, NameDuplicationException {
090        super(container, name);
091    }
092
093    /** Output the data read in the prefire.
094     *  @exception IllegalActionException If there's no director.
095     */
096    @Override
097    public void fire() throws IllegalActionException {
098        super.fire();
099
100        for (int i = 0; i < _dataSize; i++) {
101            output.send(i, new DoubleToken(_data[i]));
102        }
103    }
104
105    /** Open the file at the URL, and set the width of the output.
106     *  @exception IllegalActionException Not thrown in this base class
107     */
108    @Override
109    public void initialize() throws IllegalActionException {
110        super.initialize();
111        _dataSize = output.getWidth();
112        _data = new double[_dataSize];
113        attributeChanged(sourceURL);
114    }
115
116    /** Read one row from the input and prepare for output them.
117     *  @exception IllegalActionException If an IO error occurs.
118     */
119    @Override
120    public boolean prefire() throws IllegalActionException {
121        try {
122            _dataSize = output.getWidth();
123
124            if (_data.length != _dataSize) {
125                _data = new double[_dataSize];
126            }
127
128            String oneRow = _reader.readLine();
129
130            if (oneRow == null) {
131                return false;
132            }
133
134            StringTokenizer tokenizer = new StringTokenizer(oneRow);
135            int columnCount = tokenizer.countTokens();
136
137            if (_dataSize > columnCount) {
138                _dataSize = columnCount;
139            }
140
141            for (int i = 0; i < _dataSize; i++) {
142                _data[i] = Double.valueOf(tokenizer.nextToken()).doubleValue();
143            }
144
145            return super.prefire();
146        } catch (IOException ex) {
147            throw new IllegalActionException(this, ex, "prefire() failed");
148        }
149    }
150
151    ///////////////////////////////////////////////////////////////////
152    ////                         private members                   ////
153    // Cache of one row.
154    // FIXME: Should we clone this?
155    private double[] _data;
156
157    // Valid enties in the data array.
158    // FIXME: Should we clone this?
159    private int _dataSize;
160}