Book a Demo

Author Topic: Objects within a Model  (Read 4621 times)

crd

  • EA User
  • **
  • Posts: 34
  • Karma: +0/-0
    • View Profile
Objects within a Model
« on: May 29, 2010, 03:57:33 am »
Is there any great way to pull only objects that are within a model or do I have to traverse the packages?  I have the id of the Model Package and want to find all requirements within that package.  Since I am relatively new to writing against the EA API, I wanted to make sure I wasn't missing anything.

Model 1
  - Package One
        - Package One.One
                - Requirement One
                - Requirement Two
                - Requirement Three
  - Package Two
Model 2
  - Package One
        - Package One.One
                - Requirement Four
                - Requirement Five
                - Requirement Six
  - Package Two

Thanks,
Carol

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Objects within a Model
« Reply #1 on: May 29, 2010, 04:55:45 am »
Carol,

The easiest way is to use Repository.GetElementSet passing a query as parameter that returns the id's of all objects you are interested in.

Geert

crd

  • EA User
  • **
  • Posts: 34
  • Karma: +0/-0
    • View Profile
Re: Objects within a Model
« Reply #2 on: June 02, 2010, 03:03:37 am »
Thank you for your response Geert.  What I am struggling with is how to write the SQL.  

A statement such as:
SELECT t_object.Name as ReqName FROM t_object WHERE t_object.Object_Type='Requirement'"
will grab all of the Requirements in the Repository.

I have the package id of Model 1 and want any requirements in that package or any of its children, children's children, etc.

The objects returned will only have the value of the immediate parent.  I want to know if it falls under part of the selected tree branch.  

Also, I have been running my SQL using Repository.SQLQuery.  I am guessing there is a time and a place for whether to run Repository.GetElementSet or the Repository.SQLQuery and would love to be enlightened.

Thanks for your help!
Carol

crd

  • EA User
  • **
  • Posts: 34
  • Karma: +0/-0
    • View Profile
Re: Objects within a Model
« Reply #3 on: June 02, 2010, 10:30:44 pm »
Geert,
Would you please post a simple sample of how you are using the the Repository.GetElementSet  command.  I know how to use collections, I just want to see a sample of the the SQL you are using.
Thanks so much!
Carol

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Objects within a Model
« Reply #4 on: June 02, 2010, 11:01:14 pm »
Carol,

I call GetElementSet() whenever I need EA.Elements.
Unfortunately there is no similar operation to get EA.Method, EA.Attribute, EA...., so for those cases I use SQLQuery(), and getMethodByID() or getAttributeByID() for each of the id's returned by SQLQuery()

To get all elements of a package recursively you should normally need a recursive query. For some reason however EA doesn't allow recursive queries to be passed in those operations, so I just use "enough" nested levels to be reasonably sure I have everything.

In this example I have 10 levels of nested packages:
Code: [Select]
string sqlGetElements = @" SELECT o.Object_ID FROM t_object as o
                              left join t_package p on o.Package_ID = p.Package_ID
                              left join t_package p2 on p.Parent_ID = p2.Package_ID
                              left join t_package p3 on p2.Parent_ID = p3.Package_ID
                              left join t_package p4 on p3.Parent_ID = p4.Package_ID
                              left join t_package p5 on p4.Parent_ID = p5.Package_ID
                              left join t_package p6 on p5.Parent_ID = p6.Package_ID
                              left join t_package p7 on p6.Parent_ID = p7.Package_ID
                              left join t_package p8 on p7.Parent_ID = p8.Package_ID
                              left join t_package p9 on p8.Parent_ID = p9.Package_ID

                              where
                              p.package_ID = " + startingPackageID + @"
                              or p2.package_ID = " + startingPackageID + @"
                              or p3.package_ID = " + startingPackageID + @"
                              or p4.package_ID = " + startingPackageID + @"
                              or p5.package_ID = " + startingPackageID + @"
                              or p6.package_ID = " + startingPackageID + @"
                              or p7.package_ID = " + startingPackageID + @"
                              or p8.package_ID = " + startingPackageID + @"
                              or p9.package_ID = " + startingPackageID;
return eaRepository.GetElementSet(sqlGetElements, 2));

Geert

crd

  • EA User
  • **
  • Posts: 34
  • Karma: +0/-0
    • View Profile
Re: Objects within a Model
« Reply #5 on: June 03, 2010, 12:23:37 am »
Geert, thank you!  

So my bright idea this morning was to build an IN clause for my WHERE statement.  I use a routine that takes the selected package and finds all of the child packages by recursively checking Package.packages.  I build these into a comma separated list.

My statement now reads:
SELECT t_object.Name as ReqName FROM t_object WHERE t_object.Object_Type='Requirement' AND t_object.Package_ID In (n,n,n,n,n)  

Thank you for your help  Always learning.  
Carol
« Last Edit: June 03, 2010, 12:27:19 am by crd »