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.
Namespace=OvenMachineNS BaseClassMachine=OvenBase BaseClassStates=StateBase SeparateStateClasses=NO
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{ Super, Completed, CookingPause, Idle, Cooking, __UNKNOWN_STATE__ } public enum Events : int { evDoorOpen, evDec, evTimeout, evDoorClosed, evInc, evPwrLow, evPwrHigh, OVEN_NO_MSG } ... 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: if(msg==Events.evDoorClosed){ if(hal.timer_preset()>0){ /* Transition from Idle to Cooking */ evConsumed=1; /* Action code for transition */ hal.timer_start(); /* OnEntry code of state Cooking */ hal.ovenOn(); /* adjust state variables */ stateVarSuper = States.Cooking; }else{ /* Intentionally left blank */ } /*end of event selection */ }else{ /* Intentionally left blank */ } /*end of event selection */ break; /* end of case Idle */ case States.Cooking: ... break; /* end of case Super */ default: /* Intentionally left blank */ break; } /* 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.
Leave your comments
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