ptlang
, was created to make it easier to write and document star class definitions to run under Ptolemy. Instead of writing all the class definitions and initialization code required for a Ptolemy star, the user can concentrate on writing the action code for a star and let the preprocessor generate the standard initialization code for portholes, states, etc. The preprocessor generates standard C++ code, divided into two files (a header file with a
.h
extension and an implementation file with a
.cc
extension). It also generates standardized documentation, in a file with a
.html
extension, to be included in the manual. In releases before Ptolemy 0.7, Ptolemy used .t
files, which conained
troff source
YYY
in domain XXX
should appear in file with the name XXXYYY.pl
. The class that implements this star will be named XXXYYY
. Then, running the command
XXXYYY.cc
, XXXYYY.h
, and XXXYYY.html
. Implementation of the preprocessorThe preprocessor is written in
yacc
and C. It does not attempt to parse the parts of the language that consist of C++ code (for example, go
methods); for these, it simply counts curly braces to find the ends of the items in question. It outputs #line
directives so the C++ compiler will print error messages, if any, with respect to the original source file.
SDFRect.pl
:
SDFRect.h
, SDFRect.cc
and SDFRect.html
; the names are determined not by the input filename but by concatenating the domain and name fields. These files define a class named SDFRect
.At the time of this writing, only one type of declaration may appear at the top level of a Ptolemy language file, a
defstar
, used to define a star. Sometime in the future, a defgalaxy
section may also be supported. The defstar
section is itself composed of subitems that define various attributes of the star. All subitems are of the form
body
may itself be composed of sub-subitems, or may be C++ code (in which case the Ptolemy language preprocessor checks it only for balanced curly braces). Note that the keywords are not reserved words; they may also be used as identifiers in the body.
defstar
directive. The items are given in the order in which they typically appear in a star definition (although they can appear in any order). An alphabetical listing and summary of directives is given in table
name
This is a required item, and has the syntax
name { identifier } domain
This is a required item; it specifies the domain, such as SDF. The syntax is:
domain { identifier }identifier
specifies the domain (again, case is important). derivedfrom
This optional item indicates that the star is derived from another class. Syntax:
derivedfrom { identifier }identifier
specifies the base class. The .h
file for the base class is automatically included in the output .h
file, assuming it can be located (you may need to create a makefile).LMS
star in the SDF
domain is derived from the FIR
star. The full name of the base class is SDFFIR
, but the derivedfrom
statement allows you to say eitherderivedfrom
statement may also be written derivedFrom
or derived
. descriptor
This item defines a short description of the class. This description is displayed by the
profile
pigi command. It has the syntaxtext
is simply a section of text that will become the short descriptor of the star. You may also write desc
instead of descriptor
. A principal use of the short descriptor is to get on-screen help, so the descriptor should not include any troff formatting commands. Unlike the htmldoc
(described below), it does not pass through troff. The following are legal descriptors:
A multi-line descriptor. The same line breaks and spacing
will be used when the descriptor is displayed on the screen.
} htmldoc
directive, explained below. However, it should be long enough so that it is sufficient to explain the function of the star. version
This item contains two entries as shown below
version { number MO/DA/YR }number
is a version number, and the MO/DA/YR
is the version date. If you are using SCCS for version control then the following syntax will work well:
version { %W% %G% }%W%
will be replaced with a string of the form: @(#)
filename
num, where num is the version number, and %G%
will be replaced with a properly formatted date. author
This optional entry identifies the author or authors of the star. The syntax is
author { author1, author2 and author3 } acknowledge
This optional entry attaches an acknowledgment section to the documentation. The syntax is
acknowledge { arbitrary single line of text } copyright
This optional entry attaches a copyright notice to the .h
, .cc
, and .t
files. The syntax is
copyright { copyright information }
copyright {1994 The Regents of the University of California}%Q%
keyword to update the date when a file is changed. A typical copyright line might look like:
California} location
This item describes the location of a star definition. The following descriptions are used, for example:
location { SDF dsp library }directory
is the location of the star. This item is for documentation only. explanation
This item is used to give longer explanations of the function of the stars. In releases previous to Ptolemy 0.7, this item included troff formatting directives. In Ptolemy 0.7 and later, this item has been superceded by the htmldoc
item. htmldoc
This item is used to give longer explanations that include HTML format directives. The Tycho system includes an HTML viewer that can be used to display star documentation. The HTML output of ptlang
can be viewed by any HTML viewer, but certain features, such as the <tcl></tcl>
directive are only operational when viewed with Tycho. For complete documentation for the Tycho HTML viewer, see the HTML viewer Help menu. state
This item is used to define a
state or parameter. Recall that by definition, a parameter is the initial value of a state. Here is an example of a state definition:
name { gain }
type { int }
default { 10 }
desc { Output gain. }
attributes { A_CONSTANT|A_SETTABLE }
}name
field is the name of the state; the type
field is its type, which may be one of
int
,
float
,
string
,
complex
,
fix
,
intarray
,
floatarray
,
complexarray
,
precision
, or
stringarray
. Case is ignored for the type argument.
default
item specifies the default initial value of the state; its argument is either a string (enclosed in quotation marks) or a numeric value. The above entry could equivalently have been written:
default { "1.0" }ComplexArray
:
default {
"(-.040609,0.0) (-.001628,0.0) (.17853,0.0) (.37665,0.0)"
"(.37665,0.0) (.17853,0.0) (-.001628,0.0) (-.040609,0.0)"
}real
and imag
evaluate to integers or floats. precision
state is used to give the precision of
fixed-point values. These values may be other states or may be internal to the star. The default can be specified in either of two ways:
The
desc
(or
descriptor
) item, which is optional but highly recommended, attaches a descriptor to the state. The same formatting options are available as with the star descriptor. Finally, the
attributes
keyword specifies state attributes. At present, two attributes are defined for all states:
A_CONSTANT
and
A_SETTABLE
(along with their complements
A_NONCONSTANT
and
A_NONSETTABLE
). If a state has the A_CONSTANT
attribute, then its value is not modified by the run-time code in the star (it is up to you as the star writer to ensure that this condition is satisfied). States with the A_NONCONSTANT
attribute may change when the star is run. If a state has the A_SETTABLE
attribute, then user interfaces (such as pigi
) will prompt the user for values when directives such as edit-parameters are given. States without this attribute are not presented to the user; such states always start with their default values as the initial value. If no attributes are specified, the default is A_CONSTANT
|A_SETTABLE
. Thus, in the above example, the attributes
directive is unnecessary. The notation "A_CONSTANT
|A_SETTABLE
" indicates a logical "or" of two flags. Confusingly, this means that they both apply (A_CONSTANT
and A_SETTABLE
).Code generation stars use a great number of attributes, many specific to the language model for which code is being generated. Read chapter 13, "Code Generation", and the documentation for the appropriate code generation domain to learn more about these.
Mechanisms for accessing and updating states in C++ methods associated with a star are explained below, in sections 2.4.3 on page 2-21 and 2.4.4 on page 2-23.
An alternative form for the state
directive is defstate
. The subitems of the state
directive are summarized in table
state
, it contains subitems. Here is an example:
name
specifies the porthole name. This is a required item. type
specifies the particle type. The scalar types are
int
,
float
,
fix
,
complex
,
message
, or
anytype
. Again, case does not matter for the type value. The matrix types are
int_matrix_env
,
float_matrix_env
,
complex_matrix_env
, and
fix_matrix_env
. The type
item may be omitted; the default type is anytype
. For more information on all of these, please see chapter
4,
"Data Types".
The
numtokens
keyword (it may also be written num
or numTokens
) specifies the number of tokens consumed or produced on each firing of the star. This only makes sense for certain domains (SDF, DDF, and BDF); in such domains, if the item is omitted, a value of one is used. For stars where this number depends on the value of a state, it is preferable to leave out the numtokens
specification and to have the setup
method set the number of tokens (in the SDF domain and most code generation domains, this is accomplished with the
setSDFParams
method). This item is used primarily in the SDF and code generation domains, and is discussed further in the documentation of those domains. ANYTYPE
to specify a link between the types of two portholes. The syntax is
type { = name }name
is the name of another porthole. This indicates that this porthole inherits its type from the specified porthole. For example, here is a portion of the definition of the SDF Fork
star:
input {
name{input}
type{ANYTYPE}
}
outmulti {
name{output}
type{= input}
desc{ Type is inherited from the input. }
} constructor
This item allows the user to specify extra C++ code to be executed in the constructor for the class. This code will be executed after any automatically generated code in the constructor that initializes portholes, states, etc. The syntax is:
constructor { body }body
is a piece of C++ code. It can be of any length. Note that the constructor is invoked only when the class is first instantiated; actions that must be performed before every simulation run should appear in the setup
or begin
methods, not the constructor. conscalls
You may want to have data members in your star that have constructors that require arguments. These members would be added by using the public
, private
, or protected
keywords. If you have such members, the conscalls
keyword provides a mechanism for passing arguments to the constructors of those members. Simply list the names of the members followed by the list of constructor arguments for each, separated by commas if there is more than one. The syntax is:
conscalls { member1(arglist), member2(arglist) }member1
, and member2
should have been previously defined in a public
, private
, or protected
section (see
page 2-14).
destructor
This item inserts code into the destructor for the class. The syntax is:
destructor { body }begin
method, or setup
method; termination functions that happen with every run should appear in the wrapup
function1. The optional keyword inline
may appear before destructor
; if so, the destructor function definition appears inline, in the header file. Since the destructor for all stars is virtual, this is only a win when the star is used as a base for derivation.
setup
This item defines the setup
method, which is called every time the simulation is started, before any
compile-time scheduling is performed. The syntax is:
setup { body }inline
may appear before the setup
keyword. It is common for this method to set parameters of input and output portholes, and to initialize states. The code syntax for doing this is explained starting on
page 2-16. In some domains, with some targets, the setup
method may be called more than once during initiation. You must keep this in mind if you use it to allocate or initialize memory.
begin
This item defines the begin
method, which is called every time the simulation is started, but after the scheduler setup
method is called (i.e., after any
compile-time scheduling is performed). The syntax is:
begin { body } go
This item defines the action taken by the star when it is fired. The syntax is:
go { body }inline
may appear before the go
keyword. The go method will typically read input particles and write outputs, and will be invoked many times during the course of a simulation. The code syntax for the body is explained starting on
page 2-16.
wrapup
This item defines the wrapup
method, which is called at the completion of a simulation. The syntax is:
wrapup { body }inline
may appear before the wrapup
keyword. The wrapup method might typically display or store final state values. The code syntax for doing this is explained starting on
page 2-16. Note that the wrapup
method is not invoked if an error occurs during execution. Thus, the wrapup
method cannot be used reliably to free allocated memory. Instead, you should free memory from the previous run in the setup
or begin
method, prior to allocating new memory, and in the destructor.
public, protected, private
These three keywords allow the user to declare extra members for the class with the desired protection. The syntax is:
protkey { body }protkey
is public
, protected
, or private
. Example, from the XMgraph
star:
protected {
XGraph graph;
double index;
}XGraph
, defined in the Ptolemy kernel, and a double-precision number. If any of the added members require arguments for their constructors, use the conscalls
item to specify them. ccinclude, hinclude
These directives cause the .cc
file, or the .h
file, to #include
extra files. A certain number of files are automatically included, when the preprocessor can determine that they are needed, so they do not need to be explicitly specified. The syntax is:
ccinclude { inclist }
hinclude { inclist }inclist
is a comma-separated list of include files. Each filename must be surrounded either by quotation marks or by "<
" and ">
" (for system include files like <math.h>
). code
This keyword allows the user to specify a section of arbitrary C++ code. This code is inserted into the .cc
file after the include files but before everything else; it can be used to define static non-class functions, declare external variables, or anything else. The outermost pair of curly braces is stripped. The syntax is:
code { body } header
This keyword allows the user to specify an arbitrary set of definitions that will appear in the header file. Everything between the curly braces is inserted into the .h
file after the include files but before everything else. This can be used, for example, to define classes used by your star. The outermost pair of curly braces is stripped. method
The method
item provides a fully general way to specify an additional method for the class of star that is being defined. Here is an example:
name { exec }
access { protected }
arglist { "(const char* extraOpts)" }
type { void }
code {
// code for the exec method goes here
}
}method
keyword, which must be one of the following:
virtual
inline
pure
pure virtual
inline virtualvirtual
keyword makes a virtual member function. If the pure
virtual
keyword is given, a pure virtual member function is declared (there must be no code
item in this case). The function type pure
is a synonym for pure
virtual
. The inline
function type declares the function to be inline. method
subitems:
name:
access:
public
, protected
, or private
. If the item is omitted, protected
is assumed.
arglist:
type:
void
(no value is returned).
code:
pure
keyword appears, in which case this item cannot appear.
exectime
This item defines the optional myExecTime()
function, which is used in code generation to specify how many time units are required to execute the star's code. The syntax is:
exectime { body }inline
may appear before the exectime
keyword. The body
defines the body of a function that returns an integer value. codeblock
Codeblocks are parametrized blocks of code for use in code generation stars. Their use and format is discussed in detail in the code generation chapters. The syntax is:
codeblock {
code
...
}
Copyright © 1990-1997, University of California. All rights reserved.