Book a Demo

Author Topic: iterate throught tree and find elements (  (Read 8060 times)

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
iterate throught tree and find elements (
« 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

Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13471
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: iterate throught tree and find elements (
« Reply #1 on: October 07, 2011, 06:43:52 pm »
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

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: iterate throught tree and find elements (
« Reply #2 on: October 07, 2011, 07:22:11 pm »
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!
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13471
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: iterate throught tree and find elements (
« Reply #3 on: October 07, 2011, 08:33:38 pm »
It's going to be more like

Code: [Select]
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

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: iterate throught tree and find elements (
« Reply #4 on: October 07, 2011, 10:31:28 pm »
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!
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13471
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: iterate throught tree and find elements (
« Reply #5 on: October 08, 2011, 12:10:37 am »
Recursion. Please look it up.

Geert

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: iterate throught tree and find elements (
« Reply #6 on: October 08, 2011, 04:11:48 am »
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.
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: iterate throught tree and find elements (
« Reply #7 on: October 08, 2011, 06:36:59 pm »
Basic programming skill is what Geert means. IOW go to school.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13471
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: iterate throught tree and find elements (
« Reply #8 on: October 08, 2011, 11:12:05 pm »
Allright, just for this once :-X
Something like this: (untested)
Code: [Select]
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

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: iterate throught tree and find elements (
« Reply #9 on: October 09, 2011, 04:55:44 am »
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:

Quote
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
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13471
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: iterate throught tree and find elements (
« Reply #10 on: October 09, 2011, 05:11:41 pm »
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