views:

115

answers:

2

Is there a simple .NET code generator for a Event-driven finite state machine?

I am tired of doing this by hand for user-interface objects. I just need the enum definition, the switch statement, and each function call. I do not need a "library".

See Event-driven finite state machine at http://en.wikipedia.org/wiki/Event_driven_finite_state_machine.

State Machines in C# 3.0 using T4 Templates at http://aabs.wordpress.com/2008/06/26/state-machines-in-c-30-using-t4-templates/ seems like it is what I want but the link to the example code is broken.

A: 

Like you said, you just need:

An enum definition, holding a variable of it, a naming convention that includes in the method name the corresponding enum state, a call with reflection and updating the variable holding the enum.

The above isn't with generated code, but I don't see why not using it. You should be able to bake one quickly. An alternative instead of using reflection, would be holding in a list for each state which Func<> will be called, and generating that should be easy with a T4 template.


ps. I tend to be a lot more helpful, but the way the question is phrased it begged for it ^-^. That said, I'm kind of serious with the above, doing either of the above isn't that much time (specially with stackoverflow help in any part the OP might not know) / and the OP wrote that like he has done those by hand tons of times.

eglasius
-1 for not answering question.
AMissico
well, I'm missing that badge that you get when deleting an answer with -3 votes, so its k, lol. Back to being serious, that T4 sample you linked in the edit looks more complex than needed, I strongly advice to keep it simple and base it in a simple c# enum + use conventions like I mentioned in my answer.
eglasius
@eglasisu - Ha, I got that badge a few weeks ago. -1 more to go. I understand your answer and not discounting it. Just looking for something specific to event-driven, more mature, and stable that someone may know about. T4 is new to me, but I believe I can use it in the future.
AMissico
@AMissico what did you end up doing? :)
eglasius
@eglasisu: For now I hand coded again, but I think I might have found what I was looking for. (Need to evaluate, and don't have reference now.) Plus, I found an old VB6 class I had for generating code based on .ini file. Moreover, in spare time I been looking for SCXML(?) .NET Library. I am onto another project, but I plan on keeping the question updated with references I have found. Then come up with a workable solution. I like the idea of a T4 template. I want to wrap this up nicely so it can be easily found and used.
AMissico
A: 

If I were in your position, I would download the trial version of Enterprise Architect:

http://www.sparxsystems.com.au/products/ea/index.html

I would then model my scenario and then use the code generator. Depending on how many you need to generate you might want to look into some of the more advanced options in the product, such as scripting that might allow you to generate multiple time changing parameters based on a list.

P.S. I don't work for Sparx systems :-), I do love their tools though.

RobD
don't know about the tool, but that sounds so much like killing a fly with a cannon :P
eglasius
-1, not simple.
AMissico