0

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?

Bryce
  • 45
  • 1
  • 7

2 Answers2

1

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.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Thank you very much. I need the first result to be: `==> [weight:0.4, ordinal:0] ==> [weight:0.4, ordinal:1] ==> [weight:0.4, ordinal:2]` Still learning my way around graph so I could be thinking about it in the wrong way. Thanks again. I have a query getting the edges in the order I want but need to set that order explicitly on the edge using a property I have called ordinal. – Bryce Jun 27 '18 at 20:36
  • if you need the "ordinal" edge property value to start at 0 then define the initial sack value using `withSack(-1)` so that when it increments on the first loop the `sack()` value with increment by `constant(1)` to 0. – stephen mallette Jun 28 '18 at 00:43
1

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
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • I know... I see `.count("x")` or `.incr("x")` or `.set("x", 1, sum)` on the horizon. We need something for global counters or scalar values in general. – Daniel Kuppitz Jul 02 '18 at 18:04