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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.