Book a Demo

Author Topic: Issue while code generation in C  (Read 6955 times)

YannM

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Issue while code generation in C
« on: February 05, 2020, 05:20:37 am »
Hi all,

I'm using EA 13.5 and I'm trying to generate C code from a UML state machine.
Generally speaking it works when no attribute is used in the class: C code is generated, imported in an IAR project and compiling.

My issue so far is specific to UML class declaration with one or more attributes. The class is called "class".
In the simulation part, I can manipulate those attributes by using this.attribute1.
When the code is generated, I have code like this->attribute1 = 1;

But when the compiler returns the following error
Error[Pe136]: struct "class" has no field "attribute1"

The reason is that the attributes of my class are not part of the "class" structure but declared as local variables after C code generation.

Can I solve this issue from EA ? I mean is there a way for the C code generation to make attributes part of the structure "class" and not as local variables ?



qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Issue while code generation in C
« Reply #1 on: February 05, 2020, 05:49:24 am »
C has no object concept, does it?

q.

YannM

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Issue while code generation in C
« Reply #2 on: February 05, 2020, 06:49:57 am »
How is that useful to my question  :o ?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Issue while code generation in C
« Reply #3 on: February 05, 2020, 09:10:53 am »
I was just wondering why someone uses a hammer to open a can...

q.

YannM

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Issue while code generation in C
« Reply #4 on: February 05, 2020, 09:04:16 pm »

This is the explanation we can find in user guide "Executable StateMachines" from Sparx
Quote
Access Context's member variable from State behavior and Transition Effect
Enterprise Architect's Executable StateMachine supports simulation for C, C++, C#, Java and JavaScript.
For C and C++, the syntax differs from C#, Java and JavaScript in accessing the context's member variables. C and C++
use the pointer '->' while the others simply use '.'; however, you can always use this.variableName to access the
variables. Enterprise Architect will translate it to this->variableName for C and C++.
So for all languages, simply use this format for the simulation:
this.variableName

Examples:
In the transition's effect:
   this.authorizeCnt++;
In some state's entry, do or exit behavior:
   this.foo += this.bar;
Note: by default Enterprise Architect is only replacing 'this->' with 'this' for C and C++; For example:
   this.foo = this.bar + myObject.iCount + myPointer->iCount;
will be translated to:
   this->foo = this->bar + myObject.iCount + myPointer->iCount;


However the attribute variableName when declared in the class, will be "generated" in C as a local variable.
Thus the use of this.variableName (automatically generated) is obviously an error for the compiler.

How can I solve the issue ?

Try example Executable State Machine > Simulation Commands from Sparx: C code is generated, but can not be compiled.

YannM

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Issue while code generation in C
« Reply #5 on: February 06, 2020, 11:06:09 pm »
For those interested, part of the solution is to change Configure > Model > Options > C > Object Oriented Support from False to True.

However the automatic conversion of this.variableName to this->variableName does not work! May be another option to change somewhere ?

Quote
Enterprise Architect's Executable StateMachine supports simulation for C, C++, C#, Java and JavaScript.
For C and C++, the syntax differs from C#, Java and JavaScript in accessing the context's member variables. C and C++
use the pointer '->' while the others simply use '.'; however, you can always use this.variableName to access the
variables. Enterprise Architect will translate it to this->variableName for C and C++.
So for all languages, simply use this format for the simulation:
this.variableName

bknoth2

  • EA User
  • **
  • Posts: 129
  • Karma: +2/-0
    • View Profile
Re: Issue while code generation in C
« Reply #6 on: February 07, 2020, 03:53:45 am »
In my experience generating state machine code for C++, I had to examine and modify the code generation templates to get the behavior I wanted. You may have to do the same, and the answer to your question about converting . to -> can almost certainly be found in those templates. In EA V15.1, the templates are found through the ribbon Develop>Preferences->Options->Edit Code Templates.

If you are fortunate, someone will have a simple answer and all will be well. If not, the templates tell all but will take study.


YannM

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Issue while code generation in C
« Reply #7 on: February 07, 2020, 06:28:02 pm »
Hi,

I checked the templates until "Class Body Impl" that has this code.
Quote
%if genOptStmCUseNewTemplate == "T" and stmContextHasStatemachine == "T"%
/* Begin - EA generated code for StateMachine */
\n
%StmContextDefinitionImpl%
\n
/* End - EA generated code for StateMachine */

I have no idea where I can find the code executed for StmContextDefinitionImpl. Any idea where I can find this ?