Creating primitives derived from TclScript

A large number of useful primitives can be derived from the TclScript primitive.

For example, the TkShowValues primitive used in Example 1 is derived from the TclScript primitive. This primitive takes inputs of any type and displays their value in a window that is optionally located in the control panel. It has three settable parameters:

label

a string parameter giving a label to identify the display

put_in_control_panel

an int parameter that specifies whether the display should be put in the control panel or in its own window

wait_between_outputs

an int parameter that specifies whether the execution of the system should pause each time a new value is displayed. If it does, then a mouse click in the display restarts the system.

Conspicuously absent is the tcl_file parameter of the TclScript primitive from which this is derived. The file is hard coded into the definition of the primitive by the following C++ statement included in the begin method.

tcl_file = "$MLD/MLD_Libraries/SDF/TclTk/tkShowValues.tcl";

The parameter is hidden from the user of the primitive by the following statement included in the constructor.

tcl_file.clearAttributes(A_SETTABLE);

Thus, the user only sees the parameters that are defined in the derived primitive. This is a key part of customizing the primitive.

A second issue is that of communicating the new parameter values to the Tcl script. For example, the Tcl script will need to know the value of the label parameter in order to create the label for the display. The TclScript primitive automatically makes all the parameters of any derived primitive available as array entries in the global array whose name is given by the global variable starID.

To read the value of the label parameter in the Tcl script, use the expression [set ${starID}(label)]. The confusing syntax is required to ensure that Tcl uses the value of starID as the name of the array. The string label is just the index into the array. The set command in Tcl, when given only one argument, returns the value of the variable whose name is given by the argument.

Some programmers may prefer an alternative way to refer to parameters that is slightly more readable. The Tcl statement

upvar #0 $starID params

allows subsequent statements to refer to parameters simply as $params(param_name). The upvar command with argument #0 declares the local variable params equivalent to the global variable whose name is given by the value of starID.

Many more examples can be found in MLD Libraries→SDF Domain→Tcl/Tk.

Writing Tcl primitives for DE domain

In the discrete-event (DE) domain, primitives are fired in chronological order according to the time stamps of the new data that has arrived at their input ports. The Tcl interface class TclStarIfc, which was originally written with the SDF domain in mind, works well for some types of DE primitives. Specifically, any primitive with an input in the DE domain can use this class effectively. Consequently, a basic Tcl/Tk primitive, TclScript, has been written for the DE domain. The TclScript primitive can have any number of input or output ports.