Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Heero_91 on April 09, 2010, 07:41:03 pm

Title: Code generation in C with State Machine
Post by: Heero_91 on April 09, 2010, 07:41:03 pm
Hi everyone,

I have to use (for the first time in my life) entreprise architect in order to generate source code with "only" a state diagram. I have to generate the code in C (no object please, this is not needed ;) )but I'm having trouble doing it on EA.

I have created a new project, with one package and one class diagram in it. In this class diagram I have created one class (with few attributs and one method). I have attached the state machine diagram to my class (using right click->add->statemachine) and i have put some states in it!

After that if I asked to generate the code in C, ths generation is done but i'm not getting the source code for my diagram. But if I'm asking for the same code in C++ (or C#, java...) I'm able to see my diagram.

Can you explain to me how to get my source code for the same diagram in C. I must have miss-done something, but I can't figure what :(

In the same time, if you can explain to me (or provide me a link/project example) about how to add action to states and transitions, it would be very nice.

Thanks a lot

Heero
Title: Re: Code generation in C with State Machine
Post by: deefer on April 09, 2010, 07:46:33 pm
The code generation templates for "C" don't support the state machine generation if you don't enable the OO support.

You can customize them manually to do it, enable the OO support and take inspiration form its code templates (but it won't be easy, I tell you...)
Davide
Title: Re: Code generation in C with State Machine
Post by: Heero_91 on April 09, 2010, 07:52:08 pm
I'm not sure to understand very weel what you're saying... (I'm french ^^)

what's the "OO support"?

And if I activate it I have to customize? How so?

And "take inspiration firm (I guess from ^^) its code template"? I Really don't see what you're talking about :S
Title: Re: Code generation in C with State Machine
Post by: deefer on April 09, 2010, 07:55:47 pm
OO = object oriented support (tools/options/C language)

Start to take a look at the help (look for code generation in the index)

Anyway, I'm right now trying to get some code working from the code generation for state machines in "C" but it is full of problems... my suggestion is: if it is a very simple state machine, then use the code generator, otherwise write it manually...
Title: Re: Code generation in C with State Machine
Post by: Heero_91 on April 09, 2010, 07:59:12 pm
I will talk with my project manager this afternoon and we'll see!

Thanks a lot anyway :)
Title: Re: Code generation in C with State Machine
Post by: Heero_91 on April 09, 2010, 09:41:51 pm
Ok, I have talked with my project manager and he still wants to use EA in C with code generation (maybe just for the moment, we'll see ...)

Anyway, I would like to ask, why I will have to modify the code template as you said before? (It seems a litle bit complicated, no?)
Title: Re: Code generation in C with State Machine
Post by: Heero_91 on April 10, 2010, 01:12:14 am
Sorry for the "flood" ...

I have also tried for hours to add source code to any of my state in the state machine but I can't! I think I'm not in searching in the right field or something like that, can you indicate me how to do that?

If you can also do the same with the transitions, it would be very nice.

Thanks a lot ...
Title: Re: Code generation in C with State Machine
Post by: Nizam Mohamed on April 12, 2010, 04:46:01 pm
Please check the discussions in http://www.sparxsystems.com/cgi-bin/yabb/YaBB.cgi?num=1267093081/11#11
Title: Re: Code generation in C with State Machine
Post by: Aaron B on April 12, 2010, 04:52:20 pm
Hi Heero,
Please confirm which edition of Enterprise Architect are you using. Code generation from behavioral diagrams is only supported in the Business and Software Engineering, Systems Engineering and Ultimate editions of Enterprise Architect.
Title: Re: Code generation in C with State Machine
Post by: Heero_91 on April 12, 2010, 05:18:11 pm
I'm using the trial edition of EA (Ultimate choosen each time, don't worry ;) ) and I'm able to generate the source code from my state machine in C, this is not the problem anymore ...

Now my problem is that I don't know how to put some behavior in my states. I would like, as an example, to put some conditions in order to know which transition the state needs to choose, or just the current behavior of the state (modify variable, print something ...). But I'm searching for this fonctionality since hours, and I'm unable to find anything :(

I would guess this is in the scenario that I have to put my comportement, but how to fill it? Is it some place else?

Also what is the interest of the "Files" option in each state?
Title: Re: Code generation in C with State Machine
Post by: Nizam Mohamed on April 13, 2010, 08:50:52 am
refer http://www.sparxsystems.com/uml_tool_guide/uml_dictionary/transition.html
Title: Re: Code generation in C with State Machine
Post by: Heero_91 on April 13, 2010, 05:13:03 pm
I have found this exact page yesterday so I have been able to get code for my transition, but I still get the main problem.

How to put behavior in my states? I have searched on the help, on the forum, on the EAexemple and so far I have found nothing :( . This is blocking me since days, so if you can just explain it to me.
Title: Re: Code generation in C with State Machine
Post by: Geert Bellekens on April 13, 2010, 05:19:07 pm
Heero

I have absolutely no clue how this will affect code generation, since I don't use that feature.
However if you want to add behavior in a UML way to states you need to use the "operations" option of the states.
There you will find a slightly modified dialog that allows you to add entry/do/exit activities.
In the behavior tab of those you can specify the behavior, or link an existing behavioral element.

Geert

PS. The "Files" tab is standard to all elements in EA. It allows you to link a document or url to it. I guess it's mostly used for documentation purposes.
Title: Re: Code generation in C with State Machine
Post by: Nizam Mohamed on April 13, 2010, 05:19:52 pm
by defining do, entry or exit operations to the state, refer
http://www.sparxsystems.com/uml_tool_guide/modeling_with_uml/operationsmainpage.html

to specify behavior , refer
http://www.sparxsystems.com/uml_tool_guide/modeling_with_uml/operationsdetail.html

for an example refer "Project Models.Systems Engineering Model.Implementation Model.Software.C++.IO.SearchExternalMedia.ReadSerialPorts" Node in the supplied EAExample.eap file.
Title: Re: Code generation in C with State Machine
Post by: Heero_91 on April 13, 2010, 06:23:34 pm
Ok, thanks, it's finally generating source code in my states :D

I have the impression that I still have a lot of questions on this software (this is the first time in my life I'm using a soft in order to generate code...)

I was looking into the links you gave me, and I have a question... Why when you're creating your method attached to your state you need to put the "generated code" (because this is not generated at all, I'm not even getting the variables of my states, but this is not the problem ^^) into the area Behavior and not initial code? What is the purpose of the "initial code" in this case?

And I was wondering, how the state machine is able to move from Entry to Do or Exit, because I can't see the source code linked to this ... (actually I can see the enumeration, but not the changes :S )

I have the impression that for the moment we don't know the value of the "CommandType" and so we can't figure what is the comportement into the state diagram :(

Sorry for all those questions, but I really would like to understad how everything works in EA. ;)
Title: Re: Code generation in C with State Machine
Post by: Makulik on April 13, 2010, 08:08:22 pm
Hi Heero,

Sorry for I'm getting into this discussion, when everything seems to be solved for you already. Since you're mentioning that C should be the final output for the generated code, I'm guessing you have to deploy your FSM on a fairly limited target (restricted RAM and ROM resources)?!?

There was another approach mentioned here on the forum., not to directly generate the target languages code but to generate SMC intermediate code for the FSM and produce the final C code using the SMC-ompiler. The SMC input language allows a much better control over certain FSM features, and the templates are more flexible and easier to understand than EA's code generation templates.

If you'd like to have more details about this approach, and why it might be the better choice for you, feel free to contact me.

You may also contact 'wowbagger' who published the SMC approach here a while ago, search for it in the forum articles.

WBR
günther
Title: Re: Code generation in C with State Machine
Post by: HorstKargl on May 04, 2010, 08:20:40 pm
Hi,

EA comes with two different Code Templates for C. One for object oriented and one for "normal". You cannot see the differences in the Code Generation Template selection, but the Templates are different.

To use object oriented support for C, set "object oriented support  to true". This can be set in Tools | options | source code engineering.

... you can also adapt the C template for object oriented support to your need.

Best Regards,
Horst