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

3

3

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

For example, I want to add two edges having the same label 'label1', one from vertex v-1 to vertex v-2 and the other one from vertex v-2 to v-3.

Part of the code would be:

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

But JUNG does not allow to add two edges with the same label. It 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 can I add two edges having the same label?

Thanks.

Edit:

I just read that there is a way to assign a weight value to an edge, that is by using EdgeWeightLabeller, but these weight values should be integers. So it does not seem to solve the problem.

Nominalism answered 19/2, 2012 at 19:38 Comment(3)
Can you add a space to the second edge? "label1 "Creeper
Thank You Joop Eggen. That is really a good idea! It solves the problem for two edges, which is what I have asked here :) But I asked for two edges for the sake of simplicity. Actually I'll get many such edges. So I do not know how my graph will look like if I add a space for each such edge label. Anyway thank you again for your idea.Nominalism
Searching a vertex or an edge will be difficult just by its name. What happens if you want more than two vertices with the same label name? Would you append more spaces? I don't think so. How about you define a custom Java class (user data) that contains a few fields; one field for a unique id and one field for its name; apply this class as the generic vertex part (you can do the same for the generic edge part but with another custom class). Use vertexLabelTransformer() to displays the name part of the class instance, but use the unique id for searching, etc.Cheongsam
F
1

Labels are not required to be the toString() of the edges; that's just the default. Take a look at PluggableRendererContext to see how to supply a Transformer that provides a property for each element of the graph.

I'd also check out the section in the JUNG 2 manual (on the wiki) that talks about user data: http://sourceforge.net/apps/trac/jung/wiki/JUNGManual#UserData

Fluke answered 19/2, 2012 at 23:47 Comment(1)
Thank you Joshua! I am glad that there exists a method. I am still learning about these transformers. I will try your suggestion.Nominalism
A
1

When I have this problem, I make my label String (your is already a String) and make its value like this: "ID_OF_FIRST_VERTEX:ID_OF_SECOND_VERTEX:EDGE_VALUE". Then, to display just a value, I do use transformation. Its simple, it just takes the edge_value from name of edge.

In this sample I use a separator ":".

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, ":");
    }
});

Of course you dont have to use StringUtils from Apache Commons, a normal String.subString will work here as well.

Hope it helps.

Alroi answered 30/5, 2013 at 20:22 Comment(0)
P
0

Here is an MCVE example.

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);
}


}

The Result:

enter image description here

Poole answered 30/10, 2014 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.