Book a Demo

Author Topic: Transformation : iterate through all classes  (Read 4345 times)

dirc

  • EA User
  • **
  • Posts: 46
  • Karma: +0/-0
    • View Profile
Transformation : iterate through all classes
« on: May 16, 2008, 06:37:32 pm »
I have got the rest of mytransformation working and I am stuck with one last problem: I am trying to generate a single class which is a factory for all the classes that are being transformed.

I currently have class template for this and I want to do something like the following:


Class
{
    for each class in the class list
    Operation
   {
        name=%qt%Create%className%%qt%
   }

}

Where do I start in doing this? I'm sure it must be a simple task but the  'language' is a bit obtuse and there are not many samples to work from.




dirc

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Transformation : iterate through all classes
« Reply #1 on: May 19, 2008, 08:42:08 am »
The solution is custom templates.  Basically an additional named template that you can call.  You'll need something like this.

File Template
Code: [Select]
... The rest of your file template ...
Class
{
  Add a transform reference here.
%list="Namespace__ClassCreators" @indent="  " @separator="\n\n"%
}

Namespace__ClassCreators Template
Code: [Select]
%list="Namespace__ClassCreators" @indent="" @separator="\n\n"%
%list="Class__Creator Template " @indent="" @separator="\n\n"%

Class__Creator Template
Code: [Select]
Operation
{
  name=%qt%Create%className%%qt%
}
And that should do it.  (Note, I haven't entered this into EA or run it so there may be errors, but it should get you going.)

dirc

  • EA User
  • **
  • Posts: 46
  • Karma: +0/-0
    • View Profile
Re: Transformation : iterate through all classes
« Reply #2 on: May 20, 2008, 06:57:08 pm »
mmm.. I had done something similar already:

File

Code: [Select]
Package
{
      name="BusinessService"
      
      %Class__BusinessServiceLogicFactory%
      
}

%list="Namespace" @separator="\n\n" @indent="  "%

Class__BusinessServiceLogicFactory:

Code: [Select]
%PI="\n  "%

Class
{
      name="BusinessServiceLogicFactory"
      stereotype="BusinessServiceLogicFactory"
      
      Tag
      {
            name="static"
            value="true"
      }

      %list="Class__BusinessServiceLogicFactoryMethod" @indent="" @separator="\n\n"%       
}


Class__BusinessServiceLogicFactoryMethod:

Code: [Select]
Operation
{
  name=%qt%Create%className%%qt%
  scope="public"
  static="true"
}

The problem is that the Class__BusinessServiceLogicFactoryMethod never seems to get run... I have put in illegal characters to try to force an error but nothing happens.

Any ideas? :-?
dirc

dirc

  • EA User
  • **
  • Posts: 46
  • Karma: +0/-0
    • View Profile
Re: Transformation : iterate through all classes
« Reply #3 on: May 20, 2008, 07:26:54 pm »
To reiterate my problem:

Create a single class that has a factory method for each class in the transform.

The problem seems to be getting the list of classes in scope when I generate this single class.

I have tried moving the transform everywhere to see if I can get this working, but I can either generate several classes with the same name(?!!) or a single class with no methods.
dirc

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Transformation : iterate through all classes
« Reply #4 on: May 21, 2008, 08:41:55 am »
Indeed it won't ever be run.  I'll try to explain what is going on.

When you call %Class__BusinessServiceLogicFactory% EA enters a class context, there is always a class avaiable in the templates but you can't really control what one.  Any lists over custom class templates from that template will be listing over the inner classes of that class.

So, if you remove the intermediate class template (copy it up into the file template) then the whole thing should work.