Class LineReader

  • All Implemented Interfaces:
    java.lang.Cloneable, Actor, Executable, FiringsRecordable, Initializable, TypedActor, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable
    Direct Known Subclasses:
    CSVReader, ExpressionReader

    public class LineReader
    extends Source

    This actor reads a file or URL, one line at a time, and outputs each line as a string. The file or URL is specified using any form acceptable to FileParameter. Before an end of file is reached, the endOfFile output produces false. In the iteration where the last line of the file is read and produced on the output port, this actor produces true on the endOfFile port. In that iteration, postfire() returns false. If the actor is iterated again, after the end of file, then prefire() and postfire() will both return false, output will produce the string "EOF", and endOfFile will produce true.

    In some domains (such as SDF), returning false in postfire() causes the model to cease executing. In other domains (such as DE), this causes the director to avoid further firings of this actor. So usually, the actor will not be invoked again after the end of file is reached.

    This actor reads ahead in the file so that it can produce an output true on endOfFile in the same iteration where it outputs the last line.

    This actor can skip some lines at the beginning of the file or URL, with the number specified by the numberOfLinesToSkip parameter. The default value of this parameter is 0.

    If you need to reset this line reader to start again at the beginning of the file, the way to do this is to call initialize() during the run of the model. This can be done, for example, using a modal model with a transition where reset is enabled.

    Since:
    Ptolemy II 2.2
    Version:
    $Id$
    Author:
    Edward A. Lee, Yuhong Xiong
    See Also:
    FileParameter
    Pt.AcceptedRating:
    Yellow (cxh)
    Pt.ProposedRating:
    Green (eal)
    • Field Detail

      • endOfFile

        public TypedIOPort endOfFile
        An output port that produces false until the end of file is reached, at which point it produces true. The type is boolean.
      • fileOrURL

        public FilePortParameter fileOrURL
        The file name or URL from which to read. This is a string with any form accepted by FileParameter.
        See Also:
        FileParameter
      • numberOfLinesToSkip

        public Parameter numberOfLinesToSkip
        The number of lines to skip at the beginning of the file or URL. This parameter contains an IntToken, initially with value 0. The value of this parameter must be non-negative.
      • _currentLine

        protected java.lang.String _currentLine
        Cache of most recently read data.
      • _nextLine

        protected java.lang.String _nextLine
        The next line after the current line.
      • _reader

        protected java.io.BufferedReader _reader
        The current reader for the input file.
      • _firstFiring

        protected boolean _firstFiring
        First firing indicator.
    • Method Detail

      • attributeChanged

        public void attributeChanged​(Attribute attribute)
                              throws IllegalActionException
        If the specified attribute is fileOrURL and there is an open file being read, then close that file and open the new one; if the attribute is numberOfLinesToSkip and its value is negative, then throw an exception. In the case of fileOrURL, do nothing if the file name is the same as the previous value of this attribute.
        Overrides:
        attributeChanged in class NamedObj
        Parameters:
        attribute - The attribute that has changed.
        Throws:
        IllegalActionException - If the specified attribute is fileOrURL and the file cannot be opened, or the previously opened file cannot be closed; or if the attribute is numberOfLinesToSkip and its value is negative.
      • initialize

        public void initialize()
                        throws IllegalActionException
        If this is called after prefire() has been called but before wrapup() has been called, then close any open file re-open it, skip the number of lines given by the numberOfLinesToSkip parameter, and read the first line to be produced in the next invocation of prefire(). This occurs if this actor is re-initialized during a run of the model.
        Specified by:
        initialize in interface Initializable
        Overrides:
        initialize in class AtomicActor<TypedIOPort>
        Throws:
        IllegalActionException - If the file or URL cannot be opened, or if the lines to be skipped and the first line to be sent out in the fire() method cannot be read.
      • _openAndReadFirstTwoLines

        protected void _openAndReadFirstTwoLines()
                                          throws IllegalActionException
        Open the file and read the first line, putting its value into the _currentLine variable. Also, read the second line, putting its value in the _nextLine variable.
        Throws:
        IllegalActionException - If the file cannot be read.