I'm printing each edge with the source and destination but they are repeated when I use getEdgeCount() in mxgraph java

118 Views Asked by At

I'm using the following function to show me each edge in the graph with its source and destination. However, I noticed that some edges appear out of nowhere.

public void showEdges()
    {
        Object[] list = graph.getChildVertices(graph.getDefaultParent());
        for(int i=0; i< list.length; i++)
        {
            mxICell vertex = (mxICell) list[i];
            for (int j =0; j<vertex.getEdgeCount(); j++)
            {
                mxICell edge = vertex.getEdgeAt(j);
                String destination =(String) (edge.getTerminal(false)).getValue();
                System.out.println("vertex-" + i + " is connected to " +
                        destination + " with weight " + (String)edge.getValue());
            }
        }
         
    }

What I have drawn is in this link: https://pin.it/7b0Qh7v

The output :

vertex-0 is connected to 0 with weight 2
vertex-0 is connected to 2 with weight 15
vertex-1 is connected to 2 with weight 17
vertex-2 is connected to 2 with weight 17
vertex-2 is connected to 2 with weight 15

What I expected was :

vertex-0 is connected to 0 with weight 2
vertex-0 is connected to 2 with weight 15
vertex-1 is connected to 2 with weight 17

Is there a solution to this problem?

1

There are 1 best solutions below

0
On

Now I know the problem with my code. The method getEdgesAt(int index) returns all edges whether they are incoming or outgoing and what I want in order for my method to work is only the outgoing edges. I replaced the method with this :

    public void getEdges()
    {
        Object[] list = graph.getChildVertices(graph.getDefaultParent());
        for(int i=0; i< list.length; i++)
        {
            mxICell vertex = (mxICell) list[i];
            Object[] edges = graph.getEdges(vertex, graph.getDefaultParent(), false, true, true);
            for (int j =0; j<edges.length; j++)
            {
                String destination =(String) (((mxICell)edges[j]).getTerminal(false)).getValue();
                System.out.println("vertex-" + i + " is connected to " +
                        destination + " with weight " + (String)edge.getValue());
            }
            
        }
    }