Sinelabore

Generate production quality code from state diagrams created with Enterprise Architect#

SinelaboreRT generates readable and maintainable code from hierarchical UML state machines created with Enterprise Architect. With its C/C++ backend it is well suited for embedded real-time and low-power applications. With support for code generation in additional widely used programming languages, it is also well suited for backend and IoT software.

When using Enterprise Architect, there are a number of tool specific issues that need to be considered. These are discussed in the following section. Let us first look at how a rather complex state machine looks in Enterprise Architect.

A rather complex state chart designed in Enterprise Architect. It shows regions, sub-machines and several pseudo-states like a choice state.

A rather complex state chart designed in Enterprise Architect. It shows regions, sub-machines and several pseudo-states like a choice state.

Organizing your project#

Typically you have organized your project into packages. These packages contains classes which again can contain a state machine. The code-generator needs to know how to find your state-based class in the exported XMI diagram. The path to your class in your project tree must be specified on the command line using the -t flag. If you want to create several state diagrams it is necessary to put them into separate packages!

The figure below shows the project browser window for the state diagram from above. In this project not just the complex_class is present but also two other classes Class1 and Class2. From the EA Project Browser window you can directly derive the path to the state machine which is -t "Model:Class Model:complex_class" in this case. The different parts of the path must be separated by colons.

Enterprise Architect’s project browser. From the used structure the path can be directly derived.

Enterprise Architect’s project browser. From the used structure the path can be directly derived.

Exporting your project to XMI#

The main purpose of XMI is to enable the exchange of models between modelling tools from different vendors. Therefore, most UML modelling tools support the import and export of XMI files. The exported XMI file is the basis for the code generator. During the development of the XMI standard, several versions have been created. Use the latest available version of UML/XMI. At the time of writing, it is version version 2.5.1.

The code-generator expects your class at a certain hierarchy in the model (model, class-model, class). EA always exports from the selected node – e.g. the class model – downwards. Then the code-generator is not able to find the right class. Therefore always select the top level node in the project browser before opening the publish dialog. E.g. Model in the example above.

State Details#

Enterprise Architect allows you to define entry, do and exit actions of a state. There are currently four options to specify the entry/do/exit action code.

  1. Simply select the state and use the Features/Behaviour field to directly type in the action. See the figure below as example.

    Action code specified in the name field of the action function. This is ok if the action code fits in one line.

Action code specified in the name field of the action function. This is ok if the action code fits in one line.

  1. It is also possible to specify code for the entry/exit/do action code using the Behaviour text field. See the figure below as example. This makes it easier to specify longer action codes. If the code is found in the behaviour property field, the text in the action name field is no longer used. If you have specified more than one entry/exit/do action, the code from the different entry/exit/do behaviour fields will be merged. Note that you must always enter some text in the action name field. As example type in a short description of what the action code does, as shown in the diagram. Note: For some reason there is no syntax highlighting for this field.

    A state with entry/exit/do code specified following option b) in the state function’s behaviour field.

A state with entry/exit/do code specified following option b) in the state function’s behaviour field.

  1. It is also possible to specify code for the entry/exit/do action code using the Code text field. This entry field has syntax highlighting. If code is available there all other fields from above are ignored. Please note that you always have to put in some text in the operation name field. As example type in a short description of what the action code does, as shown in the diagram. Note: The code field is not in the UML part of the XMI export but in the EA specific part.

    A state with entry/exit/do code specified following option b) in the state function’s behaviour field.

A state with entry/exit/do code specified following option b) in the state function’s behaviour field.

  1. You can use a comment and specify entry/do/exit action code as plain text. The comment text must follow the syntax as described in States. The comment must be linked to the appropriate state. To edit the text double click on the comment and enter the definitions as needed. You can attach such comment to a normal state, a child state of a composite state or a sub-state machine state. Make sure to use plain text (i.e. no special font or color etc.) the code-generator is not able to extract the text correctly otherwise. Use this option if you want to specify inner events in a state. There seems to be no other way to specify such events in EA.

    A state with entry/exit/do code specified following option a). And an inner-event following option c)

A state with entry/exit/do code specified following option a). And an inner-event following option c)

Regions#

A region is an orthogonal part of a state. It allows to express parallelism within a state. A state can have two or more regions. Region contains states and transitions. To add a region in EA

  1. Right click to a state and select Advanced

  2. Then select Define Concurrent Substates.

  3. Then add as much concurrent regions as you need

Transitions#

Double-clicking on a transition brings up the transition properties dialog (see (a) in the figure below). This dialog allows you to define the event that triggers the state transition. You can specify a guard (i.e. a statement that evaluates to true or false), an effect of the trigger and the trigger itself. The trigger type must be set to type Signal. EA enforces the event type specification. This type is not needed by the code-generator and it is recommended to use only one type for all the triggers. To re-use an existing trigger text use the selection field (three dots). Otherwise a new event is created every time even if you use the same trigger text.

A transition can have more than one trigger. This is the same as two transitions with one trigger each. But can lead to clearer diagrams in case of many transitions between two states sharing the same action code.

The code-generator expects triggers on class level in your model. Move the triggers to that level in the Project Browser. See (b) in the figure below for an example.

EA9 transition properties dialog. The dialog of older versions of EA looks a bit different but offered basically the same fields.

(a) EA9 transition properties dialog. The dialog of older versions of EA looks a bit different but offered basically the same fields.

Triggers must be stored directly under the class. Otherwise the code-generator does not find them. Move them below the class in the Model Browser as shown in this figure.

(b) Triggers must be stored directly under the class. Otherwise the code-generator does not find them. Move them below the class in the Model Browser as shown in this figure.

Relevant aspects when using transitions.

History State#

Use the Shallow History pseudo-state if you want to make a state a history state. Place the pseudo-state in the state that should have history.

Deep History#

Use the Deep History pseudo state if you want to make all child states history states. Place the pseudo-state in the state that should have history. Using a Deep History pseudo state has the same effect as adding a Shallow History marker to all composite child states.

Choices#

With EA 7.5 it seems to be not possible anymore to specify transitions without a trigger name (which is in general ok). But for transitions starting from a choice state only guards should be specified (no trigger!). To ensure a clear design only type in one or more spaces as trigger name. The generator detects this and ignores the trigger name. It is recommended to use the same “empty” event for all the transitions starting from choices.

Constraints#

The code-generator can automatically derive testcases from the EA state-machine model. See Model-based testing for more details.

Adding Operations and Attributes to Classes#

It is possible to add attributes and operations to the class containing the state machine. Clicking on the class shows the class features where attributes and operations can be easily added. For operations it is also possible to add code in the behaviour field (not the code field). Attributes are added to the instance data and always require a default value. Operations are mapped to C-functions and added to the header and implementation files of the state machine (only if an implementation is given)

The figure below shows an attribute and operation added to a class. For operations and attributes it is recommended to use own data types defined as primitive types. Create them in the same package where the class is in. If you use standard EA data types there is an automatic mapping of EA types to own data types provided in the configuration file.

Adding a new attribute to a class with a state machine. Use own data types defined as primitive types in the same package where the class is in.

Adding a new attribute to a class with a state machine. Use own data types defined as primitive types in the same package where the class is in.

Sub-Machine States#

Sub-machine states allow to “hide” the children states of a parent state with hierarchy. You only see the content of the sub-machine state if you double click on the state which opens the internal view. From the code generator point of view a sub-machine is a normal state with children. There is absolutely no difference compared to a normal hierarchical state.

To create a sub-machine do the following:

  1. Place a normal state

  2. Right click on the state and select New Diagram

  3. Select Composite Structure Diagram. Now an ∞ symbol indicates the transformation.

  4. Double click to the state and you are able to place children into the sub-machine.

Note: Do not mix up a sub-machine with a state-machine. In EA both have the same symbol i.e. a state with ∞ sign on the lower right. But a sub-machine is a normal state transformed into a sub-machine. Placing state-machines in another state is possible in EA but has no meaning and is not supported by the code generator!

Using a sub-machine state instead of a state has some other consequences:

  1. It is not possible to connect a transition starting from a state outside the sub-machine to a state inside the sub-machine. Or vice versa. Use entry and exit pseudo states for this purpose (see next section).

  2. EA does not allow to “transform” a sub-machine state into a state with children. You have to do this manually by moving states etc. in the Model Browser.

The following figure shows two state machines which produce exactly the same code – one using a sub-machine, one using a normal hierarchical design.

Machine m1 using nested states

(a) Machine m1 using nested states

Machine m2 using sub-states

(b) Machine m2 using sub-states

This figure shows two state machines producing exactly the same code but one is using a sub-machine state (internals set visible) and one uses a normal hierarchical design. I.e. m1 ⇔ m2.

In principle sub-machines can be located in normal states (or states) or regions.

Entry and Exit Points#

Note: Entry and exit points are only useful together with a sub-machine. Read the previous section before if you are not familiar with the usage of sub-machines.

When entering a sub-machine usually the initial state is entered. I.e. the transition ends at the border of the sub-machine state. If this should not be the case for a specific transition it is possible to place an entry point inside the sub-machine state. This entry point serves as glue between the sub-machine state and the internal of the sub-machine.

Exit states provide a similar function. By default only transitions can be modelled starting from the sub-machine. If a transition should start from a specific state inside the sub-machine and enter a state outside the sub-machine an exit state can be used. Again this exit state serves as glue between the transition starting inside the sub-machine and ending one level up at another state.

To use entry and exit points in your model use the following recipe:

  1. Drag and drop an entry and/or exit point in a sub-machine state

  2. Give the points a meaningful name.

  3. Double click on the sub-machine state to open the sub-machine diagram

  4. Place references of the points here. Do not create new entry/exit points with the same name!

  5. Connect transitions

Alternatively (since version 12 of EA) follow the recipe below. In this case the entry and exit points are displayed on the border of the state with sub-machine.

  1. Select the sub-machine state that should have an entry or exit point

  2. Right click on the sub-machine state to bring up the context menu and then select ’Add’.

  3. Add either an entry or exit point. See the figure below.

    Top level state diagram with sub-machine

(a) Top level state diagram with sub-machine

Sub-machine state

(b) Sub-machine state

Context Diagram

(c) Context Diagram

Create an entry or exit point using the context menu of a sub-machine state.

  1. Inside the sub-machine state drag and drop the existing entry/exit point into the diagram (as link). Do not create new entry/exit points with the same name here!

  2. Connect transitions

Limitations when using entry and exit points:

  • An exit point can have more than one incoming transitions inside the sub-machine. But only one outgoing transition from the sub-machine state.
  • An entry state can have more than one incoming transitions on the sub-machine state. But only one outgoing transition inside the sub-machine.
  • The transition leaving the exit or entry point must end in a normal state. Chaining of entry / exit states or connecting these transitions to choices or junctions is not allowed.

The figure below shows an example for a diagram with a sub-machine and the internals of this sub-machine.

Top level diagram with the sub-machine state S2

(a) Top level diagram with the sub-machine state S2

Internals of the sub-machine state S2

(b) Internals of the sub-machine state S2

This figure shows the top level diagram with a sub-machine state and the internals of the sub-machine state. The transitions between these two diagrams are glued together using entry and exit points.

Supported / Unsupported#

The code-generator supports a subset of the design elements provided by Enterprise Architect. The supported elements are:

  • Hierarchical states
  • (Signal-)Events with event name, guard and action
  • Initial and final pseudo-states
  • History states (deep, flat), Extended history handling (see History States in the manual (PDF))
  • Choices
  • Junctions
  • Constraints
  • Sub-machine states on various levels
  • Entry and exit pseudo-states
  • Regions on various levels

The unsupported elements are:

  • Syncstates
  • Terminate and Fork/Join

Example: a microwave oven#

Using Enterprise Architect#

The following example shows how the code generator workflow works with Enterprise Architect using a simple microwave oven.

Microwave oven with requirements

Requirements#

  1. Cooking time can be adjusted from 0 s to 60 s.
  2. Cooking starts if the cooking time is > 0s and the door is closed.
  3. If the door is opened during cooking, the microwave generator is switched off and the cooking time stops.
  4. Cooking continues if the cooking time is not over and the door is closed again.
  5. Cooking stops if the cooking time is over or the time is adjusted to zero.
  6. Cooking time and power can be changed at any time.
  7. The state machine below shows a possible solution.

The basis is a class model that contains the oven class with the state machine. The class can contain attributes and methods which are added to the generated code.

Adding attributes and operations in Enterprise Architect

The key point is that the state machine for the microwave oven looks as follows:

State machine of a simple microwave oven

To generate C code, export your model to XMI and call the code generator as follows. With -t you can specify which class to generate code from. The path must match the path to the class in your model.

java -jar codegen.jar -p EA -t "Model:View:testclass" -o oven oven.xml

After model validation (for example duplicate-state checks), full state machine code is generated.

The following generated files provide a concrete example of the resulting code structure and API.

oven.c
/*
 * (c) Sinelabore Software Tools GmbH, 2026
 *
 * All rights reserved. Reproduction, modification,
 * use or disclosure to third parties without express
 * authority is forbidden.
 */

/* Command line options: -p EA -t Model:Class Model:first_example_step3 -o oven first_example_step3.xml   */
/* This file is generated from first_example_step3.xml - do not edit manually  */
/* Generated on: Wed Oct 05 20:37:16 CEST 2011 / version 2.5 */

#include "mydefs.h"
#include "oven.h"
#include "oven_ext.h"
#include "oven_hlp.h"
#include <stdio.h>

extern unsigned char msg;
extern T_PWR pwr;

void  oven(OVEN_INSTANCEDATA_T *instanceVar){

	OVEN_EV_CONSUMED_FLAG_T evConsumed = 0U;

	
	/*execute entry code of default state once to init machine */
	if(instanceVar->superEntry==1U){
		ovenOff();

		instanceVar->superEntry=0U;
	}

	/* action code */
	/* just a comment */

	switch (instanceVar->stateVar) {

		case Super:

			switch (instanceVar->stateVarSuper) {

				case Completed:
					if(msg==(OVEN_EVENT_T)evDoorOpen){
						/* Transition from Completed to Idle */
						evConsumed=1U;

						/* OnEntry code of state Idle */
						ovenOff();

						/* adjust state variables  */
						instanceVar->stateVarSuper = Idle;
					}else{
						/* Intentionally left blank */
					} /*end of event selection */
				break; /* end of case Completed  */

				case Cooking:
					if(msg==(OVEN_EVENT_T)evDoorOpen){
						/* Transition from Cooking to CookingPause */
						evConsumed=1U;

						/* Action code for transition  */
						ovenOff();
						timer_pause();

						/* adjust state variables  */
						instanceVar->stateVarSuper = CookingPause;
					}else if(msg==(OVEN_EVENT_T)evTimeout){
						/* Transition from Cooking to Completed */
						evConsumed=1U;

						/* Action code for transition  */
						ovenOff();
						timer_clear();

						/* adjust state variables  */
						instanceVar->stateVarSuper = Completed;
					}else{
						/* Intentionally left blank */
					} /*end of event selection */
				break; /* end of case Cooking  */

				case CookingPause:
					if(msg==(OVEN_EVENT_T)evDoorClosed){
						/* Transition from CookingPause to Cooking */
						evConsumed=1U;

						/* Action code for transition  */
						timer_cont();

						/* OnEntry code of state Cooking */
						ovenOn();

						/* adjust state variables  */
						instanceVar->stateVarSuper = Cooking;
					}else{
						/* Intentionally left blank */
					} /*end of event selection */
				break; /* end of case CookingPause  */

				case Idle:
					if(msg==(OVEN_EVENT_T)evDoorClosed){
						if(timer_preset()>0){
							/* Transition from Idle to Cooking */
							evConsumed=1U;

							/* Action code for transition  */
							timer_start();

							/* OnEntry code of state Cooking */
							ovenOn();

							/* adjust state variables  */
							instanceVar->stateVarSuper = Cooking;
						}else{
							/* Intentionally left blank */
						} /*end of event selection */
					}else{
						/* Intentionally left blank */
					} /*end of event selection */
				break; /* end of case Idle  */

				default:
					/* Intentionally left blank */
				break;
			} /* end switch Super */

		/* Check if event was already processed  */
		if(evConsumed==0U){

			if(msg==(OVEN_EVENT_T)evDec){
				/* Transition from Super to Super */
				evConsumed=1U;
				
				if(instanceVar->stateVarSuper==Completed){
					

				}else if(instanceVar->stateVarSuper==Cooking){
					

				}else if(instanceVar->stateVarSuper==CookingPause){
					

				}else if(instanceVar->stateVarSuper==Idle){
					
				}

				/* Action code for transition  */
				timer_dec();

				instanceVar->stateVar = Super;/* entry chain  */
				if(instanceVar->stateVarSuper==Completed){
					
				}else if(instanceVar->stateVarSuper==Cooking){
					ovenOn();

				}else if(instanceVar->stateVarSuper==CookingPause){
					
				}else if(instanceVar->stateVarSuper==Idle){
					ovenOff();

				}

			}else if(msg==(OVEN_EVENT_T)evInc){
				/* Transition from Super to Super */
				evConsumed=1U;
				
				if(instanceVar->stateVarSuper==Completed){
					

				}else if(instanceVar->stateVarSuper==Cooking){
					

				}else if(instanceVar->stateVarSuper==CookingPause){
					

				}else if(instanceVar->stateVarSuper==Idle){
					
				}

				/* Action code for transition  */
				timer_inc();

				instanceVar->stateVar = Super;/* entry chain  */
				if(instanceVar->stateVarSuper==Completed){
					
				}else if(instanceVar->stateVarSuper==Cooking){
					ovenOn();

				}else if(instanceVar->stateVarSuper==CookingPause){
					
				}else if(instanceVar->stateVarSuper==Idle){
					ovenOff();

				}

			}else if(msg==(OVEN_EVENT_T)evPwr){
				/* Transition from Super to Super */
				evConsumed=1U;
				
				if(instanceVar->stateVarSuper==Completed){
					

				}else if(instanceVar->stateVarSuper==Cooking){
					

				}else if(instanceVar->stateVarSuper==CookingPause){
					

				}else if(instanceVar->stateVarSuper==Idle){
					
				}

				/* Action code for transition  */
				ovenSetPwr(pwr);

				instanceVar->stateVar = Super;/* entry chain  */
				if(instanceVar->stateVarSuper==Completed){
					
				}else if(instanceVar->stateVarSuper==Cooking){
					ovenOn();

				}else if(instanceVar->stateVarSuper==CookingPause){
					
				}else if(instanceVar->stateVarSuper==Idle){
					ovenOff();

				}

			}else{
				/* Intentionally left blank */
			} /*end of event selection */
		}
		break; /* end of case Super  */

		default:
			/* Intentionally left blank */
		break;
	} /* end switch stateVar_root */
}

/* Implementation of the reset machine function */
void ovenResetMachine(OVEN_INSTANCEDATA_T *instanceVar){
	instanceVar->superEntry = 1U;
	instanceVar->stateVar = Super;
	instanceVar->stateVarSuper = Idle;

}
// Helper(s) to reset history
void ovenResetHistorySuper(OVEN_INSTANCEDATA_T *instanceVar){
	instanceVar->stateVarSuper=Idle;
}

// Helper(s) to find out if the machine is in a certain state

uint8_t ovenIsInSuper(OVEN_INSTANCEDATA_T *instanceVar){
	return(((instanceVar->stateVar== Super)) ? (1U) : (0U));
}

uint8_t ovenIsInIdle(OVEN_INSTANCEDATA_T *instanceVar){
	return(((instanceVar->stateVarSuper== Idle)&&(instanceVar->stateVar== Super)) ? (1U) : (0U));
}

uint8_t ovenIsInCompleted(OVEN_INSTANCEDATA_T *instanceVar){
	return(((instanceVar->stateVarSuper== Completed)&&(instanceVar->stateVar== Super)) ? (1U) : (0U));
}

uint8_t ovenIsInCookingPause(OVEN_INSTANCEDATA_T *instanceVar){
	return(((instanceVar->stateVarSuper== CookingPause)&&(instanceVar->stateVar== Super)) ? (1U) : (0U));
}

uint8_t ovenIsInCooking(OVEN_INSTANCEDATA_T *instanceVar){
	return(((instanceVar->stateVarSuper== Cooking)&&(instanceVar->stateVar== Super)) ? (1U) : (0U));
}
oven.h
/*
 * (c) Sinelabore Software Tools GmbH, 2026
 *
 * All rights reserved. Reproduction, modification,
 * use or disclosure to third parties without express
 * authority is forbidden.
 */

/* Command line options: -p EA -t Model:Class Model:first_example_step3 -o oven first_example_step3.xml   */
/* This file is generated from oven.h - do not edit manually*/
/* Generated on: Wed Oct 05 20:37:16 CEST 2011 / version 2.5 */

#ifndef __OVEN_H__
#define __OVEN_H__

/* Header to define simple data types */
#include <stdint.h>

/* State definitions */
typedef enum{
	Super,
	Idle,
	Completed,
	CookingPause,
	Cooking,
	__OVEN_LAST_STATE__} OVEN_STATES_T;

/* Predefined types required by the codegen. You can provide your own  */
/* definitions by setting the following define somewhere in your built env. */
#ifndef __PROVIDE_OWN_OVEN_STATEMACHINE_TYPES__
	typedef uint8_t OVEN_ENTRY_FLAG_T;
	typedef OVEN_STATES_T OVEN_STATEVAR_T;
	typedef uint8_t OVEN_INST_ID_T;
	typedef uint8_t OVEN_EV_CONSUMED_FLAG_T;
#endif

/* Instance data definition of state machine */
typedef struct{
	OVEN_ENTRY_FLAG_T superEntry;
	OVEN_STATEVAR_T stateVar;
	OVEN_STATEVAR_T stateVarSuper;
	OVEN_INST_ID_T inst_id;
} OVEN_INSTANCEDATA_T;

/* Helper to initialize the machine's instance data */
#define OVEN_INSTANCEDATA_INIT {\
	1,\
	(OVEN_STATEVAR_T) Super /* set init state of top state */,\
	(OVEN_STATEVAR_T) Idle /* set init state of Super */,\
	0 /* instance id */ } 

/* Helper(s) to reset history */
#define OVEN_RESET_HISTORY_SUPER(instance) ((&instance)->stateVarSuper=Idle)

/*Prototype of the state machine function */
void  oven(OVEN_INSTANCEDATA_T *instanceVar);

/*Prototoye of the reset machine function */
void ovenResetMachine(OVEN_INSTANCEDATA_T *instanceVar);

// Helper(s) to reset history
void ovenResetHistorySuper(OVEN_INSTANCEDATA_T *instanceVar);

// Helper(s) to find out if the machine is in a certain state
uint8_t ovenIsInSuper(OVEN_INSTANCEDATA_T *instanceVar);
uint8_t ovenIsInIdle(OVEN_INSTANCEDATA_T *instanceVar);
uint8_t ovenIsInCompleted(OVEN_INSTANCEDATA_T *instanceVar);
uint8_t ovenIsInCookingPause(OVEN_INSTANCEDATA_T *instanceVar);
uint8_t ovenIsInCooking(OVEN_INSTANCEDATA_T *instanceVar);

/* Macros to test if the machine is in a certain state. */
#define OVEN_IS_IN_SUPER(instance)((((&instance)->stateVar==Super)) ? (1U) : (0U))
#define OVEN_IS_IN_IDLE(instance)((((&instance)->stateVarSuper==Idle)&&((&instance)->stateVar==Super)) ? (1U) : (0U))
#define OVEN_IS_IN_COMPLETED(instance)((((&instance)->stateVarSuper==Completed)&&((&instance)->stateVar==Super)) ? (1U) : (0U))
#define OVEN_IS_IN_COOKINGPAUSE(instance)((((&instance)->stateVarSuper==CookingPause)&&((&instance)->stateVar==Super)) ? (1U) : (0U))
#define OVEN_IS_IN_COOKING(instance)((((&instance)->stateVarSuper==Cooking)&&((&instance)->stateVar==Super)) ? (1U) : (0U))

#endif
oven_ext.h
/*
 * (c) Sinelabore Software Tools GmbH, 2026
 *
 * All rights reserved. Reproduction, modification,
 * use or disclosure to third parties without express
 * authority is forbidden.
 */

/* Command line options: -p EA -t Model:Class Model:first_example_step3 -o oven first_example_step3.xml   */
/* This file is generated from first_example_step3.xml - do not edit manually */
/* Generated on: Wed Oct 05 20:37:16 CEST 2011 */

#ifndef __OVEN_EXT_H__ 
#define __OVEN_EXT_H__ 

/*Events which can be sent to the state-machine  */
typedef enum
{
	evDoorOpen=0U,
	evDec,
	evTimeout,
	evPwr,
	evDoorClosed,
	evInc,
	OVEN_NO_MSG
} OVEN_EVENT_T;

#endif

More Information#