Author Topic: Problem with Update method  (Read 10227 times)

bilon

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Problem with Update method
« on: May 03, 2018, 12:02:22 am »
I have simple script changing single property of elements attribute and subsequently iterating through elements attributes to display the new value. But the old value is displayed. Here is the script:

Sub Main
   Dim tab AS EA.Element
   Dim col AS EA.Attribute
   For Each tab In Repository.GetTreeSelectedElements
      For Each col In tab.Attributes
         If col.Name = "TABLE_NAME" Then
            Select Case col.Type 
               Case "varchar"
               col.Type = "char"
               col.Update
               Session.Output "1: " & col.Name & " " &  col.Type
               '1: TABLE_NAME char
            Case Else   
            End Select
         End If      
      Next
      'tab.Attributes.Refresh
      For Each col In tab.Attributes
         If col.Name = "TABLE_NAME" Then
            Session.Output "2: " & col.Name & " " &  col.Type
            '2: TABLE_NAME varchar
         End If
      Next
   Next
End Sub

The new value is correctly saved to model, so why it is not displayed in the script?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Problem with Update method
« Reply #1 on: May 03, 2018, 12:17:46 am »
You need a Refresh of the collection in between the 2 loops.

q.

bilon

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: Problem with Update method
« Reply #2 on: May 03, 2018, 07:41:56 pm »
As you can see, Refresh is already there. But commented, because it doesn't help.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Problem with Update method
« Reply #3 on: May 03, 2018, 08:47:32 pm »
I think you need to
    tab.Refresh()
as well.

Quote from: User Guide link=https://www.sparxsystems.com/enterprise_architect_user_guide/13.5/automation/element2.html
Notes: Refreshes the element features in the Project Browser.

Usually called after adding or deleting attributes or methods, when the user interface is required to be updated as well.

/U
My theories are always correct, just apply them to the right reality.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Problem with Update method
« Reply #4 on: May 03, 2018, 10:39:12 pm »
I might be wrong, but the refresh.line seems to be commented out.

q.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Problem with Update method
« Reply #5 on: May 04, 2018, 05:12:04 pm »
Yes, but he's refreshing the Collection, not the Element. According to the API documentation, you need to refresh the element after adding attributes.

/U
My theories are always correct, just apply them to the right reality.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Problem with Update method
« Reply #6 on: May 04, 2018, 06:46:50 pm »
I shouldn't lean in too much without testing, but refresh is to update a collection. He's changing elements of a collection and you need to tell EA to get the updates since it's using a cache of the old values. Since the refresh is commented in his code, this must be the culprit.

q.

bilon

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: Problem with Update method
« Reply #7 on: May 04, 2018, 11:31:00 pm »
The reason why tab.Attributes.Refresh is commented, is, that tested it but it didn't work, so I commented it. Even tab.Refresh doesn't work!

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Problem with Update method
« Reply #8 on: May 05, 2018, 01:14:57 am »
Well, I just ran your script and it worked with NO issues.

q.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Problem with Update method
« Reply #9 on: May 07, 2018, 08:00:28 pm »
I shouldn't lean in too much without testing, but refresh is to update a collection.
I know, but it's implemented by other classes as well, and the user guide (as quoted in my first post) explicitly states that you need to Refresh() the Element itself after adding attributes.

Well, I just ran your script and it worked with NO issues.

.... and that's the end of this argument. :)

/U
My theories are always correct, just apply them to the right reality.

bilon

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: Problem with Update method
« Reply #10 on: May 10, 2018, 10:22:59 pm »
What do you mean with "worked with no issues"? Does it mean, that the output looked like this:
1: TABLE_NAME char
2: TABLE_NAME char

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Problem with Update method
« Reply #11 on: May 11, 2018, 12:20:40 am »
Lucky you, it was still in my output window:
Quote
1: TABLE_NAME char   
2: TABLE_NAME varchar   
1: TABLE_NAME char   
2: TABLE_NAME varchar   

(output from 2 runs).

q.

bilon

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: Problem with Update method
« Reply #12 on: May 16, 2018, 01:01:18 am »
In this case, it isn't definitely without issues. The issue is, that after setting datatype to "char" in step 1, it is displayed as "varchar" in step 2. They should be same.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Problem with Update method
« Reply #13 on: May 16, 2018, 04:36:44 am »
Ah, now I see. Unfortunately you reply here in long distances so I'm out each time. I see that the 2nd loop shows the non-updated values. But EA is doing the change (this is why I posted that it works correctly) since afterwards the types ARE changed. So the question is: why does the 2nd loop show the wrong values. Hmm. Tricky indeed.

q.
« Last Edit: May 16, 2018, 06:06:26 pm by qwerty »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Problem with Update method
« Reply #14 on: May 16, 2018, 04:55:39 am »
Replacing the 2nd loop like this
Code: [Select]
     dim tab1 as EA.Element
     set tab1 = Repository.GetElementByID(tab.ElementID)
     For Each col In tab1.Attributes
         If col.Name = "TABLE_NAME" Then
            Session.Output "2: " & col.Name & " " &  col.Type
            '2: TABLE_NAME varchar
         End If
      Next
fixed the issue. Honestly, I never ever used a collection more than once. This Refresh-thingy is just fishy in my opinion. And obviously it's broken too. Feel free to send a bug report.

q.

P.S. Probably the Refresh has only an effect if you added or removed elements from it. In any case it's broken.
« Last Edit: May 16, 2018, 06:09:46 pm by qwerty »