首页 > 解决方案 > 如何使用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

非常感谢您的帮助!

标签: graphgremlinjanusgraphweighted

解决方案


您可以在 Gremlin 中结合 asackmathstep 来完成这一切

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您可以将步骤pathsack内部结合起来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]    

推荐阅读