ptolemy.copernicus.kernel
Class Copernicus

java.lang.Object
  extended by ptolemy.copernicus.kernel.Copernicus

public class Copernicus
extends java.lang.Object

A Standalone application that generates code using the Ptolemy II code generation system. This class acts a wrapper for the copernicus.*.Main classes by providing defaults arguments for the various backends. The generatorAttribute Parameter names a MoML file that contains definitions for other Parameters and Variables that control the compilation and execution of the model The default compilation arguments are read in from a file named compileCommandTemplate.in, variables are substituted and the compile command executed and then default arguments are read in from a file named runCommandTemplate.in.

For example:

 java -classpath $PTII ptolemy.copernicus.kernel.Copernicus foo.xml
 
will read in the $PTII/ptolemy/copernicus/java/compileCommandTemplate.in, substitute in the appropriate variables and then generate code for foo.xml

The default code generator is the deep java code generator in $PTII/ptolemy/copernicus/java.

The argument that names the xml file containing the model to generate code for should be a relative pathname. The xml file argument is converted into a URL internally. If no xml file argument is specified, then code is generated for $PTII/ptolemy/domains/sdf/demo/OrthogonalCom/OrthogonalCom.xml

Generating code is fairly complex, so there are many other parameters that can be set as the other arguments.

The general format is -VariableName VariableValue, for example: -codeGenerator "shallow"

For example:

 java -classpath $PTII ptolemy.copernicus.kernel.GenerateCode -codeGenerator "shallow" foo.xml
 

The initial parameters, their values and any documentation can be printed with

 java -classpath $PTII ptolemy.copernicus.kernel.GenerateCode -help
 
If you have rebuilt Ptolemy II from sources, and have a shell such as bash available, then you can use $PTII/bin/copernicus as a shortcut. For example
 $PTII/bin/copernicus -codeGenerator "shallow" foo.xml
 

The details of how this class works can be found in the GeneratorAttribute documentation.

Since:
Ptolemy II 2.0
Version:
$Id: Copernicus.java,v 1.89 2007/12/06 18:28:57 cxh Exp $
Author:
Christopher Hylands
Accepted Rating:
Red (cxh)
Proposed Rating:
Red (cxh)

Field Summary
protected  java.lang.String[] _commandFlags
          The command-line options that are either present or not.
protected  java.lang.String[][] _commandOptions
          The command-line options that take arguments.
protected  java.lang.String _commandTemplate
          The form of the command line.
protected static boolean _test
          If true, then auto exit after a few seconds.
protected  boolean _verbose
          If true, then print debugging information.
static java.lang.String GENERATOR_NAME
          The name of the GeneratorAttribute
 
Constructor Summary
Copernicus(java.lang.String[] args)
          Parse the specified command-line arguments and then execute any specified commands.
 
Method Summary
protected  boolean _parseArg(java.lang.String arg)
          Parse a command-line argument.
protected  void _parseArgs(java.lang.String[] args)
          Parse the command-line arguments.
protected  void _saveParsedArgs()
          Save arguments that were parsed in the generatorAttribute of the model.
static java.lang.String commandToRun(GeneratorAttribute generatorAttribute)
          Return the command to run the generated code.
static void compileAndRun(CompositeActor model, GeneratorAttribute generatorAttribute)
          Possibly create the generated code and run it.
static int executeCommand(java.lang.String command)
          Execute a command in a subshell, and print out the results in standard error and standard out.
static java.lang.String exportMoMLToTemporaryFile(NamedObj namedObj)
          Export the MoML of the namedObj argument to a temporary file.
static void main(java.lang.String[] args)
          Create a new instance of this application, passing it the command-line arguments.
static java.util.HashMap newMap(NamedObj namedObj)
          Deprecated. See CodeGeneratorUtilities.newMap(NamedObj)
static java.io.BufferedReader openAsFileOrURL(java.lang.String inputFileName)
          Deprecated. See CodeGeneratorUtilities.openAsFileOrURL(String)
 CompositeActor readInModel(java.lang.String modelPathOrURL)
          Read in a MoML class, either as a top level model or a file, initialize the model, then create instance classes for actors.
static void substitute(java.io.BufferedReader inputFile, java.util.Map substituteMap, java.lang.String outputFileName)
          Deprecated. See CodeGeneratorUtilities.substitute(BufferedReader, Map, String)
static java.lang.String substitute(java.lang.String input, java.util.Map substituteMap)
          Deprecated. See CodeGeneratorUtilities.substitute(String, Map)
static void substitute(java.lang.String inputFileName, java.util.Map substituteMap, java.lang.String outputFileName)
          Deprecated. See CodeGeneratorUtilities.substitute(String, Map, String)
static java.lang.String substitute(java.lang.String inputFileName, NamedObj namedObj)
          Deprecated. See CodeGeneratorUtilities.substitute(String, NamedObj)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GENERATOR_NAME

public static final java.lang.String GENERATOR_NAME
The name of the GeneratorAttribute

See Also:
Constant Field Values

_commandFlags

protected java.lang.String[] _commandFlags
The command-line options that are either present or not.


_commandOptions

protected java.lang.String[][] _commandOptions
The command-line options that take arguments.


_commandTemplate

protected java.lang.String _commandTemplate
The form of the command line.


_test

protected static boolean _test
If true, then auto exit after a few seconds.


_verbose

protected boolean _verbose
If true, then print debugging information.

Constructor Detail

Copernicus

public Copernicus(java.lang.String[] args)
           throws java.lang.Exception
Parse the specified command-line arguments and then execute any specified commands.

Parameters:
args - The command-line arguments.
Throws:
java.lang.Exception - If command line arguments have problems.
Method Detail

commandToRun

public static java.lang.String commandToRun(GeneratorAttribute generatorAttribute)
                                     throws java.lang.Exception
Return the command to run the generated code. The generatorAttribute argument contains a the runCommandTemplateFile parameter that refers to a template file that contains the command to run the generated code after the parameters from generatorAttribute are substituted in.

Parameters:
generatorAttribute - The GeneratorAttribute that contains the Parameters that determine the command to run.
Returns:
The command to run the generated code.
Throws:
java.lang.Exception

compileAndRun

public static void compileAndRun(CompositeActor model,
                                 GeneratorAttribute generatorAttribute)
                          throws java.lang.Exception
Possibly create the generated code and run it. What actually happens depends on the values of the compile and run parameters in generatorAttribute

Parameters:
generatorAttribute - The GeneratorAttribute that contains the parameters that determine the commands to create and run the generated code.
Throws:
java.lang.Exception

executeCommand

public static int executeCommand(java.lang.String command)
                          throws java.lang.Exception
Execute a command in a subshell, and print out the results in standard error and standard out. Lines that begin with an octothorpe '#' are ignored. Substrings that start and end with a double quote are considered to be a single argument.

Parameters:
command - The command to execute.
Returns:
the exit status of the process, which is usually 0 if the process executed normally.
Throws:
java.lang.Exception

exportMoMLToTemporaryFile

public static java.lang.String exportMoMLToTemporaryFile(NamedObj namedObj)
                                                  throws java.lang.Exception
Export the MoML of the namedObj argument to a temporary file. The file is deleted when the Java virtual machine terminates.

Parameters:
namedObj - The NamedObj to export
Returns:
The name of the temporary file that was created
Throws:
java.lang.Exception - If the temporary file cannot be created.
See Also:
File.createTempFile(java.lang.String, java.lang.String, java.io.File)

main

public static void main(java.lang.String[] args)
Create a new instance of this application, passing it the command-line arguments.

Parameters:
args - The command-line arguments.

newMap

public static java.util.HashMap newMap(NamedObj namedObj)
                                throws IllegalActionException
Deprecated. See CodeGeneratorUtilities.newMap(NamedObj)

Given a NamedObj, generate a HashMap containing String key/value pairs where each key is a Parameter contained in the namedObj argument, and each value is the value of the Parameter.

Parameters:
namedObj - The NamedObj that contains Parameters.
Throws:
IllegalActionException

openAsFileOrURL

public static java.io.BufferedReader openAsFileOrURL(java.lang.String inputFileName)
                                              throws java.io.FileNotFoundException,
                                                     java.io.IOException
Deprecated. See CodeGeneratorUtilities.openAsFileOrURL(String)

Given a string that names a file or URL, try to open as a file, and then as a URL.

Parameters:
inputFileName - The name of the file or URL to open
Returns:
A BufferedReader that refers to the inputFileName
Throws:
java.io.FileNotFoundException - If the file cannot be found.
java.io.IOException - If there were problems creating the BufferedReader.

readInModel

public CompositeActor readInModel(java.lang.String modelPathOrURL)
                           throws IllegalActionException,
                                  NameDuplicationException
Read in a MoML class, either as a top level model or a file, initialize the model, then create instance classes for actors.

The MoML class name is processed as follows:

  1. The momlClassName argument is assumed to be a dot separated top level model name such as ptolemy.domains.sdf.demo.OrthogonalCom.OrthogonalCom and inserted into a MoML fragment:

    <entity name="ToplevelModel" class=" + momlClassName + "/> and then passed to MoMLParser.parse().

  2. If the parse fails, then the name is tried as a relative MoML file name and passed to MoMLParser.parseFile().

Throws:
IllegalActionException - If the model cannot be parsed.
NameDuplicationException

substitute

public static java.lang.String substitute(java.lang.String input,
                                          java.util.Map substituteMap)
Deprecated. See CodeGeneratorUtilities.substitute(String, Map)

Given a string and a Map containing String key/value pairs, substitute any keys found in the input with the corresponding values.

Parameters:
input - The input string that contains substrings like "@codeBase@".
substituteMap - The Map of String keys like "@codeBase@" and String values like "../../..".
Returns:
A string with the keys properly substituted with their corresponding values.

substitute

public static java.lang.String substitute(java.lang.String inputFileName,
                                          NamedObj namedObj)
                                   throws java.io.FileNotFoundException,
                                          java.io.IOException
Deprecated. See CodeGeneratorUtilities.substitute(String, NamedObj)

Read in the contents of inputFileName, and for each Parameter in namedObj, search for strings like @ParameterName@ in inputFileName, and substitute in the value of the Parameter and return the results.

Parameters:
inputFileName - The name of the file to read from.
namedObj - The NamedObj that contains Parameters to be searched for in inputFileName.
Returns:
The contents of inputFileName after doing the substitutions
Throws:
java.io.FileNotFoundException
java.io.IOException

substitute

public static void substitute(java.io.BufferedReader inputFile,
                              java.util.Map substituteMap,
                              java.lang.String outputFileName)
                       throws java.io.FileNotFoundException,
                              java.io.IOException
Deprecated. See CodeGeneratorUtilities.substitute(BufferedReader, Map, String)

Read in the contents of inputFile, and replace each matching String key found in substituteMap with the corresponding String value and write the results to outputFileName.

Parameters:
inputFile - A BufferedReader that refers to the file to be read in.
substituteMap - The Map of String keys like "@codeBase@" and String values like "../../..".
outputFileName - The name of the file to write to.
Throws:
java.io.FileNotFoundException
java.io.IOException
See Also:
substitute(String, Map, String)

substitute

public static void substitute(java.lang.String inputFileName,
                              java.util.Map substituteMap,
                              java.lang.String outputFileName)
                       throws java.io.FileNotFoundException,
                              java.io.IOException
Deprecated. See CodeGeneratorUtilities.substitute(String, Map, String)

Read in the contents of inputFileName, and replace each matching String key found in substituteMap with the corresponding String value and write the results to outputFileName.

Parameters:
inputFileName - The name of the file to read from.
substituteMap - The Map of String keys like "@codeBase@" and String values like "../../..".
outputFileName - The name of the file to write to.
Throws:
java.io.FileNotFoundException
java.io.IOException
See Also:
substitute(BufferedReader, Map, String)

_parseArg

protected boolean _parseArg(java.lang.String arg)
                     throws java.lang.Exception
Parse a command-line argument.

Returns:
True if the argument is understood, false otherwise.
Throws:
java.lang.Exception - If something goes wrong.

_parseArgs

protected void _parseArgs(java.lang.String[] args)
                   throws java.lang.Exception
Parse the command-line arguments.

Throws:
java.lang.Exception - If an argument is not understood or triggers an error.

_saveParsedArgs

protected void _saveParsedArgs()
                        throws java.lang.Exception
Save arguments that were parsed in the generatorAttribute of the model.

Throws:
java.lang.Exception