如何使用janusgraph中的自定义函数计算边的权重?

如何使用janusgraph中的自定义函数计算边的权重?,graph,gremlin,janusgraph,weighted,Graph,Gremlin,Janusgraph,Weighted,因此,我们在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', 2

因此,我们在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
过程中的单个值,您可以在
联合
步骤中组合
路径
sack
。使用
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]    

您可以将
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
过程中的单个值,您可以在
联合
步骤中组合
路径
sack
。使用
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]    

我在答案上加了一句,表示结果被包括在内。完美的开尔文先生。顺便说一句,这本书很棒!我在答案上加了一句,表示结果被包括在内。完美的开尔文先生。顺便说一句,这本书很棒!