Book a Demo

Author Topic: Access generated composite's encapsulated objects?  (Read 5942 times)

Chris Cortes

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Access generated composite's encapsulated objects?
« on: February 24, 2009, 07:04:22 am »
(In follow-up to my other post about generating useable composites:
http://www.sparxsystems.com/cgi-bin/yabb/YaBB.cgi?num=1234932050)

Given API's limitations regarding composites & keeping in mind that I don't want to manually hack the repository:

What is the best way to create a composite, while also having access to it's encapsulated objects?

Is that even possible? I ask because my encapsulated object count: Package.Element.Elements.Count is always coming back: == 0.

Advanced thanks again all.
- Chris


Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Access generated composite's encapsulated obje
« Reply #1 on: February 24, 2009, 08:08:49 am »
Quote
... my encapsulated object count: Package.Element.Elements.Count is always coming back: == 0.
Use the Package.Elements collection instead.  For a package the Element.Elements collection will always be empty.

Chris Cortes

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Access generated composite's encapsulated obje
« Reply #2 on: February 24, 2009, 08:49:35 am »
Hmm, maybe I wasn't clear. The object was added to the element using element.elements.addnew() not  the package.  I should query the package?



Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Access generated composite's encapsulated obje
« Reply #3 on: February 25, 2009, 08:37:16 am »
The element of a package should never have sub elements.  The elements that a package owns should belong to the packages elements collection.  So you should add your sub elements directly to the package.

I suspect that if EA allows you to add elements to the package element, it will create data integrity issues (regardless of whether the integrity checker picks it up or not.)

But saying that won't really help you.  Maybe if you described a bit more of what you're actually trying to do, I (or someone else) can offer suggestions of other ways to do it.
« Last Edit: February 25, 2009, 08:37:57 am by simonm »

Chris Cortes

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Access generated composite's encapsulated obje
« Reply #4 on: February 25, 2009, 06:16:00 pm »
Thanks Simon, you were correct.  

This is generally what I did (from memory):
Code: [Select]
//argElement is an EA.Element that is passed into this static method

EA.Element thisElement;
thisElement = (EA.Element)argElement;

var addElement = (EA.Element)thisElement.Elements.AddNew("someName","someType");

_updateThis = addElement.Update();
thisPackage.Refresh();

So when I queried: element.elements.count, I always got 0.

Turns out that the element was added to the package, and querying package.elements.count and filtering by type is the way to go. Thanks.

So, why is the Element.Elements.Count even there?

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Access generated composite's encapsulated obje
« Reply #5 on: February 26, 2009, 12:15:09 am »
Hi Chris,

I suspect this is there because some elements can own others. For example, a Class may be the parent of another (child) Class.

Due to the way EA handles a Package internally, there is an Element that corresponds to each Package. This is where most of the 'core' attributes - those that are common across UML elements - of the Package 'element' are stored.

However, because a Package has a much more 'powerful' (for lack of a better word) paradigm for ownership, and due to the way EA relies on packages to structure projects, the ownership of child elements is handled by the Package element itself rather than its corresponding Element.

If you were coding against an non-package element (say a Class) then you might well want to query the Class to iterate over its child elements.

That leaves the question of whether the children of an Element (not a Package) can be found at all. The answer is yes. Note that an Element has two attributes to track this, PackageID and ParentID. In the EA help you will see how these are handled.

Note as well that the children of an parent Element (again, one that is not a Package) are included in the Elements collection of the Package that owns the parent element.

Better now?
No, you can't have it!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Access generated composite's encapsulated obje
« Reply #6 on: February 26, 2009, 08:01:34 am »
Well said, and exactly right.