How to add two edges having the same label (but different endpoints) in a JUNG?

How to add two edges having the same label but different endpoints?

For example, I want to add two edges that have the same label “label1”, one of the vertices v-1 to the vertex v-2, and the other from the vertices v-2 to v-3.

Code Part:

g.addEdge("label1","v-1","v-2");
g.addEdge("label1","v-2","v-3");

But the JUNG does not allow you to add two edges with the same label. This gives an error:

edge label1 already exists in this graph with endpoints [v-1, v-2] and cannot be added with endpoints [v-2, v-3]

How to add two edges having the same label?

Thank.

Edit:

, , EdgeWeightLabeller, . , .

+3
3

toString() ; . PluggableRendererContext, , , .

JUNG 2 ( ), : http://sourceforge.net/apps/trac/jung/wiki/JUNGManual#UserData

+1

, String ( String) : "ID_OF_FIRST_VERTEX: ID_OF_SECOND_VERTEX: EDGE_VALUE". , , . , edge_value .

":".

VisualizationViewer vv = new VisualizationViewer(layout, dim);
//other operations
vv.getRenderContext().setEdgeLabelTransformer(new Transformer<String, String>() {
    @Override
    public String transform(String c) {
        return StringUtils.substringAfterLast(c, ":");
    }
});

, StringUtils Apache Commons, String.subString.

, .

+1

Here is an example of MCVE .

package stackoverflow;

import javax.swing.JFrame;
import org.apache.commons.collections15.Transformer;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;


public class JungNetwork {

public static Graph<String, String> getGraph() 
{
    Graph<String, String> g = new DirectedSparseMultigraph<String, String>();

    g.addVertex("v1");
    g.addVertex("v2");
    g.addVertex("v3");
    g.addEdge("label1", "v1", "v2");
    g.addEdge("label2", "v2", "v3");
    g.addEdge("label3", "v3", "v1");
    return g;
}


public static void main(String[] args) 
{
    JFrame f = new JFrame();
    final Graph<String, String> g = getGraph();
    VisualizationViewer<String, String> vv =    new VisualizationViewer<String, String>(new FRLayout<String, String>(g));

    final Transformer <String, String> edgeLabel = new Transformer<String, String>(){

        @Override
        public String transform(String edge) {
            // TODO Auto-generated method stub
            if (edge.equals("label1")|| edge.equals("label2")){
                return "label1";
            }else
            return "label3";
        }

    };


    vv.getRenderContext().setLabelOffset(15);
    vv.getRenderContext().setEdgeLabelTransformer(edgeLabel);

    f.getContentPane().add(vv);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}


}

Result:

enter image description here

0
source

All Articles