|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectptolemy.kernel.util.NamedObj
ptolemy.kernel.util.Attribute
ptolemy.cg.kernel.generic.GenericCodeGenerator
public abstract class GenericCodeGenerator
Base class for code generator.
| Yellow (eal) |
| Yellow (eal) |
| Nested Class Summary | |
|---|---|
(package private) class |
GenericCodeGenerator.GeneratorPackageListParser
Parse the generatorPackageList parameter. |
| Nested classes/interfaces inherited from class ptolemy.kernel.util.NamedObj |
|---|
NamedObj.ContainedObjectsIterator |
| Field Summary | |
|---|---|
private java.util.Map<java.lang.Object,CodeGeneratorAdapter> |
_adapterStore
A map giving the code generator adapters for each actor. |
protected java.lang.String |
_codeFileName
The name of the file that was written. |
private static java.lang.String[] |
_commandFlags
The command-line options that are either present or not. |
private static java.lang.String[][] |
_commandOptions
The command-line options that take arguments. |
private static java.lang.String |
_commandTemplate
The form of the command line. |
protected static java.lang.String |
_eol
End of line character. |
protected ExecuteCommands |
_executeCommands
Execute commands to run the generated code. |
private GenericCodeGenerator.GeneratorPackageListParser |
_generatorPackageListParser
|
protected CompositeEntity |
_model
The model we for which we are generating code. |
private java.lang.String |
_outputFileExtension
The extension of the output file. |
private static java.util.List<java.lang.String> |
_parameterNames
List of parameter names seen on the command line. |
private static java.util.List<java.lang.String> |
_parameterValues
List of parameter values seen on the command line. |
protected java.lang.String |
_sanitizedModelName
The sanitized model name. |
FileParameter |
codeDirectory
The directory in which to put the generated code. |
StringParameter |
generatorPackage
The name of the package in which to look for helper 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. |
| Fields inherited from class ptolemy.kernel.util.NamedObj |
|---|
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS |
| Constructor Summary | |
|---|---|
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)
Deprecated. Set generatorPackage and use @{link #GenericCodeGenerator(container, name) instead. |
|
| Method Summary | |
|---|---|
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. |
private static java.lang.Class<?> |
_getCodeGeneratorClass(java.lang.String generatorPackageValue)
Get the code generator associated with the generatePackage parameter. |
private java.lang.String |
_getOutputFileExtension()
Return the file extension for generated files. |
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 elpsed time is greater than 10 seconds. |
protected void |
_reset()
Reset the code generator. |
private void |
_updateParameters(NamedObj model)
Set the parameters in the model stored in _parameterNames to the values given by _parameterValues. |
private static java.lang.String |
_usage()
Return a string summarizing the command-line arguments. |
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.String |
comment(java.lang.String comment)
Return a formatted comment containing the specified string. |
DecoratedAttributes |
createDecoratedAttributes(NamedObj target)
Return the decorated attributes for the target NamedObj. |
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 |
getMethodVisibiliyString()
Return the visibility signature for run() and execute() (for Java). |
static void |
main(java.lang.String[] args)
Generate code for a model. |
private static boolean |
parseArg(java.lang.String arg)
Parse a command-line argument. |
void |
setCodeGenerator(GenericCodeGenerator codeGenerator)
This method is used to set the code generator for a adapter class. |
void |
setContainer(NamedObj container)
Set the container of this object to be the given container. |
void |
setExecuteCommands(ExecuteCommands executeCommands)
Set the command executor, which can be either non-graphical or graphical. |
void |
setTypesOfDecoratedVariables(DecoratedAttributes decoratedAttributes)
Set the current type of the decorated attributes. |
| Methods inherited from class ptolemy.kernel.util.Attribute |
|---|
_checkContainer, _getContainedObject, _propagateExistence, clone, getContainer, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, setName, updateContent |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface ptolemy.kernel.util.Nameable |
|---|
description, getContainer, getDisplayName, getFullName, getName, getName, setName |
| Field Detail |
|---|
public FileParameter codeDirectory
$HOME/cg/".
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 CompositeEntity _model
protected java.lang.String _sanitizedModelName
private java.util.Map<java.lang.Object,CodeGeneratorAdapter> _adapterStore
protected java.lang.String _codeFileName
private static java.lang.String[] _commandFlags
private static java.lang.String[][] _commandOptions
private static final java.lang.String _commandTemplate
private GenericCodeGenerator.GeneratorPackageListParser _generatorPackageListParser
private static java.util.List<java.lang.String> _parameterNames
private static java.util.List<java.lang.String> _parameterValues
private java.lang.String _outputFileExtension
| Constructor Detail |
|---|
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.| Method Detail |
|---|
public void attributeChanged(Attribute attribute)
throws IllegalActionException
attributeChanged in class NamedObjattribute - The attribute that changed.
IllegalActionException - If the change is not acceptable
to this container.public java.lang.String comment(java.lang.String comment)
comment - The string to put in the comment.
public DecoratedAttributes createDecoratedAttributes(NamedObj target)
throws IllegalActionException,
NameDuplicationException
createDecoratedAttributes in interface Decoratortarget - The NamedObj that will be decorated.
IllegalActionException - If the attribute is not of an
acceptable class for the container, or if the name contains a period.
NameDuplicationException - If the name coincides with
an attribute already in the container.
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 getMethodVisibiliyString()
public java.lang.String getMethodExceptionString()
public static void main(java.lang.String[] args)
throws java.lang.Exception
For example:
java -classpath $PTII ptolemy.codegen.kernel.CodeGenerator $PTII/ptolemy/codegen/c/actor/lib/test/auto/Ramp.xmlor
$PTII/bin/ptinvoke ptolemy.codegen.kernel.CodeGenerator $PTII/ptolemy/codegen/c/actor/lib/test/auto/Ramp.xml
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 Attributecontainer - 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 void setTypesOfDecoratedVariables(DecoratedAttributes decoratedAttributes)
throws IllegalActionException
setTypesOfDecoratedVariables in interface DecoratordecoratedAttributes - The decorated attributes
IllegalActionException - If the attribute is not of an
acceptable class for the container, or if the name contains a period.
protected int _executeCommands()
throws IllegalActionException
IllegalActionException - Not thrown in this base class.
protected int _generateCode(java.lang.StringBuffer code)
throws KernelException
$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.
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 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.adapterClassName - The dot separated name of the adapter.
IllegalActionException - If the adapter class cannot be found.
protected boolean _isTopLevel()
throws IllegalActionException
IllegalActionException
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()
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.
private java.lang.String _getOutputFileExtension()
throws IllegalActionException
IllegalActionException - If getting the value of the
generatorPackage parameter throws it.private void _updateParameters(NamedObj model)
model - The model in which to update parameters.
private static boolean parseArg(java.lang.String arg)
throws java.lang.Exception
arg - The command-line argument to be parsed.
java.lang.Exception - If something goes wrong.private static java.lang.String _usage()
private static java.lang.Class<?> _getCodeGeneratorClass(java.lang.String generatorPackageValue)
throws IllegalActionException
generatorPackageValue - The value of the generatorPackage parameter.
IllegalActionException - If the adapter class cannot be found.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||