Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Stefan Bolleininger on October 07, 2011, 01:05:40 am
-
Hi,
is there a easy possibility to find elements within a package tree?
i want to find all elements with type requirement wich are somewhere within one package and its packages. I want to try to workaround the auto counter and put down all aliases into the same form.
therefore i have the package name and select my desired aliasname with it.
i start the function manually with an button and want to have all elements called with the same style.
e.g. Normally all elements are known with REQ-XXX
specific for GUI-requirements i want to call them GUI-XXX
the "-" is my seperator for which i look with string.explode but how can i call all my elements?
thanks for answers
-
There are basically two ways to go about:
1. You iterate all package.Elements, and then recurse down to the elements of the package.Packages untill you find what you need. Downside: performance
2. You use an SQL Query to find all the elements you need. Downside: SQL doesn't do recursive, so you'll have to work with a limited number of nesting levels.
You might also want to combine both methods: recursion for find the package id's, and SQL for getting the actual elements.
Geert
-
Hi Geert,
thank you for your answers.
The recursion and iteration through the repository is somehow a little blackbox for me.
Could you give me some lines for these iteration?
How can i sort out the objects.
is : if (aPackage.Element.Type == "Requirement") the right way to check an object WITHIN a package?
This is somehow a bit strange for me :-?
Thank you!
-
It's going to be more like
foreach (EA.Element element in Package.Elements)
{
if (element.Type == "Requirement")
{
//do something with this element
}
}
There's a difference between Package.Element and Package.Elements
Geert
-
Ok, thank you for that.
But what happens if an elemnt is within another package?
How can i iterate through the tree?
i could do that with another for-loop for scanning in a package, but if i got maybe 5 levels deep? how is that possible to solve?
Thanks!
-
Recursion. Please look it up.
Geert
-
Geert, i'm very sorry but i don't get it.....
Mind ran out of memory......Brain dumped
I don't find the way who to iterate ans also search in Forum/EA User Guide doesn't bring up anything for me.
-
Basic programming skill is what Geert means. IOW go to school.
q.
-
Allright, just for this once :-X
Something like this: (untested)
private List<EA.Element> findRequirements(EA.Package package)
{
List<EA.Element> requirements = new List<EA.Element>()
foreach (EA.Element element in Package.Elements)
{
if (element.Type == "Requirement")
{
requirements.Add(element);
}
}
foreach (EA.Package subPackage in package.Packages)
{
requirements.AddRange(findRequirements(subPackage));
}
return requirements;
}
Geert
-
Hi,
after a good night and a good startup today i just thought about and ok, you're right.
i did my correction in this way:
public static void keycorrector(EA.Package keypackage, string keyname)
{
foreach (EA.Element element in keypackage.Elements)
{
if (element.Type == "Requirement")
{
string ziffern = null;
ziffern = element.Alias;
string[] pieces;
pieces = ziffern.Split(new string[] { "-" },StringSplitOptions.None);
element.Alias = keyname + pieces[1];
element.Update();
pieces = null;
}
}
foreach (EA.Package package in keypackage.Packages)
{
keypackage = package;
keycorrector(keypackage, keyname);
}
}
I will look at "addrange" in the next time, what i can do with it.
can you explain me the advantage/disadvantages of both solutions?
Thanks
Sethor
-
My solution just creates a flat list of Requirements, that you then can iterate with a "simple" foreach to do something.
Your solution is more efficient, my solution more generic. You could use my operation to do a number of things with the requirements of a package tree, whereas your solution is only usable for this one alias correction.
Geert