Class ProceduralCodeGenerator
- java.lang.Object
-
- ptolemy.kernel.util.NamedObj
-
- ptolemy.kernel.util.Attribute
-
- ptolemy.cg.kernel.generic.GenericCodeGenerator
-
- ptolemy.cg.kernel.generic.RunnableCodeGenerator
-
- ptolemy.cg.kernel.generic.program.ProgramCodeGenerator
-
- ptolemy.cg.kernel.generic.program.procedural.ProceduralCodeGenerator
-
- All Implemented Interfaces:
java.lang.Cloneable
,Changeable
,Debuggable
,DebugListener
,Decorator
,Derivable
,ModelErrorHandler
,MoMLExportable
,Moveable
,Nameable
- Direct Known Subclasses:
CCodeGenerator
,FMIMACodeGenerator
,JavaCodeGenerator
public class ProceduralCodeGenerator extends ProgramCodeGenerator
Base class for Procedural code generator.- Since:
- Ptolemy II 10.0
- Version:
- $Id$
- Author:
- Bert Rodiers
- Pt.AcceptedRating:
- red (rodiers)
- Pt.ProposedRating:
- red (rodiers)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class ptolemy.kernel.util.NamedObj
NamedObj.ContainedObjectsIterator
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.String
_compileCommandDefault
The initial default value of the compileCommand parameter.protected java.util.Set<java.lang.String>
_includes
Set of include command line arguments where each element is a string, for example "-I/usr/local/include".protected java.util.List<java.lang.String>
_libraries
List of library command line arguments where each element is a string, for example "-L/usr/local/lib".Parameter
compile
If true, then compile the generated code.StringParameter
compileCommand
The command to use to compile the generated code if the useMake parameter is false.Parameter
generateEmbeddedCode
If true, then generate code for that uses the reflection for Java and JNI for C and is embedded within the model The default value is false and this parameter is not usually editable by the user.-
Fields inherited from class ptolemy.cg.kernel.generic.program.ProgramCodeGenerator
_modifiedVariables, _newTypesUsed, _portTypeMap, _portTypeMap2, _portTypeMap3, _portTypeMaxIndex, _portTypeMaxIndex2, _portTypeMaxIndex3, _primitiveTypes, _runCommandDefault, _tokenFuncUsed, _typeFuncUsed, _variablesAsArrays, generateComment, inline, maximumLinesPerBlock, measureTime, sourceLineBinding, useMake, variablesAsArrays, verbosity
-
Fields inherited from class ptolemy.cg.kernel.generic.RunnableCodeGenerator
_substituteMap, run, runCommand
-
Fields inherited from class ptolemy.cg.kernel.generic.GenericCodeGenerator
_codeFileName, _eol, _executeCommands, _generateInSubdirectory, _model, _sanitizedModelName, codeDirectory, generateInSubdirectory, generatorPackage, generatorPackageList, INDENT1, INDENT2, INDENT3, overwriteFiles
-
Fields inherited from class ptolemy.kernel.util.NamedObj
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
-
-
Constructor Summary
Constructors Constructor Description ProceduralCodeGenerator(NamedObj container, java.lang.String name, java.lang.String outputFileExtension, java.lang.String templateExtension)
Create a new instance of the ProceduralCodeGenerator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.String
_concatenateElements(java.util.Collection<java.lang.String> collection)
Given a Collection of Strings, return a string where each element of the Set is separated by a space.protected int
_executeCommands()
Execute the compile and run commands in the codeDirectory directory.protected void
_reset()
Reset the code generator.protected void
_writeMakefile(CompositeEntity container, java.lang.String currentDirectory)
Read in a template makefile, substitute variables and write the resulting makefile.void
addInclude(java.lang.String includeCommand)
Add an include command line argument the compile command.void
addLibrary(java.lang.String libraryCommand)
Add a library command line argument the compile command.void
addLibraryIfNecessary(java.lang.String libraryCommand)
If the compile command does not yet containe a library, add a library command line argument the compile command.java.lang.Object
clone(Workspace workspace)
Clone the object into the specified workspace.void
markFunctionCalled(java.lang.String name, ProceduralTemplateParser templateParser)
Add called functions to the set of overloaded functions for later use.-
Methods inherited from class ptolemy.cg.kernel.generic.program.ProgramCodeGenerator
_addActorIncludeDirectories, _addActorLibraries, _analyzeTypeConversions, _codeDirectoryAsFile, _finalPassOverCode, _formatComment, _generateBodyCode, _generateCode, _generateIncludeFiles, _generatePreinitializeCode, _generatePreinitializeMethodBodyCode, _generatePreinitializeMethodEntryCode, _generatePreinitializeMethodExitCode, _generatePreinitializeMethodProcedureName, _generateSharedCode, _getFireFunctionArguments, _instantiateAdapter, _printExecutionTime, _recordStartTime, _setupAdapter, _splitBody, _splitVariableDeclaration, _templateParserClass, _writeVariableDeclarations, addModifiedVariables, attributeChanged, codeGenType, comment, comment, containsCode, generateClosingEntryCode, generateClosingExitCode, generateConstantDefinition, generateFireFunctionCode, generateFireFunctionCompositeEnd, generateFireFunctionCompositeStart, generateFireFunctionMethodInvocation, generateFireFunctionMethodName, generateFireFunctionVariableAndMethodName, generateFireFunctionVariableDeclaration, generateInitializeCode, generateInitializeEntryCode, generateInitializeExitCode, generateInitializeProcedureName, generateLineInfo, generateMainEntryCode, generateMainExitCode, generatePackageStatement, generatePortName, generatePostfireCode, generatePostfireEntryCode, generatePostfireExitCode, generatePostfireProcedureName, generatePtIOPortName, generatePtIOPortSize, generatePtTypedCompositeActorName, generatePtTypedCompositeActorSize, generateTypeConvertCode, generateVariableDeclaration, generateVariableInitialization, generateVariableName, generateWrapupCode, generateWrapupEntryCode, generateWrapupExitCode, generateWrapupProcedureName, getModifiedVariables, getTemplateExtension, invokeGenerateUpdatePortOffsets, isPrimitive, isPrimitive, ptolemyType, setModifiedVariables, splitLongBody, splitVariableDeclaration, targetType, updateCommandOptions
-
Methods inherited from class ptolemy.cg.kernel.generic.RunnableCodeGenerator
_runCommand, _setupCommands, _updateSubstituteMap
-
Methods inherited from class ptolemy.cg.kernel.generic.GenericCodeGenerator
_copyCFilesTosrc, _copyCFileTosrc, _getAdapter, _getAdapterClassFilter, _getAutoGeneratedAdapter, _getOutputFilename, _isTopLevel, _printTimeAndMemory, _resetAll, _writeCode, _writeCodeFileName, createDecoratorAttributes, decoratedObjects, generateCode, generateCode, generateCode, generateCopyright, getAdapter, getCodeFileName, getComponent, getExecuteCommands, getMethodExceptionString, getMethodVisibilityString, isGlobalDecorator, main, setCodeGenerator, setContainer, setExecuteCommands
-
Methods inherited from class ptolemy.kernel.util.Attribute
_checkContainer, _getContainedObject, _propagateExistence, getContainer, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, setName, updateContent
-
Methods inherited from class ptolemy.kernel.util.NamedObj
_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
-
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
-
compile
public Parameter compile
If true, then compile the generated code. The default value is a parameter with the value true.
-
compileCommand
public StringParameter compileCommand
The command to use to compile the generated code if the useMake parameter is false. The initial default value is "make -f @modelName@.mk". Various '@' delimited key/value pairs will be automatically substituted. In the default case @modelName@ will be replaced with a sanitized (Java-safe) version of the model name.If the string "@help:all@" appears, then all the key/value pairs are echoed at run time, though this may not result in a syntactically correct command.
If useMake is true, then the value of this parameter is ignored.
-
generateEmbeddedCode
public Parameter generateEmbeddedCode
If true, then generate code for that uses the reflection for Java and JNI for C and is embedded within the model The default value is false and this parameter is not usually editable by the user. This parameter is set to true when CompiledCompositeActor is run in an interpreted Ptolemy model. This parameter is set to false when a model contains one or more CompiledCompositeActors and C or Java code is being generated for the model.
-
_includes
protected java.util.Set<java.lang.String> _includes
Set of include command line arguments where each element is a string, for example "-I/usr/local/include".
-
_libraries
protected java.util.List<java.lang.String> _libraries
List of library command line arguments where each element is a string, for example "-L/usr/local/lib". This variable is a list so as to preserve the order that the library commands were added to the list of libraries matters, see the manual page for the -L option of the ld command.
-
_compileCommandDefault
protected static final java.lang.String _compileCommandDefault
The initial default value of the compileCommand parameter. The constructor of a derived class may compare the value of compileCommand and this variable and decide to override the value of the compileCommand parameter with a new value.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProceduralCodeGenerator
public ProceduralCodeGenerator(NamedObj container, java.lang.String name, java.lang.String outputFileExtension, java.lang.String templateExtension) throws IllegalActionException, NameDuplicationException
Create a new instance of the ProceduralCodeGenerator.- Parameters:
container
- The container.name
- The name of the ProceduralCodeGenerator.outputFileExtension
- The extension of the output file. (for example c in case of C and java in case of Java)templateExtension
- The extension of the template files. (for example c in case of C and j in case of Java).- Throws:
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
-
addInclude
public void addInclude(java.lang.String includeCommand)
Add an include command line argument the compile command.- Parameters:
includeCommand
- The include command, for example "-I/usr/local/include".
-
addLibrary
public void addLibrary(java.lang.String libraryCommand)
Add a library command line argument the compile command.- Parameters:
libraryCommand
- The library command, for example "-L/usr/local/lib".- See Also:
addLibraryIfNecessary(String)
-
addLibraryIfNecessary
public void addLibraryIfNecessary(java.lang.String libraryCommand)
If the compile command does not yet containe a library, add a library command line argument the compile command.- Parameters:
libraryCommand
- The library command, for example "-L/usr/local/lib".- See Also:
addLibrary(String)
-
clone
public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
Clone the object into the specified workspace.- Overrides:
clone
in classRunnableCodeGenerator
- Parameters:
workspace
- The workspace for the new object.- Returns:
- A new NamedObj.
- Throws:
java.lang.CloneNotSupportedException
- If any of the attributes cannot be cloned.- See Also:
NamedObj.exportMoML(Writer, int, String)
,NamedObj.setDeferringChangeRequests(boolean)
-
markFunctionCalled
public void markFunctionCalled(java.lang.String name, ProceduralTemplateParser templateParser) throws IllegalActionException
Add called functions to the set of overloaded functions for later use. If the function starts with "Array_", add everything after the "Array_" is added to the set of token functions used.- Parameters:
name
- The name of the function, for example "Double_equals"templateParser
- The corresponding templateParser that contains the codeBlock.- Throws:
IllegalActionException
- If there is a problem adding a function to the set of overloaded functions.
-
_concatenateElements
protected java.lang.String _concatenateElements(java.util.Collection<java.lang.String> collection)
Given a Collection of Strings, return a string where each element of the Set is separated by a space.- Parameters:
collection
- The Collection of Strings.- Returns:
- A String that contains each element of the Set separated by a space.
-
_reset
protected void _reset() throws IllegalActionException
Reset the code generator.- Overrides:
_reset
in classProgramCodeGenerator
- Throws:
IllegalActionException
- Not thrown in this base class, thrown by the parent if the container of the model cannot be set to null.
-
_executeCommands
protected int _executeCommands() throws IllegalActionException
Execute the compile and run commands in the codeDirectory directory.- Overrides:
_executeCommands
in classRunnableCodeGenerator
- Returns:
- The return value of the last subprocess that was executed or -1 if no commands were executed.
- Throws:
IllegalActionException
- If there are problems reading parameters or executing the commands.
-
_writeMakefile
protected void _writeMakefile(CompositeEntity container, java.lang.String currentDirectory) throws IllegalActionException
Read in a template makefile, substitute variables and write the resulting makefile.If a
.mk.in
file with the name of the sanitized model name, then that file is used as a template. For example, if the model name isFoo
and the fileFoo.mk.in
exists, then the fileFoo.mk.in
is used as a makefile template.If no
.mk.in
file is found, then the makefile template can be found by looking up a resource name makefile.in in the package named by the generatorPackage parameter. Thus, if the generatorPackage has the value "ptolemy.codegen.c", then we look for the resource "ptolemy.codegen.c.makefile.in", which is usually found as$PTII/ptolemy/codegen/c/makefile.in
.The makefile is written to a directory named by the codeDirectory parameter, with a file name that is a sanitized version of the model name, and a ".mk" extension. Thus, for a model named "Foo", we might generate a makefile in "$HOME/codegen/Foo.mk".
Under Java under Windows, your
$HOME
variable is set to the value of theuser.home
System property, which is usually something likeC:\Documents and Settings\yourlogin
, thus for usermrptolemy
the makefile would beC:\Documents and Settings\mrptolemy\codegen\Foo.mk
.See the parent class
ProgramCodeGenerator._writeMakefile(CompositeEntity, String)
for variable that are substituted by the parent class.This class substitutes the following variables:
@PTCGIncludes@
- The elements of the set of include command arguments that
were added by calling
addInclude(String)
, where each element is separated by a space.
- Overrides:
_writeMakefile
in classProgramCodeGenerator
- Parameters:
container
- The composite actor for which we generate the makefilecurrentDirectory
- The director in which the makefile is to be written.- Throws:
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.
-
-