gomethod. Consider an example, the
LastOfNstar discards the first N-1 particles from the
inputporthole and routes the last one to the
outputporthole . The value N is read from the
controlinput. Since the control data varies, the number of particles to read from the
inputporthole is variable, as expected for a DDF star. We can specify that the
inputporthole is dynamic by setting the
numfield of the
inputdeclaration to be 0 using the preprocessor format:
waitFormethod of the
DDFStarclass (actually, it is defined in the base class,
waitFormethod takes a porthole as an argument, and an optional integer as a second argument. It indicates that the star should fire when amount of data specified by the integer (default is 1) is available on the specified port. In the above example, the
setupmethod specifies that the star should first wait for a
controlinput. When a
controlinput arrives, the
gomethod reads the control value, and uses
waitForto specify that the star should fire next when the specified number of inputs have arrived at
input. The private member
readyToGois used to keep track of which input we are waiting for. The line
num) to be consumed.
The next example is a DDF star with a dynamic output porthole: a
DownCounterstar has a dynamic
outputporthole that will generate the down-counter sequence of integer data starting from the value read through the
inputporthole. The code in the
gomethod is self-explanatory.
It is possible, if a bit strange, for a star to alternate between SDF-like behavior and DDF-like behavior. To assert that its next firing should be under SDF rules, the star calls. The following example shows a star that uses the same input for control and data. An integer input specifies the number of particles that will be consumed on the next firing. After these particles have been consumed, the star reverts to SDF behavior to collect the next control input. In the following,
numare private integers.
clearWaitPort()in the setup method, the star begins as an SDF star. It consumes one data, stores its value in
num, and issues a
waitForcommand. This changes its behavior to DDF and specifies the number of input tokens that are required. On the next firing, it will read
numinput tokens and copy them to the output, and then it will revert to SDF behavior.