Skip to content

Simulation

During a statechart simulation full access to the C data structures is possible on all layers. The user can inspect them as well as modify them in the simulation view.

The state machine below exemplifies this. Initially it defines two rectangles a and b with certain widths and heights. The state machine calculates the rectangles' respective area size, stores their sizes in two int32_t variables named area_a and area_b, and compares them. Depending on the result, it proceeds to state A is larger or to A is smaller. Only if both a and b have the same area – not necessarily the same width and height –, the state machine proceeds to its final state.

When one of the states A is larger or A is smaller is active, the rectangles' properties can be changed. Triggering the compare_size event transitions to the Check state which repeats the area size comparison as described above.

The rectangle comparison statechart

The rectangle comparison statechart

The state machine’s definitions are as follows:

import: "rectangle.h"

interface:
    var a: Rectangle
    var b: Rectangle
    var area_a: int16_t
    var area_b: int16_t

internal:
    event compare_size

The Rectangle datatype is defined in a new header file rectangle.h with the following contents:

#include "./point.h"

typedef struct {
    Point lowerLeft;
    int16_t width, height;
} Rectangle;

In order to simulate the statechart, right-click on the statechart file in the project explorer and select Run As → Statechart Simulation from the context menu.

The statechart simulation

  • starts,
  • performs the initializing actions specified in the transition from the initial state to the Check state,
  • in the Check state, calculates the rectangles' areas and stores the results in the area_a and area_b variables,
  • transitions to the A is larger state, because its guard condition is fulfilled, and
  • stops, waiting for the compare_size event to occur.

Inspecting C data structures

Inspecting C data structures

Inspecting C data structures

The simulation view in the screenshot above is showing the state machine’s variables and their values. Click to open or close the nested data structures. The image below shows in particular

  • the rectangles' respective width and height values as have been set initially,
  • the calculated areas of the a and b rectangles,
  • the coordinates of the points defining the respective lower left corner of the rectangles.

Warning: Simple C variables and fields in C data structure are not initialized. Never try to read a variable or field you haven’t written before, because it might contain arbitrary values.

Even if the Point data structures in the example above look like having been initialized to defined values, they are not. Without going into details, in C, variables are generally not initialized. This also holds for statechart variables from the C integration. If you are reading a variable, make sure you have written to it before. Otherwise you might get surprising and non-deterministic results.

Modifying C data structures

Change a variable’s or field’s value as follows:

  1. Click on the value displayed in the simulation view.
  2. Enter the new value into the text field, see figure "Modifying C data values" where a.height is being edited and the previous value 7 is replaced by 3.
  3. Press the [Enter] key to quit editing and to write the new value to the variable or field. Giving the input focus to another window has the same effect.
  4. You can cancel editing by pressing the [Esc] key. The variable’s or field’s value remains unchanged.

Modifying C data values

Modifying C data values

In the example, click compare_size to trigger the event. The state machine transitions to the Check state, recalculates the areas, and behaves as explained above.

Rectangle areas modified and rechecked

Rectangle areas modified and rechecked