Book a Demo

Author Topic: Recursive run down all elements in a package  (Read 12319 times)

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Recursive run down all elements in a package
« on: February 22, 2016, 03:44:08 am »
Hi,

Is there a simple way to get all the decedent elements underneath a specific package recursively?
i.e. all the elements, classes, diagrams or other packages underneath a package in one command?

Thanks.


qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Recursive run down all elements in a package
« Reply #1 on: February 22, 2016, 03:57:50 am »
You can create a somewhat recursive SQL, but not all engines support that and it's tricky too. It's easier to write a short script to create the result set. The query builder has an interface to process results from a script.

q.

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Re: Recursive run down all elements in a package
« Reply #2 on: February 22, 2016, 07:32:56 am »
do you have a short example for packages drill down from top down?

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Re: Recursive run down all elements in a package
« Reply #3 on: February 22, 2016, 08:45:36 am »
I guess if there is no short way to do it,
I managed to run on each group separately.

Another small issue I have is moving a cloned package from where it was created to another place.
I tried 2 options which did not work:
1. Setting the clonePackageId to new packageId but I cant replace read only parameter.
2. Used this query:
string sql = "update t_object set package_id = " + packageId2 + " where package_id = " + clonedPackageId;
repository.SQLQuery(sql);
got an error that an update can not be performed and select is needed
Is there a good 3rd option?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Recursive run down all elements in a package
« Reply #4 on: February 22, 2016, 02:40:21 pm »
This is the fastest way I know.
It may seem like lot of code, but it is significantly faster then iterating all .Elements collections recursively.

See also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Projects/Project%20E/General%20Scripts/Util.vbs

Code: [Select]
'returns all elements in the package tree (so elements in the package and all subpackages recursively)
function getAllElementsInPackageTree(package)
dim packageList
set packageList = getPackageTree(package)
dim packageIDString
packageIDString = makePackageIDString(packageList)
dim getElementsSQL
getElementsSQL = "select o.Object_ID from t_object o where o.Package_ID in (" & packageIDString & ")"
dim elements
set elements = getElementsFromQuery(getElementsSQL)
set getAllElementsInPackageTree = elements
end function

'returns an ArrayList of the given package and all its subpackages recursively
function getPackageTree(package)
dim packageList
set packageList = CreateObject("System.Collections.ArrayList")
addPackagesToList package, packageList
set getPackageTree = packageList
end function

'add the given package and all subPackges to the list (recursively
function addPackagesToList(package, packageList)
dim subPackage as EA.Package
'add the package itself
packageList.Add package
'add subpackages
for each subPackage in package.Packages
addPackagesToList subPackage, packageList
next
end function

'make an id string out of the package ID of the given packages
function makePackageIDString(packages)
dim package as EA.Package
dim idString
idString = ""
dim addComma
addComma = false
for each package in packages
if addComma then
idString = idString & ","
else
addComma = true
end if
idString = idString & package.PackageID
next
'if there are no packages then we return "0"
if packages.Count = 0 then
idString = "0"
end if
'return idString
makePackageIDString = idString
end function

'returns an ArrayList with the elements accordin tot he ObjectID's in the given query
function getElementsFromQuery(sqlQuery)
dim elements
set elements = Repository.GetElementSet(sqlQuery,2)
dim result
set result = CreateObject("System.Collections.ArrayList")
dim element
for each element in elements
result.Add Element
next
set getElementsFromQuery = result
end function

Also Repository.SQLQuery is only for readonly queries. You should set the parentID on the cloned package to move it to another location.

Geert

Helmut Ortmann

  • EA User
  • **
  • Posts: 970
  • Karma: +42/-1
    • View Profile
Re: Recursive run down all elements in a package
« Reply #5 on: February 23, 2016, 04:50:41 pm »
Hi,

with Model Search (SQL Search) in EA its pretty easy:


Code: [Select]
select * from t_object where t_object.Package_ID in (#Branch#)

Insert the SQL code in the Model Search, select a package in the browser and run the search.

See also user guide / online help: http://sparxsystems.com/enterprise_architect_user_guide/12.1/building_models/creating_filters.html

Regards,

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

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Re: Recursive run down all elements in a package
« Reply #6 on: February 26, 2016, 03:30:10 am »
Thanks Helmut and Geert.
That solved my issues.

Tzafrir