Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Adler 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
-
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.
-
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
-
Adler, you may find GetElementsByQuery useful.
Use the search 'Element Name'.
Geert, look up GetElementSet.
Unfortunately, neither works for anything except elements.
-
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.
-
You would need something like
SELECT Object_ID FROM t_object WHERE Name = "your name"
The returned object_id can be fed into GetElementByID. Accordingly SELECT Diagram_ID FROM t_diagram WHERE Name = "your name"
would be used for GetDiagramByID.
You can combine that with packages. Add AND ParentID = <pck-id>
where <pck-id> is the id of the according package.
My Inside book elaborates on that technique.
q.
-
you can use this method to extract your needed results from your sql result string:
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 ::)