如何在JUNG中添加具有相同标签(但不同端点)的两条边? [英] How to add two edges having the same label (but different endpoints) in JUNG?

查看:133
本文介绍了如何在JUNG中添加具有相同标签(但不同端点)的两条边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加具有相同标签但不同端点的两条边?

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

例如,我想添加两条具有相同标签'label1'的边,一条来自顶点v-1到顶点v-2,另一个从顶点v-2到v-3。

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.

部分代码是:

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

但是JUNG不允许添加具有相同标签的两条边。它给出了一个错误:

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


此图表中已存在边缘标签1,端点为[v-1,v-2]且不能为
添加端点[v-2,v-3]

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?

谢谢。

我刚读过有一种方法可以为边缘分配权重值,即使用 EdgeWeightLabeller ,但这些权重值应该是整数。所以它似乎没有解决问题。

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.

推荐答案

当我遇到这个问题时,我制作了我的标签字符串(你已经是一个字符串)并使其值如下: ID_OF_FIRST_VERTEX:ID_OF_SECOND_VERTEX:EDGE_VALUE。然后,为了显示一个值,我确实使用转换。它很简单,只需要从边缘名称获取edge_value。

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

当然你不必使用Apache Commons的StringUtils,普通的String.subString在这里可以用作好吧。

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

希望有所帮助。

这篇关于如何在JUNG中添加具有相同标签(但不同端点)的两条边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆