Search code examples
c#visual-studioadd-inenterprise-architect

Iterating through hierarchy of packages in Enterprise Architect using C#


I have to iterate and collect upto 5 levels of packages from Enterprise Architect. There are various such level inside the main root package. Till now i have been using recursive way like below:

public void GetAllPackages(EA.Package dumpPackage)
{
    foreach (EA.Package packages in dumpPackage.Packages)
    {
        //Save that package in object
        GetAllPackages(packages);
    }
}

But now since the data is more this loop is taking a lot of time nearly 1 minute to complete. Is there any faster way to iterate and collect all these packages.


Solution

  • What you can do is to get all packages in s single query with

    Repository.SQLQuery("SELECT package_id, parent_id FROM t_package")
    

    Now place them in a hash so you have a tree with all children of a package referenced. That is, each entry receives a list of child packages so you can easily traverse this tree for any package you need.

    A recursive SQL is not possible (at least in the SQL dialects I know). But the above will for sure be much faster since the bottleneck are the repetitive SQLs for single packages EA will issue during a recursive traverse.

    Alternative 1 This solution requires that you create a query with the SQL query builder:

    SELECT pkg.package_id FROM t_object o, t_package pkg
    WHERE
      pkg.Package_id in (#Branch#)
      AND o.Package_ID = pkg.package_id
      AND o.Object_Type = 'Package'
    

    Now you need to focus on the package you want to recurse and then run the search with

    Respository.GetElementsByQuery (<QueryName>, "")
    

    where <QueryName> is the name of your query.

    I have no idea how this will improve performance. The #Branch# macro will be evaluated internally in EA and I don't know how this is done. Hopefully it is faster than a script recursion. Just try the above query manually and see if it's fast enough.

    Alternative 2 You could use a SQL with a fixed number of JOINs to dive into a number of levels like in

    t_object INNER JOIN t_package AS ObjectParent ON  t_object.package_id = ObjectParent.package_id)
    LEFT JOIN t_package AS PackageParent1 ON (ObjectParent.Parent_ID = PackageParent1.package_id))
    LEFT JOIN t_package AS PackageParent2 ON (PackageParent1.Parent_ID = PackageParent2.package_id))
    

    But that's rather RDBMS-dependent and you have to try out yourself.