ptolemy.codegen.c.targets.openRTOS.domains.giotto.kernel
Class GiottoDirector

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.actor.sched.StaticSchedulingDirector
                  extended by ptolemy.codegen.c.domains.giotto.kernel.GiottoDirector
                      extended by ptolemy.codegen.c.targets.openRTOS.domains.giotto.kernel.GiottoDirector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, ActorCodeGenerator, ComponentCodeGenerator, Changeable, Debuggable, DebugListener, Derivable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

public class GiottoDirector
extends GiottoDirector

Code generator helper associated with the GiottoDirector class. This class is also associated with a code generator.

Since:
Ptolemy II 8.0
Version:
$Id: GiottoDirector.java 59167 2010-09-21 17:08:02Z cxh $
Author:
Shanna-Shaye Forbes, Man-Kit Leung, Ben Lickly
See Also:
Serialized Form
Accepted Rating:
Red (sssf)
Proposed Rating:
Red (sssf)

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 static int _MAX_PRIORITY_LEVEL
           
 
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
GiottoDirector(GiottoDirector giottoDirector)
          Construct the code generator helper associated with the given GiottoDirector.
 
Method Summary
private  int _depthInGiottoHierarchy()
          Return how may Giotto directors are above this director.
private  java.lang.String _generateActorsCode()
          Generates methods for all the actors seen by this director * @exception IllegalActionException
protected  java.lang.String _generateBlockCode(java.lang.String blockName, java.util.List args)
          Given a block name, generate code for that block.
(package private)  java.lang.String _generateDriverCode()
          Generate the content of driver methods.
private static java.lang.String _generateDriverName(NamedObj namedObj)
          Generate sanitized name for the given named object.
protected  java.lang.String _generateFireCode()
          Generate the fire code for the director.
protected  java.lang.String _generateInputVariableDeclaration()
          Generate input variable declarations.
(package private)  java.lang.String _generateOutputDriverCode()
          Generate the content of output driver methods.
protected  java.lang.String _generateOutputVariableDeclaration()
          Generate output variable declarations.
private  java.lang.String _generatePortVariableDeclarations(Actor actor)
          Generate PORT variables.
 java.lang.String _generateTypeConvertFireCode(IOPort source, IOPort sink)
          Generate the type conversion fire code.
private  java.util.ArrayList<java.lang.String>[] _getActorFrequencyTable()
          Generates a two dimensional array of actors at each frequency
private  java.lang.String _getActorName(Actor actor)
          Generates a string of the actor's name
private  java.util.List<Actor> _getActors(int frequencyValue)
           
private  java.util.HashSet<java.lang.Integer> _getAllFrequencies()
          Generates a set of the different frequencies seen by this Giotto Director
private  int _getFrequency(Actor actor)
          Determines the frequeny of the actor passed in as a parameter
private  double _getPeriod()
          Determines the period of this Giotto Director.
private  java.lang.String _getPortName(IOPort port)
          Generates a string representation of the port's name
private  int _getPriority(Actor actor)
          Determines the true frequency of the actor , which can be used as it's priority I.E.
private  int _getStackSize(java.util.List<Actor> actors)
          Determines the stack size necessary for the actors passed in
private  java.lang.String _getThreadName(int frequencyValue)
          Generates the name of a thread for a specified frequency value.
private  boolean _isTopDirectorFSM()
          Determines if this director is inside an FSM director
private  boolean _isTopGiottoDirector()
          Determines if this Giotto director is the top most Giotto Director
private  int _lcm(int[] array)
          This method was copied from the GiottoScheduler method.
private  java.lang.String _targetType(Type ptType)
          Determines the target type of Type.
private  java.lang.String _typeConversion(TypedIOPort source, TypedIOPort sink)
          Determines which type conversion is necessary when going from the source to the sink port
 java.lang.String generateFireCode()
          Generate the fire code for the director.
 java.lang.String generateFireFunctionCode()
          Generate The fire function code.
 java.lang.String generateInitializeCode()
          Generate the initialize code.
 java.lang.String generateMainLoop()
          Generate C code for the content of the main loop.
 void generateModeTransitionCode(java.lang.StringBuffer code)
          Generate the mode transition code.
 java.lang.String generateMyThreads()
          Generate code to create the threads.
 java.lang.String generatePostFireCode()
          Generate the postfire code.
 java.lang.String generatePreinitializeCode()
          Generate the preinitialize code.
 java.lang.String generateSchedulerThread(java.lang.String period)
          Generate code for the scheduler thread.
 void generateTransferInputsCode(IOPort inputPort, java.lang.StringBuffer code)
          Generate code for transferring enough tokens to complete an internal iteration.
 void generateTransferOutputsCode(IOPort outputPort, java.lang.StringBuffer code)
          Generate code for transferring enough tokens to fulfill the output production rate.
 java.lang.String generateVariableDeclaration()
          Generate a variable declaration for the period parameter, if there is one.
 java.lang.String getDriverReference(TypedIOPort port, java.lang.String[] channelAndOffset, boolean forComposite, boolean isWrite, CodeGeneratorHelper helper)
          Return a reference to the driver.
 java.util.Set getHeaderFiles()
          Get the files needed by the code generated from this helper class.
 java.lang.String getReference(TypedIOPort port, java.lang.String[] channelAndOffset, boolean forComposite, boolean isWrite, CodeGeneratorHelper helper)
          Return an unique label for the given port channel referenced by the given helper.
 double getWCET()
          Return the worst case execution time (WCET) seen by this director.
 
Methods inherited from class ptolemy.codegen.c.domains.giotto.kernel.GiottoDirector
_createDynamicOffsetVariables, _createOffsetVariablesIfNeeded, _createOffsetVariablesIfNeeded, _updatePortBufferSize, createOffsetVariablesIfNeeded, getBufferSize
 
Methods inherited from class ptolemy.codegen.actor.Director
_ceilToPowerOfTwo, _getIndentPrefix, _updateConnectedPortsOffset, _updatePortOffset, generateCodeForGet, generateCodeForSend, generateOffset, generatePostfireCode, generateVariableInitialization, generateWrapupCode, getComponent, getIncludeDirectories, getLibraries, getLibraryDirectories, getModifiedVariables, getReference, getReferenceChannels
 
Methods inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
_createBufferSizeAndOffsetMap, _createInputBufferSizeAndOffsetMap, _findClosedParen, _generateBlockCode, _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, getSharedCode, 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

_MAX_PRIORITY_LEVEL

private static int _MAX_PRIORITY_LEVEL
Constructor Detail

GiottoDirector

public GiottoDirector(GiottoDirector giottoDirector)
Construct the code generator helper associated with the given GiottoDirector.

Parameters:
giottoDirector - The associated ptolemy.domains.giotto.kernel.GiottoDirector
Method Detail

generateFireCode

public java.lang.String generateFireCode()
                                  throws IllegalActionException
Generate the fire code for the director.

Specified by:
generateFireCode in interface ActorCodeGenerator
Overrides:
generateFireCode in class StaticSchedulingDirector
Returns:
A string containing the fire code.
Throws:
IllegalActionException - If thrown by the superclass or thrown while generating code for the director.

generateFireFunctionCode

public java.lang.String generateFireFunctionCode()
                                          throws IllegalActionException
Generate The fire function code. This method is called when the firing code of each actor is not inlined. Each actor's firing code is in a function with the same name as that of the actor.

Specified by:
generateFireFunctionCode in interface ActorCodeGenerator
Overrides:
generateFireFunctionCode in class Director
Returns:
The fire function code.
Throws:
IllegalActionException - If thrown while generating fire code.

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 - Not Thrown in this base class.

generateInitializeCode

public java.lang.String generateInitializeCode()
                                        throws IllegalActionException
Generate the initialize code.

Specified by:
generateInitializeCode in interface ComponentCodeGenerator
Overrides:
generateInitializeCode in class GiottoDirector
Returns:
The initialize code of the directory.
Throws:
IllegalActionException - If thrown while appending to the the block or processing the macros.

generateMainLoop

public java.lang.String generateMainLoop()
                                  throws IllegalActionException
Generate C code for the content of the main loop.

Overrides:
generateMainLoop in class StaticSchedulingDirector
Returns:
String containing the content of the main method.
Throws:
IllegalActionException - If thrown while traversing the model.

generateModeTransitionCode

public void generateModeTransitionCode(java.lang.StringBuffer code)
                                throws IllegalActionException
Generate the mode transition code. The mode transition code generated in this method is executed after each global iteration, e.g., in HDF model. Do nothing in this base class.

Specified by:
generateModeTransitionCode in interface ActorCodeGenerator
Overrides:
generateModeTransitionCode in class Director
Parameters:
code - The string buffer that the generated code is appended to.
Throws:
IllegalActionException - Not thrown in this base class.

generateMyThreads

public java.lang.String generateMyThreads()
                                   throws IllegalActionException
Generate code to create the threads.

Returns:
the code for the threads.
Throws:
IllegalActionException - If thrown while accessing the model.

generatePostFireCode

public java.lang.String generatePostFireCode()
                                      throws IllegalActionException
Generate the postfire code.

Returns:
The generated postfire code.
Throws:
IllegalActionException - If thrown while appending to the the block or processing the macros.

generatePreinitializeCode

public java.lang.String generatePreinitializeCode()
                                           throws IllegalActionException
Generate the preinitialize code.

Specified by:
generatePreinitializeCode in interface ActorCodeGenerator
Overrides:
generatePreinitializeCode in class GiottoDirector
Returns:
A string of the preinitialize code for the helper.
Throws:
IllegalActionException - Not thrown in this base class.

getReference

public java.lang.String getReference(TypedIOPort port,
                                     java.lang.String[] channelAndOffset,
                                     boolean forComposite,
                                     boolean isWrite,
                                     CodeGeneratorHelper helper)
                              throws IllegalActionException
Return an unique label for the given port channel referenced by the given helper. By default, this delegates to the helper to generate the reference. Subclass may override this method to generate the desire label according to the given parameters.

Overrides:
getReference in class Director
Parameters:
port - The given port.
channelAndOffset - The given channel and offset.
forComposite - Whether the given helper is associated with a CompositeActor
isWrite - The type of the reference. True if this is a write reference; otherwise, this is a read reference.
helper - The specified helper.
Returns:
an unique reference label for the given port channel.
Throws:
IllegalActionException - If the helper throws it while generating the label.

getDriverReference

public java.lang.String getDriverReference(TypedIOPort port,
                                           java.lang.String[] channelAndOffset,
                                           boolean forComposite,
                                           boolean isWrite,
                                           CodeGeneratorHelper helper)
                                    throws IllegalActionException
Return a reference to the driver. This method is similar to the getReference() method however it it tailored for use by a driver method.

Parameters:
port - The port whose information is desired.
channelAndOffset - The given channel and offset.
forComposite - Whether the given helper is associated with a CompositeActor
isWrite - The type of the reference. True if this is a write reference; otherwise, this is a read reference.
helper - The specified helper.
Returns:
an unique reference label for the given port channel.
Throws:
IllegalActionException - If the helper throws it while generating the label.

generateSchedulerThread

public java.lang.String generateSchedulerThread(java.lang.String period)
                                         throws IllegalActionException
Generate code for the scheduler thread.

Parameters:
period - The period of the Giotto director.
Returns:
Code that creates the Scheduler thread.
Throws:
IllegalActionException - If thrown while accessing the model.

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 GiottoDirector
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.

generateTransferOutputsCode

public void generateTransferOutputsCode(IOPort outputPort,
                                        java.lang.StringBuffer code)
                                 throws IllegalActionException
Generate code for transferring enough tokens to fulfill the output production rate.

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

generateVariableDeclaration

public java.lang.String generateVariableDeclaration()
                                             throws IllegalActionException
Generate a variable declaration for the period parameter, if there is one.

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

_generateBlockCode

protected java.lang.String _generateBlockCode(java.lang.String blockName,
                                              java.util.List args)
                                       throws IllegalActionException
Given a block name, generate code for that block. This method is called by actors helpers that have simple blocks that do not take parameters or have widths.

Overrides:
_generateBlockCode in class CodeGeneratorHelper
Parameters:
blockName - The name of the block.
args - A list of arguments to be passed to the code block.
Returns:
The code for the given block.
Throws:
IllegalActionException - If illegal macro names are found, or if there is a problem parsing the code block from the helper .c file.

_generateFireCode

protected java.lang.String _generateFireCode()
                                      throws IllegalActionException
Generate the fire code for the director. In this case the fire code is simply the OpenRTOS thread code.

Overrides:
_generateFireCode in class CodeGeneratorHelper
Returns:
The generated code.
Throws:
IllegalActionException - If the thread code cannot be generated.

_generateInputVariableDeclaration

protected java.lang.String _generateInputVariableDeclaration()
                                                      throws IllegalActionException
Generate input variable declarations.

Returns:
a String that declares input variables.
Throws:
IllegalActionException - If thrown while getting port information.

_generateOutputVariableDeclaration

protected java.lang.String _generateOutputVariableDeclaration()
                                                       throws IllegalActionException
Generate output variable declarations.

Returns:
a String that declares output variables.
Throws:
IllegalActionException - If thrown while getting port information.

_depthInGiottoHierarchy

private int _depthInGiottoHierarchy()
Return how may Giotto directors are above this director.


_generateActorsCode

private java.lang.String _generateActorsCode()
                                      throws IllegalActionException
Generates methods for all the actors seen by this director * @exception IllegalActionException

Throws:
IllegalActionException

_generateDriverName

private static java.lang.String _generateDriverName(NamedObj namedObj)
Generate sanitized name for the given named object. Remove all underscores to avoid conflicts with systems functions.

Parameters:
namedObj - The named object for which the name is generated.
Returns:
The sanitized name.

_generateDriverCode

java.lang.String _generateDriverCode()
                               throws IllegalActionException
Generate the content of driver methods. For each actor update it's inputs to the outputs stored in ports. The PORT allows double buffering, in this case the output variable is used as the port. PORT here is simply a common variable, not a PORT in the general Ptolemy II actor sense NOTE: Duplicate ports connected through a fork are removed. IE. if an input is connected to a fork and the fork is connected to two other places... it removes the first place from the list of places and keeps the last place need to ask Jackie if there is a way to work around this b/c Reciever [][] recievers = getRecievers doesn't work.

Returns:
code that copies inputs from a port in a driver method
Throws:
IllegalActionException

_generateOutputDriverCode

java.lang.String _generateOutputDriverCode()
                                     throws IllegalActionException
Generate the content of output driver methods. The output driver updates the value of a port to be that of the output of the latest execution of a task.

Returns:
code that copies outputs to a port
Throws:
IllegalActionException

_generateTypeConvertFireCode

public java.lang.String _generateTypeConvertFireCode(IOPort source,
                                                     IOPort sink)
                                              throws IllegalActionException
Generate the type conversion fire code. This method is called by the Director to append necessary fire code to handle type conversion.

Parameters:
source - The source port, ignored by this method.
sink - The sink port, ignored by this method.
Returns:
The generated code, in this case, return the empty string.
Throws:
IllegalActionException - Not thrown in this base class.

getWCET

public double getWCET()
               throws IllegalActionException
Return the worst case execution time (WCET) seen by this director.

Overrides:
getWCET in class Director
Returns:
The Worst Case Execution Time (WCET).
Throws:
IllegalActionException - If there is a problem determining the WCET or a problem accessing the model.

_generatePortVariableDeclarations

private java.lang.String _generatePortVariableDeclarations(Actor actor)
                                                    throws IllegalActionException
Generate PORT variables. A PORT allows control over the value read A port is an efficient way to handle double buffering

Returns:
port variables
Throws:
IllegalActionException

_getActorFrequencyTable

private java.util.ArrayList<java.lang.String>[] _getActorFrequencyTable()
                                                                 throws IllegalActionException
Generates a two dimensional array of actors at each frequency

Returns:
ArrayList with actor names at each frequency
Throws:
IllegalActionException

_getActors

private java.util.List<Actor> _getActors(int frequencyValue)

_getActorName

private java.lang.String _getActorName(Actor actor)
Generates a string of the actor's name

Parameters:
actor -

_getAllFrequencies

private java.util.HashSet<java.lang.Integer> _getAllFrequencies()
                                                         throws IllegalActionException
Generates a set of the different frequencies seen by this Giotto Director

Returns:
a Hashset of integers containing the frequencies seen by the Giotto director
Throws:
IllegalActionException

_getPortName

private java.lang.String _getPortName(IOPort port)
Generates a string representation of the port's name

Parameters:
port -

_getFrequency

private int _getFrequency(Actor actor)
                   throws IllegalActionException
Determines the frequeny of the actor passed in as a parameter

Parameters:
actor - * @exception IllegalActionException
Throws:
IllegalActionException

_getPriority

private int _getPriority(Actor actor)
                  throws IllegalActionException
Determines the true frequency of the actor , which can be used as it's priority I.E. If the actor has a frequency 1, and it is inside a composite actor at the top of the director chain with frequency2, the actor's true frequency is 1*2 = 2, which is the priority it should be given.

Parameters:
actor - * @exception IllegalActionException
Throws:
IllegalActionException

_getPeriod

private double _getPeriod()
                   throws IllegalActionException
Determines the period of this Giotto Director. If it is not the top most Giotto director return the period of the Executive Giotto Director

Returns:
the period of the top most/ executive Giotto director
Throws:
IllegalActionException

_getStackSize

private int _getStackSize(java.util.List<Actor> actors)
Determines the stack size necessary for the actors passed in

Parameters:
actors -

_getThreadName

private java.lang.String _getThreadName(int frequencyValue)
Generates the name of a thread for a specified frequency value.

Parameters:
frequencyValue -
Returns:
the string containing the generated thread name

_isTopGiottoDirector

private boolean _isTopGiottoDirector()
Determines if this Giotto director is the top most Giotto Director


_isTopDirectorFSM

private boolean _isTopDirectorFSM()
Determines if this director is inside an FSM director

Returns:
true if inside an FSM director, false otherwise.

_lcm

private int _lcm(int[] array)
This method was copied from the GiottoScheduler method. It determines the least common multiple of the numbers passed in in array and returns the value


_typeConversion

private java.lang.String _typeConversion(TypedIOPort source,
                                         TypedIOPort sink)
Determines which type conversion is necessary when going from the source to the sink port

Parameters:
source - - the source port
sink - - the sink port
Returns:
a string indicating which type of conversion needs to happen i.e. InttoDouble, etc.

_targetType

private java.lang.String _targetType(Type ptType)
Determines the target type of Type. Currently forces the Token type to default to an integers