Book a Demo

Author Topic: Code Generation with a Constructor  (Read 5309 times)

dabeita

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Code Generation with a Constructor
« on: February 10, 2005, 11:57:59 am »
Greetings.

How would I generate code like the following within a template?

class Cat
{
  private int age;
  private String color;

  public Cat( CatVo inputVo)
  {
     this.age   = inputVo.getAge();  
     this.color = inputVo.getColor();  
  }
}

Where this.age   = inputVo.getAge();  and
this.color = inputVo.getColor();    
would be generated.

Would I need to use EXEC_ADD_IN?

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Code Generation with a Constructor
« Reply #1 on: February 10, 2005, 03:39:26 pm »
It's possible to do using a custom template for attribute and calling a list of that.  (See Code Engineering | Code Template Framework | Code Templates | Custom Templates in the help file)

The hardest bit would be getting getAge and getColor in the case you want.  You can do that though by using
Code: [Select]
$var = %LEFT(attName,"1")%
$var = %TO_UPPER($var)%
get$var%MID(attName,"1")%

I hope that helps.

Simon

dabeita

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Code Generation with a Constructor
« Reply #2 on: February 15, 2005, 07:52:32 am »
Simon,

Well, I gave your suggestion a try and no luck.  I can get the attribute names, but I don't see how to iterate through them via some loop mechanism.  Maybe my question was unclear.  Here is what I want to do.  In general, for every attribute in a class, I want to create the lines mentioned in my last post.

So here is what I want to do:

1.  Get a list of attribute names. Seperated by a space.
2.  Iterate through the list creating:
     this.<attributeName> = paramName.get<AttributeName>();

I've tried numerous ways using the template language, but I don't see a way to do looping in the templates.

So, after looking at other posts, I tried EXEC_ADD_IN, but after creating a VB ActiveX DLL, EA doesn't appear to use the DLL.

Here is what I did following all the steps in the help for creating an AddIn.

1.  Created a VB ActiveX VB DLL.
2.  Created the following class as you would see in the .cls.

VERSION 1.0 CLASS
BEGIN
 MultiUse = -1  'True
 Persistable = 0  'NotPersistable
 DataBindingBehavior = 0  'vbNone
 DataSourceBehavior  = 0  'vbNone
 MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "EATest"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Function EA_Connect(Repository As EA.Repository) As String
   EA_Connect = "MDG"
End Function
Function MyTest(Repository As EA.Repository, Args As Variant) As String
   Dim arg As String
   arg = Args(LBound(Args))
   MyTest = "ReturnString"
End Function

3.  Make the .dll.  I called it EATestAddin.dll

4.  Regsvr32.exe  D:\projects\CCM-V5\Enterprise Architect\EATestAddin\EATestAddin.dll

5.  Added key EATestAddin to HKEY_CURRENT_USER\Software\Sparx Systems\EA400\EA\AddIns.

6.  Set the value of EATestAddin to EATestAddin.EATest

7.  Use the following in an operation template.

Test1    $test=%EXEC_ADD_IN("EATestAddin","MyTest",opName)%
$test
Test2

8.  Generate code.  The Code generates and I see Test1 and Test2 in the code.  I don't see that the EXEC_ADD_IN even called EATestAddin.

Can you help me out?  Is there anyway to log the processing of a template?  

Thanks,

Dave

benc

  • EA Administrator
  • EA User
  • *****
  • Posts: 200
  • Karma: +0/-0
    • View Profile
Re: Code Generation with a Constructor
« Reply #3 on: February 15, 2005, 08:08:24 pm »
Hi Dave,

This is in response to your EXEC_ADD_IN query - Simon will get back re customizing code templates.

Sounds like you're roughly on the right track with the add-in. Couple of points :


1. You probably don't need to make your add-in an MDG add-in (at least not at this stage). Being an MDG add-in has some advantages, but there's also other baggage you need to deal with ... so return an empty string in your EA_Connect function.

2. Make sure EA is actually talking to your add-in. (If it's not, you will be prompted when starting up EA). The following sample functions should be enough to test with. It includes a dummy menu option just so you know things are working. Other than that, I used the same EXEC_ADD_IN call from the template that you did and got the intended result...

Code: [Select]

Function EA_Connect(Repository As EA.Repository) As String
   EA_Connect = ""
End Function

Function EA_GetMenuItems(Repository As EA.Repository, Location As String, MenuName As String) As Variant
   EA_GetMenuItems = "Hello"
End Function

Sub EA_GetMenuState(Repository As EA.Repository, MenuLocation As String, MenuName As String, ItemName As String, IsEnabled As Boolean, IsChecked As Boolean)
   IsEnabled = True
End Sub

Sub EA_MenuClick(Repository As EA.Repository, MenuLocation As String, MenuName As String, ItemName As String)
   MsgBox "Hello World!", vbInformation
End Sub

Function MyTest(Repository As EA.Repository, Args As Variant) As String
   Dim arg As String
   arg = Args(LBound(Args))
   MyTest = "ReturnString"
End Function


Having done the above, are you at least able to see your add-in from the add-ins menu? Let me know if it still isn't working for you...

One other note : Body code for methods is only inserted on initial forward generation - ie. when you generate/overwrite the file initially, or forward synch a  new method into your existing code.

I hope this helps

Regards,
Ben
« Last Edit: February 15, 2005, 08:13:11 pm by benc »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Code Generation with a Constructor
« Reply #4 on: February 15, 2005, 08:31:54 pm »
Here's some further instructions for what you can do.
  • Create a new custom template of type Attribute, I'm calling it Set.
  • That adds an entry Attribute__Set to the list of templates.  Edit it and set it to be:
    Code: [Select]
    $var = %LEFT(attName,"1")%
    $var = %TO_UPPER($var)%
    this.%attName% = the%className%.get$var%MID(attName,"1")%;
  • Edit the Class Body (or class body impl) template to add a list over the custom template defined above.  eg.  Change this (from C++ class body impl)
    Code: [Select]
    %if genOptGenCopyConstructor == "T" and genOptGenCopyConstructorInline != "T" and classHasCopyConstructor != "T"%
    $consPrefix%className%(const %className%& the%className%){\n\n}
    %endIf%
    to this.
    Code: [Select]
    %if genOptGenCopyConstructor == "T" and genOptGenCopyConstructorInline != "T" and classHasCopyConstructor != "T"%
    $consPrefix%className%(const %className%& the%className%){\n%list="Attribute__Set" @separator="\n" @indent="\t"%\n}
    %endIf%
  • This should work equally well if put in the operation body template, although as Ben said, this won't be synchronised.
This example should do most of what you're wanting to do.  I hope it helps.

Simon

dabeita

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Code Generation with a Constructor
« Reply #5 on: February 16, 2005, 06:40:16 am »
Thanks Simon and Ben.

Ben,  I tried your example, but don't see the addin.  Where is the addin menu?  Is it under Tools?  I need a little more help.

Dave


dabeita

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Code Generation with a Constructor
« Reply #6 on: February 16, 2005, 06:42:24 am »
Additionally, EA doesn't give me any message about the addin when it comes up.

dabeita

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Code Generation with a Constructor
« Reply #7 on: February 16, 2005, 10:41:24 am »
Ben,

Disregard my last posts.  I had a problem with my Registry.

Everything works and I see the Addin Menu.

Thanks.