Author Topic: Scripting: convert package element to package  (Read 6452 times)

bITs.EA

  • EA User
  • **
  • Posts: 80
  • Karma: +2/-0
    • View Profile
Scripting: convert package element to package
« on: April 12, 2016, 02:09:12 am »
In the code below, I select objects with a certain stereotype (in reality, all these objects are packages). This results in a collection of elements (which are also packages).

How can I convert the Element to a Package? (Package to Element is possible via "Package.Element", but I can't find the opposite in the manual...)

Code: [Select]
dim collection as EA.Collection

dim sql, count, testTxt
sql = "SELECT t_object.Object_ID FROM t_object WHERE t_object.stereotype = 'ProcessLevel4'"
count = 0
testTxt = ""

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

for each packageElement in collection
'Store name and stereotype of package Element
testTxt = testTxt & vbcrlf & packageElement.Name & "-" & packageElement.StereotypeEx

'Get diagrams in Package

'TO DO: convert Element to Package to get diagrams in package

next

Msgbox "# = " & collection.Count & vbcrlf & vbcrlf & testTxt

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13251
  • Karma: +554/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Scripting: convert package element to package
« Reply #1 on: April 12, 2016, 02:37:28 am »
You can use

Code: [Select]
Dim package as EA.Package
set package = Repository.GePackageByGUID(myElement.ElementGUID)

This has always worked for me, but according to a Sparxian in a post a few days ago you might be better off using

Code: [Select]
set package = Repository.GePackageByID(myElement.Miscdata(0))
(might still contain some syntax errors, haven't tried it out)

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Scripting: convert package element to package
« Reply #2 on: April 12, 2016, 07:01:30 am »
Geert is right. The funny thing is, that the GUID secret was first posted by another Sparxian here. Taking the conservative change policy at Sparx it's probably harmless to stick with the GUID xD Once it wont work it's easy to switch over to the other variant.

It would be nice if the two Sparxians could talk to each other and come up with a common guide line for EA users.

q.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Scripting: convert package element to package
« Reply #3 on: April 12, 2016, 05:23:30 pm »
FWIW, I always use MiscData, never the these-two-GUIDs-are-supposed-to-be-identical thing. It goes against the very idea of a GUID, so it just feels plain wrong.

But also, most structural relationships (parent elements/packages, connector start/end) are expressed with the numeric identifier, not the GUID, so switching to using the GUID all of a sudden makes the code un-neat. (Although several other relationships are represented with the GUIDs, so it does depend on the situation.)

Finally, my knee-jerk reaction is always to stay away from string comparisons as long as I can since integer comparisons are way faster. But to compare an integer with a MiscData field you need a cast, which might end up costing you more. But my gut still says go with the MiscData.


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

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8595
  • Karma: +256/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Scripting: convert package element to package
« Reply #4 on: April 12, 2016, 05:30:27 pm »
FWIW, I always use MiscData, never the these-two-GUIDs-are-supposed-to-be-identical thing. It goes against the very idea of a GUID, so it just feels plain wrong.

[SNIP]

/Uffe
I don't think it does go against the idea of a GUID.  I mean we ARE taking about two views of the same concept.  If the GUID values ARE different, then "that's a bug, " - by definition.

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: 13251
  • Karma: +554/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Scripting: convert package element to package
« Reply #5 on: April 12, 2016, 05:38:19 pm »
Finally, my knee-jerk reaction is always to stay away from string comparisons as long as I can since integer comparisons are way faster. But to compare an integer with a MiscData field you need a cast, which might end up costing you more. But my gut still says go with the MiscData.

It's exactly the issue of the cast that made me switch to compairing guid's.
To make it extra tricky: if you are using SQL, depending on the type of database you can simply use "pdata1 = Package_ID" or you have to use  trick like "
Code: [Select]
pdata1 like Package_ID" when joining.
That isn't an issue when joining using the GUID's

Geert

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Scripting: convert package element to package
« Reply #6 on: April 12, 2016, 08:19:52 pm »
FWIW, I always use MiscData, never the these-two-GUIDs-are-supposed-to-be-identical thing. It goes against the very idea of a GUID, so it just feels plain wrong.

[SNIP]

/Uffe
I don't think it does go against the idea of a GUID.  I mean we ARE taking about two views of the same concept.  If the GUID values ARE different, then "that's a bug, " - by definition.

Paolo
Nnnn... o.
No, I have to take issue with that.

A GUID is supposed to be a globally unique identifier for some concrete thing in realization space, not some abstract concept. In this case, the abstract concept is "a UML package as implemented in EA", but in realization space we're talking about "the EA.Package aspects of a package" and "the EA.Element aspects of a package" which are demonstrably two different types of thing.

If you allow reuse of GUIDs between different types of thing, they cease to be GU. They're just IDs, which have to be contextualized to be interpretable.

Put another way, if they are GUIDs you should be able to do a massive
Code: (SQL) [Select]
select ea_guid from t_package
union select ea_guid from t_object
union select ea_guid from t_attribute
...
and be assured that each entry only appears once.

Otherwise they're not GUIDs at all. They're just... well, ea_guids. :)

All of which is off-topic, btw. Both suggestions in Geert's first post work, the rest is a matter of style.


/Uffe
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: Scripting: convert package element to package
« Reply #7 on: April 12, 2016, 09:11:08 pm »
Uffe, do you really think that Sparxians take general concepts of - say - GUI as granted? Or rules how to design databases and fitting API aka MVC? So the GUID is as Unique as EA.

q.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Scripting: convert package element to package
« Reply #8 on: April 19, 2016, 12:50:13 am »
Out of curiosity I use this great new search in the help and entered MiscData. I got two results. The 2nd for connectors and the first for element which tells nothing but:
Quote
Notes: Read only

This low-level property provides information about the contents of the PData x fields.

These database fields are not documented, and developers must gain understanding of these fields through their own endeavors to use this property.

MiscData is zero based, therefore:

    MiscData(0) corresponds to PData1
    MiscData(1) to PData2

and so on.

Nothing about what actually is stored in these. So  the only "real" way is to loop through packages of the parent and find that with the same element-part.

q.
« Last Edit: April 19, 2016, 12:52:33 am by qwerty »