Author Topic: How to compare objects  (Read 3506 times)

bilon

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
How to compare objects
« on: September 14, 2018, 10:03:16 pm »
I have two variables refencing packages and I want to check if they point to the same package. I'm sure they do. The test "p1.PackageID = p2.PackageID" is true, but the test "p1 Is p2" is false. Why?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: How to compare objects
« Reply #1 on: September 15, 2018, 01:23:56 am »
What do you mean by "variables"?

q.

bilon

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: How to compare objects
« Reply #2 on: September 17, 2018, 08:18:57 pm »
Try something like this, just replace ELEMENT_1 and ELEMENT_2 by real elements lying in the same package:
Code: [Select]
Sub TestObj
Dim pcg1 As EA.Package
Dim pcg2 As EA.Package
Dim coll
Set coll = Repository.GetElementsByQuery("Simple","ELEMENT_1")
Set pcg1 = Repository.GetPackageByID(coll(0).PackageID)
Set coll = Repository.GetElementsByQuery("Simple","ELEMENT_2")
Set pcg2 = Repository.GetPackageByID(coll(0).PackageID)
session.output "pcg1: " & pcg1.Name & ", " & pcg1.PackageID
session.output "pcg2: " & pcg2.Name & ", " & pcg2.PackageID
If (pcg1 Is pcg2) Then
session.output "Packages match"
Else
    session.output "Packages don't match"
End If
If (pcg1.PackageID = pcg2.PackageID) Then
session.output "IDs match"
Else
    session.output "IDs don't match"
End If
End Sub

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to compare objects
« Reply #3 on: September 17, 2018, 08:31:20 pm »
The vbscript Is operator returns true if both variables point to the same object instance.

The API does not have some sort of "single object space" concept.
So each time you use Repository.GetPackageByID() you get a different object instance, possibly representing the same package in the model.

So you can either implement your own single object space using Dictionaries or something like that, or just compare the object instances with their unique identifier.
For packages you can use the PackageID or the PackageGUID for that purpose.

If case you are using the PackageID, please not that each object type (=>table in the repository) has it's own ID range. This means you can have a package with ID = 120, but also an element with ID 120 and a connector with ID 120.

If GUID's are unique across object types, except for the packages and the elements that represent that same package. Those always have the same GUID.

So if you were building your own "cache" of all objects you have used you could use a single Dictionary if you use the GUID. If you use the ID then you'll have to create a dictionary for each object type.

Geert

bilon

  • EA User
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
Re: How to compare objects
« Reply #4 on: September 17, 2018, 10:17:33 pm »
Thanks for your reply, now I understand. I already use comparison by PackageIDs, but I thought, it would be just a workaround. Now I see, it's final solution  :-)