0
votes

I am trying to loop through an arbitrary amount of edges on a vertex and assign the current iterator/loop count to the edge. How would I go about doing that?

Pseudo code g.V(12345).outE('contains').loop{through all outE contains edges}{__.property('ordinal',it.loopCount)}

Something like that?

2

2 Answers

1
votes

You can use sack() to accomplish this. As an example, I'll use the "modern" toy graph that ships with TinkerPop:

gremlin> g.withSack(0).V(1).
......1>   repeat(outE().
......2>          sack(sum).
......3>            by(constant(1)).
......4>          property('loops',sack()).
......5>          inV())
gremlin> g.V(1).outE().valueMap()
==>[weight:0.4,loops:1]
==>[weight:0.5,loops:1]
==>[weight:1.0,loops:1]
gremlin> g.V(1).outE().inV().outE().valueMap()
==>[weight:1.0,loops:2]
==>[weight:0.4,loops:2]

So sack() basically is going to hold the loop count and you can assign that to your edge.

1
votes

Starting with 1:

gremlin> g.V(1).outE().
           groupCount("x").
             by(constant("c")).
           property("ordinal", select("x").select("c")).iterate()
gremlin> g.V(1).outE().valueMap(true)
==>[id:9,weight:0.4,ordinal:1,label:created]
==>[id:7,weight:0.5,ordinal:2,label:knows]
==>[id:8,weight:1.0,ordinal:3,label:knows]

To start with 0 you'll need some extra work:

gremlin> g.withSideEffect("x", ["c": 0L]).V(1).outE().
           property("ordinal", select("x").select("c")).
           groupCount("x").
             by(constant("c")).iterate()
gremlin> g.V(1).outE().valueMap(true)
==>[id:9,weight:0.4,ordinal:0,label:created]
==>[id:7,weight:0.5,ordinal:1,label:knows]
==>[id:8,weight:1.0,ordinal:2,label:knows