Class NamedProgramCodeGeneratorAdapter

    • Constructor Detail

      • NamedProgramCodeGeneratorAdapter

        public NamedProgramCodeGeneratorAdapter​(NamedObj component)
        Construct the code generator adapter associated with the given component.
        Parameters:
        component - The associated component.
    • Method Detail

      • analyzeTypeConvert

        public void analyzeTypeConvert()
                                throws IllegalActionException
        Find out each output port that needs to be converted for the actor associated with this adapter. Then, mark these ports along with the sink ports (connection).
        Throws:
        IllegalActionException - Not thrown in this base class.
      • copyFilesToCodeDirectory

        public static long copyFilesToCodeDirectory​(NamedObj namedObj,
                                                    ProgramCodeGenerator codeGenerator)
                                             throws java.io.IOException,
                                                    IllegalActionException
        Copy files to the code directory. The optional fileDependencies codeBlock consists of one or more lines where each line names a file that should be copied to the directory named by the codeDirectory parameter of the code generator. The file is only copied if a file by that name does not exist in codeDirectory or if the source file was more recently modified than the destination file.

        Using the fileDependencies code block allows actor writers to refer to code defined in other files.

        Parameters:
        namedObj - If this argument is an instance of ptolemy.actor.lib.jni.EmbeddedCActor, then the code blocks from EmbeddedCActor's embeddedCCode parameter are used.
        codeGenerator - The code generator from which the codeDirectory parameter is read.
        Returns:
        The modification time of the most recent file.
        Throws:
        java.io.IOException - If there is a problem reading the codeDirectory parameter.
        IllegalActionException - If there is a problem reading the codeDirectory parameter.
      • getDefaultBlocks

        public static java.lang.String[] getDefaultBlocks()
        Return an array of strings that are regular expressions of all the code blocks that are appended automatically by default. Since the content of the array are regex, users should use matches() instead of equals() to compare their strings.
        Returns:
        Array of string regular expressions of names of code blocks that are appended by default.
      • getAddTimeString

        public java.lang.String getAddTimeString()
                                          throws IllegalActionException
        Return a string that represents the additional time.
        Returns:
        In this base class, return the empty string. derived classes may want to return the value of the delay parameter in the target language.
        Throws:
        IllegalActionException - Not thrown in this baseclass. Derived classes may throw it if there is a problem accessing ports or parameters in the associated actor.
      • getSourceTimeString

        public java.lang.String getSourceTimeString​(java.lang.String timeVariable)
                                             throws IllegalActionException
        Return a string that represents the source time.
        Parameters:
        timeVariable - The variable to be set in the generated code.
        Returns:
        In this base class, return the empty string. Derived classes may want to return code that sets the time variable to the time stamp.
        Throws:
        IllegalActionException - Not thrown in this baseclass. Derived classes may throw it if there is a problem accessing ports or parameters in the associated actor.
      • getTimeSourcePortName

        public java.lang.String getTimeSourcePortName()
        Return the name of the port that is the time source.
        Returns:
        In this base class, return the empty string. Derived classes should return the name of the port that is the time source.
      • getFireFunctionParameters

        public java.lang.String getFireFunctionParameters()
        Return the parameters for the fire function.
        Returns:
        In this base class, return the empty string.
      • generateFireCode

        public java.lang.String generateFireCode()
                                          throws IllegalActionException
        Generate the fire code. In this base class, add the name of the associated component in the comment. It checks the inline parameter of the code generator. If the value is true, it generates the actor fire code and the necessary type conversion code. Otherwise, it generate an invocation to the actor function that is generated by generateFireFunctionCode.
        Returns:
        The generated code.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • 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.
        Returns:
        The fire function code.
        Throws:
        IllegalActionException - If thrown while generating fire code.
      • generateMainLoop

        public java.lang.String generateMainLoop()
                                          throws IllegalActionException
        Generate a main loop for an execution under the control of a director. In this base class, this simply delegates to generateFireCode() and generatePostfireCOde().
        Returns:
        Whatever generateFireCode() returns.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • generateTypeConvertFireCode

        public java.lang.String generateTypeConvertFireCode()
                                                     throws IllegalActionException
        Generate the type conversion fire code. This method is called by the Director to append necessary fire code to handle type conversion.
        Returns:
        The generated code.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • generateInitializeCode

        public java.lang.String generateInitializeCode()
                                                throws IllegalActionException
        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.
        Returns:
        The initialize code of the containing composite actor.
        Throws:
        IllegalActionException - If thrown while appending to the the block or processing the macros.
      • generateModeTransitionCode

        public void generateModeTransitionCode​(java.lang.StringBuffer code)
                                        throws IllegalActionException
        Generate 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.
        Parameters:
        code - The string buffer that the generated code is appended to.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • generatePostfireCode

        public java.lang.String generatePostfireCode()
                                              throws IllegalActionException
        Generate the postfire code. In this base class, do nothing. Subclasses may extend this method to generate the postfire code of the associated component and append the code to the given string buffer.
        Returns:
        The generated postfire code.
        Throws:
        IllegalActionException - If thrown while appending to the the block or processing the macros.
      • generatePrefireCode

        public java.lang.String generatePrefireCode()
                                             throws IllegalActionException
        Generate the prefire code of the associated composite actor.
        Returns:
        The prefire code of the associated composite actor.
        Throws:
        IllegalActionException - If illegal macro names are found.
      • generatePreinitializeCode

        public java.lang.String generatePreinitializeCode()
                                                   throws IllegalActionException
        Generate the preinitialize code. In this base class, return an empty string. This method generally does not generate any execution code and returns an empty string. Subclasses may generate code for variable declaration, defining constants, etc.
        Returns:
        A string of the preinitialize code for the adapter.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • generatePreinitializeMethodBodyCode

        public java.lang.String generatePreinitializeMethodBodyCode()
                                                             throws IllegalActionException
        Generate the preinitialization method body.

        Typically, the preinitialize code consists of variable declarations. However, AutoAdapter generates method calls that instantiate wrapper TypedCompositeActors, so we need to invoke those method calls.

        Returns:
        a string for the preinitialization method body. In this base class, return the empty string.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • generateSimpleName

        public static final java.lang.String generateSimpleName​(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.
        See Also:
        CodeGeneratorAdapter.generateName(NamedObj)
      • generateTypeConvertFireCode

        public java.lang.String generateTypeConvertFireCode​(boolean forComposite)
                                                     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:
        forComposite - True if we are generating code for a composite.
        Returns:
        The generated code.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • generateVariableDeclaration

        public java.lang.String generateVariableDeclaration()
                                                     throws IllegalActionException
        Generate variable declarations for inputs and outputs and parameters. Append the declarations to the given string buffer.
        Returns:
        code The generated code.
        Throws:
        IllegalActionException - If the adapter class for the model director cannot be found.
      • generateVariableInitialization

        public java.lang.String generateVariableInitialization()
                                                        throws IllegalActionException
        Generate variable initialization for the referenced parameters.
        Returns:
        code The generated code.
        Throws:
        IllegalActionException - If the adapter class for the model director cannot be found.
      • generateWrapupCode

        public java.lang.String generateWrapupCode()
                                            throws IllegalActionException
        Generate the wrapup code. In this base class, do nothing. Subclasses may extend this method to generate the wrapup code of the associated component and append the code to the given string buffer.
        Returns:
        The generated wrapup code.
        Throws:
        IllegalActionException - If thrown while appending to the the block or processing the macros.
      • getHeaderFiles

        public java.util.Set<java.lang.String> getHeaderFiles()
                                                       throws IllegalActionException
        Get the files needed by the code generated from this adapter class. This base class returns an empty set. Typically, the "includeFiles" block is used to set list of files needed. See TemplateParser.getHeaderFiles().
        Returns:
        A set of strings that are header files needed by the code generated from this adapter class.
        Throws:
        IllegalActionException - Not Thrown in this base class.
      • getIncludeDirectories

        public java.util.Set<java.lang.String> getIncludeDirectories()
                                                              throws IllegalActionException
        Return a set of directories to include for the generated code.
        Returns:
        A Set containing the contents of the actor's "includeDirectories" block in its template.
        Throws:
        IllegalActionException - If thrown when getting or reading the CodeStream.
      • getBufferSize

        public int getBufferSize​(IOPort port,
                                 int channelNumber)
                          throws IllegalActionException
        Get the buffer size of the given port of this actor.
        Parameters:
        port - The given port.
        channelNumber - The given channel.
        Returns:
        The buffer size of the given port and channel.
        Throws:
        IllegalActionException - If the getBufferSize() method of the actor helper class throws it.
      • getLibraries

        public java.util.Set<java.lang.String> getLibraries()
                                                     throws IllegalActionException
        Return a set of libraries to link in the generated code.
        Returns:
        A Set containing the libraries in the actor's "libraries" block in its template.
        Throws:
        IllegalActionException - If thrown when getting or reading the CodeStream.
      • getLibraryDirectories

        public java.util.Set<java.lang.String> getLibraryDirectories()
                                                              throws IllegalActionException
        Return a set of directories to find libraries in.
        Returns:
        A Set containing the directories in the actor's "libraryDirectories" block in its template.
        Throws:
        IllegalActionException - If thrown when getting or reading the CodeStream.
      • getModifiedVariables

        public java.util.Set<Parameter> getModifiedVariables()
                                                      throws IllegalActionException
        Return a set of parameters that will be modified during the execution of the model. The actor gets those variables if it implements ExplicitChangeContext interface or it contains PortParameters.
        Returns:
        a set of parameters that will be modified.
        Throws:
        IllegalActionException - If an actor throws it while getting modified variables.
      • getParameterValue

        public final java.lang.String getParameterValue​(java.lang.String name,
                                                        NamedObj container)
                                                 throws IllegalActionException
        Return the value or an expression in the target language for the specified parameter of the associated actor. If the parameter is specified by an expression, then the expression will be parsed. If any parameter referenced in that expression is specified by another expression, the parsing continues recursively until either a parameter is directly specified by a constant or a parameter can be directly modified during execution in which case a reference to the parameter is generated.
        Parameters:
        name - The name of the parameter.
        container - The container to search upwards from.
        Returns:
        The value or expression as a string.
        Throws:
        IllegalActionException - If the parameter does not exist or does not have a value.
      • getReference

        public final java.lang.String getReference​(java.lang.String name,
                                                   boolean executive)
                                            throws IllegalActionException
        Return the reference to the specified parameter or port of the associated actor. For a parameter, the returned string is in the form "fullName_parameterName". For a port, the returned string is in the form "fullName_portName[channelNumber][offset]", if any channel number or offset is given. FIXME: need documentation on the input string format.
        Parameters:
        name - The name of the parameter or port
        executive - If true, then look for the reference in the executive director (the director of the container).
        Returns:
        The reference to that parameter or port (a variable name, for example).
        Throws:
        IllegalActionException - If the parameter or port does not exist or does not have a value.
      • getReference

        public java.lang.String getReference​(java.lang.String name,
                                             boolean isWrite,
                                             boolean executive)
                                      throws IllegalActionException
        Return the reference to the specified parameter or port of the associated actor. For a parameter, the returned string is in the form "fullName_parameterName". For a port, the returned string is in the form "fullName_portName[channelNumber][offset]", if any channel number or offset is given. FIXME: need documentation on the input string format.
        Parameters:
        name - The name of the parameter or port
        isWrite - Whether to generate the write or read offset.
        executive - If true, then look for the reference in the executive director (the director of the container).
        Returns:
        The reference to that parameter or port (a variable name, for example).
        Throws:
        IllegalActionException - If the parameter or port does not exist or does not have a value.
      • getDeclareSharedCode

        public java.util.Set<java.lang.String> getDeclareSharedCode()
                                                             throws IllegalActionException
        Generate the declaration of the shared code (only for C code) At run time of the generated code, the code generated by this method is run first, so any initialization of variables of this adapter should be done in this method. Note that at code generation time, setupAdapter() is run before this method. In this base class, return an empty set. Subclasses may generate code for variable declaration, defining constants, etc.
        Returns:
        An empty set in this base class.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • getSharedCode

        public java.util.Set<java.lang.String> getSharedCode()
                                                      throws IllegalActionException
        Generate the shared code. At run time of the generated code, the code generated by this method is run first, so any initialization of variables of this adapter should be done in this method. Note that at code generation time, setupAdapter() is run before this method. In this base class, return an empty set. Subclasses may generate code for variable declaration, defining constants, etc.
        Returns:
        An empty set in this base class.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • getSinkChannels

        public static java.util.List<ProgramCodeGeneratorAdapter.Channel> getSinkChannels​(IOPort port,
                                                                                          int channelNumber)
                                                                                   throws IllegalActionException
        Return a list of channel objects that are the sink input ports given a port and channel. Note the returned channels are newly created objects and therefore not associated with the adapter class.
        Parameters:
        port - The given output port.
        channelNumber - The given channel number.
        Returns:
        The list of channel objects that are the sink channels of the given output channel.
        Throws:
        IllegalActionException - If thrown while getting the remote or deep receivers.
      • getTypeConvertChannels

        public java.util.Set<ProgramCodeGeneratorAdapter.Channel> getTypeConvertChannels()
        Get the set of channels that need to be type converted.
        Returns:
        Set of channels that need to be type converted.
      • getTypeConvertReference

        public static java.lang.String getTypeConvertReference​(ProgramCodeGeneratorAdapter.Channel channel)
        Generate a variable reference for the given channel. This variable reference is needed for type conversion. The source adapter get this reference instead of using the sink reference directly. This method assumes the given channel is a source (output) channel.
        Parameters:
        channel - The given source channel.
        Returns:
        The variable reference for the given channel.
      • getTypeConvertSinkChannels

        public java.util.List<ProgramCodeGeneratorAdapter.Channel> getTypeConvertSinkChannels​(ProgramCodeGeneratorAdapter.Channel source)
        Get the list of sink channels that the given source channel needs to be type converted to.
        Parameters:
        source - The given source channel.
        Returns:
        List of sink channels that the given source channel needs to be type converted to.
      • setupAdapter

        public void setupAdapter()
                          throws IllegalActionException
        Perform any setup or initialization of the adapter. Note that this is not the Ptolemy initialize() method, this method merely sets up any codegen-time variables in the adapters. In this base class, nothing is done.
        Throws:
        IllegalActionException - If an error occurrs while initializing an adapter.
      • targetType

        public final java.lang.String targetType​(Type ptType)
        Get the corresponding type in the target language from the given Ptolemy type.
        Parameters:
        ptType - The given Ptolemy type.
        Returns:
        The target language data type.
      • _generateFireCode

        protected java.lang.String _generateFireCode()
                                              throws IllegalActionException
        Generate the fire code. In this base class, add the name of the associated component in the comment. It checks the inline parameter of the code generator. If the value is true, it generates the actor fire code and the necessary type conversion code. Otherwise, it generate an invocation to the actor function that is generated by generateFireFunctionCode.
        Returns:
        The generated code.
        Throws:
        IllegalActionException - Not thrown in this base class.
      • _generateTypeConvertStatement

        protected java.lang.String _generateTypeConvertStatement​(ProgramCodeGeneratorAdapter.Channel source,
                                                                 ProgramCodeGeneratorAdapter.Channel sink,
                                                                 int offset)
                                                          throws IllegalActionException
        Generate the type conversion statement for the particular offset of the two given channels. This assumes that the offset is the same for both channel. Advancing the offset of one has to advance the offset of the other.
        Parameters:
        source - The given source channel.
        sink - The given sink channel.
        offset - The given offset.
        Returns:
        The type convert statement for assigning the converted source variable to the sink variable with the given offset.
        Throws:
        IllegalActionException - If there is a problem getting the adapters for the ports or if the conversion cannot be handled.