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 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
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
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.
Change a variable’s or field’s value as follows:
[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.
[Esc]
key. The variable’s or field’s value remains unchanged.
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