Author Topic: Get All Requirements From Use Case using C#  (Read 8029 times)

Andre Rezende

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Get All Requirements From Use Case using C#
« on: June 25, 2012, 11:25:41 pm »
Hi everyone.
  I did the follow code:
Code: [Select]
 Collection reqs = elUseCase.Requirements;
                    foreach (Requirement req in reqs)
                    {
                        String x = req.Name;
                    }
 
But the Element "elUseCase" did NOT get my linked Requirements. What´s wrong? I would like to know all linked requirements that use case has attached.
Thaks

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #1 on: June 25, 2012, 11:32:21 pm »
The requirements collection is not the linked requirements (see my scripting book). Instead you need to iterate over the connectors colletion to find the opposite requirement classes.

q.

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #2 on: June 26, 2012, 12:04:59 am »
You should look to the following method:
Code: [Select]
string connected_elements = element.GetRelationSet(EA.EnumRelationSetType.rsRealizeEnd);

Element.GetRelationSet may help you with some connections id does NOT allow all element connections.

If you want all connectors you will need to query for them.

Code: [Select]
"Select * FROM t_connector WHERE End_Object_ID =" + element.ElementID +"

Best regards,

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

Andre Rezende

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #3 on: June 26, 2012, 12:20:02 am »
I would like to get all requirements associated with an use case.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #4 on: June 26, 2012, 01:27:22 am »
Use the methods Stefan suggests.
Quote
string GetRelationSet (short Type) — Returns a string containing a comma-separated list of EaElement.ElementIDs of directly and indirectly related elements based on the given Type.
Recurses using the same relation type on all elements it finds, retrieving all dependencies and sub-dependencies of the current element. For example, Object1 depends on Object2, which depends on Object3, therefore this method returns Object2 and Object3.
To obtain only the direct relationships of the element, use the Connectors collection instead.
From my scripting book.

q.

JavierD

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #5 on: June 27, 2012, 10:51:36 pm »
Hi!

I'm having the same problems as descibed above...

I have tried itterating through both the Requirements and the Connectors associated with the Element.

I'm just interested in the Requirements associated with my UseCase, how do i obtain that information?
With regards, Javier

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get All Requirements From Use Case using C#
« Reply #6 on: June 27, 2012, 11:19:59 pm »
Javier,

The Connectors connect your use case to the requirements (which themselves are elements).
The connector contains the object_id's of both source and target.
You then have to get the your requirement from the repository using Repository.GetElementByID (long ElementID)

Geert

JavierD

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #7 on: June 27, 2012, 11:39:30 pm »
Geert,

I have been looking through the API dokumentation for the Automation Interface... Which of the Connector attribut is it that i need to use to get the the get the Requirements that are vissible in the UseCase properties windows in EA, ClientEnd or ClientId?

Thanx in advance.
Regards //Javier

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get All Requirements From Use Case using C#
« Reply #8 on: June 27, 2012, 11:42:49 pm »
You'll probably need either clientID or supplierID
depends on which is the source and which is the target of your connector.
What I would do is check the id of one, if it's the ID of your use case then you should look at the other end.

Geert

JavierD

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #9 on: June 27, 2012, 11:51:25 pm »
Ok, I'll try that.

Thanx for your help!

Regards //Javier

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #10 on: June 28, 2012, 04:10:03 pm »
Some idea about it, where i played around the dependency-tracing:

Code: [Select]
public bool traceschleife(EA.Package reqpackage, EA.Repository _Repo, String oType)
        {
            foreach (EA.Element element in reqpackage.Elements)
            {
                if (element.Type == oType)
                {
                    string upcome = "";
                    string test = "";
                    try
                    {
                        string rsquery = ea.ENARTalis_main.xmlnode(_Repo.SQLQuery("Select Start_Object_ID FROM t_connector WHERE End_Object_ID = " + element.ElementID.ToString() + ""), "</Start_Object_ID>");
                        if (rsquery != "")
                        {
                            string[] partof = rsquery.Split(new Char[] { ',' });
                            foreach (string s in partof)
                            {
                                string old = upcome;
                                string testold = test;
                                EA.Element elas = _Repo.GetElementByID(Convert.ToInt32(s));
                                if (elas.Type == "Requirement")
                                {
                                    upcome = old + " " + elas.Alias.ToString();

                                    try
                                    {
                                        string twoquery = ea.ENARTalis_main.xmlnode(_Repo.SQLQuery("Select Start_Object_ID FROM t_connector WHERE End_Object_ID = " + elas.ElementID.ToString() + ""), "</Start_Object_ID>");
                                        if (twoquery != "")
                                        {
                                            string[] parttwo = twoquery.Split(new Char[] { ',' });
                                            foreach (string s2 in parttwo)
                                            {
                                                string oldtwo = upcome;
                                                string testtwo = test;

                                                EA.Element elas2 = _Repo.GetElementByID(Convert.ToInt32(s2));
:

To get out the informations about all connected elements I used a string-split method within ea.ENARTalis_main.xmlnode which has to be build by your own :-)
Afterwards you may use an generic iteration about the elements.

It was just an idea about it and not very fast, however it works.

Maybe you want to use something or correct the code/simplify :-)

Have fun,

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

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get All Requirements From Use Case using C#
« Reply #11 on: June 28, 2012, 04:26:45 pm »
If you are looking for something like that: I have similar code in my Add-In framework in the Model class:
https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EAWrappers/Model.cs
Code: [Select]
   /// <summary>
    /// returns the elementwrappers that are identified by the Object_ID's returned by the given query
    /// </summary>
    /// <param name="sqlQuery">query returning the Object_ID's</param>
    /// <returns>elementwrappers returned by the query</returns>
    public List<ElementWrapper> getElementWrappersByQuery(string sqlQuery)
    {
      // get the nodes with the name "ObjectID"
      XmlDocument xmlObjectIDs = this.SQLQuery(sqlQuery);
      XmlNodeList objectIDNodes = xmlObjectIDs.SelectNodes("//Object_ID");
      List<ElementWrapper> elements = new List<ElementWrapper>();
      
      foreach( XmlNode objectIDNode in objectIDNodes )
      {
            ElementWrapper element = this.getElementWrapperByID(int.Parse(objectIDNode.InnerText));
        if (element != null)
        {
              elements.Add(element);
        }
      }
      return elements;
    }

If you call that operation using the query
Code: [Select]
select c.End_Object_ID as Object_ID from t_connector c
where c.Connector_Type = 'Realisation'
and c.Start_Object_ID = <myUseCaseID>
union
select c.Start_Object_ID as Object_ID from t_connector c
where c.Connector_Type = 'Realisation'
and c.End_Object_ID = <myUseCaseID>
(or use both queries separately if the union is problematic)
you should be able to get the required objects rather quickly.
This might be faster then simply looping all connectors, especially if you have a lot of connectors.

Geert
« Last Edit: June 28, 2012, 04:27:28 pm by Geert.Bellekens »

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: Get All Requirements From Use Case using C#
« Reply #12 on: June 28, 2012, 04:46:18 pm »
Hi,

I use your framework to add the last elements history list  ;)

This is nearly identical with my idea, but we are different from the startpoint:
You are selecting one element als start, while i use all elements from a package.

If you are not iterating down a few more levels, your generic solution my be quite fast - you don't need to seperatly call up all elements, they are already listed.

maybe i should take a closer look to your wrappers :-)

Greetings

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

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get All Requirements From Use Case using C#
« Reply #13 on: June 28, 2012, 05:01:17 pm »
Quote
This is nearly identical with my idea, but we are different from the startpoint:
You are selecting one element als start, while i use all elements from a package.
Well, that's the beauty about SQL, in that case you just change your query
 
Code: [Select]
select c.End_Object_ID as Object_ID from t_connector c
inner join t_object o on c.Start_Object_ID = o.Object_ID
                                    and o.Object_Type = 'UseCase'
where c.Connector_Type = 'Realisation'
and o.Package_ID = <MyPackageID>
union
select c.Start_Object_ID as Object_ID from t_connector c
inner join t_object o on c.End_Object_ID = o.Object_ID
                                    and o.Object_Type = 'UseCase'
where c.Connector_Type = 'Realisation'
and o.Package_ID = <MyPackageID>
No need to change any C# code.

Geert