如何使用 janusgraph 中的自定义函数计算边的权重?
How to calculate the weight of an edge wth a custom function in janusgraph?
因此我们在 Janusgraph 中绘制了以下图表:
g.addV().property('nameV', 'source').as('1').
addV().property('nameV', 'destiny2').as('2').
addV().property('nameV', 'destiny4').as('3').
addE('connects').from('1').to('2').property('nameE', 'edge1').property('bw', 2000).property('latency', 100).
addE('connects').from('2').to('3').property('nameE', 'edge2').property('bw', 100).property('latency', 200).
addE('connects').from('1').to('3').property('nameE', 'edge3').property('bw', 3000).property('latency', 500).iterate();
并且此查询使用 带宽 (bw) 作为路径上每条边的权重,给出了两个节点之间的最短路径:
g.V().has('nameV', 'source').repeat(outE().inV().simplePath()).until(has('nameV', 'destiny4')).
path().as('p').
by(coalesce(values('bw'), constant(0.0))).
map(unfold().sum()).as('xyz').
select('p', 'xyz').
order().by('xyz', asc).limit(1).
next();
我需要的是一种使用边缘参数的自定义函数来计算每条边缘(在查询时)的权重的方法,例如:100*bw/latency
非常感谢您的帮助!
您可以结合 sack
和 math
步骤在 Gremlin
中完成这一切
gremlin> g.withSack(0).
......1> V().has('nameV', 'source').
......2> repeat(outE().
......3> sack(sum).
......4> by(project('bw','lat').
......5> by('bw').
......6> by('latency').
......7> math('100*bw/lat')).
......8> inV().
......9> simplePath()).
.....10> until(has('nameV', 'destiny4')).
.....11> order().
.....12> by(sack(),desc).
.....13> path()
==>[v[0],e[6][0-connects->2],v[2],e[7][2-connects->4],v[4]]
==>[v[0],e[8][0-connects->4],v[4]]
更新(扩展):
要更改结果以包括计算值以及 bw
沿途的各个值,您可以将 path
和 sack
组合在 union
步骤。使用 local
步骤,以便 fold
单独应用于每个路径,而不是所有路径。
gremlin> g.withSack(0).
......1> V().has('nameV', 'source').
......2> repeat(outE().
......3> sack(sum).
......4> by(project('bw','lat').
......5> by('bw').
......6> by('latency').
......7> math('100*bw/lat')).
......8> inV().
......9> simplePath()).
.....10> until(has('nameV', 'destiny4')).
.....11> order().
.....12> by(sack(),desc).
.....13> local(
.....14> union(
.....15> path().
.....16> by('nameV').
.....17> by(valueMap('bw','latency')),
.....18> sack()).
.....19> fold())
==>[[source,[bw:2000,latency:100],destiny2,[bw:100,latency:200],destiny4],2050.0]
==>[[source,[bw:3000,latency:500],destiny4],600.0]
因此我们在 Janusgraph 中绘制了以下图表:
g.addV().property('nameV', 'source').as('1').
addV().property('nameV', 'destiny2').as('2').
addV().property('nameV', 'destiny4').as('3').
addE('connects').from('1').to('2').property('nameE', 'edge1').property('bw', 2000).property('latency', 100).
addE('connects').from('2').to('3').property('nameE', 'edge2').property('bw', 100).property('latency', 200).
addE('connects').from('1').to('3').property('nameE', 'edge3').property('bw', 3000).property('latency', 500).iterate();
并且此查询使用 带宽 (bw) 作为路径上每条边的权重,给出了两个节点之间的最短路径:
g.V().has('nameV', 'source').repeat(outE().inV().simplePath()).until(has('nameV', 'destiny4')).
path().as('p').
by(coalesce(values('bw'), constant(0.0))).
map(unfold().sum()).as('xyz').
select('p', 'xyz').
order().by('xyz', asc).limit(1).
next();
我需要的是一种使用边缘参数的自定义函数来计算每条边缘(在查询时)的权重的方法,例如:100*bw/latency
非常感谢您的帮助!
您可以结合 sack
和 math
步骤在 Gremlin
gremlin> g.withSack(0).
......1> V().has('nameV', 'source').
......2> repeat(outE().
......3> sack(sum).
......4> by(project('bw','lat').
......5> by('bw').
......6> by('latency').
......7> math('100*bw/lat')).
......8> inV().
......9> simplePath()).
.....10> until(has('nameV', 'destiny4')).
.....11> order().
.....12> by(sack(),desc).
.....13> path()
==>[v[0],e[6][0-connects->2],v[2],e[7][2-connects->4],v[4]]
==>[v[0],e[8][0-connects->4],v[4]]
更新(扩展):
要更改结果以包括计算值以及 bw
沿途的各个值,您可以将 path
和 sack
组合在 union
步骤。使用 local
步骤,以便 fold
单独应用于每个路径,而不是所有路径。
gremlin> g.withSack(0).
......1> V().has('nameV', 'source').
......2> repeat(outE().
......3> sack(sum).
......4> by(project('bw','lat').
......5> by('bw').
......6> by('latency').
......7> math('100*bw/lat')).
......8> inV().
......9> simplePath()).
.....10> until(has('nameV', 'destiny4')).
.....11> order().
.....12> by(sack(),desc).
.....13> local(
.....14> union(
.....15> path().
.....16> by('nameV').
.....17> by(valueMap('bw','latency')),
.....18> sack()).
.....19> fold())
==>[[source,[bw:2000,latency:100],destiny2,[bw:100,latency:200],destiny4],2050.0]
==>[[source,[bw:3000,latency:500],destiny4],600.0]