Author Topic: setting attribute.Default = "X" doesn't work  (Read 8358 times)

eric.meredith

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
setting attribute.Default = "X" doesn't work
« on: June 21, 2017, 12:23:51 pm »
I am a newbie to EA, but a "classic" in app dev.

This snippet should work, me thinks:
See this code snippet below:
Dim currentAttribute As EA.Attribute
Set currentAttribute = eaConn.addOrUpdateAttribute(currentClass, “IP_ADDRESS”, stereotype, description, attrType)
currentAttribute.Default = "1.2.3.4"

Now – I’ve obviously left a lot out here, but MOST of this works exactly as you would expect, enabling me to import server definitions into our SPARX repo quite nicely. The last line, however, does not work – and yet it appears to be correct.

It “IS” correct syntactically. It is supposed to set the Initial Value of an Attribute, in this case an attribute called IP_ADDRESS. In fact, If I use this code to create a Node (a server) and to create the IP ADDRESS attribute, it does everything EXCEPT insert the IP number. If I manually insert the IP number in EA, and then use the code to retrieve the attribute, why shock of wonder the IP_ADDRESS is there and is correct.

Thought? Help!!

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8599
  • Karma: +256/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: setting attribute.Default = "X" doesn't work
« Reply #1 on: June 21, 2017, 04:44:47 pm »
Hi Eric,

The "secret sauce" you may be missing is that you have to issue a: currentAttribute.Update - to push the value back to the repository.

HTH,
Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: setting attribute.Default = "X" doesn't work
« Reply #2 on: June 21, 2017, 11:11:18 pm »
Or you can add the default value as parameter the addOrUpdateAttribute() operation

If I recall it correctly (I wrote that operation originally I suspect) that operation will already do an Update(), so by adding the parameter you avoid saving the attribute twice.

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: setting attribute.Default = "X" doesn't work
« Reply #3 on: June 21, 2017, 11:44:05 pm »
Geert, I think that just exists in one of your own wrappers.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: setting attribute.Default = "X" doesn't work
« Reply #4 on: June 21, 2017, 11:56:44 pm »
I think the code originally came from the excel importer in which I add or update an attribute depending or whether an attribute with that name already exists on the that element.
So there will be call to Update() inside the addOrUpdateAttribute() operation or it doesn't do anything.

The original code goes like this:
Code: [Select]
'-------------------------------------------------------------
' Author:   Geert Bellekens
' Date:     31/08/2009
' Description: adds or updates the attriute with the given name
'   stereotype and description and type in the given parent Class
'-------------------------------------------------------------
Public Function addOrUpdateAttribute(parentClass As EA.element, name As String, stereotype As String, description As String, attrType As String) As EA.Attribute
    Dim myAttribute As EA.Attribute
    'try to find existing attribute with the given name
    Set myAttribute = getAttributeByName(parentClass, name)
    If myAttribute Is Nothing Then
        'no existing attribute, create new
        Set myAttribute = parentClass.attributes.AddNew(name, "Attribute")
    End If
    'set properties
    myAttribute.stereotype = stereotype
    myAttribute.Notes = description
    myAttribute.Type = attrType
    'save attribute
    myAttribute.Update
    'refresh attributes collection
    parentClass.attributes.Refresh
    'return attribute
    Set addOrUpdateAttribute = myAttribute
End Function

Geert

eric.meredith

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: setting attribute.Default = "X" doesn't work
« Reply #5 on: June 22, 2017, 05:02:14 am »
So .. I think I tried all of the suggestion here, and it still not working. Here is a longer snippet of code, but understand EVERYTHING ELSE IS WORKING (sorry about the caps, but you must understand..) that all other fields are updating correctly.

Geert, when I look at the addOrUpdateAttribute  function, the 4th parameter (Description) actually maps to 'Notes', not Initial Value - which is what I'm trying to set. Stated another way -- bring up the Attributes dialog box, and look at the six columns (Name, Type, Scope, Stereotype, Alias, Initial Value). How do I set "Initial Value"?

Thank you so much for the help.

Code:
Code: [Select]
    'Add attribute #1
    Dim currentAttribute As EA.Attribute
    attrType = ""
    If row.Count > CONST_ATTRIBUTE1 Then
        attrName = row.Item(CONST_ATTRIBUTE1)
        attrValue = row.Item(CONST_VALUE1)
        If Len(Trim(attrName)) > 0 And Len(Trim(attrValue)) > 0 Then
            stereotype = ""
            Set currentAttribute = eaConn.addOrUpdateAttribute(currentClass, attrName, stereotype, attrValue, attrType)
            currentAttribute.Default = attrValue
            eaConn.addOrUpdateAttributeTag currentAttribute, attrName, attrValue
        End If
    End If


Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8066
  • Karma: +118/-20
    • View Profile
Re: setting attribute.Default = "X" doesn't work
« Reply #6 on: June 22, 2017, 09:37:57 am »
You're not calling Update on the attribute after setting the default. I'm assuming that "all other fields" are being set before update is called inside addOrUpdateAttribute.

You may want to modify addOrUpdateAttribute so that it takes a default as an extra parameter.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: setting attribute.Default = "X" doesn't work
« Reply #7 on: June 22, 2017, 01:29:39 pm »
BTW I can confirm that EA.Attribute.Default actually works.
I've used it in a number of other situations, so there is probably not bug in the API here.

Geert

eric.meredith

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: setting attribute.Default = "X" doesn't work
« Reply #8 on: June 23, 2017, 01:16:38 am »
That was it! I was not calling the .Update method. In my defense, everything else WAS updated without calling .Update - but ... My bad.

Thanks.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: setting attribute.Default = "X" doesn't work
« Reply #9 on: June 23, 2017, 02:02:45 pm »
everything else WAS updated without calling .Update
No it wasn't. The call to update() was just in another operation you called.

Geert

eric.meredith

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
EAConnector in Microsoft Access
« Reply #10 on: July 27, 2017, 02:37:04 am »
I tried to port the Excel code over to Microsoft Access and immediately discovered that EAConnector is not a valid object. Is there a replacement?
Eric

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: setting attribute.Default = "X" doesn't work
« Reply #11 on: July 31, 2017, 11:22:12 pm »
Eric,

The EAConnector is a class VBA class I wrote myself, it's not part of the Sparx API
If yo don't copy it to your Access project then it will obviously not work.

Geert

eric.meredith

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: setting attribute.Default = "X" doesn't work
« Reply #12 on: August 09, 2017, 11:24:01 am »
My bad - I didn't ask the question well.

I have copied the files that I think are necessary, and when I launch Access and create a Module, I can see/load the Enterprise Architect Object Model 2.10. From there I can try to dimension an object as I would have done in Excel.

Dim EAConx as New EAConnector

BUT! In Access, 'EAConnector' is not available, I can only see 'EA' - see attached pic.

What am I missing?

Eric

https://drive.google.com/open?id=0B-iz-xyINOgCZE1XNVplRHhEQ28

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: setting attribute.Default = "X" doesn't work
« Reply #13 on: August 09, 2017, 02:36:13 pm »
You didn't copy the class EAConnector from the Excel VBA environment.
Press F11 in the Excel and inspect all class modules there.

Geert

eric.meredith

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: setting attribute.Default = "X" doesn't work
« Reply #14 on: August 10, 2017, 02:26:05 am »
You were right - I breezed right over the classes in Excel.
Thanks.