|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectptolemy.kernel.util.NamedObj
ptolemy.kernel.InstantiableNamedObj
ptolemy.kernel.Entity
ptolemy.kernel.ComponentEntity
ptolemy.actor.AtomicActor
ptolemy.actor.TypedAtomicActor
ptolemy.actor.lib.Source
ptolemy.actor.lib.io.LineReader
public class LineReader
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. It reads the first line in preinitialize(), and subsequently reads a new line in each invocation of postfire(). The line read is produced on the output in the next iteration after it is read.
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.
FileParameter
,
Serialized Form
Yellow (cxh) |
Green (eal) |
Nested Class Summary |
---|
Nested classes/interfaces inherited from class ptolemy.kernel.Entity |
---|
Entity.ContainedObjectsIterator |
Field Summary | |
---|---|
protected java.lang.String |
_currentLine
Cache of most recently read data. |
private boolean |
_firedSinceWrapup
Indicator that the fire() method has been called, but wrapup has not. |
private java.lang.String |
_previousFileOrURL
Previous value of fileOrURL parameter. |
private boolean |
_reachedEOF
Indicator that we have reached the end of file. |
protected java.io.BufferedReader |
_reader
The current reader for the input file. |
TypedIOPort |
endOfFile
An output port that produces false until the end of file is reached, at which point it produces true. |
FileParameter |
fileOrURL
The file name or URL from which to read. |
Parameter |
numberOfLinesToSkip
The number of lines to skip at the beginning of the file or URL. |
Fields inherited from class ptolemy.actor.lib.Source |
---|
output, trigger |
Fields inherited from class ptolemy.actor.AtomicActor |
---|
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested |
Fields inherited from class ptolemy.kernel.util.NamedObj |
---|
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS |
Fields inherited from interface ptolemy.actor.Executable |
---|
COMPLETED, NOT_READY, STOP_ITERATING |
Constructor Summary | |
---|---|
LineReader(CompositeEntity container,
java.lang.String name)
Construct an actor with the given container and name. |
Method Summary | |
---|---|
private void |
_openAndReadFirstLine()
Open the file and read the first line. |
void |
attributeChanged(Attribute attribute)
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. |
java.lang.Object |
clone(Workspace workspace)
Clone the actor into the specified workspace. |
void |
fire()
Output the data read in the preinitialize() or in the previous invocation of postfire(), if there is any. |
void |
initialize()
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(). |
boolean |
postfire()
Read the next line from the file. |
boolean |
prefire()
Return false if there is no more data available in the file. |
void |
preinitialize()
Open the file or URL, skip the number of lines specified by the numberOfLinesToSkip parameter, and read the first line to be sent out in the fire() method. |
void |
wrapup()
Close the reader if there is one. |
Methods inherited from class ptolemy.actor.TypedAtomicActor |
---|
_addPort, _fireAt, _fireAt, attributeTypeChanged, clone, newPort, typeConstraintList, typeConstraints |
Methods inherited from class ptolemy.kernel.ComponentEntity |
---|
_adjustDeferrals, _checkContainer, _getContainedObject, _propagateExistence, getContainer, instantiate, isAtomic, isOpaque, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence, setName |
Methods inherited from class ptolemy.kernel.Entity |
---|
_description, _exportMoMLContents, _removePort, _validateSettables, connectedPortList, connectedPorts, containedObjectsIterator, getAttribute, getPort, getPorts, linkedRelationList, linkedRelations, portList, removeAllPorts, setClassDefinition, uniqueName |
Methods inherited from class ptolemy.kernel.InstantiableNamedObj |
---|
_setParent, exportMoML, getChildren, getElementName, getParent, getPrototypeList, isClassDefinition, isWithinClassDefinition |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface ptolemy.actor.Actor |
---|
createReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, newReceiver, outputPortList |
Methods inherited from interface ptolemy.actor.Executable |
---|
isFireFunctional, isStrict, iterate, stop, stopFire, terminate |
Methods inherited from interface ptolemy.actor.Initializable |
---|
addInitializable, removeInitializable |
Methods inherited from interface ptolemy.kernel.util.Nameable |
---|
description, getContainer, getDisplayName, getFullName, getName, getName, setName |
Methods inherited from interface ptolemy.kernel.util.Derivable |
---|
getDerivedLevel, getDerivedList, propagateValue |
Field Detail |
---|
public TypedIOPort endOfFile
public FileParameter fileOrURL
FileParameter
public Parameter numberOfLinesToSkip
protected java.lang.String _currentLine
protected java.io.BufferedReader _reader
private boolean _firedSinceWrapup
private java.lang.String _previousFileOrURL
private boolean _reachedEOF
Constructor Detail |
---|
public LineReader(CompositeEntity container, java.lang.String name) throws IllegalActionException, NameDuplicationException
container
- The container.name
- The name of this actor.
IllegalActionException
- If the actor cannot be contained
by the proposed container.
NameDuplicationException
- If the container already has an
actor with this name.Method Detail |
---|
public void attributeChanged(Attribute attribute) throws IllegalActionException
attributeChanged
in class NamedObj
attribute
- The attribute that has changed.
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.public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
clone
in class AtomicActor
workspace
- The workspace for the new object.
java.lang.CloneNotSupportedException
- If a derived class contains
an attribute that cannot be cloned.NamedObj.exportMoML(Writer, int, String)
,
NamedObj.setDeferringChangeRequests(boolean)
public void fire() throws IllegalActionException
fire
in interface Executable
fire
in class Source
IllegalActionException
- If there's no director.public void initialize() throws IllegalActionException
initialize
in interface Initializable
initialize
in class AtomicActor
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.public boolean postfire() throws IllegalActionException
postfire
in interface Executable
postfire
in class AtomicActor
IllegalActionException
- If there is a problem reading
the file.public boolean prefire() throws IllegalActionException
prefire
in interface Executable
prefire
in class Source
IllegalActionException
- If the superclass throws it.public void preinitialize() throws IllegalActionException
preinitialize
in interface Initializable
preinitialize
in class AtomicActor
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.public void wrapup() throws IllegalActionException
wrapup
in interface Initializable
wrapup
in class AtomicActor
IllegalActionException
- If an IO error occurs.private void _openAndReadFirstLine() throws IllegalActionException
IllegalActionException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |