根据路径编辑 sack() 值

edit sack() value depending on path

我有通往两个目标的三个路径。所有边都有不同的强度,我用 sack() 收集它们。 我想比其他路径更重地权衡一些路径(与边缘强度无关)。在此示例中:通过 'important' 顶点的路径应具有因子 5,通过 'unimportant' 的路径应具有因子 0.5.

我将如何完成?

到目前为止我有这个查询,因为按目标顶点对结果进行分组并添加路径权重对我来说很重要。

    .union(
        //all paths via important nodes
        outE().has('weight').sack(mult).by('weight')
        .inV().hasLabel('important')
        .outE().has('weight').sack(mult).by('weight')
        .inV(),
        //all paths via UNimportant nodes
        outE().has('weight').sack(mult).by('weight')
        .inV().hasLabel('unimportant')
        .outE().has('weight').sack(mult).by('weight')
        .inV()
    )
    .valueMap(true)
    .group().by().by(sack().sum())
    .unfold()
    .order().by(values, desc)
    .project('alg', 'rank', 'map').by(constant('#1')).by(values).by(keys)

我试图用 math() 影响 sack 值,但它只影响 'print' 值,而不影响 sack 内的值。 以下查询显示了这一点,但我无法按 vertexId 等进行分组和排序...

g.withSack(1.0f).V('v0')
    .union(
        //all paths via important nodes
        outE().has('weight').sack(mult).by('weight')
        .inV().hasLabel('important')
        .outE().has('weight').sack(mult).by('weight')
        .inV()
        .sack().math('_ * 5'),
        //all paths via UNimportant nodes
        outE().has('weight').sack(mult).by('weight')
        .inV().hasLabel('unimportant')
        .outE().has('weight').sack(mult).by('weight')
        .inV()
        .sack().math('_ * 0.5')
    )

数据:

%%gremlin
g.addV('start').property(id, 'v0')
  .addV('important').property(id, 'v1')
  .addV('goal').property(id, 'v2')
  .addV('unimportant').property(id, 'v3')
  .addV('goal').property(id, 'v4')
  .addE('link').property('weight', 0.4).from(V('v0')).to(V('v1'))
  .addE('link').property('weight', 0.4).from(V('v1')).to(V('v2'))
  .addE('link').property('weight', 0.4).from(V('v2')).to(V('v3'))
  .addE('link').property('weight', 0.5).from(V('v0')).to(V('v3'))
  .addE('link').property('weight', 0.5).from(V('v3')).to(V('v2'))
  .addE('link').property('weight', 0.5).from(V('v1')).to(V('v4'))

(我的代码使用 gremlin 在 Neptune 上运行:{'version':'tinkerpop-3.4.11'})

您可以将 important/unimportant 路径中的麻袋乘以常数。

gremlin> g.withSack(1.0f).V('v0').union(
......1>         outE().has('weight').sack(mult).by('weight')
......2>         .inV().hasLabel('important')
......3>         .outE().has('weight').sack(mult).by('weight')
......4>         .inV().sack(mult).by(constant(5)),
......5>         outE().has('weight').sack(mult).by('weight')
......6>         .inV().hasLabel('unimportant')
......7>         .outE().has('weight').sack(mult).by('weight')
......8>         .inV().sack(mult).by(constant(0.5))
......9>        ).
.....10>     valueMap(true).
.....11>     group().by().by(sack().sum()).
.....12>     unfold().
.....13> order().by(values, desc).
.....14>     project('alg', 'rank', 'map').by(constant('#1')).by(values).by(keys)

==>[alg:#1,rank:1.000,map:[id:v4,label:goal]]
==>[alg:#1,rank:0.9250,map:[id:v2,label:goal]]