Switching between SDF and DDF behavior

It is possible, although a bit uncommon, for a primitive to alternate between SDF-like and DDF-like behavior. To assert that its next firing should be under SDF rules, the primitive calls clearWaitPort().

The following example shows a primitive 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 primitive reverts to SDF behavior to collect the next control input. In the following example, readyToGo and num are private integers.

setup
{
  clearWaitPort();
  readyToGo = FALSE;
}

go
{
  int i;
  if (!readyToGo)
  {
    // get input token from Geodesic
    input.receiveData();
    num = int(input%0);
    waitFor(input, num);
    readyToGo = TRUE;
  }
  else
  {
    for (i = 0; i < num; i++)
    {
      input.receiveData();
      output%0 << int(input%0);
      output.sendData();
    }
    readyToGo = FALSE;
    clearWaitPort();
  }
}

Because of the clearWaitPort() in the setup method, the primitive begins as an SDF primitive. It consumes one data, stores its value in num, and issues a waitFor command. This changes its behavior to DDF and specifies the number of input tokens that are required. On the next firing, it will read num input tokens and copy them to the output, and then it will revert to SDF behavior.