public class CSVReader extends LineReader
This actor reads a file or URL, one line at a time, and outputs each line except the first as a record. The first line of the file gives the names of the fields of the output records. The remaining lines give the values of the fields. The output is an ordered record token, which means that the order defined in the first line is preserved.
NOTE: By default, this actor imposes no type constraints on its output. To use it in a model, you must either enable backward type inference (a parameter at the top level of the model), or explicitly declare the output type (by selecting Configure-Ports in the context menu). If you use backward type inference, then the constraints are inferred from how you use the output. For example, if you extract a record field of a particular type, then the output will be constrained to be a record that contains that field. If you declare output types specifically, then every line read from the file must conform. For example, if you set the output the type constraint to "[x = int, y = double]" then the output will be an ordered record where the first field is named "x" and has type int, and the second field is named "y" and has type double. If any line in the file violates this typing, then an exception will be thrown.
If any line has more values than the first line, then the trailing values will be ignored. If any line has fewer values than the first line, then the field values will be an empty string.
By default, the separator between field names and values is a comma, so the file format is the standard CSV (comma-separated value) format. The separator parameter enables changing the separator to tabs or semicolons.
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 two lines in preinitialize(), and subsequently reads a new line in each invocation of postfire(). The data type of the output is also set in preinitialize(), after reading the first line, which defines the structure of the record. line read is produced on the output in the next iteration after it is read.
FileParameter
Entity.ContainedObjectsIterator
Modifier and Type | Field and Description |
---|---|
StringParameter |
separator
A specification of the separator between items in the table.
|
Parameter |
trimSpaces
If true, then trim spaces around each field name and value.
|
_currentLine, _firstFiring, _nextLine, _reader, endOfFile, fileOrURL, numberOfLinesToSkip
_triggered, output, trigger
_typesValid
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
COMPLETED, NOT_READY, STOP_ITERATING
Constructor and Description |
---|
CSVReader(CompositeEntity container,
java.lang.String name)
Construct an actor with the given container and name.
|
Modifier and Type | Method and Description |
---|---|
protected java.util.Set<Inequality> |
_defaultTypeConstraints()
Override the default to eliminate the default type constraints/.
|
void |
attributeChanged(Attribute attribute)
If the specified attribute is separator then set a local
variable with the value of the separator.
|
void |
fire()
Output the data read in the preinitialize() or in the previous
invocation of postfire(), if there is any.
|
void |
wrapup()
Wrapup execution of this actor.
|
_openAndReadFirstTwoLines, clone, initialize, postfire
_customTypeConstraints, prefire
_containedTypeConstraints, _fireAt, _fireAt, attributeTypeChanged, clone, isBackwardTypeInferenceEnabled, newPort, typeConstraintList, typeConstraints
_actorFiring, _actorFiring, _declareDelayDependency, addActorFiringListener, addInitializable, connectionsChanged, createReceivers, declareDelayDependency, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, preinitialize, pruneDependencies, recordFiring, removeActorFiringListener, removeDependency, removeInitializable, setContainer, stop, stopFire, terminate
_adjustDeferrals, _checkContainer, _getContainedObject, _propagateExistence, getContainer, instantiate, isAtomic, isOpaque, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence, setName
_addPort, _description, _exportMoMLContents, _removePort, _validateSettables, connectedPortList, connectedPorts, containedObjectsIterator, getAttribute, getPort, getPorts, linkedRelationList, linkedRelations, portList, removeAllPorts, setClassDefinition, uniqueName
_setParent, exportMoML, getChildren, getElementName, getParent, getPrototypeList, isClassDefinition, isWithinClassDefinition
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _executeChangeRequests, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, addChangeListener, addDebugListener, addHierarchyListener, attributeDeleted, attributeList, attributeList, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getFullName, getModelErrorHandler, getName, getName, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, notifyOfNameChange, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, validateSettables, workspace
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
createReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, newReceiver, outputPortList
isFireFunctional, isStrict, iterate, stop, stopFire, terminate
addInitializable, preinitialize, removeInitializable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
getDerivedLevel, getDerivedList, propagateValue
public StringParameter separator
public Parameter trimSpaces
public CSVReader(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.public void attributeChanged(Attribute attribute) throws IllegalActionException
attributeChanged
in class LineReader
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 void fire() throws IllegalActionException
fire
in interface Executable
fire
in class LineReader
IllegalActionException
- If there's no director.public void wrapup()
wrapup
in interface Initializable
wrapup
in class LineReader
protected java.util.Set<Inequality> _defaultTypeConstraints()
_defaultTypeConstraints
in class TypedAtomicActor