Top Up Prev Next Bottom Contents Index Search

4.6 Writing Stars That Manipulate Any Particle Type

Ptolemy allows stars to declare that inputs and outputs are of type ANYTYPE. A star
may need to do this, for example, if it simply copies its inputs without regard to type, as in the
case of a Fork star, or if it calls a generic function that is overloaded by every data type, such
as sink stars which call the print method of the type.

The following is an example of a star that operates on ANYTYPE particles:


defstar {
name {Fork}
domain {SDF}
desc { Copy input particles to each output. }
input {
name{input}
type{ANYTYPE}
}
outmulti {
name{output}
type{= input}
}
go {
MPHIter nextp(output);
PortHole* p;
while ((p = nextp++) != 0)
(*p)%0 = input%0;
}
}

Notice how in the definition of the output type, the star simply says that its output type will be the same as the input type. ptlang translates this definition into an ANYTYPE output porthole and a statement in the star constructor that reads

output.inheritTypeFrom(input); as you can see by examining the .cc file generated for SDFFork.

During galaxy setup, the Ptolemy kernel assigns actual types to ANYTYPE portholes, making use of the types of connected portholes and inheritTypeFrom connections. For example, if a Fork's input is connected to an output porthole of type INT, the Fork's input becomes type INT, and then so do its output(s) thanks to the inheritTypeFrom connection. At runtime there is no such thing as an ANYTYPE porthole; every porthole has been resolved to some specific data type, which can be obtained from the porthole using the resolvedType() method. (However, this mechanism does not distinguish among the various subclasses of Message, so if you are using Message particles you still need to check the actual type of each Message received.)

Porthole type assignment is really a fairly complex and subtle algorithm, which is discussed further in the Ptolemy Kernel Manual. The important properties for a star writer to know are these:

go {
double value = double(in%0);
...
}
but this is not strictly necessary in the current system.



Top Up Prev Next Bottom Contents Index Search

Copyright © 1990-1997, University of California. All rights reserved.