Finding the ends of a Connector?

General discussions about code engineering

Moderator: Moderators

Finding the ends of a Connector?

Postby » Fri Apr 02, 2021 12:41 pm

Hello. Attempting to make a plugin that can analyze a model for various traits-but currently hitting a rather major stumbling block: not being able to figure out the connections between various objects. While I can get the connectors themselves, I can't seem to have any functions produce what's on either end-the connectors appear to be largely blank objects. Here's the currently-used code for it, anybody able to offer a suggestion?

Code: Select all
public String connectends() {
String restate="";
      Tree tree = getTree();
      if (tree != null)
         final Element element = getSelectedElement(tree);
         if (element != null)
            try {element.accept(visitor);} catch (Exception e) {e.printStackTrace();}
         ArrayList<Element> all=new ArrayList<>();
         for (int i = 0; i < all.size(); i++) {Element current = all.get(i);try{current.accept(visitor);}catch (Exception e){e.printStackTrace();}all.addAll(current.getOwnedElement());}
         ArrayList<String> list=new ArrayList<String>();
         Element tests=all.get(0);
         int flag=0;
           for (int index=0;index < all.size(); index++) {
              String teststring=tests.getHumanName();
              String tester="";
              if (teststring.startsWith("Connector")&&flag==0){
                 list.add(teststring+": "+tester+"\n ");
           for (String strad:list) {
    return restate;

Currently, just for reasons of making the trial runs more readable, only tries for the first connector (thus, the flag). Ideally, would be an element function on the connector itself, so it would just be swapping out the "tester=String.valueOf(tests.getOwner().getHumanName());" for something else, but not holding my breath on that.
Forum Newbie
Forum Newbie
Posts: 1
Posts Rating:1
Joined: Thu Feb 04, 2021 9:44 am

Re: Finding the ends of a Connector?

Postby ogirdor » Wed Apr 14, 2021 6:05 am


in case that you are still looking for an answer, please take a look at the code below. It is a groovy macro. It will look for all connectors attached to the currently selected diagram element and print both ends. It collects direct and indirect (via ports) connectors.

Code: Select all
import com.nomagic.magicdraw.core.Application
import com.nomagic.magicdraw.core.Project
import com.nomagic.magicdraw.uml.ConnectorsCollector
import com.nomagic.magicdraw.uml.symbols.PresentationElement
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdinternalstructures.ConnectableElement
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdinternalstructures.Connector
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdinternalstructures.ConnectorEnd

//get currently selected element in diagram
Project currentProject = Application.getInstance().getProject()
List<PresentationElement> selElems = Objects.requireNonNull(currentProject.getActiveDiagram()).getSelected()
    Application.getInstance().getGUILog().log("Select only one element")
} else {
    Element selElem = selElems.get(0).getElement()
    //get all connectors (direct and indirect)
    Collection<Connector> allConnectors = ConnectorsCollector.collectConnectors(selElem as ConnectableElement)
    //for each connector, print elements at both ends
    allConnectors.each{ conn ->
        Application.getInstance().getGUILog().log("connector: "+conn)
        List<ConnectorEnd> theEnds = conn.getEnd()
        //Application.getInstance().getGUILog().log(theEnds.size() as String)
        theEnds.each{theConnEnd ->
            Application.getInstance().getGUILog().log("connEnd: "

Good luck.
Forum Beginner
Forum Beginner
Posts: 45
Posts Rating:13
Joined: Wed Jun 03, 2020 4:41 am

Re: Finding the ends of a Connector?

Postby » Sat May 08, 2021 12:17 am

Hey guys what are you searching for here? If you looking for the HomeGoods Guest Satisfaction Survey then here is the post which will guide you about HomeGoods Survey and help for completing the whole survey.
Forum Newbie
Forum Newbie
Posts: 1
Posts Rating:0
Joined: Sat May 08, 2021 12:16 am

Return to Code engineering

Who is online

Users browsing this forum: No registered users and 1 guest