code generation from StateMaschine in c#


Is there any working example or tutorial of generating code from a state maschine diagramm in C# ?

I am getting only empty files without any error message. I am pretty sure, I do something stupid wrong but I cant guess what...

Thank you !

27 July 2017 13:16:03 behr GmbH

Add Comment

Dusan Rodina - 27 July 2017 13:43:33


There are two source code generation templates for state machines. One (called "StateMachine") is for behavior state machine diagrams, another template (called "ProtocolStateMachine") is for protocol state machine diagrams.

The type of state machine diagram can be set in the Diagram Properties dialog - right click somewhere on free space in a diagram, choose Properties from the context menu, then choose State Machine tab and in the Type group choose Behavior or Protocol type.

In a behavior state machine, you should use Transition tool for transitions and for protocol state machine, you should use Protocal Transition. If you use the context bar (under a selected element) to add new states, the correct transition type is chosen automatically according the state machine type.

The correct way how to create a new protocol state machine and generate the source code for it is:

1. Create a class in a class diagram and select it (or select an existing class) - the class should have some operations

2. Right click on the class and choose Add Nested Diagram

3. Choose State Machine diagram in New Diagram dialog and type its name

4. Right click on free space in this state machine diagram

5. Choose Properties

6. Open State Machine tab

7. Change the state machine type to Protocol

8. Add states and transitions to your diagram

9. In Ribbon, choose Process tab and click on Source Code in Generation group

10. Check your state machine diagram in the project tree in Source Code Generation dialog

11. Change language to C#

12. Change template to ProtocolStateMachine

13. Click on Generate

14. The source code for your state machine should be generated

In case of any other questions feel free to let me know.

behr GmbH 9 August 2017 19:01:21

RE: source code generation

Sorry for the late answer, but other projects were more important...

Thank you very much for your detailed answer.

Source code generation is working now, but... hm ... I expected a little bit more code.

Is there a link or literature on which your code generation is based where I can read something more?

Dusan Rodina - 9 August 2017 21:59:13

RE: RE: source code generation

No worries, I understand and I thank you for your feedback.

The State Machine template was implemented by State design pattern (

Any source code generation template can be edited in the application using Source Code Template Editor. In case you would like to adjust some outputs by yourself, it is possible. Or if you have an example of code, which you would expected or preferred as the output of the source code generation, you can send it us and we can include the improved (or a new template) to one of next version.

We constantly work on new improvements and enhancements, so if something is not good enough right now, it may be soon. If you would have any tips for improvements in any form (few words, short description, link, picture, etc.) or you find something you don't like about Software Ideas Modeler, do not hesitate let me know.

behr GmbH 13 August 2017 11:52:06

RE: RE: RE: source code generation

Thank you for your fast response !

OK, I still need some help :(

and changing the templates is a bit beyond my abilities.

1. I think, "InitialState" and "FinalState" must also be generated like any other state.

"InitialState" should be the default state ?

2. Properties/Tab_SourceCodes/<Do> : is never generated

3. Properties/Tab_State/StandardActions : are not generated

4. Properties/Tab_State/CustomActions : are not generated

5. In files "*_Context" and "*_BaseClass" the namespace is always "Model 1" (with space !), DiagrammProperties/General/Custom namespace seems not be used. Renaming default name "Model 1" to "Model1" has also no effect, namespace is still generated as "Model 1"

6. I need some small example how to use the generated classes, I am a newbie in C#. I would think 2 or 3 lines should be enough, or ?

7 I tried to generate C++ and files (to compare and understand), but with all settings I could think off I only got empty classes... or nothing.



Dusan Rodina - 18 August 2017 23:50:11

RE: RE: RE: RE: source code generation

Now I need few days for the answer.

1. FinalState will be included to generation in the next version. InitialState will be not generated as it is only a PseudoState according to UML standard - it should specify the "real" state where we want to start.

2. Generation of Do action will be added in the next version.

3. Standard Actions will be added to generated code as comments in the next version. The code of these actions need to be specified in the Source Code tab.

4. It could be added in the next version or in one of next versions.

5. The name is generated by the folder name. After renaming the folder, the name in the generated code should change too. Changing the custom namespace of diagram should also work. In my test project it works, if it does not work for you, it would need further examination of the issue. I could also provide you an example project.

The default name of folders was changed in the current version to not contain a space by default to prevent the problems with naming in the generated source codes.

6. If we have two state Draft and Open, initial state leads to Draft and we have also a context class StateMachine with operations Open and WriteState, we can write the following code:

StateMachine sm = new StateMachine();

sm.WriteState(); // It writes the current state to the console, now it returns Draft


sm.WriteState(); // It writes the current state to the console, now it returns Open

I can prepare also a complete tutorial on this topic also with an example project.

7. Templates for state machine are not yet available for VB.NET and C++, only for C#.

I hope I helped you a bit. In case of any other questions feel free to let me know.