Book a Demo

Author Topic: Creating new element falis with C#  (Read 5782 times)

Ulrich Schemmel

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Creating new element falis with C#
« on: December 18, 2008, 03:54:58 am »
I tried to create a new requirement using the automation interface.
The requirement gets created, but the cast (see code below) falis.
Has anybody an idea what's wrong here?

            EA.Package aPackage = Repository.GetTreeSelectedPackage();
            object o = aPackage.Elements.AddNew("Test1", "Requirement");
            aPackage.Elements.Refresh();
            EA.Requirement rq = (EA.Requirement)o;
            rq.Update();

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Creating new element falis with C#
« Reply #1 on: December 18, 2008, 09:03:39 am »
Please disregard my post.  I obviously didn't really read the code...  :-[

You'll need to update the element (o) before you can add a requirement.
« Last Edit: December 22, 2008, 08:23:13 am by simonm »

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Creating new element falis with C#
« Reply #2 on: December 18, 2008, 09:20:22 am »
Also, an EA.Requirement object is not the same as an EA.Element object of type "Requirement".  You should not try to cast it in this way.

An Element of type "Requirement" is referred to as an External Requirement, while object type EA.Requirement is for Internal Requirements (also called Responsibilities).  EA.Requirement objects are created using the Element.Requirements collection.

To understand the difference between internal and external requirements, see the following links:
http://www.sparxsystems.com/uml_tool_guide/what_is_uml_modeling/internalrequirements.html
http://www.sparxsystems.com/uml_tool_guide/what_is_uml_modeling/externalrequirements.html

To create a new Requirement Element (external requirement):

Code: [Select]
EA.Package aPackage = Repository.GetTreeSelectedPackage();
EA.Element element = (EA.Element)aPackage.Elements.AddNew("Test1", "Requirement");
element.Update();

To create an internal requirement:

Code: [Select]
EA.Element element = [...]
EA.Requirement req = (EA.Requirement)element.Requirements.AddNew("Test1", "Functional");
req.Update();

Ulrich Schemmel

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Creating new element falis with C#
« Reply #3 on: December 18, 2008, 07:10:50 pm »
Hi Aaron,

thank you for the fast reply!
I ran the following code (see below).
The update works but the cast in the last line falis with an "InvalidCastException":
Unable to cast COM object of type 'System.__ComObject' to interface type 'EA.Requirement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{74AFC4B3-8593-4EB4-90DE-AA05DA85D4AD}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Do you have any idea whats wrong here?

Best Regards
Ulli

Code: [Select]
EA.Package aPackage = Repository.GetTreeSelectedPackage();
EA.Element element = (EA.Element)aPackage.Elements.AddNew("Test1", "Requirement");
element.Update();
EA.Requirement rq = (EA.Requirement)element;

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Creating new element falis with C#
« Reply #4 on: December 19, 2008, 09:01:44 am »
Please read my previous response again.

You cannot cast an EA.Element object as EA.Requirement.  An EA.Element object of type "Requirement" is not the same as an EA.Requirement object.

Ulrich Schemmel

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Creating new element falis with C#
« Reply #5 on: December 19, 2008, 07:24:09 pm »
Ok, so I have created an External Requirement that is not of type EA.Requirement. What’s a bit curious here is that I requested the Elements-collection to create an "Requirement" and if I ask the newly created element what type it is by using the property "Type" it says that it’s a "Requirement". But form your comment I have learned that it is not.  :-/ Ok, anyway, what do I have to do if I want to access the newly created "External Requirement"? What EA type is it? I did not found an EA.ExternalRequirement or something similar.

Best Regards
Ulrich

Frank Horn

  • EA User
  • **
  • Posts: 535
  • Karma: +1/-0
    • View Profile
Re: Creating new element falis with C#
« Reply #6 on: December 19, 2008, 07:43:33 pm »
Quote
Ok, anyway, what do I have to do if I want to access the newly created "External Requirement"? What EA type is it?

It's of type EA.Element, that's all. From a UML point of view it's a class.

I suppose what you actually want are the requirement specific properties, like Status, Difficulty, or Priority.

I'm not using the automation interface very much and I have not tried this out, but I would look for them in the EA.Element.CustomProperties collection. If you don't find them there, you could still check the even more obscure EA.Element.Properties collection and the EA.Element.MiscData string. None of this is properly documented, so you may want to pester EA with support requests as well in case you have a support subscription.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Creating new element falis with C#
« Reply #7 on: December 20, 2008, 03:50:16 am »
Yes, an External Requirement is simply an EA Element. The Type attribute of the element - that is, o.Type in the case here - will return the string "Requirement" if the element represents an external requirement.

Once you've established that you have the correct element, then you can find such things as difficulty, priority and status by reading the o.Difficulty, o.Priority and o.Status attributes. These attributes can be populated for other elements - those that do not represent external requirements - so make sure you confirm that you are actually referencing the correct element.

Check the Type and other attributes in the EA documentation. Look in the SDK under Element, then go to the subsection for Element. Yes, that is an intentional repetition; see below.

HTH, David

PS: If you are dealing with 'internal' requirements, then these are properties of a 'parent' element. They are returned from the Requirements collection of an element. You'll find this stuff documented in the Element Features section of the EA help, once again under the Element heading (a sibling of the above-mentioned Element subsection).
No, you can't have it!

Ulrich Schemmel

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Creating new element falis with C#
« Reply #8 on: January 14, 2009, 07:27:08 pm »
Ok, now I understand how it works.
Anyhow I think it would be a better solution if you would implement all the classes (concepts) you are dealing with in the interface. It would be better to have the classes "ExternalRequirement" and "InternalRequirement" even if they are empty. Better for understanding an for extending the model in future. Imagine the case where you have to extend the concept "InternalRequirement" with some aditional properties. Either you have to put these properties in the "Element" class whats ugly or you have to introduce the class "InternalRequirement" what will break your custmers code.

Regards
Ulrich