public class NamedProgramCodeGeneratorAdapter extends ProgramCodeGeneratorAdapter
Subclasses should override generateFireCode(), generateInitializeCode() generatePostfireCode(), generatePreinitializeCode(), and generateWrapupCode() methods by appending a corresponding code block.
Subclasses should be sure to properly indent the code by
either using the code block functionality in methods like
_generateBlockCode(String) or by calling
CodeStream.indent(String)
,
for example:
StringBuffer code = new StringBuffer(); code.append(super.generateWrapupCode()); code.append("// Local wrapup code"); return processCode(CodeStream.indent(code.toString()));
Modifier and Type | Class and Description |
---|---|
protected class |
NamedProgramCodeGeneratorAdapter.VariableScope
This class implements a scope, which is used to generate the parsed
expressions in target language.
|
ProgramCodeGeneratorAdapter.Channel
NamedObj.ContainedObjectsIterator
_component, _defaultBlocks, _eol, _templateParser
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
Constructor and Description |
---|
NamedProgramCodeGeneratorAdapter(NamedObj component)
Construct the code generator adapter associated
with the given component.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
_generateFireCode()
Generate the fire code.
|
protected java.lang.String |
_generateTypeConvertStatement(ProgramCodeGeneratorAdapter.Channel source,
ProgramCodeGeneratorAdapter.Channel sink,
int offset)
Generate the type conversion statement for the particular offset of
the two given channels.
|
void |
analyzeTypeConvert()
Find out each output port that needs to be converted for the
actor associated with this adapter.
|
static long |
copyFilesToCodeDirectory(NamedObj namedObj,
ProgramCodeGenerator codeGenerator)
Copy files to the code directory.
|
java.lang.String |
generateFireCode()
Generate the fire code.
|
java.lang.String |
generateFireFunctionCode()
Generate The fire function code.
|
java.lang.String |
generateInitializeCode()
Generate the initialize code.
|
java.lang.String |
generateMainLoop()
Generate a main loop for an execution under the control of
a director.
|
void |
generateModeTransitionCode(java.lang.StringBuffer code)
Generate mode transition code.
|
java.lang.String |
generatePostfireCode()
Generate the postfire code.
|
java.lang.String |
generatePrefireCode()
Generate the prefire code of the associated composite actor.
|
java.lang.String |
generatePreinitializeCode()
Generate the preinitialize code.
|
java.lang.String |
generatePreinitializeMethodBodyCode()
Generate the preinitialization method body.
|
static java.lang.String |
generateSimpleName(NamedObj namedObj)
Generate sanitized name for the given named object.
|
java.lang.String |
generateTypeConvertFireCode()
Generate the type conversion fire code.
|
java.lang.String |
generateTypeConvertFireCode(boolean forComposite)
Generate the type conversion fire code.
|
java.lang.String |
generateVariableDeclaration()
Generate variable declarations for inputs and outputs and parameters.
|
java.lang.String |
generateVariableInitialization()
Generate variable initialization for the referenced parameters.
|
java.lang.String |
generateWrapupCode()
Generate the wrapup code.
|
java.lang.String |
getAddTimeString()
Return a string that represents the additional time.
|
int |
getBufferSize(IOPort port)
Return the buffer size of a given port, which is the maximum of the
bufferSizes of all channels of the given port.
|
int |
getBufferSize(IOPort port,
int channelNumber)
Get the buffer size of the given port of this actor.
|
NamedObj |
getComponent()
Get the component associated with this adapter.
|
java.util.Set<java.lang.String> |
getDeclareSharedCode()
Generate the declaration of the shared code (only for C code)
At run time of the generated code,
the code generated by this method is run first, so any
initialization of variables of this adapter should be done in
this method.
|
static java.lang.String[] |
getDefaultBlocks()
Return an array of strings that are regular expressions of all the
code blocks that are appended automatically by default.
|
java.lang.String |
getFireFunctionParameters()
Return the parameters for the fire function.
|
java.util.Set<java.lang.String> |
getHeaderFiles()
Get the files needed by the code generated from this adapter class.
|
java.util.Set<java.lang.String> |
getIncludeDirectories()
Return a set of directories to include for the generated code.
|
java.util.Set<java.lang.String> |
getLibraries()
Return a set of libraries to link in the generated code.
|
java.util.Set<java.lang.String> |
getLibraryDirectories()
Return a set of directories to find libraries in.
|
java.util.Set<Parameter> |
getModifiedVariables()
Return a set of parameters that will be modified during the execution
of the model.
|
java.lang.String |
getName()
Return the name of the object.
|
java.lang.String |
getParameterValue(java.lang.String name,
NamedObj container)
Return the value or an expression in the target language for
the specified parameter of the associated actor.
|
java.lang.String |
getReference(java.lang.String name,
boolean executive)
Return the reference to the specified parameter or port of the
associated actor.
|
java.lang.String |
getReference(java.lang.String name,
boolean isWrite,
boolean executive)
Return the reference to the specified parameter or port of the
associated actor.
|
java.util.Set<java.lang.String> |
getSharedCode()
Generate the shared code.
|
static java.util.List<ProgramCodeGeneratorAdapter.Channel> |
getSinkChannels(IOPort port,
int channelNumber)
Return a list of channel objects that are the sink input ports given
a port and channel.
|
java.lang.String |
getSourceTimeString(java.lang.String timeVariable)
Return a string that represents the source time.
|
java.lang.String |
getTimeSourcePortName()
Return the name of the port that is the time source.
|
java.util.Set<ProgramCodeGeneratorAdapter.Channel> |
getTypeConvertChannels()
Get the set of channels that need to be type converted.
|
static java.lang.String |
getTypeConvertReference(ProgramCodeGeneratorAdapter.Channel channel)
Generate a variable reference for the given channel.
|
java.util.List<ProgramCodeGeneratorAdapter.Channel> |
getTypeConvertSinkChannels(ProgramCodeGeneratorAdapter.Channel source)
Get the list of sink channels that the given source channel needs to
be type converted to.
|
void |
setupAdapter()
Perform any setup or initialization of the adapter.
|
java.lang.String |
targetType(Type ptType)
Get the corresponding type in the target language
from the given Ptolemy type.
|
_generateBlockByName, getAdapter, getCodeGenerator, getTemplateParser, processCode, setCodeGenerator, setTemplateParser, toString
createDecoratorAttributes, generateName
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _description, _executeChangeRequests, _exportMoMLContents, _getContainedObject, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateExistence, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, addHierarchyListener, attributeChanged, attributeDeleted, attributeList, attributeList, attributeTypeChanged, clone, clone, containedObjectsIterator, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttribute, getAttributes, getChangeListeners, getClassName, getContainer, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getElementName, getFullName, getModelErrorHandler, getName, getPrototypeList, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, notifyOfNameChange, propagateExistence, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setName, setPersistent, setSource, sortContainedObjects, toplevel, uniqueName, validateSettables, workspace
public NamedProgramCodeGeneratorAdapter(NamedObj component)
component
- The associated component.public void analyzeTypeConvert() throws IllegalActionException
IllegalActionException
- Not thrown in this base class.public static long copyFilesToCodeDirectory(NamedObj namedObj, ProgramCodeGenerator codeGenerator) throws java.io.IOException, IllegalActionException
fileDependencies
codeBlock consists of one or
more lines where each line names a file that should be copied
to the directory named by the codeDirectory parameter
of the code generator. The file is only copied if a file by
that name does not exist in codeDirectory or if the
source file was more recently modified than the destination
file.
Using the fileDependencies
code block allows
actor writers to refer to code defined in other files.
namedObj
- If this argument is an instance of
ptolemy.actor.lib.jni.EmbeddedCActor, then the code blocks
from EmbeddedCActor's embeddedCCode parameter are used.codeGenerator
- The code generator from which the
codeDirectory parameter is read.java.io.IOException
- If there is a problem reading the
codeDirectory parameter.IllegalActionException
- If there is a problem reading the
codeDirectory parameter.public NamedObj getComponent()
getComponent
in class ProgramCodeGeneratorAdapter
public static java.lang.String[] getDefaultBlocks()
public java.lang.String getAddTimeString() throws IllegalActionException
IllegalActionException
- Not thrown in this baseclass.
Derived classes may throw it if there is a problem accessing
ports or parameters in the associated actor.public java.lang.String getSourceTimeString(java.lang.String timeVariable) throws IllegalActionException
timeVariable
- The variable to be set in the generated
code.IllegalActionException
- Not thrown in this baseclass.
Derived classes may throw it if there is a problem accessing
ports or parameters in the associated actor.public java.lang.String getTimeSourcePortName()
public java.lang.String getFireFunctionParameters()
public java.lang.String generateFireCode() throws IllegalActionException
IllegalActionException
- Not thrown in this base class.public java.lang.String generateFireFunctionCode() throws IllegalActionException
IllegalActionException
- If thrown while generating fire code.public java.lang.String generateMainLoop() throws IllegalActionException
IllegalActionException
- Not thrown in this base class.public java.lang.String generateTypeConvertFireCode() throws IllegalActionException
IllegalActionException
- Not thrown in this base class.public java.lang.String generateInitializeCode() throws IllegalActionException
IllegalActionException
- If thrown while appending to the
the block or processing the macros.public void generateModeTransitionCode(java.lang.StringBuffer code) throws IllegalActionException
code
- The string buffer that the generated code is appended to.IllegalActionException
- Not thrown in this base class.public java.lang.String generatePostfireCode() throws IllegalActionException
IllegalActionException
- If thrown while appending to the
the block or processing the macros.public java.lang.String generatePrefireCode() throws IllegalActionException
IllegalActionException
- If illegal macro names are found.public java.lang.String generatePreinitializeCode() throws IllegalActionException
IllegalActionException
- Not thrown in this base class.public java.lang.String generatePreinitializeMethodBodyCode() throws IllegalActionException
Typically, the preinitialize code consists of variable declarations. However, AutoAdapter generates method calls that instantiate wrapper TypedCompositeActors, so we need to invoke those method calls.
IllegalActionException
- Not thrown in this base class.public static final java.lang.String generateSimpleName(NamedObj namedObj)
namedObj
- The named object for which the name is generated.CodeGeneratorAdapter.generateName(NamedObj)
public java.lang.String generateTypeConvertFireCode(boolean forComposite) throws IllegalActionException
forComposite
- True if we are generating code for a composite.IllegalActionException
- Not thrown in this base class.public java.lang.String generateVariableDeclaration() throws IllegalActionException
IllegalActionException
- If the adapter class for the model
director cannot be found.public java.lang.String generateVariableInitialization() throws IllegalActionException
IllegalActionException
- If the adapter class for the model
director cannot be found.public java.lang.String generateWrapupCode() throws IllegalActionException
IllegalActionException
- If thrown while appending to the
the block or processing the macros.public java.util.Set<java.lang.String> getHeaderFiles() throws IllegalActionException
TemplateParser.getHeaderFiles()
.IllegalActionException
- Not Thrown in this base class.public java.util.Set<java.lang.String> getIncludeDirectories() throws IllegalActionException
IllegalActionException
- If thrown when getting or reading
the CodeStream.public int getBufferSize(IOPort port) throws IllegalActionException
port
- The given port.IllegalActionException
- If the
getBufferSize(IOPort, int)
method throws it.public int getBufferSize(IOPort port, int channelNumber) throws IllegalActionException
port
- The given port.channelNumber
- The given channel.IllegalActionException
- If the getBufferSize() method of the
actor helper class throws it.public java.util.Set<java.lang.String> getLibraries() throws IllegalActionException
IllegalActionException
- If thrown when getting or reading
the CodeStream.public java.util.Set<java.lang.String> getLibraryDirectories() throws IllegalActionException
IllegalActionException
- If thrown when getting or reading
the CodeStream.public java.lang.String getName()
getName
in interface Nameable
getName
in class NamedObj
NamedObj.setName(String)
public java.util.Set<Parameter> getModifiedVariables() throws IllegalActionException
IllegalActionException
- If an actor throws it while getting
modified variables.public final java.lang.String getParameterValue(java.lang.String name, NamedObj container) throws IllegalActionException
name
- The name of the parameter.container
- The container to search upwards from.IllegalActionException
- If the parameter does not exist or
does not have a value.public final java.lang.String getReference(java.lang.String name, boolean executive) throws IllegalActionException
name
- The name of the parameter or portexecutive
- If true, then look for the reference in the
executive director (the director of the container).IllegalActionException
- If the parameter or port does not
exist or does not have a value.public java.lang.String getReference(java.lang.String name, boolean isWrite, boolean executive) throws IllegalActionException
name
- The name of the parameter or portisWrite
- Whether to generate the write or read offset.executive
- If true, then look for the reference in the
executive director (the director of the container).IllegalActionException
- If the parameter or port does not
exist or does not have a value.public java.util.Set<java.lang.String> getDeclareSharedCode() throws IllegalActionException
setupAdapter()
is run before this method. In this base class,
return an empty set. Subclasses may generate code for variable
declaration, defining constants, etc.IllegalActionException
- Not thrown in this base class.public java.util.Set<java.lang.String> getSharedCode() throws IllegalActionException
setupAdapter()
is run before this method. In this base class,
return an empty set. Subclasses may generate code for variable
declaration, defining constants, etc.IllegalActionException
- Not thrown in this base class.public static java.util.List<ProgramCodeGeneratorAdapter.Channel> getSinkChannels(IOPort port, int channelNumber) throws IllegalActionException
port
- The given output port.channelNumber
- The given channel number.IllegalActionException
- If thrown while getting the
remote or deep receivers.public java.util.Set<ProgramCodeGeneratorAdapter.Channel> getTypeConvertChannels()
public static java.lang.String getTypeConvertReference(ProgramCodeGeneratorAdapter.Channel channel)
channel
- The given source channel.public java.util.List<ProgramCodeGeneratorAdapter.Channel> getTypeConvertSinkChannels(ProgramCodeGeneratorAdapter.Channel source)
source
- The given source channel.public void setupAdapter() throws IllegalActionException
IllegalActionException
- If an error occurrs while
initializing an adapter.public final java.lang.String targetType(Type ptType)
ptType
- The given Ptolemy type.protected java.lang.String _generateFireCode() throws IllegalActionException
IllegalActionException
- Not thrown in this base class.protected java.lang.String _generateTypeConvertStatement(ProgramCodeGeneratorAdapter.Channel source, ProgramCodeGeneratorAdapter.Channel sink, int offset) throws IllegalActionException
source
- The given source channel.sink
- The given sink channel.offset
- The given offset.IllegalActionException
- If there is a problem getting the
adapters for the ports or if the conversion cannot be handled.