Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Manfred Kröpfli on August 07, 2009, 06:30:42 pm

Title: How to find all instances of a base class?
Post by: Manfred Kröpfli on August 07, 2009, 06:30:42 pm
Hi there

I have a base class element and I want to find all its instances. My idea is to use the ClassifierID attribute and match it against the base class ElementID. Yes, an easy thing with SQL, but in my addin I want to find a compatible solution to use it against *.eap or Oracle repositories.

BTW, I haven't found a way using the model search query builder, either. That way I would be able to get a compatible XML query usable in any addin, too.

Thanks for any helpful hints
Manfred
Title: Re: How to find all instances of a base class?
Post by: gilles2 on August 08, 2009, 02:02:20 am
Hi,

On Repository class there are two interesting methods :
1. AddDefinedSearches (string sXML) where you add a new search
2. GetElementsByQuery (string QueryName, string SearchTerm) : the first parameter is the name of the "Search" you want to use.

And finally, in EA, in  Manage Searches wizard, you can export  searches in XML format to get a valid sample.

Gilles
Title: Re: How to find all instances of a base class?
Post by: Mike Fechner on August 08, 2009, 04:48:12 am
Do you have an actual sample to find the instances of a class?
Title: Re: How to find all instances of a base class?
Post by: Manfred Kröpfli on August 10, 2009, 04:42:25 pm
Hi Gilles

works fine, thanks.

@Mike: I defined an SQL search using the Mangaged Searches wizard (Edit->Find in Model...), exported it to XML and used it as gilles explained. My SQL string for query e.g. named "FindInstances":

Code: [Select]
select ea_guid AS CLASSGUID, Object_Type AS CLASSTYPE, Name FROM t_object
where t_object.Classifier_guid = '<Search Term>'

My C# code:
Code: [Select]
string xmlDefinedSearch = "<?xml version=\"1.0\" encoding=\"windows-1252\"?>";
xmlDefinedSearch += "<RootSearch><Search Name=\"FindInstances\" GUID=\"{BA0200DF-87AB-4b05-A982-C63F979CED86}\" PkgGUID=\"-1\" Type=\"0\" LnksToObj=\"0\" CustomSearch=\"0\" AddinAndMethodName=\"\">";
xmlDefinedSearch += "<SrchOn><RootTable Filter=\"select ea_guid AS CLASSGUID, Object_Type AS CLASSTYPE, Name FROM t_object where t_object.Classifier_guid = '&lt;Search Term&gt;'\" Type=\"-1\">";
xmlDefinedSearch += "<TableName Display=\"Find instances by classifier\" Name=\"\"/><TableHierarchy Display=\"\" Hierarchy=\"\"/></RootTable></SrchOn><LnksTo/></Search></RootSearch>";
repository.AddDefinedSearches(xmlDefinedSearch);

// base class is used by what elements?
EA.Collection elements = repository.GetElementsByQuery("OneCRM.FindInstances", ((EA.Element)node.Item).ElementGUID);
foreach (EA.Element instanceof in elements)
      ...
Cheers
Manfred
Title: Re: How to find all instances of a base class?
Post by: Mike Fechner on August 11, 2009, 08:08:09 am
Many Thanks Manfred,

Works perfectly!