Book a Demo

Author Topic: How to create Parameterized Classes (Templates)?  (Read 8925 times)

Johan Pettersson

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
How to create Parameterized Classes (Templates)?
« on: August 29, 2008, 06:51:58 pm »
I'm trying to model a parameterized class. I create a normal class and tries to perform the steps described in the "Enterprise Architect User Guide" in "Parameterized Classes (Templates)":

  • I go to "Properties" and "Details" tab on the class.
  • I choose "Parameterised" in the "Type" combobox under "Templates".
  • I proceed to add parameters. When I do this I want to add a class parameter. But when I use the type combo box I can only choose between defined classes. This is what I would expect to choose between when actually choosing parameter _values_ when creating an instance of the template! My actual parameter ought to be of the type "class".

Apart from failing to create a proper parameterized class I fail to understand how to create an instance of a parameterized class.

In conclusion:
  • What is wrong in the way I try to create a parameterized class above?
  • How do I create an instance of my parameterized class?

I use evaluation of Enterprise Architect 7.1 Corporate on Windows XP 64bit.

Thanks in advance!

/Johan
« Last Edit: August 29, 2008, 06:54:46 pm by johan.pettersson »

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #1 on: August 29, 2008, 09:09:32 pm »
Don't know for sure.

I only use this feature to design generic classes in .Net. While EA uses the same constructs the semantics are a bit different. I have not had any real trouble with data types in the parameters.

As to creating an instance, try control-drag from your (predefined) class in the Project Browser to your diagram.

Look up Run State in the EA help. This might be what you need for instance values.

I may not have covered all of what you need, but perhaps this will get you onto the right (or a more fruitful) track. You don't want to get hung up during an evaluation.

HTH, David
No, you can't have it!

Johan Pettersson

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #2 on: August 29, 2008, 10:22:45 pm »
Thanks for your answer, David!

What I'm looking for here is something a bit different I think. I'll try to be more precise:

Let's say we have the following C++ class:

Code: [Select]
template <class TYPE, int i>
class ATemplateClass
{
public:
  AMethod(TYPE data);
};

I'm trying to model this using EA. The only way to do this that I know of is a parameterized class. The problem arises when expressing the parameters in EA: The "i" parameter is not a problem. The type of this parameter is an int. That works. I can do that. But the TYPE parameter is of the type "class". The value of this parameter is itself a class or type. This I cannot do from what I can see although this is exactly what they have in the picture in the user manual (I just can't see how they arrived there). In Visio I can do this by making the type of the parameter unspecified. This is how this would look when done in Visio:



In that example I also have a bound element, an instance of the template. The parameters are, as you can see, the class "AnotherClass" for TYPE and the int value 10 for i. I want to accomplishing exactly this in EA. And the problems are that a) I cannot understand how to make the type of TYPE "class" and I can't understand how to create the instance, the bound element.

I tried dragging my parameterized class into the diagram from the project browser as you suggested. While holding down control. But I was only offered the usual creation of simple link or instance. I tried instance (since instance of a parameterized class ought to be a class) but this only created an object of the class (which shouldn't even be possible since a parameterized class cannot be instantiated directly).

I may have misunderstood the concepts here but I can't figure out how.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #3 on: August 30, 2008, 01:08:21 am »
I think it was more likely my memory of how I did this with Generics. I keep thinking I got the most or all of it to work. I'll try to remember more and will post back if anything else comes to mind.
No, you can't have it!

Johan Pettersson

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #4 on: August 30, 2008, 09:30:43 pm »
Thanks, David!

jjyoung

  • EA User
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #5 on: November 06, 2008, 11:03:26 am »
BUMP.

I've been trying to find out how to do this also.  It seems like it should be fairly straightforward to do.  Why is the info on it so hard to find?

Frank Horn

  • EA User
  • **
  • Posts: 535
  • Karma: +1/-0
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #6 on: November 06, 2008, 06:39:30 pm »
Quote
In Visio I can do this by making the type of the parameter unspecified.

You can do this in EA as well, and I think this is what one usually does for a template parameter of type class.

Could you try to import source code using this construct? It may show what EA makes of it and what is supported and what isn't.

Reverse engineering just to see how things are done is a poor substitute for proper documentation of course, but I guess we all have to do it more often than once.

jjyoung

  • EA User
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #7 on: November 07, 2008, 06:36:05 am »
Okay, I think I finally figured out how to do this.  I'll break it down into multiple steps:

Create the parameterised (i.e. Template) class
1. Go into the Class Properties->Details page.
2. In the Temnplates section set Type to "Parameterized"
3. Add any template variables you wish.  If you want a Parameter 'T' to be a Class type to be specified later, you would set the Parameter to 'T' and the Type to "Class".

Create a realization which binds the Template to specific classes
1. Create another class and attach it to the parameterized class via a Realization relationship with a stereotype of <<bind>>.  Note that it doesn't appear that the <<bind>> stereotype is actually necessary, but it should be there for completeness.
2. Go to the Class Properties->Details page of the new class
3. In the Template section set the Type to "Instanciated"
4. In the Arguments entry box insert the concrete classes that you want to substitute for the template classes separated by commas.  Note that the order of the Arguments must be the same order as the template parameters in the parameterized class.  In addition, the Arguments don't contain the parameter entries from the parameterized class, only the Type specifications.

You should note that the graphics for the bound (i.e. "Instanciated") class does not adhere to the UML standard graphical representation.  Also, there doesn't appear to be any way to create an attribute for a class which binds a parameterized class without going through the above
« Last Edit: November 07, 2008, 06:37:52 am by jjyoung »

geoffs

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #8 on: November 23, 2008, 05:02:54 am »
Interesting discussion! It is too bad that there is not proper documentation to detail the steps for doing these sorts of operations. Nevertheless, I too arrived at basically the same sequence for creating an instantiation of a parameterised class. But...

I have encountered a problem when I have a class that is inheriting from two parent classes, one of which is not a parameterised class and the other of which is a parameterised class. Apparently, EA has no way during code generation (that I can determine) to specify that the instantiated argument of the derived class should only be applied to the one parent parameterised class. Instead, it proceeds to put the template argument onto the declarations for both parent classes:

class class1 : public class2<arg>, public class3<arg>

What I want is:

class class1 : public class2, public class3<arg>

Has anyone else encountered this? Any solutions?

Didro

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #9 on: December 02, 2008, 08:47:22 pm »
Hello, I'm interesting in the solution of the problem too. How I can model in EA such code (C++):

Code: [Select]
template <class T1>
class A
{...}

template <class T2>
class B: public A<T2>
{...}

?

Thank you

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #10 on: December 03, 2008, 05:10:51 am »
My guess - and it really is a guess; I don't do any C++ work anymore - is that you would go about this in stages, from the bottom up.

(Someplace in your model) create an element for A. Make it a template (or parameterized) class, whichever EA offers you in the toolbox. In the parameters section create a single item of type Class (and name it T1). Directions are floating around the EA help. [Though I remember them being a bit vague; I needed them to model .Net generics.]

Start at the structural level of your model, to explain what each of the classes are. Now create a template class for B. This time fill in the parameter section with a class named T2.

When it comes time to create lower levels of your model, you will explain how your classes are actually used in your code, and their relationships to each other. For this you will model instances of A and B. For each instance of B you will fill in the details of the parameter. When it comes time to generate code EA should fill in the details as you want.

If you run into any snags - even if it is only interpreting the documentation or a bit of finger trouble - stop before you get tangle up. Use your favorite development environment - outside of EA - and create a very brief bit of code by hand, with the constructs you've identified above. Reverse engineer the code into an empty EA package (or project, to make sure nothing is already there to interfere). EA will create the structural diagrams (only, I think) for you. Take a look at how they are put together. When you are comfortable with the structural diagrams, create a small instance model and fill in the blanks.

When you are at the above point, stop and create a model of your own (so there is nothing in EA referring to the source code you imported). Try generating 'fresh' source code (to an empty directory) from this second mode. From this point you should be able to tidy things up and get moving.

Please let us know what works.

David

[edit]Hint: Make sure you have the Tagged Values window open in EA when you are inspecting the imported model. You can dock this window and EA will remember the setting.[/edit]
« Last Edit: December 03, 2008, 05:12:29 am by Midnight »
No, you can't have it!

Didro

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: How to create Parameterized Classes (Templates
« Reply #11 on: December 04, 2008, 04:31:41 am »
Thank you, for irrefragable answer.

I use the Reverse engineer and I see, that the code from my previous post should be modelled by using Argumetns field in Class Detail Window (I write T2 in the Argumetns field of Class B Detail Window).

But the problem, described by geoffs, could not be solved in such a way.

Thank you