Author Topic: Finding Diagrams and Elements by name  (Read 6102 times)

Adler

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Finding Diagrams and Elements by name
« on: October 22, 2013, 08:13:18 pm »
Hell Community,

is there a way access elements and diagrams by their name in scripts?

I have checked the Repository class but it only seems to offer ways to access the elements by the id resp.  uuid.

Greetings
Adler

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Finding Diagrams and Elements by name
« Reply #1 on: October 22, 2013, 08:21:28 pm »
You need to traverse the package tree and the included collections for elements and diagrams (for elements you need to recurse their embedded elements) and check the name. Some collections offer a GetByName, but I would not make use of them since the name is not unique and you get no idea which element is returned for non-unique names.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13288
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Finding Diagrams and Elements by name
« Reply #2 on: October 22, 2013, 08:33:40 pm »
What I usually do in such circumstances is use Repository.SQLQuery to get the ID(s) of the things I need, and then use Repository.Get<thing>ByID to actually retrieve the object.

Geert

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8064
  • Karma: +118/-20
    • View Profile
Re: Finding Diagrams and Elements by name
« Reply #3 on: October 23, 2013, 08:50:56 am »
Adler, you may find GetElementsByQuery useful.

Use the search 'Element Name'.

Geert, look up GetElementSet.

Unfortunately, neither works for anything except elements.
« Last Edit: October 23, 2013, 08:52:36 am by simonm »

Adler

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Finding Diagrams and Elements by name
« Reply #4 on: October 25, 2013, 12:46:08 am »
Hi,

and thanks to all for the suggestions.

@qwerty
I assume that by "traversing the package tree" you mean something like a couple of nested while loops for checking the whole model entity-by-entity. If so, then id like to avoid that if possible. In bigger models this could become kind of messy. For searching I find that expression-based solutions like SQL or XPath look a lot better in code. But of course its a potential solution and also a matter of personal preference I think.
 
@Geert
That sounds promising, but I'm a bit confused. The EA help file says about the SQLQuery method:

"Enables execution of a SQL select statement against the current repository"

So I assume that there is a "Repository" table somewhere in the guts of the EA, try:

dim container
container = Repository.SQLQuery("SELECT * FROM Repository;")

and get the error message:
"... cannot find input table or query 'Repository' ... "

Are there any preparation steps to do before I can apply the SQLQuery?

@Simon M
Yes, the GetElementsByQuery is a useful method for fast searches. But as qwerty says, the problem about the element name is that its not unique. So I'd prefer something that allows me to include further parameters (package names, diagram names or maybe even stereotypes and attribute values) into my searching expression.



qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Finding Diagrams and Elements by name
« Reply #5 on: October 25, 2013, 05:35:41 am »
You would need something like
Code: [Select]
SELECT Object_ID FROM t_object WHERE Name = "your name" The returned object_id can be fed into GetElementByID. Accordingly
Code: [Select]
SELECT Diagram_ID FROM t_diagram WHERE Name = "your name" would be used for GetDiagramByID.

You can combine that with packages. Add
Code: [Select]
AND ParentID = <pck-id> where <pck-id> is the id of the according package.

My Inside book elaborates on that technique.

q.
« Last Edit: October 25, 2013, 05:40:24 am by qwerty »

stao

  • EA User
  • **
  • Posts: 137
  • Karma: +0/-0
    • View Profile
Re: Finding Diagrams and Elements by name
« Reply #6 on: October 25, 2013, 06:50:33 am »
you can use this method to extract your needed results from your sql result string:

Code: [Select]
public static List<String> getSQLContent(String queryResult, String nodeName)
        {
            String startTag = "<" + nodeName + ">";
            String endTag = "</" + nodeName + ">";

            List<String> returnVal = new List<string>();

            
            string[] splitted = queryString.Split(new String[] { startTag }, StringSplitOptions.RemoveEmptyEntries);

            returnVal.AddRange(splitted);

            if (returnVal.Count > 0 && !queryString.StartsWith(startTag) )
            {
                returnVal.RemoveAt(0);          
            }

            for(int i = 0; i < returnVal.Count; i++)
            {
                returnVal[i] = returnVal[i].Split(new String[] { endTag }, StringSplitOptions.RemoveEmptyEntries)[0];
            }

            if (returnVal.Count == 0)
            {
                returnVal.Add("");
            }
            return returnVal;
        }

you must be aware that the returned List size is never 0 (contains "" as a single entry if wanted result was not found in the string).
This has a reason in our Addin i cant remember right now  ::)