Book a Demo

Author Topic: Proper use of getElementSet  (Read 7259 times)

Chris Tatem

  • EA User
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Proper use of getElementSet
« on: March 11, 2010, 08:18:25 am »
I am trying to use getElementSet to return a collection of elements.  

Quote
Method:
GetElementSet (string IDList, long Options)
Type: Collection
Notes: Returns a set of elements as a collection based on a comma-separated list of ElementID values. By default, if no values are provided in the IDList parameter, all objects for the entire project are returned.
Parameters
· IDList: String - a comma-separated list of ElementID values
· Options: Long - modifies default behaviour of this method
· 1 - Returns empty collection when empty IDList parameter is given.
· 2 - Use IDList string as an SQL query to populate this collection.

The documentation suggests that this should work

Code: [Select]
EA.Collection elementSet = Repository.GetElementSet("289306", 0);
foreach (EA.Element anElement in elementSet)
    { process anElement }

This breaks with a memory error on what should be an empty set (there is no corresponding element with elementID=289306, at least in the t_object table).  The debugger reports the result (elementSet) as having count=1.
This works if there are any elements found - 1 or more.

How do I test for found, not found, or empty?

Changing 0 to 1 as "options" doesn't affect the result.  This is in C#.

(Note, ultimately what I want to do is "select Object_ID from t_object where classifier = n" as the SQL parameter to GetElementSet(sqlstring, 2) to get all the instances of a particular classifier; I have narrowed down my memory problem to an empty set, I didn't see any other quick way to get all the instances of an element)

Thanks
Chris

mrf

  • EA User
  • **
  • Posts: 311
  • Karma: +0/-0
    • View Profile
Re: Proper use of getElementSet
« Reply #1 on: March 11, 2010, 08:55:46 am »
Hi Chris,

Just out of interest, which build of EA are you using? We did fix a problem with GetElementSet in build 851.

The documentation states that if no values are provided in the IDList parameter, all objects for the entire project are returned. However from what I remember that was also the case if the provided Object ID could not be found, or the provided query returned no results.

For large projects, attempting to jam the entire set of objects into an automation collection could forseeably cause an out of memory issue. If you're running a build older than 851 then I recomend upgrading. If you're running build 851 or later, please send us a bug report.
Best Regards,

Michael

[email protected]
"It is more complicated than you think." - RFC 1925, Section 2.8

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Proper use of getElementSet
« Reply #2 on: March 11, 2010, 03:48:13 pm »
Chris,

To temporarily get past the issue with the empty collection you could first use Repository.SQLQuery to test for the existence of the elements you are looking for.
That returns an xml string with the fields you specified in the query.
If there are any results from SQLQuery you could either use GetElementSet, or GetElementByID (since you already have the id's) to get the actual element objects.

Geert

PS. If you are only reading some of the properties of the elements you might even want to skip instantiating the elements all together and work only with the result of SQLQuery. For operations on a large number of elements this will dramatically improve performance.

Chris Tatem

  • EA User
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Proper use of getElementSet
« Reply #3 on: March 12, 2010, 12:07:23 am »
Michael, the memory error I was getting wasn't out of memory and I can certainly understand getting too much back - that is why I was trying to be clever and limit my results, knowing that I didn't care to flip through large sets and do my own filtering... I was getting an invalid memory check - undoubedtly due to my casting the result set as something it really wasn't - that much I understood, but didn't know why.

And I'm running build 833, so will look into upgrading.

Geert - I like your ideas, at least a select count() would allow me to avoid an empty set problem and even better for me right now, the SQLQuery on a subset of attributes to get me going will work (at this point, building a list of these, so few attributes).  I am not (yet anyway) going deep in the properties of an element, but when/if I do I can instantiate it.  My current models aren't large enough to "page through" yet, but certainly could over time if large sets are in my future.

In general I don't like to bypass interfaces like these, for all the obvious reasons, but in this case it's a simple enough query and I won't fool myself into thinking I really have the element instantiated :)  Perhaps I'll create a lightweight, lazily instantiated object wrapping element that provides me a "list" friendly interface (essentially an indentifying string with a key/ID/GUID).

Thanks for the help.

Chris
« Last Edit: March 12, 2010, 12:08:40 am by ctatem »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Proper use of getElementSet
« Reply #4 on: March 12, 2010, 12:26:42 am »
Quote
In general I don't like to bypass interfaces like these, for all the obvious reasons, but in this case it's a simple enough query and I won't fool myself into thinking I really have the element instantiated :)  Perhaps I'll create a lightweight, lazily instantiated object wrapping element that provides me a "list" friendly interface (essentially an indentifying string with a key/ID/GUID).
 
I think that's an excellent idea. I never deal with object from the EA library directly, but always use wrappers.
I've documented the general pattern of my tooling here: http://themodelfactory.org/Pattern:Modelling_Tooling_Framework

Geert

mrf

  • EA User
  • **
  • Posts: 311
  • Karma: +0/-0
    • View Profile
Re: Proper use of getElementSet
« Reply #5 on: March 12, 2010, 08:52:51 am »
Quote
I was getting an invalid memory check - undoubedtly due to my casting the result set as something it really wasn't - that much I understood, but didn't know why.

Ah yes that does ring some bells, I remember now that an invalid object id returned something like a number rather than an empty set. That should all be fixed in build 851+ though, so if you can upgrade I'm pretty positive that it will fix your problems.
Best Regards,

Michael

[email protected]
"It is more complicated than you think." - RFC 1925, Section 2.8