Book a Demo

Author Topic: TemplateBinding - help understanding  (Read 5162 times)

DarrenDickens

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
TemplateBinding - help understanding
« on: June 29, 2017, 07:49:17 pm »
I am attempting to edit an instantiation's "actual value" and am having difficulties. Can you help?

I have a class called GenericClass with a "formal parameter" Timer_expired.
I have a class called InstanceClass with a Generalization connector to GenericClass. The connector has a single TemplateBinding with .FormalName="Timer_expired" and .ActualName="20" showing that I am instantiating the generic with the value 20.

I now want to change the value to add "-a" via Automation.

I have noticed that the Help system says that the ActualName is Read/Write so I had...

Code: [Select]
' version 1
Set oInstanceClass = mrep.GetElementByGUID(<guid of InstanceClass>)    ' p1 = INPUT, return = EA.Element
Set oTemplateBinding = GetTemplateBinding(oInstanceClass, "Timer_expired")   ' p1, p2 = INPUT, return = EA.TemplateBinding
oTemplateBinding.ActualName = oTemplateBinding.ActualName & "-a"   ' Change the name a bit
Debug.Print oTemplateBinding.Update
Debug.Print oTemplateBinding.ActualName

The above sometimes prints False indicating a failure to update and the name is not altered. I need to know the FULL reason. I have a guess from my results below but this may only be half the story.



The GetTemplateBinding is a routine that I wrote since there is no Repository.GetTemplateBindingByGUID. I can get the GUID from an SQL query on t_xref with "Timer_expired". The GetTemplateBinding returns the template binding by taking the oInstanceClass to get the generalization connector (iterating over oInstanceClass.Connectors). From the generalization connector I then iterate over oConnector.TemplateBindings looking for .FormalName = the input string, i.e. "Timer_expired". When (if) I find it then I return the found object else return Nothing.

Interestingly if I take the code from my GetTemplateBinding and insert it inline into the above code then I get the following code

Code: [Select]
' version 2
Set oInstanceClass = mrep.GetElementByGUID(<guid of InstanceClass>)    ' p1 = INPUT, return = EA.Element
For Each oConnector In oInstanceClass.Connectors
    If oConnector.Type = "Generalization" Then
        Set oGeneralizationConnector = oConnector
    End If
Next oConnector
For Each oParameter In oGeneralizationConnector.TemplateBindings
    If oParameter.FormalName = "Timer_expired" Then
        Set oTemplateBinding = oParameter
    End If
Next oParameter
oTemplateBinding.ActualName = oTemplateBinding.ActualName & "-a"   ' Change the name a bit
Debug.Print oTemplateBinding.Update
Debug.Print oTemplateBinding.ActualName

Now the .Update prints True meaning that the update worked, and the name gets "-a" added, so the name prints out longer each time the code is run.

Interestingly, if I insert the extra line "Set oGeneralizationConnector = Nothing" to give this new code

Code: [Select]
' version 3
Set oInstanceClass = mrep.GetElementByGUID(<guid of InstanceClass>)    ' p1 = INPUT, return = EA.Element
For Each oConnector In oInstanceClass.Connectors
    If oConnector.Type = "Generalization" Then
        Set oGeneralizationConnector = oConnector
    End If
Next oConnector
For Each oParameter In oGeneralizationConnector.TemplateBindings
    If oParameter.FormalName = "Timer_expired" Then
        Set oTemplateBinding = oParameter
    End If
Next oParameter
Set oGeneralizationConnector = Nothing
oTemplateBinding.ActualName = oTemplateBinding.ActualName & "-a"   ' Change the name a bit
Debug.Print oTemplateBinding.Update
Debug.Print oTemplateBinding.ActualName

Now the .Update still prints True but the name does not get added to; as though EA says "I've successfully updated it but I haven't really".

This gave me the idea to return the Generalization connector at the same time as the TemplateBinding item so my code now looks like

Code: [Select]
' Version 4
Set oInstanceClass = mrep.GetElementByGUID(<guid of InstanceClass>)    ' p1 = INPUT, return = EA.Element
Call GetTemplateBinding(oInstanceClass, "Timer_expired", oGeneralizationConnector, oTemplateBinding)   ' p1, p2 = INPUT, p3, p4 = OUTPUT
oTemplateBinding.ActualName = oTemplateBinding.ActualName & "-a"   ' Change the name a bit
Debug.Print oTemplateBinding.Update
Debug.Print oTemplateBinding.ActualName

The above code now prints True and the name gets longer each time it runs. Interestingly I do not have to use the oGeneralizationConnector in this code at all. It seems that just the existence of the connector is enough to make everything work OK.

If I run the v2 code serveral times then v4 code everything works as expected. If I then call v1 or v3 then the first call to it will work (name gets increased) but subsequent calls the name stays the same length. If I then call v2 or v4 then the first call the name stays the same length (it doesn't work) but subsequent calls start working again (name gets increased). Weird.

I have noticed that the Help system says that the "TemplateBinding Class" attribute ActualName is Read/Write but the "Connector Class" attribute TemplateBinding is Read only. Perhaps this is part of the story, but I couldn't find anything in the help to help!

Question 1 - Why do I need the connector object in scope when I edit the TemplateBinding?
Question 2 - Are there any other things I need "in scope" or is just the connector and the TemplateBinding the whole solution?
Question 3 - Why does jumping between v2/v4 and v1/v3 cause the first call between jumps to change behaviour?
Question 4 - What other items in EA automation have this same "feature"?

Thanks
Darren


qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: TemplateBinding - help understanding
« Reply #1 on: June 29, 2017, 08:42:03 pm »
I guess you're digging in a muddy area where only few adventurers have been before. I suggest to send a bug report regarding the ActualName.

q.

DarrenDickens

  • EA User
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: TemplateBinding - help understanding
« Reply #2 on: July 04, 2017, 12:52:31 am »
I raised a ticket with Sparx so we'll see if they can shed any light on my questions.
Thanks
Darren