ptolemy.codegen.c.actor
Class IOPort

java.lang.Object
  extended by ptolemy.kernel.util.NamedObj
      extended by ptolemy.codegen.kernel.CodeGeneratorHelper
          extended by ptolemy.codegen.c.kernel.CCodeGeneratorHelper
              extended by ptolemy.codegen.c.actor.IOPort
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, ActorCodeGenerator, ComponentCodeGenerator, PortCodeGenerator, Changeable, Debuggable, DebugListener, Derivable, ModelErrorHandler, MoMLExportable, Moveable, Nameable
Direct Known Subclasses:
ModalPort, ModalPort, MultiCompositePort, RefinementPort, RefinementPort, RefinementPort, TransitionRefinementPort, TransitionRefinementPort, TypedIOPort

public class IOPort
extends CCodeGeneratorHelper
implements PortCodeGenerator

Code generator helper for IOPort.

Since:
Ptolemy II 8.0
Version:
$Id: IOPort.java 57046 2010-01-27 23:35:53Z cxh $
Author:
Man-Kit Leung
See Also:
Serialized Form
Accepted Rating:
Red (mankit)
Proposed Rating:
Red (mankit)

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
protected  java.util.HashMap<CodeGeneratorHelper.Channel,java.lang.Integer> _bufferSizes
          A HashMap that keeps track of the bufferSizes of each channel of the actor.
protected  java.util.HashMap<CodeGeneratorHelper.Channel,java.lang.Object> _readOffsets
          A HashMap that keeps track of the read offsets of each input channel of the actor.
protected  java.util.HashMap<CodeGeneratorHelper.Channel,java.lang.Object> _writeOffsets
          A HashMap that keeps track of the write offsets of each input channel of the actor.
 
Fields inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
_codeGenerator, _codeStream, _eol, _INDENT1, _INDENT2, _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
IOPort(IOPort component)
          Construct a code generator helper associated with the given IOPort.
 
Method Summary
protected  java.lang.String _generateOffset(java.lang.String offsetString, int channel, boolean isWrite)
          Generate the expression that represents the offset in the generated code.
private  CodeGeneratorHelper.Channel _getChannel(int channelNumber)
           
private  Director _getDirectorHelper()
           
private  Receiver _getReceiver(java.lang.String offset, int channel, IOPort port)
           
private  boolean _isInteger(java.lang.String numberString)
          Return true if the given string can be parse as an integer; otherwise, return false.
private  boolean _isPNQueueReceiver(Receiver receiver)
          Return true if the specified receiver is a PNQueueReceiver; otherwise, false.
private  boolean _isPthread()
           
protected  java.lang.String _updateOffset(int channel, int rate)
          Update the offset for each channel of a port.
private  java.lang.String _updatePNOffset(int rate, IOPort port, int channelNumber, Director directorHelper, boolean isWrite)
           
 java.lang.String generateCodeForGet(java.lang.String channel)
          Generate code for replacing the get() macro.
 java.lang.String generateCodeForSend(java.lang.String channel, java.lang.String dataToken)
          Generate code for replacing the send() macro.
 java.lang.String generateInitializeCode()
          Generate the initialize code.
 java.lang.String generateOffset(java.lang.String offset, int channel, boolean isWrite, Director director)
          Generate the expression that represents the offset in the generated code.
 java.lang.String generatePostFireCode()
          Generate the code for postfire().
 java.lang.String generatePreFireCode()
          Generate the code for prefire().
 int getBufferSize(int channelNumber)
          Get the buffer size of channel of the port.
 Director getDirector()
          Return the executive director.
 java.lang.Object getReadOffset(int channelNumber)
          Get the read offset of a channel of the port.
 java.lang.Object getWriteOffset(int channelNumber)
          Get the write offset of a channel of the port.
 java.lang.String initializeOffsets()
          Initialize the offsets.
 void setBufferSize(int channelNumber, int bufferSize)
          Set the buffer size of channel of the port.
 void setReadOffset(int channelNumber, java.lang.Object readOffset)
          Set the read offset of a channel of the port.
 void setWriteOffset(int channelNumber, java.lang.Object writeOffset)
          Set the write offset of a channel of the port.
 java.lang.String updateConnectedPortsOffset(int rate, Director director)
          Update the write offset of the [multiple] connected ports.
 java.lang.String updateOffset(int rate, Director directorHelper)
          Update the read offset.
 
Methods inherited from class ptolemy.codegen.c.kernel.CCodeGeneratorHelper
_generateInputVariableDeclaration, _generateOutputVariableDeclaration, _generateReferencedParameterDeclaration, _generateTypeConvertStatement, _generateTypeConvertVariableDeclaration, _getFireFunctionArguments, _replaceMacro, generateVariableDeclaration, getCodeGenerator, getHeaderFiles, getJVMHeaderFiles, getParseTreeCodeGenerator
 
Methods inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
_createBufferSizeAndOffsetMap, _createInputBufferSizeAndOffsetMap, _findClosedParen, _generateBlockCode, _generateBlockCode, _generateFireCode, _generateTypeConvertMethod, _generateTypeConvertStatements, _getCastType, _getChannelAndOffset, _getHelper, _getHelper, _getIndentPrefix, _getReference, _getReferenceChannels, _getTypeConvertChannels, _getTypeConvertReference, _indexOf, addFunctionUsed, addNewTypeUsed, analyzeTypeConvert, checkLocal, checkRemote, codeGenType, copyFilesToCodeDirectory, createOffsetVariablesIfNeeded, generateChannelOffset, generateFireCode, generateFireFunctionCode, generateFireFunctionCode2, generateModeTransitionCode, generateName, generateOffset, generatePortReference, generatePostfireCode, generatePrefireCode, generatePreinitializeCode, generateSimpleName, generateTypeConvertFireCode, generateTypeConvertFireCode, generateVariableInitialization, generateVariableName, generateWrapupCode, getBufferSize, getBufferSize, getComponent, getDefaultBlocks, getDirectorHelper, getFunctionInvocation, getIncludeDirectories, getLibraries, getLibraryDirectories, getModifiedVariables, getNewInvocation, getObject, getParameterValue, getPort, getRates, getReadOffset, getReference, getReference, getReference, getReference, getSharedCode, getSinkChannels, getSize, getSourceChannel, getWCET, 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
 
Methods inherited from interface ptolemy.codegen.kernel.ComponentCodeGenerator
generateWrapupCode, getComponent, setCodeGenerator
 

Field Detail

_bufferSizes

protected java.util.HashMap<CodeGeneratorHelper.Channel,java.lang.Integer> _bufferSizes
A HashMap that keeps track of the bufferSizes of each channel of the actor.


_readOffsets

protected java.util.HashMap<CodeGeneratorHelper.Channel,java.lang.Object> _readOffsets
A HashMap that keeps track of the read offsets of each input channel of the actor.


_writeOffsets

protected java.util.HashMap<CodeGeneratorHelper.Channel,java.lang.Object> _writeOffsets
A HashMap that keeps track of the write offsets of each input channel of the actor.

Constructor Detail

IOPort

public IOPort(IOPort component)
Construct a code generator helper associated with the given IOPort.

Parameters:
component - The given IOPort.
Method Detail

generateCodeForGet

public java.lang.String generateCodeForGet(java.lang.String channel)
                                    throws IllegalActionException
Description copied from interface: PortCodeGenerator
Generate code for replacing the get() macro. This delegates to the receiver adapter for the specified channel and asks it to generate the get code.

Specified by:
generateCodeForGet in interface PortCodeGenerator
Parameters:
channel - The channel for which the get code is generated.
Returns:
The code that gets data from the channel.
Throws:
IllegalActionException - If the director adapter class cannot be found. FIXME: potentially, we could also pass in a boolean that indicates whether the port the channel resides is a multiport, if it is, then only a static variable is needed instead of an array of length 1.

generateCodeForSend

public java.lang.String generateCodeForSend(java.lang.String channel,
                                            java.lang.String dataToken)
                                     throws IllegalActionException
Description copied from interface: PortCodeGenerator
Generate code for replacing the send() macro.

Specified by:
generateCodeForSend in interface PortCodeGenerator
Parameters:
channel - The channel for which the send code is generated.
dataToken - The token to be sent
Returns:
The code that sends the dataToken on the channel.
Throws:
IllegalActionException - If the director adapter class cannot be found. FIXME: potentially, we could also pass in a boolean that indicates whether the port the channel resides is a multiport, if it is, then only a static variable is needed instead of an array of length 1.

generateInitializeCode

public java.lang.String generateInitializeCode()
                                        throws IllegalActionException
Description copied from class: CodeGeneratorHelper
Generate the initialize code. In this base class, return empty string. Subclasses may extend this method to generate initialize code of the associated component and append the code to the given string buffer.

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

generateOffset

public java.lang.String generateOffset(java.lang.String offset,
                                       int channel,
                                       boolean isWrite,
                                       Director director)
                                throws IllegalActionException
Description copied from interface: PortCodeGenerator
Generate the expression that represents the offset in the generated code.

Specified by:
generateOffset in interface PortCodeGenerator
Parameters:
offset - The specified offset from the user.
channel - The referenced port channel.
isWrite - Whether to generate the write or read offset.
director - The helper of the director associated with the port.
Returns:
The expression that represents the offset in the generated code.
Throws:
IllegalActionException - If there is problems getting the port buffer size or the offset in the channel and offset map.

generatePostFireCode

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

Returns:
In this baseclass, return the empty string.
Throws:
IllegalActionException - Not thrown in this base class.

generatePreFireCode

public java.lang.String generatePreFireCode()
                                     throws IllegalActionException
Generate the code for prefire().

Returns:
In this baseclass, return the empty string.
Throws:
IllegalActionException - Not thrown in this base class.

getBufferSize

public int getBufferSize(int channelNumber)
                  throws IllegalActionException
Get the buffer size of channel of the port.

Specified by:
getBufferSize in interface PortCodeGenerator
Parameters:
channelNumber - The number of the channel that is being set.
Returns:
return The size of the buffer.
Throws:
IllegalActionException - If thrown while getting the width of the port of the channel.
See Also:
setBufferSize(int, int)

getDirector

public Director getDirector()
Description copied from class: CodeGeneratorHelper
Return the executive director. If there is no executive director, then return the director associated with the object passed in to the constructor.

Overrides:
getDirector in class CodeGeneratorHelper
Returns:
the executive director or the director of the actor.

getReadOffset

public java.lang.Object getReadOffset(int channelNumber)
                               throws IllegalActionException
Get the read offset of a channel of the port.

Specified by:
getReadOffset in interface PortCodeGenerator
Parameters:
channelNumber - The number of the channel.
Returns:
The read offset.
Throws:
IllegalActionException - If thrown while getting the channel.
See Also:
setReadOffset(int, Object)

getWriteOffset

public java.lang.Object getWriteOffset(int channelNumber)
                                throws IllegalActionException
Get the write offset of a channel of the port.

Specified by:
getWriteOffset in interface PortCodeGenerator
Parameters:
channelNumber - The number of the channel.
Returns:
The write offset.
Throws:
IllegalActionException - If thrown while getting the channel.
See Also:
setWriteOffset(int, Object)

initializeOffsets

public java.lang.String initializeOffsets()
                                   throws IllegalActionException
Description copied from interface: PortCodeGenerator
Initialize the offsets.

Specified by:
initializeOffsets in interface PortCodeGenerator
Returns:
The code to initialize the offsets.
Throws:
IllegalActionException - If thrown while getting the channel or initializing the offset.

setBufferSize

public void setBufferSize(int channelNumber,
                          int bufferSize)
Set the buffer size of channel of the port.

Specified by:
setBufferSize in interface PortCodeGenerator
Parameters:
channelNumber - The number of the channel that is being set.
bufferSize - The size of the buffer.
See Also:
getBufferSize(int)

setReadOffset

public void setReadOffset(int channelNumber,
                          java.lang.Object readOffset)
Set the read offset of a channel of the port.

Specified by:
setReadOffset in interface PortCodeGenerator
Parameters:
channelNumber - The number of the channel that is being set.
readOffset - The offset.
See Also:
getReadOffset(int)

setWriteOffset

public void setWriteOffset(int channelNumber,
                           java.lang.Object writeOffset)
Set the write offset of a channel of the port.

Specified by:
setWriteOffset in interface PortCodeGenerator
Parameters:
channelNumber - The number of the channel that is being set.
writeOffset - The offset.
See Also:
getWriteOffset(int)

updateConnectedPortsOffset

public java.lang.String updateConnectedPortsOffset(int rate,
                                                   Director director)
                                            throws IllegalActionException
Description copied from interface: PortCodeGenerator
Update the write offset of the [multiple] connected ports.

Specified by:
updateConnectedPortsOffset in interface PortCodeGenerator
Parameters:
rate - The rate of the channels.
director - The Director helper.
Returns:
The offset.
Throws:
IllegalActionException - If thrown while getting the channel or updating the offset.

updateOffset

public java.lang.String updateOffset(int rate,
                                     Director directorHelper)
                              throws IllegalActionException
Description copied from interface: PortCodeGenerator
Update the read offset.

Specified by:
updateOffset in interface PortCodeGenerator
Parameters:
rate - The rate of the channels.
directorHelper - The Director helper
Returns:
The offset.
Throws:
IllegalActionException - If thrown while getting the channel or updating the offset.

_generateOffset

protected java.lang.String _generateOffset(java.lang.String offsetString,
                                           int channel,
                                           boolean isWrite)
                                    throws IllegalActionException
Generate the expression that represents the offset in the generated code.

Parameters:
offsetString - The specified offset from the user.
channel - The referenced port channel.
isWrite - Whether to generate the write or read offset.
Returns:
The expression that represents the offset in the generated code.
Throws:
IllegalActionException - If there is problems getting the port buffer size or the offset in the channel and offset map.

_updateOffset

protected java.lang.String _updateOffset(int channel,
                                         int rate)
                                  throws IllegalActionException
Update the offset for each channel of a port.

Parameters:
channel - The channel for which the the offset is to be updated
rate - The rate of the channel
Returns:
If the rate is fixed, then the empty string is returned. If the rate is variable, then a string that refers to the appropriate element in the buffer is returned.
Throws:
IllegalActionException - If thrown while accessing the port or read offset

_getChannel

private CodeGeneratorHelper.Channel _getChannel(int channelNumber)

_getDirectorHelper

private Director _getDirectorHelper()
                             throws IllegalActionException
Throws:
IllegalActionException

_getReceiver

private Receiver _getReceiver(java.lang.String offset,
                              int channel,
                              IOPort port)

_isInteger

private boolean _isInteger(java.lang.String numberString)
Return true if the given string can be parse as an integer; otherwise, return false.

Parameters:
numberString - The given number string.
Returns:
True if the given string can be parse as an integer; otherwise, return false.

_isPNQueueReceiver

private boolean _isPNQueueReceiver(Receiver receiver)
Return true if the specified receiver is a PNQueueReceiver; otherwise, false.

Parameters:
receiver - The specified receiver.
Returns:
true if the specified receiver is a PNQueueReceiver; otherwise, false.

_isPthread

private boolean _isPthread()

_updatePNOffset

private java.lang.String _updatePNOffset(int rate,
                                         IOPort port,
                                         int channelNumber,
                                         Director directorHelper,
                                         boolean isWrite)
                                  throws IllegalActionException
Throws:
IllegalActionException