public abstract class GenericCodeGenerator extends Attribute implements Decorator
Derived classes usually override generateCode(StringBuffer)
.
Yellow (eal) |
Yellow (eal) |
Modifier and Type | Class and Description |
---|---|
(package private) class |
GenericCodeGenerator.GeneratorPackageListParser
Parse the generatorPackageList parameter.
|
NamedObj.ContainedObjectsIterator
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
_codeFileName
The name of the file that was written.
|
protected static java.lang.String |
_eol
End of line character.
|
protected ExecuteCommands |
_executeCommands
Execute commands to run the generated code.
|
protected boolean |
_generateInSubdirectory
The value of the generateInSubdirectory parameter.
|
protected CompositeEntity |
_model
The model we for which we are generating code.
|
protected java.lang.String |
_sanitizedModelName
The sanitized model name.
|
FileParameter |
codeDirectory
The directory in which to put the generated code.
|
Parameter |
generateInSubdirectory
If true, then generate code in a subdirectory of codeDirectory.
|
StringParameter |
generatorPackage
The name of the package in which to look for adapter class
code generators.
|
StringParameter |
generatorPackageList
The name of the package(s) in which to look for adapter
classes.
|
static java.lang.String |
INDENT1
Indent string for indent level 1.
|
static java.lang.String |
INDENT2
Indent string for indent level 2.
|
static java.lang.String |
INDENT3
Indent string for indent level 3.
|
Parameter |
overwriteFiles
If true, overwrite preexisting files.
|
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
Constructor and Description |
---|
GenericCodeGenerator(NamedObj container,
java.lang.String name)
Create a new instance of the code generator.
|
GenericCodeGenerator(NamedObj container,
java.lang.String name,
java.lang.String outputFileExtension)
Create a new instance of the code generator.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_copyCFilesTosrc(java.lang.String path,
java.lang.String directoryToCopy,
java.lang.String[] codeFileNames)
Copy files into the directory of the project.
|
protected void |
_copyCFileTosrc(java.lang.String path,
java.lang.String directoryToCopy,
java.lang.String codeFileName)
Copy a C (or h) file into the directory /src of the project.
|
protected int |
_executeCommands()
Execute the compile and run commands in the
codeDirectory directory.
|
protected int |
_generateCode(java.lang.StringBuffer code)
Generate code and append it to the given string buffer.
|
protected CodeGeneratorAdapter |
_getAdapter(java.lang.Object object)
Get the code generator adapter associated with the given object.
|
protected java.lang.Class<?> |
_getAdapterClassFilter()
Return the filter class to find adapters.
|
protected CodeGeneratorAdapter |
_getAutoGeneratedAdapter(GenericCodeGenerator codeGenerator,
java.lang.Object object)
Attempt to automatically generate an adapter.
|
protected java.lang.String |
_getOutputFilename()
Return the name of the output file.
|
protected CodeGeneratorAdapter |
_instantiateAdapter(java.lang.Object component,
java.lang.Class<?> componentClass,
java.lang.String adapterClassName)
Instantiate the given code generator adapter.
|
protected boolean |
_isTopLevel()
Test if the containing actor is in the top level.
|
protected long |
_printTimeAndMemory(long startTime,
java.lang.String message)
Print the elapsed time since the specified startTime if
the elapsed time is greater than 10 seconds.
|
protected void |
_reset()
Reset the code generator.
|
protected void |
_resetAll()
Reset the code generator, including setting the model to null.
|
protected java.lang.String |
_writeCode(java.lang.StringBuffer code)
Write the code to a directory named by the codeDirectory
parameter, with a file name that is a sanitized version of the
model name, and an extension that is the last package of
the generatorPackage.
|
protected java.lang.String |
_writeCodeFileName(java.lang.StringBuffer code,
java.lang.String codeFileName,
boolean overwriteFile,
boolean dontShowDialog)
Write the code to a directory named by the codeDirectory
parameter, with a file name that is a sanitized version of the
model name, and an extension that is the last package of
the generatorPackage.
|
void |
attributeChanged(Attribute attribute)
If the attribute is the codeDirectory parameter, then set the
base directory of the codeDirectory parameter.
|
java.lang.Object |
clone(Workspace workspace)
Clone the object into the specified workspace.
|
java.lang.String |
comment(java.lang.String comment)
Return a formatted comment containing the
specified string.
|
DecoratorAttributes |
createDecoratorAttributes(NamedObj target)
Return the decorated attributes for the target NamedObj.
|
java.util.List<NamedObj> |
decoratedObjects()
Return a list of the entities deeply contained by the container
of this resource scheduler.
|
int |
generateCode()
Generate code and write it to the file specified by the
codeDirectory parameter.
|
static int |
generateCode(java.lang.String[] args)
Generate code for a model.
|
int |
generateCode(java.lang.StringBuffer code)
Generate code.
|
java.lang.String |
generateCopyright()
Return the copyright for this code.
|
java.lang.Object |
getAdapter(java.lang.Object component)
Get the code generator adapter associated with the given component.
|
java.lang.String |
getCodeFileName()
Return the name of the code file that was written, if any.
|
NamedObj |
getComponent()
Return the associated component, which is always the container.
|
ExecuteCommands |
getExecuteCommands()
Get the command executor, which can be either non-graphical
or graphical.
|
java.lang.String |
getMethodExceptionString()
Return the exception signature (for Java).
|
java.lang.String |
getMethodVisibilityString()
Return the visibility signature for run() and execute() (for Java).
|
boolean |
isGlobalDecorator()
Return false to indicate that this decorator should not
decorate objects across opaque hierarchy boundaries.
|
static void |
main(java.lang.String[] args)
Generate code for a model.
|
void |
setCodeGenerator(GenericCodeGenerator codeGenerator)
This method is used to set the code generator for a adapter class.
|
void |
setContainer(NamedObj container)
Override the base class to first set the container, then establish
a connection with any decorated objects it finds in scope in the new
container.
|
void |
setExecuteCommands(ExecuteCommands executeCommands)
Set the command executor, which can be either non-graphical
or graphical.
|
java.lang.String[][] |
updateCommandOptions()
Return an updated array of command line options.
|
_checkContainer, _getContainedObject, _propagateExistence, getContainer, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, setName, updateContent
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _description, _executeChangeRequests, _exportMoMLContents, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, addHierarchyListener, attributeDeleted, attributeList, attributeList, attributeTypeChanged, clone, containedObjectsIterator, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, 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, setDisplayName, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, uniqueName, validateSettables, workspace
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
description, getContainer, getDisplayName, getFullName, getName, getName, setName
public FileParameter codeDirectory
$HOME/cg/
".public Parameter generateInSubdirectory
public StringParameter generatorPackage
ptolemy.cg.kernel.generic.program.procedural.c
.
ptolemy.cg.kernel.generic.html
,
or ptolemy.cg.kernel.generic.program.procedural.java
.public StringParameter generatorPackageList
generic.program.procedural.java.target1:generic.program.procedural.java.target2
.
The adapter is first searched in the first package.
Adapters are looked up by class name, where the class name
consists of "ptolemy.cg.adapter" + the package name
from this list + "adapters.". Thus, if generatorPackageList
is set to generic.program.procedural.java
, then the
ptolemy.cg.adapter.generic.program.procedural.java.adapters.
package will be searched.public Parameter overwriteFiles
public static final java.lang.String INDENT1
StringUtilities.getIndentPrefix(int)
public static final java.lang.String INDENT2
StringUtilities.getIndentPrefix(int)
public static final java.lang.String INDENT3
StringUtilities.getIndentPrefix(int)
protected static final java.lang.String _eol
protected ExecuteCommands _executeCommands
protected boolean _generateInSubdirectory
protected CompositeEntity _model
protected java.lang.String _sanitizedModelName
protected java.lang.String _codeFileName
public GenericCodeGenerator(NamedObj container, java.lang.String name) throws IllegalActionException, NameDuplicationException
container
- The container.name
- The name of the code generator.IllegalActionException
- If the super class throws the
exception or error occurs when setting the file path.NameDuplicationException
- If the super class throws the
exception or an error occurs when setting the file path.public GenericCodeGenerator(NamedObj container, java.lang.String name, java.lang.String outputFileExtension) throws IllegalActionException, NameDuplicationException
container
- The container.name
- The name of the code generator.outputFileExtension
- The extension of the output file.
(for example c in case of C and java in case of Java)IllegalActionException
- If the super class throws the
exception or error occurs when setting the file path.NameDuplicationException
- If the super class throws the
exception or an error occurs when setting the file path.public void attributeChanged(Attribute attribute) throws IllegalActionException
attributeChanged
in class NamedObj
attribute
- The attribute that changed.IllegalActionException
- If the change is not acceptable
to this container.public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
clone
in class Attribute
workspace
- The workspace for the new object.java.lang.CloneNotSupportedException
- If any of the attributes
cannot be cloned.NamedObj.exportMoML(Writer, int, String)
,
NamedObj.setDeferringChangeRequests(boolean)
public java.lang.String comment(java.lang.String comment)
comment
- The string to put in the comment.public DecoratorAttributes createDecoratorAttributes(NamedObj target)
createDecoratorAttributes
in interface Decorator
target
- The NamedObj that will be decorated.public java.util.List<NamedObj> decoratedObjects()
decoratedObjects
in interface Decorator
public int generateCode() throws KernelException
KernelException
- If the target file cannot be overwritten
or write-to-file throw any exception.public int generateCode(java.lang.StringBuffer code) throws KernelException
code
- The code buffer into which to generate the code.KernelException
- If a type conflict occurs or the model
is running.public static int generateCode(java.lang.String[] args) throws java.lang.Exception
args
- An array of Strings, each element names a MoML file
containing a model.java.lang.Exception
- If any error occurs.public java.lang.String generateCopyright()
public final java.lang.Object getAdapter(java.lang.Object component) throws IllegalActionException
component
- The given component.IllegalActionException
- If the adapter class cannot be found.public final java.lang.String getCodeFileName()
public NamedObj getComponent()
public final ExecuteCommands getExecuteCommands()
setExecuteCommands(ExecuteCommands)
public java.lang.String getMethodVisibilityString()
public java.lang.String getMethodExceptionString()
public boolean isGlobalDecorator()
isGlobalDecorator
in interface Decorator
public static void main(java.lang.String[] args) throws java.lang.Exception
For example:
java -classpath $PTII ptolemy.cg.kernel.generic.GenericCodeGenerator $PTII/ptolemy/cg/adapter/generic/program/procedural/c/adapters/ptolemy/actor/lib/test/auto/Ramp.xmlor
$PTII/bin/ptinvoke ptolemy.cg.kernel.generic.GenericCodeGenerator $PTII/ptolemy/cg/adapter/generic/program/procedural/c/adapters/ptolemy/actor/lib/test/auto/Ramp.xml
The HandSimDroid work in $PTII/ptserver uses dependency injection to determine which implementation actors such as Const and Display to use. This method reads the ptolemy/actor/ActorModule.properties file.
args
- An array of Strings, each element names a MoML file
containing a model.java.lang.Exception
- If any error occurs.public void setCodeGenerator(GenericCodeGenerator codeGenerator)
codeGenerator
- The given code generator.public void setContainer(NamedObj container) throws IllegalActionException, NameDuplicationException
setContainer
in class Attribute
container
- The given container.IllegalActionException
- If the given container
is not null and not an instance of CompositeEntity.NameDuplicationException
- If there already exists a
container with the same name.Attribute.getContainer()
public void setExecuteCommands(ExecuteCommands executeCommands)
executeCommands
- The subprocess command executor.getExecuteCommands()
public java.lang.String[][] updateCommandOptions()
protected void _copyCFileTosrc(java.lang.String path, java.lang.String directoryToCopy, java.lang.String codeFileName) throws IllegalActionException
path
- The path to the source to be copied. A relative
path has $PTII prepended to it.directoryToCopy
- The destination directory, typically
an absolute path determined by the value of the codeDirectory
parameter.codeFileName
- the name of the file to copy.IllegalActionException
- If thrown while copying
a file.protected void _copyCFilesTosrc(java.lang.String path, java.lang.String directoryToCopy, java.lang.String[] codeFileNames) throws IllegalActionException
path
- The path to the source to be copied. A relative
path has $PTII prepended to it.directoryToCopy
- The destination directory, typically
an absolute path determined by the value of the codeDirectory
parameter.codeFileNames
- the name of the file to copy.IllegalActionException
- If thrown while copying
a file.protected int _executeCommands() throws IllegalActionException
IllegalActionException
- Not thrown in this base class.protected int _generateCode(java.lang.StringBuffer code) throws KernelException
Write the code to the directory specified by the codeDirectory
parameter. The file name is a sanitized version of the model
name with a suffix that is based on last package name of the
generatorPackage parameter. Thus if the
codeDirectory is $HOME
, the name of the
model is Foo
and the generatorPackage
is ptolemy.codegen.c
, then the file that is
written will be $HOME/Foo.c
This method is the main entry point, derived classes will override this method with code that traverses the model and generates code.
code
- The given string buffer.KernelException
- If the target file cannot be overwritten
or write-to-file throw any exception.protected final CodeGeneratorAdapter _getAdapter(java.lang.Object object) throws IllegalActionException
object
- The given object.IllegalActionException
- If the adapter class cannot be found.protected java.lang.Class<?> _getAdapterClassFilter()
protected CodeGeneratorAdapter _getAutoGeneratedAdapter(GenericCodeGenerator codeGenerator, java.lang.Object object)
codeGenerator
- The code generator with which to associate the adapter.object
- The given object.protected java.lang.String _getOutputFilename() throws IllegalActionException
IllegalActionException
- If there is problem resolving
the string value of the generatorPackage parameter.protected CodeGeneratorAdapter _instantiateAdapter(java.lang.Object component, java.lang.Class<?> componentClass, java.lang.String adapterClassName) throws IllegalActionException
component
- The given component.componentClass
- The class of the component to be instantiated.
The constructor for class named by the adapterClassName argument
must take an argument of the class componentClass.adapterClassName
- The dot separated name of the adapter.IllegalActionException
- If the adapter class cannot be found.protected boolean _isTopLevel() throws IllegalActionException
IllegalActionException
- If thrown while getting the container.protected long _printTimeAndMemory(long startTime, java.lang.String message)
startTime
- The start time. Usually set to the value
of (new Date()).getTime()
.message
- A prefix to the printed message.protected void _reset() throws IllegalActionException
IllegalActionException
- If the container of the model
cannot be set to null.protected void _resetAll() throws IllegalActionException
IllegalActionException
- If the container of the model
cannot be set to null.protected java.lang.String _writeCode(java.lang.StringBuffer code) throws IllegalActionException
code
- The StringBuffer containing the code.IllegalActionException
- If there is a problem reading
a parameter, if there is a problem creating the codeDirectory directory
or if there is a problem writing the code to a file.protected java.lang.String _writeCodeFileName(java.lang.StringBuffer code, java.lang.String codeFileName, boolean overwriteFile, boolean dontShowDialog) throws IllegalActionException
code
- The StringBuffer containing the code.codeFileName
- The name of the output file.overwriteFile
- The overwrite flag.dontShowDialog
- When true the confirmation dialog won't be shown.IllegalActionException
- If there is a problem reading
a parameter, if there is a problem creating the codeDirectory directory
or if there is a problem writing the code to a file.