Defining diagram behaviour using state actions and transition labels¶
State actions and transition actions are instructions that you write inside a state or transition, respectively, to define the behaviour of the diagram during simulation.
Consider a model with a finite automaton that empirically tests one variant of the Collatz hypothesis. For a given numeric input $u$, the diagram computes the Collatz sequence $n_0=u,n_1,n_2,...$ by repeating the rule:
- If $n_i$ is even, then $n_{i+1}=n_i/2$
- If $n_i$ is odd, then $n_{i+1}=3n_i+1$
The Collatz hypothesis states that for every natural number, there is a sequence of numbers that eventually reduces to one.
The diagram consists of three states. At the beginning of the simulation, the Init state initialises the diagram data by setting:
- Local data n corresponds to the input value u.
- Local data n2 is converted to a remainder when n is divided by 2.
- Output data y is set to false
Depending on the parity of the input data, the diagram switches to either the Even or Odd state. As the state activity switches between Even and Odd states, the diagram calculates the numbers in the sequence. When the number-grid sequence reaches the value 1, the y output becomes true and the simulation stops.
State actions define what the state diagram does while the state is active. The most common types of state actions are entry, during, and exit:
entry - actions occur when a state becomes active.
during - actions occur at a time step when the state is already active and the diagram does not transition out of that state.
exit - actions occur when the diagram exits this state.
You can specify the state action type using the full keyword (entry, during, exit) or abbreviation (en, du, ex). You can also combine state action types by using commas. For example, an action with the combined type entry, during occurs at the time step when the state becomes active and at each subsequent time step as long as the state remains active.
The gradient diagram contains actions in states:
Init - when this state becomes active at the start of the simulation, the entry action determines the parity of n and sets y to false. When the diagram leaves the Init state after one time step, the exit action determines whether n is equal to one.
Even - When this state becomes active, and at each subsequent time step while the state is active, the combined entry, during action calculates the value and parity for the next number in the hail sequence, n/2.
Odd - When this state becomes active, and at each subsequent time step while the state is active, the combined entry, during action checks if n is greater than one and, if so, calculates the value and parity for the next number in the sequence of hailstones, 3*n+1.
Types of transition labels¶
Transition labels define what the state diagram does when the active state changes. The most common types of transition labels are conditions and condition actions: [Condition]{Action}.
A condition is a logical expression that specifies whether a transition occurs. If you do not specify a condition, the transition will occur one time step after the initial state becomes active.
A condition action is an instruction that is executed when the condition that provides the transition is true. A condition action is executed after the condition, but before any exit or entry state actions.
A gradient diagram contains actions over transitions:
Default transition in Init - at the beginning of the simulation, the condition action $n = u$ assigns the input value u to the local data n.
Transition from Init to Even - the $n2 == 0$ condition specifies that the transition occurs when n is even. The number 1 in the source of this transition indicates that this transition is evaluated before the transition of Init to Odd.
Transition from Odd to Even - the condition $n2 == 0$ specifies that the transition occurs when n is even.
Transition from Even to Odd - the condition $n2 ~= 0$ determines that the transition occurs when n is odd. In this case, the action of the $y = isequal(n,1)$ condition determines whether n is equal to one.
Study the behaviour of the diagram¶
Let's run a simulation for a hailstone sequence starting with the value 9. To do this, use the command control to load and run the model collatz.engee
.
Out[0]:
SimulationResult(
"n" => WorkspaceArray{Float64}("collatz/n")
,
"y" => WorkspaceArray{Float64}("collatz/y")
)
Let's get the results and study the behaviour of the diagram.
State diagram

At the time $t = 0$, the default transition to the Init state occurs. The transition action sets the value of n to 9. The Init state becomes active. Actions in entry set Init value n2 to 1 and y to false.
At the moment of time $t = 1$ the condition n2 == 0 is not fulfilled, so the diagram is preparing for transition to Odd. The exit action in Init sets y to false. The Init state becomes inactive and the Odd state becomes active. The entry action in Odd sets n to 28 and n2 to 0.
At time $t = 2$ the condition n2 == 0 is true, so the diagram prepares to transition to Even. The Odd state becomes inactive and the Even state becomes active. The action of entering Even sets n to 14 and n2 to 0.
At time $t = 3$ the condition n2 ~= 0 is not fulfilled, so the diagram does not transition to another state. The Even state remains active. The action in the during set Even sets n to 7 and n2 to 1.
During $t = 4$ condition n2 ~= 0 is true, so the diagram prepares to transition to Odd. The transition action sets y to false. The Even state becomes inactive and the Odd state becomes active. The actions of entry to Odd set n to 22 and n2 to 0.
The graph continues to calculate the hail sequence until it reaches the value $n = 1$ at time $t = 19$.
While $t = 20$ is running, the graph prepares to transition from Even to Odd. The transition action sets y to true. The Even state becomes inactive and the Odd state becomes active. The entry actions in Odd do not change n or n2.
You can also run the model simulation from the modelling environment. To do this, open the model collatz.engee
and run it. You can then look at the result of the simulation in the "Graphs" window or in the "Data Inspector" application.
On the "Applications" tab in the upper right corner, go to the "Data Inspector" section.
To see the hail sequence values, select the recorded signal n in the Data Inspector.
The "hail" sequence reaches the value of 1 after 19 iterations. You can also do the experiment with other numbers.
{"id": "e3e647c3-4113-4591-8300-daf2fcd90679", "data": [{"showlegend": true, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [9, 0, 0, 9, 9, 9], "type": "scatter", "x": [-0.4, -0.4, 0.4, 0.4, -0.4, -0.4], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [28, 0, 0, 28, 28, 28], "type": "scatter", "x": [0.6, 0.6, 1.4, 1.4, 0.6, 0.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [14, 0, 0, 14, 14, 14], "type": "scatter", "x": [1.6, 1.6, 2.4, 2.4, 1.6, 1.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [7, 0, 0, 7, 7, 7], "type": "scatter", "x": [2.6, 2.6, 3.4, 3.4, 2.6, 2.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [22, 0, 0, 22, 22, 22], "type": "scatter", "x": [3.6, 3.6, 4.4, 4.4, 3.6, 3.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [11, 0, 0, 11, 11, 11], "type": "scatter", "x": [4.6, 4.6, 5.4, 5.4, 4.6, 4.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [34, 0, 0, 34, 34, 34], "type": "scatter", "x": [5.6, 5.6, 6.4, 6.4, 5.6, 5.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [17, 0, 0, 17, 17, 17], "type": "scatter", "x": [6.6, 6.6, 7.4, 7.4, 6.6, 6.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [52, 0, 0, 52, 52, 52], "type": "scatter", "x": [7.6, 7.6, 8.4, 8.4, 7.6, 7.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [26, 0, 0, 26, 26, 26], "type": "scatter", "x": [8.6, 8.6, 9.4, 9.4, 8.6, 8.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [13, 0, 0, 13, 13, 13], "type": "scatter", "x": [9.6, 9.6, 10.4, 10.4, 9.6, 9.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [40, 0, 0, 40, 40, 40], "type": "scatter", "x": [10.6, 10.6, 11.4, 11.4, 10.6, 10.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [20, 0, 0, 20, 20, 20], "type": "scatter", "x": [11.6, 11.6, 12.4, 12.4, 11.6, 11.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [10, 0, 0, 10, 10, 10], "type": "scatter", "x": [12.6, 12.6, 13.4, 13.4, 12.6, 12.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [5, 0, 0, 5, 5, 5], "type": "scatter", "x": [13.6, 13.6, 14.4, 14.4, 13.6, 13.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [16, 0, 0, 16, 16, 16], "type": "scatter", "x": [14.6, 14.6, 15.4, 15.4, 14.6, 14.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [8, 0, 0, 8, 8, 8], "type": "scatter", "x": [15.6, 15.6, 16.4, 16.4, 15.6, 15.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [4, 0, 0, 4, 4, 4], "type": "scatter", "x": [16.6, 16.6, 17.4, 17.4, 16.6, 16.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [2, 0, 0, 2, 2, 2], "type": "scatter", "x": [17.6, 17.6, 18.4, 18.4, 17.6, 17.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [1, 0, 0, 1, 1, 1], "type": "scatter", "x": [18.6, 18.6, 19.4, 19.4, 18.6, 18.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [1, 0, 0, 1, 1, 1], "type": "scatter", "x": [19.6, 19.6, 20.4, 20.4, 19.6, 19.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.513888888888889, "title": {"text": ""}, "len": 0.9525371828521435, "x": 0.9934383202099738}, "name": "y1", "yaxis": "y", "legendgroup": "y1", "marker": {"symbol": "circle", "color": "rgba(0, 154, 250, 0.000)", "line": {"color": "rgba(0, 0, 0, 0)", "width": 1}, "size": 0}, "y": [9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 1], "type": "scatter", "x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 21, "currentCount": 21}}}], "config": {"showlegend": true, "xaxis": {"showticklabels": true, "gridwidth": 0.5, "tickvals": [0, 5, 10, 15, 20], "range": [-1.6854400000000016, 21.68544], "domain": [0.03619130941965587, 0.9934383202099738], "mirror": false, "tickangle": 0, "showline": true, "ticktext": ["0", "5", "10", "15", "20"], "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "y", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "annotations": [], "height": 400, "margin": {"l": 0, "b": 20, "r": 0, "t": 20}, "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "yaxis": {"showticklabels": true, "gridwidth": 0.5, "tickvals": [0, 10, 20, 30, 40, 50], "range": [0, 52], "domain": [0.03762029746281716, 0.9901574803149606], "mirror": false, "tickangle": 0, "showline": true, "ticktext": ["0", "10", "20", "30", "40", "50"], "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "x", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "legend": {"yanchor": "auto", "xanchor": "auto", "bordercolor": "rgba(0, 0, 0, 1)", "bgcolor": "rgba(255, 255, 255, 1.000)", "borderwidth": 1, "tracegroupgap": 0, "y": 1, "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "title": {"font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}, "text": ""}, "traceorder": "normal", "x": 1}, "width": 526.390625}}