graph - 如何使用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
非常感谢您的帮助!
解决方案
您可以在 Gremlin 中结合 asack
和math
step 来完成这一切
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]
推荐阅读
- c# - 将图像转换为 base64 时文件压缩类型发生变化
- python - 如何在 python-decouple 中使用不同的 .env 文件
- javascript - 为什么 Paper.js 看似随机翻转我的点坐标?
- html - 我需要在 herokuapp 上运行并填写 Salesforce Web-to-lead 表单
- python - TypeError:“tensorflow.python.framework.ops.EagerTensor”对象不可调用
- php - PHP codeigniter 无法加载环境变量
- amazon-web-services - 我需要一种策略来处理 CDK 中的可选 SSM 参数存储参数
- node.js - 从 typescript 构建的 JS 文件如果独立运行会停止工作,但如果从 dist 文件夹运行则不会
- sed - Sed 表达式仅转换以某些示例短语开头的某些行
- java - 从 DB 中提取的 ArrayList 被 Java 覆盖