Groovy脚本无法向Gremlin图添加边

时间:2016-12-05 01:25:46

标签: groovy graph closures gremlin tinkerpop

我试图让这个脚本能够检索文件中存在的所有边缘,并将用户评分为电影。

new File('ratings.dat').eachLine{
    line ->

    components = line.split('::');

    userId = components[0].toInteger();
    movieId = components[1].toInteger();

    g.V().has('userId', userId).as('o').V().has('movieId', movieId).addE('rated').to('o');
}

如果我在这个闭包中添加了一些调试打印,我可以看到所有信息都正确地加载到我的变量中,但是在执行结束时我计算了图表中的边数,它只是增加了1,什么时候应该是多个。一些调查表明,有效添加到图表中的边缘是最后要读取的。什么可能出错?

1 个答案:

答案 0 :(得分:2)

你永远不会执行你的遍历。您的代码应如下所示:

ArrayDeque bits = new ArrayDeque();
for (String proposition : propositions) {
    if (bits.size() == 0) {
        bits.push(proposition);
    } else {
        // Add prefix
        main.offerFirst("|(" + proposition + "," );
        // Add suffix
        main.push(")");
    }
}
StringBuilder sb = new StringBuilder();
for( String s : bits) {
   sb.append(s);
}
main = sb.toString();