UML and SysML to C - Statemachine Code Generator Specification

This section describes the UML and SysML to C statemachine code generation.

This page discusses:

Glossary

Acronym Definition
SPO Software Production Operation
BL Behavior Layer
ML Middleware Layer
BSM Behavior Specific Middleware
TSM Target Specific Middleware

Referenced Documents

Document Internal Reference
Targeted Application Concept [1]
C Code Architecture Specification [2]
UML and SysML to C - Software Production Operation Specification [3]
UML and SysML - Component Model Generator Specification [4]

General Concepts

Statemchine code generation is part of Components generation as presented in [1] and is invoked during the execution of UML to C operation as presented in [3].

This document focuses on the C code generation for the statemachine.

Supported Elements

An exhaustive list of elements used by the code generator to produce the code with a summary of the purpose and impact is below:

Diagram Symbol UML element Description
Final State Final State Final state of a statemachine.
Orthogonal State Orthogonal State State used to execute sub states in parallel.
Pseudo State DeepHistory Pseudo State DeepHistory Restart all substates of a state containing the history when a history state is called.
Pseudo State ShallowHistory Pseudo State ShallowHistory Restart direct substates of a state containing the history when a history state is called.
Pseudo State Initial Pseudo State Initial First state of a statemachine.
Pseudo State Terminate Pseudo State Terminate Not supported.
Pseudo State Fork Pseudo State Fork Used to send a transition to multiple states.
Pseudo State Join Pseudo State Join Used to receive a transition from multiple states. All input transitions must be valid to validate the join.
Pseudo State Entry Point Pseudo State Entry Point Not supported Entry point pseudostate is an entry point of a state machine or composite state. In each region of the state machine or composite state it has at most a single transition to a vertex within the same region.
Pseudo State Exit Point Pseudo State Exit Point Not supported Exit point pseudostate is an exit point of a state machine or composite state. Entering an exit point within any region of the composite state or state machine referenced by a submachine state implies the exit of this composite state or submachine state. The triggering of the transition that has this exit point as a source in the state machine enclosing the submachine or composite state.
Pseudo State Junction Pseudo State Junction Used to receive a transition from multiple states. One input transition is required to be valid to validate the join. Used to send a transition to multiple output states based on separated conditions.
Pseudo State Choice Pseudo State Choice Intermediate state splitting a transition into multiple destinations based on different conditions.
- Region A region is a part that groups elements together. A region belongs to the statemachine object or states inside it.
State State State of a statemachine. A State can have one or multiple regions. A State can have an entry, a do, and an exit attribute configured to execute a behavior. This behavior can be an OpaqueBehavior, an ActivityDiagram or a statemachine.
- Statemachine Root element of a stateamchine.
State.DeferredTrigger State.DeferredTrigger Event initiated in a state and consumed later in the statemachine.
State.Entry State.Entry Behavior executed when a state is activated from a transition.
State.Do State.Do Behavior executed everytime a statemachine is invoked.
State.Exit State.Exit Behavior executed when a state is deactivated because a transition has been validated.
State.InternalTransitions.Transition State.InternalTransitions.Transition An internal transition is semantically equivalent to a self transition except that the State is neither exited nor reentered.
State.EntryDoExit.Activity State.<Entry/Do/Exit>.Activity Activity called from a state.
State.EntryDoExit.FunctionBehavior State.<Entry/Do/Exit>.FunctionBehavior Not supported
State.EntryDoExit.Interaction State.<Entry/Do/Exit>.Interaction Not supported
State.EntryDoExit.OpaqueBehavior State.<Entry/Do/Exit>.OpaqueBehavior OpaqueBehavior called from a state.
State.EntryDoExit.ProtocoleStateMachine State.<Entry/Do/Exit>.ProtocoleStateMachine Not supported
State.EntryDoExit.StateMachine State.<Entry/Do/Exit>.StateMachine Not supported
State.Submachine State.Submachine Sub statemachine called inside a state.
Transition Transition Oriented connection between a source state and a target state.
Transition.Effect Transition.Effect An Effect describes the behavior executed when the transition is validated.
Transition.Effect.Activity Transition.Effect.Activity Activity called from a state.
Transition.Effect.FunctionBehavior Transition.Effect.FunctionBehavior Not supported
Transition.Effect.Interaction Transition.Effect.Interaction Not supported
Transition.Effect.OpaqueBehavior Transition.Effect.OpaqueBehavior OpaqueBehavior called from a state
Transition.Effect.ProtocoleStateMachine Transition.Effect.ProtocoleStateMachine Not supported
Transition.Effect.StateMachine Transition.Effect.StateMachine Not supported
Transition.Guard Transition.Guard A guard is a condition to be fulfilled to validate a transition.
Transition.Trigger Transition.Trigger Element associating an Event to a transition to define its validation criteria.
Transition.Trigger.SignalEvent Transition.Trigger.SignalEvent Event referencing a Signal which is expected to validate a transition.
Transition.Trigger.TimeEvent Transition.Trigger.TimeEvent Event referencing a timing value to wait to validate a transition.
Transition.Trigger.AnyReceiveEvent Transition.Trigger.AnyReceiveEvent Not supported
Transition.Trigger.CallEvent Transition.Trigger.CallEvent Not supported
Transition.Trigger.ChangeEvent Transition.Trigger.ChangeEvent Event defining an expression to wait for an evaluation change to validate a transition.

Note: A statemachine is expected to be executed through a PeriodicExecution. Neither a ChangeEvent, a TimeEvent, nor a SignalEvent can be used to wakeup the statemchine. Instead during the periodic execution, the statemachine pools the events fired between execution two cycles.

Note: If a transition is triggered by a SignalEvent and the SignalEvent references a Signal but does not reference an input port; the signal is inserted in all Interface Blocks referenced by input Proxy Ports. In addition, if no input Proxy port exists, an additional input Port is created in the ESP model with the name InputSignal to receive that input Signal. See [4] for more information.

Note: If a transition is triggered by a SignalEvent and the SignalEvent references a Signal and references an input port; the input Signal is collected by reading its status on the input Port.

Questions:

Q:How to send a Signal from a statemachine?

  • A: Sending a signal is done through a Send Signal Action in an Activity Diagram.

Q: How to read a value coming from a Flow Port in a Statemachine?

  • A: A value can be read through its name in an Opaque Bevahior.

Q: How to read a FlowProperty coming from a Proxy Port in a Statemachine?

  • A: A FlowProperty can be read from a Statemachine via a local ValueProperty connected to the FlowProperty. The ValueProperty is read through its name in an Opaque Bevahior.

Q: How to write a value to a Flow Port in a Statemachine?

  • A: A value can be written through its name in an Opaque Bevahior. {#a-a-value-can-be-written-via-its-name-in-an-opaque-bevahior. .3DS-Title-3}

Q: How to write a FlowProperty to a Proxy Port in a Statemachine?

  • A: A FlowProperty can be written from a Statemachine through a local ValueProperty connected to the FlowProperty. The ValueProperty is written through its name in an Opaque Bevahior. {#a-a-flowproperty-can-be-written-from-a-statemachine-via-a-local-valueproperty-connected-to-the-flowproperty.-the-valueproperty-is-written-via-its-name-in-an-opaque-bevahior. .3DS-Title-3}

Validation Constraints

-

Code Generator Specification

This section is divided into two parts:

  • The generated files specification
  • The behavior specification

Files Specifications

This Software production Operation supports UML State Machines as described in PSSM document (see [3]).

The Statemachine code is composed of:

  • <Behavior>.c
  • <Behavior>_usr.c
  • <Behavior>.h

<Behavior>.c

<Behavior>.c file implements the statemachine structure. This structure is implemented through a function mainly containing a switch block. Each case represents a state. Transitions are manages through if conditions at end of case. Opaque behavior associated to each state or transition is implemented in the separated <Behavior>_usr.c.

File <Behavior>.c is generated for each statemachine which is either the classifier behavior or a behavior called directly or indirectly by the classifier behavior.

Name of File <Behavior>.c is described in [3].

General requirements related to a file content and structure are described in [2].

To continue for the file behavior.c content

Three basic functions are generated as follows:

  • void _basic_<Behavior>_main(STATEMACHINE *instance): This function contains the global switch managing the statemachine.
  • void _basic_<Behavior>_init(STATEMACHINE *instance): This function initializes the clock and the default state.
  • void _basic_<Behavior>_end(STATEMACHINE *instance): This function ends the statemachine.

<Behavior>_usr.c

<Behavior>_usr.c file implements the statemachine opaque behaviors. The opaque behavior of each state or transition is generated in a dedicated function.

File <Behavior>_usr.c is generated for each statemachine which is either the classifier behavior or is a behavior called directly or indirectly by the classifier behavior.

Name of File <Behavior>_usr.c is described in [3].

General requirements related to file content and structure are described in [2].

The handy helpers section describes a set of macro definitions to access local variables

#define input1  instance->input1_data

Example of handy helper

A user code section is inserted to let you declare the include files after handy helpers. See [2] for more information on user code section.

Each Opaque behavior contained in state is implemented through dedicated functions. See Chapter 9.2 for more details.

Each Opaque behavior contained in transition is implemented through dedicated functions. See Chapter 9.2 for more details.

An init function is then generated to add extra behavior during init phase.

You can customize behavior in the specific user code section. See [2] for more information on user code section.

An end function is then generated to add extra behavior during end phase.

You can customize behavior of end function in the specific user code section. See [2] for more information on user code section.

A user code section is inserted before the end of a file to let you implement additional fiunctions. See [2] for more information on user code section.

#ifdef STATEMACHINE

/**********************************************************************
 * handy helpers
 **********************************************************************/
#define input1  instance->input1_data

/* BEG_USER_CODE(header) do not remove */
/* user-modifiable header part */
/* END_USER_CODE(header) do not remove */

<OPAQUE BEHAVIOR – See chapter 9.2> 

static void StateMachine_init(AddOne_struct *instance) {
    /* BEG_USER_CODE(init_state_machine) do not remove */

    /* do whatever needed */
    (void)instance; /* MISRA rule 2.7 (usused parameter) */

    /* END_USER_CODE(init_state_machine) do not remove */
}

static void StateMachine_end(AddOne_struct *instance) {
    /* BEG_USER_CODE(end_state_machine) do not remove */

    /* do whatever needed */
    (void)instance; /* MISRA rule 2.7 (usused parameter) */

    /* END_USER_CODE(end_state_machine) do not remove */
}

/* BEG_USER_CODE(footer) do not remove */

/* user-modifiable footer part */

/* END_USER_CODE(footer) do not remove */

#endif /* STATEMACHINE */

Example: <Behavior>_usr.c file template

<Behavior>.h

<Behavior>.h file implements the statemachine opaque behaviors. The opaque behavior of each state or transition is generated in a dedicated function.

File <Behavior>.h is generated for each statemachine, which is either the classifier behavior or is a behavior called directly or indirectly by the classifier behavior.

Name of File <Behavior>.h is described in [3].

The statemachine structure contains ports values, internal variables, and an internal status. This structure is referenced by the root Class. This structure is declared in a file as follows:

typedef struct {
    /* user data */
    USERDATA_T     USERDATA;

    /* Ports */
    <list of ports current values>

    /* internal */
    STATE_T        CURRENT_STATE_0;
    WALLCLOCK_T    WALL_CLOCK;
    EVENT_T        EVENT;
    Boolean        FIRST;
    Boolean        DONE_ACTIVITY;
} <StateMachineName>_struct;

Semantic Code Specifications

This section describes the behavior of the code generator depending on the UML/SysML element.

Pseudo State Initial

The pseudo state initial is the entry point of a statemachine.

One initial state is mandatory in a statemachine. See rule STM_C_001.

More than one initial state is forbidden in a statemachine. See rule STM_C_001.

The initialization section is generated at the beginning of the step function for the initial state.

If the Initial state is connected to a state, the initialization section automatically activates the state at the beginning of the step function and calls the entry action.

Initial State connected to a State with an entry action example

    if(instance->FIRST != FALSE) {
        instance->FIRST = FALSE;
        instance->CURRENT_STATE_0 = ADDONE_STATE_T_INITIAL_0;
        /* Transition INITIAL_0 -> running */
        /* @generated_from _19_0_3_16600ad_1654593633996_246628_43116 */
        running_Entry(instance); /* Entry of ADDONE_STATE_T_running */
        instance->CURRENT_STATE_0 = ADDONE_STATE_T_running;
        instance->ENTER_CLOCK_0 = instance->WALL_CLOCK;
    }

Example: Pseudo Initial State connected to a State with an entry action

If the Initial state is connected to a pseudo state fork, the initialization section automatically activates the state at beginning of the step function.

Initial State connected to a Fork

State.<Entry/Do/Exit>.OpaqueBehavior

OPAQUE BEHAVIOR

For each state which declares an Entry/Do/Exit Opaque behavior, a function containing the related behavior is generated.

See [2] for more information on user code section.

/**
 * Function implementing "<Entry/Do/Exit>" of State:
 * <state name>
 * @generated from: <list of source elements used for code generation>
 */
static void <StateName>_<Entry/Do/Exit>(STATEMACHINE *instance) {
    /* BEG_USER_CODE(<id>) do not remove */
    … Behavior as describerd in model …
    /* END_USER_CODE(<id>) do not remove */
}

Example: Generated function for an opaque behavior

Transition.Effect.Opaque Behavior

OPAQUE BEHAVIOR

For each transition that declares an Effect containing an Opaque Behavior, a function containing the related behavior is generated.

See [2] for more information on user code section.

/**
 * Documentation 
 */
static void <TransitionName>_Effect(STATEMACHINE *instance) {
/* BEG_USER_CODE(<id>) do not remove */
    … Behavior …
    /* END_USER_CODE(<id>) do not remove */
}

Example: Generated function for an opaque behavior