Component Guide 4: CodeChips

From Nova Documentation
Jump to: navigation, search

Codechip overview

Codechip prodquo: a) Program Code Dialog
Codechip prodquo: b) Chip with pins
Codechip average Program Code Dialog

A Codechip is added to Nova’s design canvas like any other component; Unlike other components, a new Codechip is a blank slate. It has no built-in content. Any benefit must come from the programming that it includes.

Right-clicking on a Codechip produces a menu with two options: Program Code and Inputs/Outputs. If you select the former you will open a dialog box that closely resembles the Set Property dialog of other components. The Program Code dialog includes 3 panes not present elsewhere: Inputs, Outputs and fields. Let us consider only the former two for now. The basic mission of a simple Codechip is to compute 1 or more output values given a set of input values. In these two panes you respectively list the names of input and output variables; these can be any legal variables and are only meaningful in the context of the Codechip.

On the left is the Program Code dialog for a simple Codechip called prodquo that accepts 2 numbers and outputs their product and quotient. Note that the inputs and outputs are separated by spaces (not commas). When you close this dialog the Codechip resembles the chip on the right. The 4 pins correspond to the 2 inputs and 2 outputs and are readily connected using the Inputs/Outputs dialog, as you would connect an Capsule chip.

When you open the Codechip pallet notice that prodquo is listed. Dragging from this list to the canvas will produce a second instance of prodquo. (The components use sequence numbers to distinguish Codechip instances). Editing either of these will cause the changes to appear in both – they are two instances of the same function. This type of Codechip, called a functional Codechip, is the most common and easiest to write. The code window many contain any legal NovaScript code. Important: make sure you assign results to the output variables.

The next image shows a typical application of Codechips: computing the average of a list of numbers. The input numList must be an array consisting of numbers only. This Codechip could be used in several places in a given model, and exported for reuse in other models. In addition to the functional Codechip form, there are two others: object Codechips and function-builders. These will be documented soon.

Return functions

An extremely useful method of utilizing the flexibility and power of a CodeChip comes form being able to return functions. The following is an example from the Cascade Example of returning a function.

Return a function from a CodeChip