SinelaboreRT Header Logo


As simple as possible, but not any simpler!

User Tools

Site Tools


Generating C# code

To generate C# code call the code generator with the new command line flag -l csharp. All states are created into one source file. The file name is determined by the -o command line switch.

The generated code does not follow the state pattern as you might expect (if you are fa- miliar with common design patterns). The reason is that the machine code is completely generated and no hand-coding is involved. So the number of classes and files in your project is reduced to a minimum. The following figure shows the structure of the generated code. The classes marked with <generated> are generated from the code generator. Classes marked with <optional> are optional and must be provided by yourself if needed.

Figure: Structure of the generated C# code.

Microwave Oven Example in C#

A microwave oven is taken from the examples folder of the code generator to demonstrate the generator features. The example realizes a simple microwave oven and can be compiled with Microsoft Visual Studio1) on Windows2). The next figure shows the state diagram of the oven. It was created with the Cadifra UML editor.

A simple hardware abstraction layer provides helper functions like a timer etc. You can see how it is called from the state machine (e.g. in an entry action) as hal.ovenOff().

You have the option to either generate an own state class per state or inline all the entry/do/exit action code directly into the generated state machine code. As there is no need for separate state classes (e.g. complex and long actions) they were inlined in this example. The generation process can be controlled with a simple configuration file as shown below. You can also see that we provide an own base class for the Oven class to implement some special functions directly usable from the state machine.


To generate code from the diagram call the generator as follows:

java -Djava.ext.dirs=../bin/ -jar  "../bin/codegen.jar" -p CADIFRA -l csharp -o Oven oven.cdd

The generated code looks as follows:

namespace OvenMachineNS
  public class Oven : OvenBase
    public enum States : int{
    public enum Events : int {
    public int ProcessEvent(Events msg){
      int evConsumed = 0;
      if(m_initialized==false) return 0;
      /* action code */
      /* just a comment */
        switch (stateVar) {
          case States.Super:
            switch (stateVarSuper) {
              case States.Idle:
                    /* Transition from Idle to Cooking */
                    /* Action code for transition  */
                    /* OnEntry code of state Cooking */
                    /* adjust state variables  */
                    stateVarSuper = States.Cooking;
                    /* Intentionally left blank */
                  } /*end of event selection */
                  /* Intentionally left blank */
                } /*end of event selection */
              break; /* end of case Idle  */
              case States.Cooking:
          break; /* end of case Super  */
            /* Intentionally left blank */
        } /* end switch stateVar_root */
    return evConsumed;
  } // end of namespace

Finally start the generated exe file and play with the microwave oven. First increment the cooking time. Then start cooking by closing the door.

The sinelabore code generator provides much more features than just code generations. Before generation an extensive check is performed to ensure your diagram is correct (e.g. each transition has an associated event, all state names are unique …). You can add trace code if needed, simulate the state machine, generate test cases and more.

If you want to try out the example yourself download the code generator demo. The oven can be found in the examples folder.

1) , 2)
Copyright Microsoft Cooperation
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies

Leave your comments

, 2015/04/16 00:30

Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read? dbgbkebgkgeedbdb

wiki/news/16aug2013.txt · Last modified: 2024/05/03 22:42 by webmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki