ptolemy.codegen.c.domains.pn.kernel
Class PNDirector

java.lang.Object
  extended by ptolemy.kernel.util.NamedObj
      extended by ptolemy.codegen.kernel.CodeGeneratorHelper
          extended by ptolemy.codegen.actor.Director
              extended by ptolemy.codegen.c.domains.pn.kernel.PNDirector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, ActorCodeGenerator, ComponentCodeGenerator, Changeable, Debuggable, DebugListener, Derivable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

public class PNDirector
extends Director

Code generator helper associated with the PNDirector class. This director initializes all the actors, then starts a thread for each actor that invokes the fire code for the actor in an infinite loop. FIXME: How to make it possible for executions to be finite?

Since:
Ptolemy II 8.0
Version:
$Id: PNDirector.java 59167 2010-09-21 17:08:02Z cxh $
Author:
Edward A. Lee (based on SDFDirector helper class)
See Also:
Serialized Form
Accepted Rating:
Red (eal)
Proposed Rating:
Yellow (eal)

Nested Class Summary
 
Nested classes/interfaces inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
CodeGeneratorHelper.Channel, CodeGeneratorHelper.VariableScope
 
Nested classes/interfaces inherited from class ptolemy.kernel.util.NamedObj
NamedObj.ContainedObjectsIterator
 
Field Summary
private  java.util.HashSet<java.lang.String> _buffers
           
 
Fields inherited from class ptolemy.codegen.actor.Director
_director, _eol, _INDENT1, _INDENT2, _INDENT3, _INDENT4
 
Fields inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
_codeGenerator, _codeStream, _parseTreeCodeGenerator, _portConversions, _referencedParameters
 
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
PNDirector(PNDirector pnDirector)
          Construct the code generator helper associated with the given PNDirector.
 
Method Summary
protected  java.lang.String _createDynamicOffsetVariables(IOPort port)
          Create offset variables for the channels of the given port.
private  java.lang.String _generateActorNameFileForDebugging()
           
protected  java.lang.String _generateNotTerminateFlag()
          Generate the notTerminate flag variable for the associated PN director.
private  void _generateThreadFunctionCode(java.lang.StringBuffer code)
           
private  java.lang.String _getActorThreadLabel(Actor actor)
          Generate the thread function name for a given actor.
 java.lang.String generateCodeForGet(IOPort port, int channel)
          Return the code for getting data from the specific port channel.
 java.lang.String generateCodeForSend(IOPort port, int channel)
          Return the code for sending data from the specific port channel.
 java.lang.String generateDirectorHeader()
          Return the generated label for the director header.
 java.lang.String generateFireCode()
          Generate the body code that lies between variable declaration and wrapup.
 java.lang.String generateInitializeCode()
          Generate the initialize code for the associated PN director.
 java.lang.String generateMainLoop()
          Return the main loop code for the associated PN director.
static java.lang.String generatePortHeader(IOPort port, int i)
          Return the generated header label for the specified port channel.
 java.lang.String generatePostfireCode()
          Generate the postfire code for the associated PN director.
 java.lang.String generatePreinitializeCode()
          Generate the preinitialize code for the associated PN director.
 void generateTransferInputsCode(IOPort inputPort, java.lang.StringBuffer code)
          Generate code for transferring enough tokens to complete an internal iteration.
 void generateTransferOutputsCode(IOPort port, java.lang.StringBuffer code)
          Generate the transfer output code for the specified port.
 java.lang.String generateVariableInitialization()
          Generate variable initialization for the referenced parameters.
 java.lang.String generateWrapupCode()
          Generate the wrapup code for the associated PN director.
 int getBufferSize(IOPort port, int channelNumber)
          Return the size of the generated buffer for the specified port channel.
 java.util.Set getHeaderFiles()
          Get the files needed by the code generated from this helper class.
 java.util.Set getLibraries()
          Return the libraries specified in the "libraries" blocks in the templates of the actors included in this CompositeActor.
 java.util.Set getSharedCode()
          Generate the shared code for the associated PN director.
 
Methods inherited from class ptolemy.codegen.actor.Director
_ceilToPowerOfTwo, _getIndentPrefix, _updateConnectedPortsOffset, _updatePortOffset, createOffsetVariablesIfNeeded, generateCodeForSend, generateFireFunctionCode, generateModeTransitionCode, generateOffset, generateVariableDeclaration, getComponent, getIncludeDirectories, getLibraryDirectories, getModifiedVariables, getReference, getReference, getReferenceChannels, getWCET
 
Methods inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
_createBufferSizeAndOffsetMap, _createInputBufferSizeAndOffsetMap, _findClosedParen, _generateBlockCode, _generateBlockCode, _generateFireCode, _generateTypeConvertMethod, _generateTypeConvertStatement, _generateTypeConvertStatements, _getCastType, _getChannelAndOffset, _getFireFunctionArguments, _getHelper, _getHelper, _getReference, _getReferenceChannels, _getTypeConvertChannels, _getTypeConvertReference, _indexOf, _replaceMacro, addFunctionUsed, addNewTypeUsed, analyzeTypeConvert, checkLocal, checkRemote, codeGenType, copyFilesToCodeDirectory, generateChannelOffset, generateFireFunctionCode2, generateName, generateOffset, generatePortReference, generatePrefireCode, generateSimpleName, generateTypeConvertFireCode, generateTypeConvertFireCode, generateVariableName, getBufferSize, getCodeGenerator, getDefaultBlocks, getDirector, getDirectorHelper, getFunctionInvocation, getNewInvocation, getObject, getParameterValue, getParseTreeCodeGenerator, getPort, getRates, getReadOffset, getReference, getReference, getReference, getReference, getSinkChannels, getSize, getSourceChannel, getWriteOffset, isPrimitive, isPrimitive, parseList, processCode, resetInputPortsOffset, setBufferSize, setCodeGenerator, setReadOffset, setWriteOffset, targetType, toString
 
Methods inherited from class ptolemy.kernel.util.NamedObj
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _debug, _debug, _debug, _debug, _debug, _description, _exportMoMLContents, _getContainedObject, _isMoMLSuppressed, _markContentsDerived, _propagateExistence, _propagateValue, _recordDecoratedAttributes, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, attributeChanged, attributeList, attributeList, attributeTypeChanged, clone, clone, containedObjectsIterator, 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, getName, getPrototypeList, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence, propagateValue, propagateValues, removeChangeListener, removeDebugListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setName, setPersistent, setSource, sortContainedObjects, toplevel, uniqueName, validateSettables, workspace
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_buffers

private final java.util.HashSet<java.lang.String> _buffers
Constructor Detail

PNDirector

public PNDirector(PNDirector pnDirector)
Construct the code generator helper associated with the given PNDirector.

Parameters:
pnDirector - The associated ptolemy.domains.pn.kernel.PNDirector
Method Detail

generateFireCode

public java.lang.String generateFireCode()
                                  throws IllegalActionException
Generate the body code that lies between variable declaration and wrapup.

Specified by:
generateFireCode in interface ActorCodeGenerator
Overrides:
generateFireCode in class Director
Returns:
The generated body code.
Throws:
IllegalActionException - If the CodeGeneratorHelper._generateFireCode() method throws the exceptions.

getHeaderFiles

public java.util.Set getHeaderFiles()
                             throws IllegalActionException
Get the files needed by the code generated from this helper class. This base class returns an empty set.

Specified by:
getHeaderFiles in interface ActorCodeGenerator
Overrides:
getHeaderFiles in class Director
Returns:
A set of strings that are header files needed by the code generated from this helper class.
Throws:
IllegalActionException - If something goes wrong.

getLibraries

public java.util.Set getLibraries()
                           throws IllegalActionException
Return the libraries specified in the "libraries" blocks in the templates of the actors included in this CompositeActor.

Specified by:
getLibraries in interface ActorCodeGenerator
Overrides:
getLibraries in class Director
Returns:
A Set of libraries.
Throws:
IllegalActionException - If thrown when gathering libraries.

generateCodeForGet

public java.lang.String generateCodeForGet(IOPort port,
                                           int channel)
                                    throws IllegalActionException
Return the code for getting data from the specific port channel.

Overrides:
generateCodeForGet in class Director
Parameters:
port - The specified port.
channel - The specified channel.
Returns:
the code for getting data from the specific port channel, in this case, return the empty string.
Throws:
IllegalActionException - Not thrown here.

generateCodeForSend

public java.lang.String generateCodeForSend(IOPort port,
                                            int channel)
                                     throws IllegalActionException
Return the code for sending data from the specific port channel.

Parameters:
port - The specified port.
channel - The specified channel.
Returns:
the code for sending data from the specific port channel, in this case, return the empty string.
Throws:
IllegalActionException - Not thrown here.

generateDirectorHeader

public java.lang.String generateDirectorHeader()
Return the generated label for the director header.

Returns:
The generated label for the director header.

generateInitializeCode

public java.lang.String generateInitializeCode()
                                        throws IllegalActionException
Generate the initialize code for the associated PN director.

Specified by:
generateInitializeCode in interface ComponentCodeGenerator
Overrides:
generateInitializeCode in class Director
Returns:
The generated initialize code.
Throws:
IllegalActionException - If the helper associated with the director throws it while generating initialize code.

generateMainLoop

public java.lang.String generateMainLoop()
                                  throws IllegalActionException
Return the main loop code for the associated PN director.

Overrides:
generateMainLoop in class Director
Returns:
The generated main loop code.
Throws:
IllegalActionException - If the helper associated with the director throws it while generating main loop code.

generatePortHeader

public static java.lang.String generatePortHeader(IOPort port,
                                                  int i)
Return the generated header label for the specified port channel.

Parameters:
port - The specified port.
i - The specified channel number.
Returns:
The generated header label for the specified port channel.

generatePostfireCode

public java.lang.String generatePostfireCode()
                                      throws IllegalActionException
Generate the postfire code for the associated PN director.

Specified by:
generatePostfireCode in interface ActorCodeGenerator
Overrides:
generatePostfireCode in class Director
Returns:
The generated postfire code.
Throws:
IllegalActionException - If the helper associated with the director throws it while generating postfire code.

generatePreinitializeCode

public java.lang.String generatePreinitializeCode()
                                           throws IllegalActionException
Generate the preinitialize code for the associated PN director.

Specified by:
generatePreinitializeCode in interface ActorCodeGenerator
Overrides:
generatePreinitializeCode in class Director
Returns:
The generated preinitialize code.
Throws:
IllegalActionException - If the helper associated with an actor throws it while generating preinitialize code for the actor.

generateTransferOutputsCode

public void generateTransferOutputsCode(IOPort port,
                                        java.lang.StringBuffer code)
                                 throws IllegalActionException
Generate the transfer output code for the specified port.

Overrides:
generateTransferOutputsCode in class Director
Parameters:
port - The specified port.
code - The given buffer to generate code to.
Throws:
IllegalActionException - Not thrown here.

generateTransferInputsCode

public void generateTransferInputsCode(IOPort inputPort,
                                       java.lang.StringBuffer code)
                                throws IllegalActionException
Generate code for transferring enough tokens to complete an internal iteration.

Overrides:
generateTransferInputsCode in class Director
Parameters:
inputPort - The port to transfer tokens.
code - The string buffer that the generated code is appended to.
Throws:
IllegalActionException - If thrown while transferring tokens.

generateVariableInitialization

public java.lang.String generateVariableInitialization()
                                                throws IllegalActionException
Generate variable initialization for the referenced parameters.

Specified by:
generateVariableInitialization in interface ActorCodeGenerator
Overrides:
generateVariableInitialization in class Director
Returns:
code The generated code.
Throws:
IllegalActionException - If the helper class for the model director cannot be found.

generateWrapupCode

public java.lang.String generateWrapupCode()
                                    throws IllegalActionException
Generate the wrapup code for the associated PN director.

Specified by:
generateWrapupCode in interface ComponentCodeGenerator
Overrides:
generateWrapupCode in class Director
Returns:
The generated preinitialize code.
Throws:
IllegalActionException - If the helper associated with an actor throws it while generating preinitialize code for the actor.

getBufferSize

public int getBufferSize(IOPort port,
                         int channelNumber)
                  throws IllegalActionException
Return the size of the generated buffer for the specified port channel. This returns the value of "initialQueueCapacity" parameter of the PNDirector

Overrides:
getBufferSize in class Director
Parameters:
port - The specified port.
channelNumber - The specified channel number.
Returns:
The size of the generated buffer.
Throws:
IllegalActionException - If an error occurs when getting the value from the parameter.
See Also:
CodeGeneratorHelper.setBufferSize(IOPort, int, int)

getSharedCode

public java.util.Set getSharedCode()
                            throws IllegalActionException
Generate the shared code for the associated PN director.

Specified by:
getSharedCode in interface ActorCodeGenerator
Overrides:
getSharedCode in class CodeGeneratorHelper
Returns:
The generated shared code.
Throws:
IllegalActionException - If the helper associated with the director throws it while generating shared code.

_createDynamicOffsetVariables

protected java.lang.String _createDynamicOffsetVariables(IOPort port)
                                                  throws IllegalActionException
Create offset variables for the channels of the given port. The offset variables are generated unconditionally.

Parameters:
port - The port whose offset variables are generated.
Returns:
Code that declares the read and write offset variables.
Throws:
IllegalActionException - If getting the rate or reading parameters throws it.

_generateNotTerminateFlag

protected java.lang.String _generateNotTerminateFlag()
Generate the notTerminate flag variable for the associated PN director. Generating notTerminate instead of terminate saves the negation in checking the flag (e.g. "while (!terminate) ...").

Returns:
The varaible label of the notTerminate flag.

_generateThreadFunctionCode

private void _generateThreadFunctionCode(java.lang.StringBuffer code)
                                  throws IllegalActionException
Parameters:
code - The given code buffer.
Throws:
IllegalActionException

_generateActorNameFileForDebugging

private java.lang.String _generateActorNameFileForDebugging()

_getActorThreadLabel

private java.lang.String _getActorThreadLabel(Actor actor)
Generate the thread function name for a given actor.

Parameters:
actor - The given actor.
Returns:
A unique label for the actor thread function.