简体   繁体   English

如何在JUNG中添加具有相同标签(但不同端点)的两条边?

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

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. 例如,我想添加两个具有相同标签'label1'的边,一个从顶点v-1到顶点v-2,另一个从顶点v-2到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. 但是JUNG不允许添加具有相同标签的两条边。 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] edge label1已存在于此图中,端点为[v-1,v-2],无法添加端点[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. 我刚刚读到有一种方法可以为边缘分配权重值,即使用EdgeWeightLabeller ,但这些权重值应该是整数。 So it does not seem to solve the problem. 所以它似乎没有解决问题。

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". 当我遇到这个问题时,我创建了我的标签字符串(你已经是一个字符串)并使其值如下:“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. 它很简单,它只是从edge的名字中获取edge_value。

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. 当然你不必使用Apache Commons的StringUtils,普通的String.subString也适用于此。

Hope it helps. 希望能帮助到你。

Labels are not required to be the toString() of the edges; 标签不需要是边的toString(); 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. 查看PluggableRendererContext,了解如何提供为图的每个元素提供属性的Transformer。

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 我还要查看JUNG 2手册(维基上)中有关用户数据的部分: http//sourceforge.net/apps/trac/jung/wiki/JUNGManual#UserData

Here is an MCVE example. 这是一个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);
}


}

The Result: 结果:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM