简体   繁体   中英

edit sack() value depending on path

I have three paths leading to two goals. All edges have a different strength and I collect them with sack(). I want to weigh some of the paths more heavily than the others (independent of the edge strength). in this sample: the paths via an 'important' vertex, should have a factor 5, those via 'unimportant' factor 0.5.

how would I accomplish that?

I have this query so far, because it is important for me to group the results by goal-vertex and add the path weights.

    .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)

I tried to influence the sack value with math(), but it only influences the 'print' value, not the value inside the sack. The following query shows this, but I can't group and sort by vertexId and so on...

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')
    )

data:

%%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'))

(my code runs on Neptune with gremlin: {'version': 'tinkerpop-3.4.11'})

You can multiply the sack in the important/unimportant path by the constant.

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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM