Why is this gremlin query with select not returning any result but without select it works?

Viewed 196

I have a linked list B -> C -> G

It's created in the TinkerPop Console with

graph = TinkerGraph.open()
g = traversal().withEmbedded(graph)

g.addV('TreeNodeEntity').property(single, 'Name', 'B').as('l1')
g.addV('TreeNodeEntity').property(single, 'Name', 'C').as('l1').addE('PreviousSiblingEntity').to(__.V().has('Name', 'B'))
g.addV('TreeNodeEntity').property(single, 'Name', 'G').as('l1').addE('PreviousSiblingEntity').to(__.V().has('Name', 'C'))

I try to get all siblings of B.

The following gremlin script returns C and G like I expected

g.V().
    has('Name', 'B').
    as('l1').
    repeat(__.
        bothE('PreviousSiblingEntity').
        otherV().
        simplePath()
    ).
    emit().
    valueMap()

But the following script doesn't give me any value.

g.V().
    has('Name', 'B').
    as('l1').
    select('l1').
    repeat(__.
        bothE('PreviousSiblingEntity').
        otherV().
        simplePath()
    ).
    emit().
    valueMap()

Background: I want to do a .inE('ParentEntity').otherV().as('l2') between as('l1') and select('l1').

Can you give me a hint why the second script doesn't give me a result?

1 Answers

There is a difference between a Gremlin Path (i.e. TinkerPop's Path object) and the path in the graph that one traverses. I think you're expecting simplePath() to work on the latter, when it is operating on the former.

The traverser is transformed as it passes through the steps of your traversal. The history of those transformations is the Path. You can see that history with the path() step:

gremlin> g.V().out().path()
==>[v[2],v[0]]
==>[v[5],v[2]]

The Path is more than just graph elements though and can contain other things:

gremlin> g.V().out().values('Name').path()
==>[v[2],v[0],B]
==>[v[5],v[2],C]

Given that definition, simplePath() looks for situation where an element in that Path is repeated and filter them away. By doing g.V().has('Name', 'B').as('l1').select('l1') you immediately create such repetition and therefore the Path is filtered away:

gremlin> g.V().has('Name', 'B').as('l1').select('l1').path()
==>[v[0],v[0]]
gremlin> g.V().has('Name', 'B').as('l1').select('l1').simplePath()
gremlin> 
Related