cts
cts

Reputation: 1820

reusable traversal components not always working with gremlin

I'm trying to create reusable components for my traversals using gremlin python by putting traversal components into functions and I'm running into a problem where some of the traversal components aren't working correctly.

As setup I'm running gremlin server using the docker container with the configuration file loading in the modern graph from the github repo

docker run -p 8182:8182 tinkerpop/gremlin-server:3.4.6 conf/gremlin-server-modern.yaml

My test python code looks like the following:

from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.process.graph_traversal import __
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

def connect_gremlin(endpoint='ws://localhost:8182/gremlin'):
    return traversal().withRemote(DriverRemoteConnection(endpoint,'g'))

def n():
    return __.values('name')

def r():
    return __.range(2,4)

g = connect_gremlin()

# works as expected
g.V().map(n()).toList()

# returns an empty list
g.V().map(n()).filter(r()).toList()

# but using range step directly works as expected
g.V().map(n()).range(2,4).toList()

I can successfully move the values step into a function but when I try to do the same thing with the range step it returns an empty list rather than the 2nd through 4th items. Anyone know what I'm doing wrong?

Upvotes: 1

Views: 149

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14391

The map step is intended to map the state of each traverser to a new state. In the context of a single traverser a range starting anywhere but zero is not going to do what you expect.

Here are some examples using Python:

>>> g.V().map(__.range(0,1)).limit(5).toList()
[v[1400], v[1401], v[1402], v[1403], v[1404]]

>>> g.V().map(__.range(0,2)).limit(5).toList()
[v[1400], v[1401], v[1402], v[1403], v[1404]]

>>> g.V().map(__.range(1,2)).limit(5).toList()
[]

This is why the values step works inside a map step and range does not.

Rather than inject code using a map step why not just incrementally add to the traversal and then iterate it when complete?

Upvotes: 0

Related Questions