Author Topic: Retrieve nested elements from an element and display name with parent name  (Read 7844 times)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13287
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
There is of course EA.Element.FQName, but that will return the full path starting from the root.

Yes, you need to recurse, but it do any good if you simply visit each element without actually doing anything.

In this method:

Code: [Select]
        private void GetEmbeddedElement(EA.Element element)
        {
            EA.Collection elements = element.Elements;
            for (short ip = 0; ip < elements.Count; ip++)
            {
                EA.Element child = (EA.Element)elements.GetAt(ip);
                GetEmbeddedElement(child);
            }
        }
You aren't doing anthing at all. It is visiting each nested element, but nothing is done with that information.

PS. you can write that a lot shorter if you do

Code: [Select]
        private void GetEmbeddedElement(EA.Element element)
        {
            //Here's where you should write the code to actually do something with the element
            foreach (var child in element.Elements)
            {
                GetEmbeddedElement(child);
            }
        }

priombiswas89

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
Many thanks for your reply. FQ name did the job. I have one more question. I was trying to distinguish initial and final node. I have seen both are of type StateNode, and normal states are of type State. Is there any way to distinguish between initial and final node?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13287
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Probably subtype or something like that.

Geert

priombiswas89

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
Probably subtype or something like that.

Geert

I am again facing trouble with the nested package. Following is the situation right now:

Image: EA State Diagram https://ibb.co/y66ZTmJ
Image: Generated YAML   https://ibb.co/87qpN7q


I was using FQName to get the name of states. It was working for one package without any further nesting. But if I nest another package inside a package, then FQName shows the package name also along with the diagram name. Is there any way to detect and get rid of the nested package name from FQName and only keep the name of the state?

I want to remove the NestedPackageWithDeepCopy from the generated YAML.

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8598
  • Karma: +256/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Look for Suppress Namespace in Options.  That should do it.  if it doesn't then it's most probably a defect.

If you type namespace into the little find command box on the ribbon, it should find it for you.

HTH,
Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

priombiswas89

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
Look for Suppress Namespace in Options.  That should do it.  if it doesn't then it's most probably a defect.

If you type namespace into the little find command box on the ribbon, it should find it for you.

HTH,
Paolo

Thanks for your reply. I want to do it programmatically in c#. Can I do it?

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8598
  • Karma: +256/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Look for Suppress Namespace in Options.  That should do it.  if it doesn't then it's most probably a defect.

If you type namespace into the little find command box on the ribbon, it should find it for you.

HTH,
Paolo
Sorry, don't know.  However, Sparxians, apparently use a different set of APIs that we users have access to.

Paolo



Thanks for your reply. I want to do it programmatically in c#. Can I do it?
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

priombiswas89

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
Look for Suppress Namespace in Options.  That should do it.  if it doesn't then it's most probably a defect.

If you type namespace into the little find command box on the ribbon, it should find it for you.

HTH,
Paolo
Sorry, don't know.  However, Sparxians, apparently use a different set of APIs that we users have access to.

I have unchecked the Show Namespace option from the diagram properties option, and still getting the namespace in FQName. I have to do it programmatically. Nevertheless, thanks for your reply.

Paolo



Thanks for your reply. I want to do it programmatically in c#. Can I do it?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13287
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
FQName will probably always return the full name, including the package.

In this case you'll need to write your own "GetMyFQName".
I don't think it's that difficult. You can find the parent element based on the EA.Element.ParentID

Just go up in the hierarchy until you reach the StateMachine.

Geert

priombiswas89

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
FQName will probably always return the full name, including the package.

In this case you'll need to write your own "GetMyFQName".
I don't think it's that difficult. You can find the parent element based on the EA.Element.ParentID

Just go up in the hierarchy until you reach the StateMachine.

Geert

What I am doing now is the following:
Code: [Select]
if (element.MetaType == "State")
                                    {
                                        //stateObj.name = Utilities.FormatElementName(element.Name, rep, element);
                                        FormatElementName(element.Name, rep, element);
                                        stateObj.name = stateName;
                                        diagramElementsObj.states.Add(stateObj);
                                    }   
 public void FormatElementName(string result, EA.Repository rep, EA.Element element)
        {
            if (element.Elements.Count == 0)
            {
                stateName = result;
                return;
            }
            foreach (EA.Element child in element.Elements)
            {
                FormatElementName(result + "/" + child.Name, rep, child);
            }
        }

I am getting the following result:

Code: [Select]
- name: State2/State3/State4
- name: State3/State4
- name: State4

But my intended result is as follows:

Code: [Select]
State2
State2/State3
State2/State3/State4

Reference image: https://ibb.co/xfzBLk0
« Last Edit: February 01, 2021, 03:24:37 am by priombiswas89 »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13287
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
You should go up, not down.
You haven't shown the code that actually iterates the states, but I'm guessing you are starting from the diagramObjects or something like that.
In that case you need to go up in the hierarchy until you reach the statemachine, and add the parent name before the current fully qualified name.

An alternative approach may be to go top down, but then you need to pass the partial path to the sub-elements when iterating.

Stepping through your code should help you understand what exactly is happening and why.

Geert