ptolemy.codegen.java.domains.modal.kernel
Class MultirateFSMDirector

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.java.domains.modal.kernel.FSMDirector
                  extended by ptolemy.codegen.java.domains.modal.kernel.MultirateFSMDirector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, ActorCodeGenerator, ComponentCodeGenerator, Changeable, Debuggable, DebugListener, Derivable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

public class MultirateFSMDirector
extends FSMDirector

Code generator helper class associated with the MultirateFSMDirector class.

Since:
Ptolemy II 8.0
Version:
$Id: MultirateFSMDirector.java 57044 2010-01-27 22:41:05Z cxh $
Author:
Gang Zhou
See Also:
Serialized Form
Accepted Rating:
Red (zgang)
Proposed Rating:
Red (zgang)

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
 
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
MultirateFSMDirector(MultirateFSMDirector director)
          Construct the code generator helper associated with the given MultirateFSMDirector.
 
Method Summary
protected  java.lang.String _createOffsetVariablesIfNeeded()
          Create read and write offset variables if needed for any output port of the container actor and any input port of contained actors.
protected  java.lang.String _createOffsetVariablesIfNeeded(IOPort port)
          Check to see if variables are needed to represent read and write offsets for the given port.
protected  java.lang.String _generateInitialTransitionCode(java.util.Iterator transitions)
          Generate code for making initial transition.
protected  void _updateConfigurationNumber(java.lang.StringBuffer codeBuffer, State state)
          Generate code for updating configuration number of the container actor according to the following equation: container's current configuration number = sum of numbers of configurations of refinements corresponding to states before the current state + configuration number of refinement corresponding to the current state The order of states is the same as in the list returned by calling entityList() on the controller.
protected  void _updatePortBufferSize(Actor refinement, int[] portRates)
          Check to see if the buffer size needed in current configuration is greater than in previous configurations.
 java.lang.String createOffsetVariablesIfNeeded()
          Generate code for declaring read and write offset variables if needed.
 java.lang.String generateFireCode()
          Generate the code for the firing of actors controlled by this director.
 java.lang.String generateInitializeCode()
          Generate the initialize code for the associated MultirateFSMDirector.
 java.lang.String generatePreinitializeCode()
          Generate the preinitialize code for this director.
 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.
 
Methods inherited from class ptolemy.codegen.java.domains.modal.kernel.FSMDirector
_generateRefinementCode, generateFireFunctionCode
 
Methods inherited from class ptolemy.codegen.actor.Director
_ceilToPowerOfTwo, _getIndentPrefix, _updateConnectedPortsOffset, _updatePortOffset, generateCodeForGet, generateCodeForSend, generateMainLoop, generateModeTransitionCode, generateOffset, generatePostfireCode, generateVariableDeclaration, generateVariableInitialization, generateWrapupCode, getBufferSize, getComponent, getHeaderFiles, getIncludeDirectories, getLibraries, 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, 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
 

Constructor Detail

MultirateFSMDirector

public MultirateFSMDirector(MultirateFSMDirector director)
Construct the code generator helper associated with the given MultirateFSMDirector.

Parameters:
director - The associated component.
Method Detail

createOffsetVariablesIfNeeded

public java.lang.String createOffsetVariablesIfNeeded()
                                               throws IllegalActionException
Generate code for declaring read and write offset variables if needed.

Specified by:
createOffsetVariablesIfNeeded in interface ActorCodeGenerator
Overrides:
createOffsetVariablesIfNeeded in class Director
Returns:
The generated code.
Throws:
IllegalActionException - If thrown while creating offset variables.

generateFireCode

public java.lang.String generateFireCode()
                                  throws IllegalActionException
Generate the code for the firing of actors controlled by this director. It generates code for firing refinements and making non-preemptive transition.

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

generateInitializeCode

public java.lang.String generateInitializeCode()
                                        throws IllegalActionException
Generate the initialize code for the associated MultirateFSMDirector. Generate code for initializing the configuration number of the container actor. If the initial state does not have a refinement, which usually happens when the purpose of making transition from the initial state is to initialize model parameters, generate code for making transition and then initializing the configuration number of the container actor.

Specified by:
generateInitializeCode in interface ComponentCodeGenerator
Overrides:
generateInitializeCode in class Director
Returns:
The generated initialize code.
Throws:
IllegalActionException - If thrown while calling the same method in its super class or updating the configuration number.

generatePreinitializeCode

public java.lang.String generatePreinitializeCode()
                                           throws IllegalActionException
Generate the preinitialize code for this director. First find out the number of configurations of the modal model which is the container actor of the director. For each configuration, find the port rates of the modal model from the port rates of the corresponding refinement and update the maximum buffer sizes needed for the ports of the refinement.

Specified by:
generatePreinitializeCode in interface ActorCodeGenerator
Overrides:
generatePreinitializeCode in class Director
Returns:
The generated preinitialize code.
Throws:
IllegalActionException - If getting the helper fails, or if generating the preinitialize code for a helper fails, or if there is a problem getting the buffer size of a port.

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.

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

_createOffsetVariablesIfNeeded

protected java.lang.String _createOffsetVariablesIfNeeded()
                                                   throws IllegalActionException
Create read and write offset variables if needed for any output port of the container actor and any input port of contained actors.

Returns:
A string containing declared read and write offset variables.
Throws:
IllegalActionException - If thrown while creating offset variables.

_createOffsetVariablesIfNeeded

protected java.lang.String _createOffsetVariablesIfNeeded(IOPort port)
                                                   throws IllegalActionException
Check to see if variables are needed to represent read and write offsets for the given port.

Parameters:
port - The given port.
Returns:
A string containing declared read and write offset variables.
Throws:
IllegalActionException - If thrown while creating offset variables.

_generateInitialTransitionCode

protected java.lang.String _generateInitialTransitionCode(java.util.Iterator transitions)
                                                   throws IllegalActionException
Generate code for making initial transition. The purpose of this method is to initialize parameter values because they are persistent from each run of the model. If not properly initialized, parameters will retain their accumulated values from previous model executions. A typical approach is to build the model such that the initial state has an outgoing transition with guard expression true, and use the set actions of this transition for parameter initialization. However, this transition can only be taken during initialization instead of after firing a refinement because the initial state does not have a refinement.

Parameters:
transitions - The iterator of transitions from initial state.
Returns:
The generated initial transition code.
Throws:
IllegalActionException - If thrown while generating transition code.

_updateConfigurationNumber

protected void _updateConfigurationNumber(java.lang.StringBuffer codeBuffer,
                                          State state)
                                   throws IllegalActionException
Generate code for updating configuration number of the container actor according to the following equation:

container's current configuration number = sum of numbers of configurations of refinements corresponding to states before the current state + configuration number of refinement corresponding to the current state

The order of states is the same as in the list returned by calling entityList() on the controller.

Parameters:
codeBuffer - The string buffer that the generated code is appended to.
state - The current state.
Throws:
IllegalActionException - If helper cannot be found, refinement cannot be found or code cannot be processed.

_updatePortBufferSize

protected void _updatePortBufferSize(Actor refinement,
                                     int[] portRates)
                              throws IllegalActionException
Check to see if the buffer size needed in current configuration is greater than in previous configurations. If so, set the buffer size to the current buffer size needed.

Parameters:
refinement - The refinement to be checked.
portRates - An int array of port rates of the refinement.
Throws:
IllegalActionException - If thrown while getting helper or buffer size.