Book a Demo

Author Topic: Finding descendants with common ancestor  (Read 5905 times)

Tage Korsdal Nielsen

  • EA Novice
  • *
  • Posts: 19
  • Karma: +0/-0
    • View Profile
Finding descendants with common ancestor
« on: March 06, 2014, 01:13:41 am »
How can I build a search, that would give me all descendants of a particular base class?

Rgds
Tage

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Finding descendants with common ancestor
« Reply #1 on: March 06, 2014, 01:41:20 am »
I'm not a SQL expert.. However, you could do quite something with SQL - even recursively. But exactly that it not what SQL is meant for. So a search could return all neighbors of an element (with direct connector relation). That would be quite easy. But returning ALL ancestors might return a massive amount of elements and there might be loops as well. So if you really want that list you're better served to script that. That would be tough enough anyway.

q.

Helmut Ortmann

  • EA User
  • **
  • Posts: 970
  • Karma: +42/-1
    • View Profile
Re: Finding descendants with common ancestor
« Reply #2 on: March 06, 2014, 02:21:25 am »
Hi,

with database like SQL Server recursive searches are no problem. They are also quite performant because database are build to almost query everything from the data.

Here you find an example of a recursive SQL:
http://stackoverflow.com/questions/14274942/sql-server-cte-and-recursion-example

The real problem is: If you do it the first time it's some effort understanding and doing it.

Script/Addin will also do the job.

Helmut
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Finding descendants with common ancestor
« Reply #3 on: March 06, 2014, 06:05:52 pm »
Helmut,

Last time I tried a recursive sql query (on SQL Server) in an SQL search it didn't work.
I had to resort to using a fixed number of levels.
In this case I would probably use code as well.
In pseudo code:
Code: [Select]
List<Element> getDescendants()
{
      // get the direct descendants using a query
    string sqlQuery = "select o.ea_guid from t_object o
            join t_connector c on o.Object_ID = c.Start_Object_ID
            where c.End_Object_ID = "+this.wrappedElement.ElementID+"
            and c.Connector_Type = 'Generalization'"
      List<Element> descendants = GetElementsByQuery(sqlQuery);
      // loop the direct descendants and add their descendants to the list
      foreach (Element descendant in descendants)
      {
            descendants.addRange(descendant.getDescendants());
      }
      //return the descendants
      return descendants
}

Geert

Helmut Ortmann

  • EA User
  • **
  • Posts: 970
  • Karma: +42/-1
    • View Profile
Re: Finding descendants with common ancestor
« Reply #4 on: March 06, 2014, 08:10:45 pm »
Geert,

some time ago I made a recursive SQL with SQL Server. I remember it took me some time to get it working.

Code has the advantage that it is DB independant.

Helmut
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)