2

我们有下面的图表。

gremlin> a = graph.addVertex("name", "alice")
==>v[4208]
gremlin> b = graph.addVertex("name", "bobby")
==>v[40968424]
gremlin> c = graph.addVertex("name", "cindy")
==>v[4192]
gremlin> d = graph.addVertex("name", "david")
==>v[40972520]
gremlin> e = graph.addVertex("name", "eliza")
==>v[40964272]
gremlin> a.addEdge("rates",b,"tag","ruby","value",9)
==>e[2ry-38w-azv9-oe3fs][4208-rates->40968424]
gremlin> b.addEdge("rates",c,"tag","ruby","value",8)
==>e[odzq5-oe3fs-azv9-38g][40968424-rates->4192]
gremlin> c.addEdge("rates",d,"tag","ruby","value",7)
==>e[170-38g-azv9-oe6lk][4192-rates->40972520]
gremlin> d.addEdge("rates",e,"tag","ruby","value",6)
==>e[oe04d-oe6lk-azv9-oe08g][40972520-rates->40964272]
gremlin> a.addEdge("rates",e,"tag","java","value",9)
==>e[366-38w-azv9-oe08g][4208-rates->40964272]
gremlin> g.E().values("tag")
==>ruby
==>ruby
==>ruby
==>ruby
==>java
gremlin> graph.tx().commit()

在脚本下面,我们可以获得所有路径的权重。

gremlin>g.V().has('name','alice').repeat(outE().has('tag','ruby').inV()).times(3).emit().group().by('name').by(path().unfold().has('value').values('value').fold()).next()
==>bobby=[9]
==>cindy=[8, 9, 8]
==>david=[8, 7, 9, 8, 7]

下面我们可以总结所有路径的权重。

gremlin>g.V().has('name','alice').repeat(outE().has('tag','ruby').inV()).times(3).emit().group().by('name').by(path().unfold().has('value').values('value').sum()).next()
==>bobby=9 sum[9]
==>cindy=25 sum[8, 9, 8]
==>david=39 sum[8, 7, 9, 8, 7]

我的问题是如何将权重与系数相加?像下面

==>bobby=[9] = 9*1
==>cindy=[8, 9, 8] = 8*1 + 9*0.5 + 8*0.25 
==>david=[8, 7, 9, 8, 7] = 8*1 + 7*0.5 + 9*0.25 + 8*0.125 + 7*0.0625

另一个要求是系数是否来自边缘属性 co?

非常感谢您的帮助!

4

2 回答 2

4

尽可能避免使用 lambda。

gremlin> g.withSack(1.0).V().has("name","alice").
           repeat(outE("rates").has("tag","ruby").
                  project("a","b","c").
                    by(inV()).
                    by(sack()).
                    by("value").as("x").
                  select("a").
                  sack(mult).by(constant(0.5))).
              times(3).emit().
            select(all, "x").
            project("name","score").
              by(tail(local, 1).select("a").values("name")).
              by(unfold().
                 sack(assign).by(select("b")).
                 sack(mult).by(select("c")).
                 sack().sum())
==>[name:bobby,score:9.0]
==>[name:cindy,score:13.00]
==>[name:david,score:14.750]

此查询返回的结果与您提供的示例略有不同,但这只是因为您的查询显示的结果不是使用提供的示例图生成的。

于 2017-08-28T14:47:36.997 回答
1

你可以做一些事情,比如g.V().has('name','alice').repeat(outE().has('tag','ruby').inV()).times(3).emit().group().by('name').by(path().unfold().has('value').values('value').fold()).next().collect{k, v -> v.withIndex().collect {Integer it, Integer idx -> return it * (1/(idx + 1))}.inject(0.0) {acc,i -> acc+i}}不要忘记,你也在使用 groovy 编程语言,所以你可以访问这些功能。

于 2017-08-28T13:20:59.003 回答