Author Topic: CodeGen a LIST of classes into one (1) code file?  (Read 4079 times)

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
CodeGen a LIST of classes into one (1) code file?
« on: January 17, 2013, 05:24:42 am »
Hi all,

I'm trying to make a Code Generator Template that collects all Classes of a certain Package (including sub packages) and then generate ONE (1) file containing data about all the classes.

I'm starting from a Delphi template, trying to list classes as I would with class members like attributes and operations, but the generator keeps asking me for a file name for each class (I want only one file, although I could of course later post process the output and merge into one file, but I'd like to see EA finnish the job in one shot).

I'm I asking for too much? So far I get the following output (but as said, I get only one class in my output):

Code: [Select]
VERSION 19
(Model
      "Business Classes"
      "AbstractRoot"
      ""
      ""
            (Class
                  "LingualDomain"
                  "RootObject"
                  "" // Attribs...
                  "" // Operations...
                  "" // More class specs...
            )
      )
      (Associations
      // ...
      )
)

// ROlf Lampa
« Last Edit: January 17, 2013, 05:26:47 am by Rolf_Lampa »
-- There's always a solution --

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #1 on: January 17, 2013, 05:55:02 am »
AFAIK EA's code generation mechanism works on a class to file basis. Such the entry points are the %File% and %FileImpl% base templates, and these are applied per class.

The only way to get around this, is to introduce a pseudo package stereotype (from metatype class) that is used to contain nested class definitions and maybe more pseudo packages. You can then provide stereotyped specializations for the %File% and %FileImpl% base templates, that will render the contents in a single code module.

I have been using this technique to define google protobuf packages, that contain a number of message and enum definitions.

I would recommend developing a profile for this situation and provide an appropriate project browser icon and shape script for the pseudo package stereotype (folder like with a special indicator), avoiding to confuse your users or yourself.

Further note:
  • You need to handle the pseudo package instances as namespace specifiers in code generation
  • Providing normal packages as child of a pseudo package will not be possible (this might cause side effects on other package based mechanisms in your model)

HTH
Günther

PS.: This question might be better placed at the Automation Interface, Add-Ins and Tools discussion board
« Last Edit: January 17, 2013, 06:22:45 am by g.makulik »
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #2 on: January 17, 2013, 06:30:20 am »
Thank you very much for this. But before I go about trying this (it doesn't seem to be the most user friendly solution) I have another thought:

I haven't tried this, but if you have tried using recursive calls, do you think it would be possible to :
    1. Call File (=Class) recursively, and if so, then
    2. how to define a controlled loop to determine first and last (class) page, and
    3. thus I being able to insert Header text and Footer tex + gently Terminate the loop.
Would that be possible? if not, why wouldn't EA enhance the template mechanism to make it possible. In that way the problem would be solved.

I also have a State project I'd like to solve in a likewise manner (having all state definitition in a central textfile, which can be updated in runtime... ).

Regards,
// Rolf Lampa

Btw, sorry for posting this in the wrong group.)
« Last Edit: January 17, 2013, 06:31:22 am by Rolf_Lampa »
-- There's always a solution --

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #3 on: January 17, 2013, 06:38:57 am »
Unfortunately there's no such constructs like looping in EA CTF templates, I don't think your idea goes down the right track.

What exactly are your concerns about 'user unfriendliness' going the MDG/Profile/stereotype way (you'll need to mark such packages that should create a code modules anyway somehow)?

Best regards,
Günther

PS. (again): State Machines/States are different, these are supposed to be childs of classes and will be generated into one code file.
« Last Edit: January 17, 2013, 06:43:07 am by g.makulik »
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #4 on: January 17, 2013, 07:01:23 am »
Quote
Unfortunately there's no such constructs like looping in EA CTF templates,
OK, I'll try your hint.

Quote
... State Machines/States are different, these are supposed to be childs of classes and will be generated into one code file.
But will there be one file per bunch of states per class?

// Rolf
« Last Edit: January 17, 2013, 07:03:00 am by Rolf_Lampa »
-- There's always a solution --

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #5 on: January 17, 2013, 07:34:04 am »
Quote
But will there be one file per bunch of states per class?

I'd guess yes. I'm not so experienced with EA's behavioral model templates because AFAIK its only available with Business & Software Engineering Edition or higher (I just have access to Corporate and Professional Editions).
Actually I'm working on my own solution for state machine code generation. But for my context I need the opposite at least (having one class per state). I found Geert Bellekens' EA AddIn Framework useful for this task and provided some extensions for behavioral state machines. I'm using the TransformationILWriter class to provide my own state machine to class model transformation (implemented as AddIn method used in a transformation template).

HTH
Günther
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 7732
  • Karma: +103/-18
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #6 on: January 17, 2013, 09:17:20 am »
Quote
I'm trying to make a Code Generator Template that collects all Classes of a certain Package (including sub packages) and then generate ONE (1) file containing data about all the classes.
No problem.

The generate package source code can do this. It will generate all classes with the same filename at the same time. The only thing that it won't do is set all classes in the package (and sub-packages) to have the same file. You can do that with a script if necessary.

If there are any questions still required after that fact ask away.
« Last Edit: January 17, 2013, 09:22:20 am by simonm »
Eve

support@sparxsystems.com

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #7 on: January 17, 2013, 09:34:27 am »
Quote
No problem.

The generate package source code can do this. It will generate all classes with the same filename at the same time.

Interesting to know this, didn't know and consider to try this way yet.
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #8 on: January 17, 2013, 09:46:17 am »
Quote
... generate package ... will generate all classes with the same filename at the same time. ... it won't...set all classes in the package (and sub-packages) to have the same file.
I'm afraid that you lost me here... I read you saying that it will generate using one filename, BUT, it won't put them into one file. It doesn't make sense to me. Is it only that the hour is late and I'm just not getting it... ?

Quote
You can do that with a script if necessary.
Do you mean that a Package helps getting them all Code Generated (which are in the Package), but, as they land in individual files I would have to postprocess the files on disk to merge them into one final file. Is that how you meant?

More reading
I'm a bit new to using this tool, so I might not have found all the documentation and example resources yet, but I'm very interested in more info about how the template mechanism works. I would have several projects waiting if I just could master the beast. :)

Thank you in advance for any hints. And feel free to move this thread to the right forum.

Regards,
// Rolf Lampa
« Last Edit: January 17, 2013, 09:49:09 am by Rolf_Lampa »
-- There's always a solution --

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 7732
  • Karma: +103/-18
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #9 on: January 17, 2013, 11:14:25 am »
The package generation dialog can set filenames. But the only way that it does that is to set a unique filename for each class. That's not what you want so don't use that feature.

Before you open the generation dialog set the Filename property of each class to the same thing.

Now open the package code generation dialog, you'll get a list of classes and their filenames. Hit generate. What happens is that in the File template you will either list over all packages or classes. Doesn't really matter which. Inside the package you'll list over the classes in the package (and again list over subpackages.) If you have multiple classes that were set to generate to the same file then they all will be generated according to the rules in the template.

You do not need to post-process them. The script was only mentioned as a way of setting the filename for everything at once.
Eve

support@sparxsystems.com

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #10 on: January 17, 2013, 11:54:03 am »
Well, that made a whole lot more sense! I will try that. :)

But the other ideas on the subject lead me to quickly learn about MDG Technology and Profiles and... other useful things. Next would be to learn scripting in EA.

Feature request:

It would be of great value to have a mechanism for "default filenames" for Classes according to the following logic:
    1. If not defined for class - use the same filename as the nearest super class' with a specified filename.
    2. If no filename is to be found in the class hierachy, use the filename of the Package (add this feature).
    3. If the Package doesn't have any filename, use the filename of the nearest parent Package (same logic as for Classes).
    4. If no filename exist at all, save
per class name (per default), in the same folder as the model.
5. Adding explicitly defined filenames per class would override all the above.
[/list]Ok, now I will test your instructions.

Best Regards,

// Rolf Lampa
« Last Edit: January 17, 2013, 12:12:09 pm by Rolf_Lampa »
-- There's always a solution --

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: CodeGen a LIST of classes into one (1) code fi
« Reply #11 on: January 17, 2013, 04:36:34 pm »
Works like a charm. <thumbs up>

Thank you very much. If you'ld consider the naming strategy I mentioned above, then this will be the perfect tool.

Been into industrial strength MDA (executable* UML models) since 1998, and I like what I see.

Best regards,
// Rolf Lampa

* That is, Bold Architecture, Win32, Bold for Delphi.
-- There's always a solution --