|
|
|
3 Uses of Expressions
3.1 Parameters
The values of most parameters of actors can be given as expressions
. The variables in the expression refer to other parameters that are
in scope, which are those contained by the same container or some
container above in the hierarchy. They can also reference variables in
a
scope-extending attribute, which includes variables defining units, as explained below in section 12. Adding parameters to actors is straightforward, as explained in the previous chapter.
3.2 Port Parameters
It is possible to define a parameter that is also a port. Such a
PortParameter provides a default value, which is
specified like the value of any other parameter. When the
corresponding port receives data, however, the default value is
overridden with the value provided at the port. Thus, this object
functions like a parameter and a port. The current value of the
PortParameter is accessed like that of any other parameter. Its
current value will be either the default or the value most recently
received on the port.
A PortParameter might be contained by an atomic actor or a composite
actor. To put one in a composite actor, drag it into a model from the
utilities library, as shown in figure
2. The resulting icon is actually a combination of two
icons, one representing the port, and the other representing the
parameter. These can be moved separately, but doing so might create
confusion, so we recommend selecting both by clicking and dragging
over the pair and moving both together.
Figure 2: A portParameter is both a port and a parameter. To
use it in a composite actor, drag it into the actor, change its
name to something meaningful and set its default value.
To be useful, a PortParameter has to be given a name (the default
name, "portParameter," is not very compelling). To change the name,
right click on the icon and select "Customize Name," as shown in
figure 2. In the figure, the name is set to
"noiseLevel." Then set the default value by either double clicking
or selecting "Configure." In the figure, the default value is set to
10.0.
An example of a library actor that uses a PortParameter is the
Sinewave actor, which is found in the sources
library in Vergil. It is shown in figure 3. If you
double click on this actor, you can set the default values for
frequency and phase. But both of these values can also be set by the
corresponding ports, which are shown with grey fill.
Figure 3: Sinewave actor, showing its port parameters, and their use at
the lower level of hierarchy
3.3 String Parameters
Some parameters have values that are always
strings of characters. Such parameters support a simple string
substitution mechanism where the value of the string can reference
other parameters in scope by name using the syntax $name, where name
is the name of the parameter in scope. For example, the StringCompare
actor in figure 4 has as the value of firstString"The
answer is $PI". This references the built-in constant PI. The value
of secondString is "The answer is 3.1415926535898". As shown in the
figure, these two strings are deemed to be equal because $PI is
replaced with the value of PI.
Figure 4: String parameters are indicated in the parameter editor
boxes by a light blue background. A string parameter can include
references to variables in scope with $name, where
name is the name of the variable. In this example, the
built-in constant $PI is referenced by name in the first parameter.
3.4 Expression Actor
Expression actor is a particularly useful actor found in the
math library. By default, it has one output and no inputs, as shown in Figure 53.5(a). The first step in using it is to add ports, as shown in (b) and (c), resulting in a new icon as shown in (d). Note: In (c) when you click on Add, you will be prompted for a Name (pick one) and a Class. Leave the Class entry blank and click OK. You then specify an expression using the port names, as shown in (e), resulting in the icon shown in (f).
Figure 5: Illustration of the Expression actor.
3.5 State Machines
Expressions give the guards for state transitions, as well as the
values used in actions that produce outputs and actions that set
values of parameters in the refinements of destination states. This
mechanism was explained in the previous chapter.