Author Topic: How to best find the superclass of a given class in script  (Read 2627 times)

StefanBrandmeier

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
How to best find the superclass of a given class in script
« on: November 21, 2023, 04:02:51 am »
Hi community,

I am looking to programmatically find the superclass of a given class in my diagram, trying to do this with the relationships of the class:

         //   find the superclass      
         while ( !connectorsEnumerator.atEnd() )
         {         
            connectorToCheck = connectorsEnumerator.item();
            if(connectorToCheck.Type = "Generalization")
            {
               parentElement = Repository.GetElementByID(connectorToCheck.SupplierID);
               break;
            }
            connectorsEnumerator.moveNext();
         }

Neither ClientID nor SupplierID of the connector seems to point to the superclass. What am I missing ?
Or is there a better way to find the superclass of a related element of a diagram element ?

Thanks for your thoughts on this.

Cheers
Stefan      

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13303
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to best find the superclass of a given class in script
« Reply #1 on: November 21, 2023, 04:46:21 am »
There is a property Element.BaseClasses you can use.

Why are you using the connectorsEnumerator?

Can't you simply loop over the element.Connectors with something like a foreach?

Your logic should be fine otherwise.

Geert

StefanBrandmeier

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: How to best find the superclass of a given class in script
« Reply #2 on: November 21, 2023, 07:41:19 pm »
Hi Geert,

thanks for you quick and competent reply. I will try Element.BaseClasses which will be much simpler of course.
As to the iterator, I guess my JScript skill is pretty basic and I don't yet know all its possibilities. Thanks for the hint.

Cheers
Stefan

StefanBrandmeier

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: How to best find the superclass of a given class in script
« Reply #3 on: November 21, 2023, 09:12:31 pm »
Here's some working code, based on Geerts suggestion, if someone is interested:

         var baseClasses as EA.Collection;
                  
         //   find the superclass, don't consider if multiple superclasses exist.
         baseClasses = relatedElement.BaseClasses;
         if (baseClasses.Count == 1)
         {
            parentElement = baseClasses.GetAt(0);
         }