Author Topic: Change stereotype in batch  (Read 21443 times)

bITs.EA

  • EA User
  • **
  • Posts: 80
  • Karma: +2/-0
    • View Profile
Change stereotype in batch
« on: April 11, 2016, 09:38:00 pm »
Hi

I want to change the stereotype of a large number of elements. I want to be sure that all the tagged values of the new stereotype are automatically added to all elements. What is the best way to execute this action?

I've found an old topic about this question (http://sparxsystems.com/forums/smf/index.php/topic,23020.msg192337.html), but I want to know if there are already other options in the newer versions of Enterprise architect...

Thanks!

S

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Change stereotype in batch
« Reply #1 on: April 11, 2016, 10:08:22 pm »
I guess the safest way it to use the API, execute a SQLSearch or the like to get the appropriate elements and set the stereotype in the EAElement. That should be safe. Dealing directly with an UPDATE in the database is no good idea since MDG stereotypes will also create entries in t_xref.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13233
  • Karma: +553/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Change stereotype in batch
« Reply #2 on: April 11, 2016, 10:27:41 pm »
If you use Element.StereotypeEx and then update() the element all corresponding tagged values are automatically created.

I think I've seen differences when using Element.Stereotype field.

Geert

bITs.EA

  • EA User
  • **
  • Posts: 80
  • Karma: +2/-0
    • View Profile
Re: Change stereotype in batch
« Reply #3 on: April 12, 2016, 01:09:04 am »
Ok, I'm trying to create a script for executing this action.

1 question: I have a query which selects all needed elements. But the result in the script is an XML formatted string (via Repository.SQLquery()).

What is the easiest way to ge the needed elementID out of a query result? Can I get some sort of "recordset" to cycle through easily?

EDIT: found the solution in topic http://sparxsystems.com/forums/smf/index.php/topic,5665.msg126095.html#msg126095

When the script is ready, I will post it!
« Last Edit: April 12, 2016, 01:27:27 am by bITs.EA »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Change stereotype in batch
« Reply #4 on: April 12, 2016, 06:57:19 am »
Depending on the language you are using select the appropriate DOM parser. There are plenty for all languages available. Then stuff the XML in and you're fine to go.

q.

bITs.EA

  • EA User
  • **
  • Posts: 80
  • Karma: +2/-0
    • View Profile
Re: Change stereotype in batch
« Reply #5 on: April 12, 2016, 07:28:26 am »
Can you give an example of a DOM parser? I never used one, but I want to learn! :)

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Change stereotype in batch
« Reply #6 on: April 12, 2016, 08:16:14 am »
Which language do you use? Basically you stuff the XML in and get a tree structure out which you can easily traverse either iterative or via paths,

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13233
  • Karma: +553/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Change stereotype in batch
« Reply #7 on: April 12, 2016, 01:05:25 pm »
If you just want elements then you can use Repository.GetElementSet(), but if you want other stuff then you'll have to use an xml parser.
Here's an example in vbscript from my VBScript Library

Code: [Select]
'converts the query results from Repository.SQLQuery from xml format to a two dimensional array of strings
Public Function convertQueryResultToArray(xmlQueryResult)
    Dim arrayCreated
    Dim i
    i = 0
    Dim j
    j = 0
    Dim result()
    Dim xDoc
    Set xDoc = CreateObject( "MSXML2.DOMDocument" )
    'load the resultset in the xml document
    If xDoc.LoadXML(xmlQueryResult) Then       
'select the rows
Dim rowList
Set rowList = xDoc.SelectNodes("//Row")

Dim rowNode
Dim fieldNode
arrayCreated = False
'loop rows and find fields
For Each rowNode In rowList
j = 0
If (rowNode.HasChildNodes) Then
'redim array (only once)
If Not arrayCreated Then
ReDim result(rowList.Length, rowNode.ChildNodes.Length)
arrayCreated = True
End If
For Each fieldNode In rowNode.ChildNodes
'write f
result(i, j) = fieldNode.Text
j = j + 1
Next
End If
i = i + 1
Next
end if
    convertQueryResultToArray = result
End Function
Geert

Helmut Ortmann

  • EA User
  • **
  • Posts: 967
  • Karma: +42/-1
    • View Profile
Re: Change stereotype in batch
« Reply #8 on: April 13, 2016, 03:39:27 pm »
Hi,

I think it would be possible to just export the model.

Change the stereotype by text editor and import it to the old or to a new repository. Check. You may have to fiddle out the GUID of the old and new stereotype.

Sometimes export, change with editor, import is a quick way, not only for change.

In the community you find a Search And Replace Addin. It may also help (I'm not sure if it supports change of stereotypes). I have made it a long time ago.

Helmut
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13233
  • Karma: +553/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Change stereotype in batch
« Reply #9 on: April 13, 2016, 03:52:36 pm »
Hi,

I think it would be possible to just export the model.

Change the stereotype by text editor and import it to the old or to a new repository. Check. You may have to fiddle out the GUID of the old and new stereotype.

Sometimes export, change with editor, import is a quick way, not only for change.

In the community you find a Search And Replace Addin. It may also help (I'm not sure if it supports change of stereotypes). I have made it a long time ago.

Helmut

The problem with that approach is that is doesn't take care of the tagged values associated with the stereotype.
Using Element.StereotypeEx will.

Geert

bITs.EA

  • EA User
  • **
  • Posts: 80
  • Karma: +2/-0
    • View Profile
Re: Change stereotype in batch
« Reply #10 on: April 20, 2016, 12:17:52 am »
I have a problem with my script for converting stereotypes. (see code below)

It seems to work perfectly
  • The package is checked out
  • The stereotype is changed (I immediately see the changen in project browser)
  • The package is check in again

BUT when I click on the changed package (which is already checked in again), the stereotype visibly changes to old+new stereotype. (in the properties, the old and new stereotype are checked)

Can anybody explain why this is happening?? And why it first shows the new stereotype and after selecting the package it adds the old one?? (I tested it several times, always same result!)

Thanks for the help!

Code: [Select]
sub main

dim collection as EA.Collection
dim packageElement as EA.Element
dim package as EA.Package
dim diagram as EA.Diagram
dim sql, count, comment
dim fromStereotype, toStereotype
fromStereotype = "Process L3"
toStereotype = "Process"

sql = "SELECT t_object.Object_ID FROM t_object WHERE t_object.stereotype = '" & fromStereotype & "' AND t_object.name LIKE '268.%'"
count = 0

set collection = Repository.GetElementSet(sql, 2) '= collection of Package ELEMENTS (no collection of packages)

for each packageElement in collection
set package = Repository.GetPackageByGUID(packageElement.ElementGUID)

if package.VersionControlGetStatus = 1 then '1 = package is checkedIn
package.VersionControlCheckout("checkout for stereotype change")
end if

'Change stereotype of package element
packageElement.StereotypeEx = toStereotype
if not packageElement.Update() then
Msgbox packageElement.GetLastError()
end if

'Create a message
comment = "stereotype: " & fromStereotype & " changed to " & toStereotype

'Check in package
package.VersionControlCheckin (comment)

next

Msgbox "# = " & collection.Count

end sub

main

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Change stereotype in batch
« Reply #11 on: April 20, 2016, 01:07:03 am »
You don't seem to checkout the parent package.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13233
  • Karma: +553/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Change stereotype in batch
« Reply #12 on: April 20, 2016, 01:09:44 am »
I think I've seen this before.
Have you tried clearing the stereotype, update, and then set the new stereotype?

Geert

bITs.EA

  • EA User
  • **
  • Posts: 80
  • Karma: +2/-0
    • View Profile
Re: Change stereotype in batch
« Reply #13 on: April 20, 2016, 06:08:00 am »
Qwerty, Why should I check out the parent package to change a stereotyoe?

Geert, clearing by setting stereotypeEx = "" or = null? (I'll try it tomorrow)

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Change stereotype in batch
« Reply #14 on: April 20, 2016, 06:34:00 am »
Because if you change a package you need to checkout it's parent! This is absolutely needed. The element is part of its parent, not of itself. Just try it out.

q.