public class FilePortParameter extends PortParameter implements FileOrURLAccessor
If the model containing this port parameter has been saved to a MoML file, then the file name can be given relative to the directory containing that MoML file. If the model has not been saved to a file, then the classpath is used for identifying relative file names.
Files can be given relative to a base, where the base is the URI of the first container above this one that has a URIAttribute. Normally, this URI specifies the file or URL containing the model definition. Thus, files that are referred to here can be kept in the same directory as the model, or in a related directory, and can moved together with the model.
The following special file names are understood:
String | Description | Property |
---|---|---|
$CWD |
The current working directory | user.dir |
$HOME |
The user's home directory | user.home |
$PTII |
The home directory of the Ptolemy II installation | ptolemy.ptII.dir |
$TMPDIR |
The temporary directory | java.io.tmpdir |
$USERNAME |
The user's account name | user.name |
If a file name begins with the reference "$CLASSPATH", then when the file is opened for reading, the openForReading() method will search for the file relative to the classpath (using the getResource() method of the current class loader). This will only work for a file that exists, and thus the openForWriting() method will not understand the "$CLASSPATH" string; this makes sense since the classpath typically has several directories, and it would not be obvious where to create the file. The asURL() method also recognizes the "$CLASSPATH" string, but not the asFile() method (which is typically used when accessing a file for writing). NOTE: If the container of this parameter also contains a variable named CLASSPATH, then the value of that variable is used instead of the Java classpath.
This parameter has two values, which may not be equal, a current value and a persistent value. The persistent value is returned by getExpression() and is set by any of three different mechanisms:
When using this parameter in an actor, care must be exercised to call update() exactly once per firing prior to calling getToken(). Each time update() is called, a new token will be consumed from the associated port (if the port is connected and has a token). If this is called multiple times in an iteration, it may result in consuming tokens that were intended for subsequent iterations. Thus, for example, update() should not be called in fire() and then again in postfire(). Moreover, in some domains (such as DE), it is essential that if a token is provided on a port, that it is consumed. In DE, the actor will be repeatedly fired until the token is consumed. Thus, it is an error to not call update() once per iteration. For an example of an actor that uses this mechanism, see Ramp.
If this actor is placed in a container that does not implement the TypedActor interface, then no associated port is created, and it functions as an ordinary file parameter. This is useful, for example, if this is put in a library, where one would not want the associated port to appear.
There are a few situations where FilePortParameter might not do what you expect:
FileParameter
,
ParameterPort
Variable.CircularDependencyError, Variable.VariableScope
NamedObj.ContainedObjectsIterator
Settable.Visibility
_port
_currentExpression, _needsEvaluation, _parserScope, _parseTreeValid, _suppressVariableSubstitution, _valueListeners
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
EXPERT, FULL, NONE, NOT_EDITABLE
Constructor and Description |
---|
FilePortParameter(NamedObj container,
java.lang.String name)
Construct a parameter with the given name contained by the specified
entity.
|
FilePortParameter(NamedObj container,
java.lang.String name,
Token token)
Construct a Parameter with the given container, name, and Token.
|
Modifier and Type | Method and Description |
---|---|
java.io.File |
asFile()
Return the file as a File object.
|
java.net.URL |
asURL()
Return the file as a URL.
|
java.lang.Object |
clone(Workspace workspace)
Clone the attribute into the specified workspace.
|
void |
close()
Close the file.
|
java.net.URI |
getBaseDirectory()
Return the directory to use as the base for relative file or URL names.
|
java.io.BufferedReader |
openForReading()
Open the file or URL for reading.
|
java.io.Writer |
openForWriting()
Open the file for writing.
|
java.io.Writer |
openForWriting(boolean append)
Open the file for writing or appending.
|
void |
setBaseDirectory(java.net.URI directory)
Set the directory to use as the base for relative file or URL names.
|
java.lang.String |
stringValue()
Return the string value of this parameter.
|
_getCurrentExpression, _propagateExistence, _setTypeConstraints, attributeChanged, getExpression, getPort, initialize, preinitialize, setContainer, setCurrentValue, setDisplayName, setExpression, setName, setToken, update
_getInitializableContainer, addInitializable, hierarchyChanged, hierarchyWillChange, removeInitializable, wrapup
addChoice, exportMoML, getChoices, removeAllChoices, removeChoice
_description, _evaluate, _notifyValueListeners, _parseIfNecessary, _propagate, _propagateToValueListeners, _propagateValue, _setToken, _setTokenAndNotify, addValueListener, getDeclaredType, getFreeIdentifiers, getParserScope, getScope, getScope, getToken, getType, getTypeTerm, getValueAsString, getVariable, getVisibility, invalidate, isKnown, isLazy, isStringMode, isSuppressVariableSubstitution, isTypeAcceptable, removeValueListener, reset, setLazy, setParseTreeEvaluator, setStringMode, setSuppressVariableSubstitution, setToken, setTypeAtLeast, setTypeAtLeast, setTypeAtMost, setTypeEquals, setTypeSameAs, setUnknown, setValueListenerAsWeakDependency, setVisibility, stringRepresentation, toString, typeConstraintList, typeConstraints, validate, valueChanged
getDefaultExpression
_checkContainer, _getContainedObject, getContainer, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, updateContent
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _executeChangeRequests, _exportMoMLContents, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, addHierarchyListener, attributeDeleted, attributeList, attributeList, attributeTypeChanged, clone, containedObjectsIterator, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getElementName, getFullName, getModelErrorHandler, getName, getName, getPrototypeList, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, notifyOfNameChange, propagateExistence, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, uniqueName, validateSettables, workspace
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addValueListener, getDefaultExpression, getDisplayName, getExpression, getValueAsString, getVisibility, removeValueListener, setExpression, setVisibility, validate
description, getContainer, getFullName, getName, getName, setName
addInitializable, removeInitializable, wrapup
public FilePortParameter(NamedObj container, java.lang.String name) throws IllegalActionException, NameDuplicationException
container
- The container.name
- The name of the parameter.IllegalActionException
- If the parameter is not of an
acceptable class for the container.NameDuplicationException
- If the name coincides with
a parameter already in the container.public FilePortParameter(NamedObj container, java.lang.String name, Token token) throws IllegalActionException, NameDuplicationException
container
- The container.name
- The name.token
- The Token contained by this Parameter.IllegalActionException
- If the parameter is not of an
acceptable class for the container.NameDuplicationException
- If the name coincides with
an parameter already in the container.public java.io.File asFile() throws IllegalActionException
The file need not exist for this method to succeed. Thus, this method can be used to determine whether a file with a given name exists, prior to calling openForWriting(). A typical usage looks like this:
FileParameter fileParameter; ... File file = fileParameter.asFile(); if (file.exists()) { ... Ask the user if it's OK to overwrite... ... Throw an exception if not... } // The following will overwrite an existing file. Writer writer = new PrintWriter(fileParameter.openForWriting());
asFile
in interface FileOrURLAccessor
IllegalActionException
- If a parse error occurs
reading the file name.getBaseDirectory()
public java.net.URL asURL() throws IllegalActionException
asURL
in interface FileOrURLAccessor
IllegalActionException
- If the file cannot be read, or
if the file cannot be represented as a URL (e.g. System.in), or
the name specification cannot be parsed.public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
clone
in class PortParameter
workspace
- The workspace for the cloned object.java.lang.CloneNotSupportedException
- If a derived class contains
an attribute that cannot be cloned.Object.clone()
public void close() throws IllegalActionException
close
in interface FileOrURLAccessor
IllegalActionException
- If the file or URL cannot be
closed.public java.net.URI getBaseDirectory()
getBaseDirectory
in interface FileOrURLAccessor
URIAttribute.getModelURI(NamedObj)
,
setBaseDirectory(URI)
public java.io.BufferedReader openForReading() throws IllegalActionException
openForReading
in interface FileOrURLAccessor
IllegalActionException
- If the file or URL cannot be
opened.getBaseDirectory()
public java.io.Writer openForWriting() throws IllegalActionException
openForWriting
in interface FileOrURLAccessor
IllegalActionException
- If the file cannot be opened
or created.getBaseDirectory()
,
asFile()
public java.io.Writer openForWriting(boolean append) throws IllegalActionException
openForWriting
in interface FileOrURLAccessor
append
- If true, then append to the file rather than
overwriting.IllegalActionException
- If the file cannot be opened
or created.getBaseDirectory()
,
asFile()
public void setBaseDirectory(java.net.URI directory)
setBaseDirectory
in interface FileOrURLAccessor
directory
- The base directory.URIAttribute.getModelURI(NamedObj)
,
getBaseDirectory()
public java.lang.String stringValue() throws IllegalActionException
((StringToken)this.getToken()).stringValue()
IllegalActionException
- If the expression cannot
be parsed or cannot be evaluated, or if the result of evaluation
violates type constraints, or if the result of evaluation is null
and there are variables that depend on this one.