Author Topic: CodeGen - one file per Association?  (Read 3308 times)

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
CodeGen - one file per Association?
« on: November 16, 2015, 07:06:31 am »
I'm designing code templates (in EA12.1). The File template usually generates one file per class, but I would like to also generate one file per Connection/Association.  Is there any way to trick the code generator to do that :question

// Rolf
-- There's always a solution --

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: CodeGen - one file per Association?
« Reply #1 on: November 16, 2015, 11:01:39 pm »
Hej Rolf,


To my knowledge, not in out-of-the-box EA.

While you can create templates which refer to each other, the underlying infrastructure is out of reach and you can't make the generator do things in a different order, or in fact tell it to do much of anything: your code is called to transform bits of the model into text, but not to create files.

What you can do is create an Add-In and call that from one of your templates with the EXEC_ADD_IN macro. The method signature only includes the class and operation being processed, but from that you can look at the relationships the class has and create whatever files you wish from that.

Note that your Add-In will get called once for each class/operation combination, so you'll need to keep track of what you've done in previous calls or accept that your connector-based files will get repeatedly overwritten during code generation.

Hoppas detta hjälper,


/Uffe
My theories are always correct, just apply them to the right reality.

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: CodeGen - one file per Association?
« Reply #2 on: November 17, 2015, 12:26:29 am »
Quote
Hej Rolf,
Hoppas detta hjälper,
Hej Uffe!
Jo, det hjälper. :)

Yes, it helps to know what can and can't be done.

I think one can avoid overwriting the connectors since the Connector's "Source" end belongs only to one class, so that will probably not be a problem.

I'll check out how to make an add-in for the EXEC_ADD_IN macro. I've never seen that one before, but I hopw I can use VBScript (I'm quite familiar with that). Hopefully there's help to guide me how to make a macro.

Thank you for replying!

// Rolf
-- There's always a solution --

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: CodeGen - one file per Association?
« Reply #3 on: November 17, 2015, 01:47:15 am »
Hallå igen,


I'm afraid you can't use VBScript for this, you have to write an Add-In.

An Add-In is compiled code, usually written in C#, which is deployed as a DLL and can receive event callbacks from the EA client. Regular scripts in EA can't. They can read and write models and other project data as available through the Object Model API, but only Add-Ins can receive calls from EA, and that's what you need for the EXEC_ADD_IN macro.

There's more details in the help file, check out the top pages under Automation and Scripting in particular. For an example of how to create a simple Add-In, you can look at Geert Bellekens' blog. It doesn't address issues around packaging and distribution so it's not much good for anything like team or enterprise situations, but it's an OK place to start.


/Uffe
My theories are always correct, just apply them to the right reality.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 11849
  • Karma: +460/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: CodeGen - one file per Association?
« Reply #4 on: November 17, 2015, 02:50:15 pm »
Quote
For an example of how to create a simple Add-In, you can look at Geert Bellekens' blog. It doesn't address issues around packaging and distribution so it's not much good for anything like team or enterprise situations, but it's an OK place to start.

/Uffe
There's another article: Tutorial: Deploy your Enterprise Architect C# add-in with an MSI package that tackles deployment.
I've listed all EA Addin writing stuff on this page: Writing EA add-ins

Geert

RIL

  • EA User
  • **
  • Posts: 142
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: CodeGen - one file per Association?
« Reply #5 on: November 17, 2015, 10:44:25 pm »
Thank you guys, I'm up hacking a C# Add-in now. I had good use of Geert's example pages.

One thing which would make it easier for newbies to get the hang of the examples - there's nowhere in the examples pointed out WHERE in the EA User Interface one should expect to see the new plug in to show up. I searched for ... well, hours  :-[ before I found the new stuff in the "Extensions" popup menu...  It seems to me to be a good idea to describe what result to expect (and thus to look for) from the examples and code. :)

But finally I got it. And, your help & examples really are awesome. Again, Thank you guys.

Doing some nasty MS Office / Access automation. Unfortunately EA is slow to tap via the API...  :-/

// Rolf
-- There's always a solution --

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 11849
  • Karma: +460/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: CodeGen - one file per Association?
« Reply #6 on: November 17, 2015, 10:59:38 pm »
Hi Rolf,

I'll look into adding that kind of info.

As for the comment about EA API to be too slow; Yes and No.

Traversing the whole model via the API is indeed slow.
What I usually do is search for the things I need using the operation Repository.SQLQuery() where I query the ID's of the thing I need.
Those ID's can then be used in operations such as Repository.GetElementByID, Repository.GetDiagramByID, and other Get[xxx]ByID.

This way you get a bit of the best of both worlds. You get the speed of the SQL queries to get the only the objects you need, and you can still use the abstraction of the API to avoid the all too nasty direct database modifications.

Another tip for better performance is to avoid iterating EA.Collections more then once.
Depending on the type of EA.Collection the API will sometimes query the database with each next.
Instead I usually put the objects in a standard .Net collection if I need to iterate it.

Geert