back() can be Element or List

44 views
Skip to first unread message

Matt Frantz

unread,
Feb 23, 2015, 1:38:48 AM2/23/15
to gremli...@googlegroups.com
I'm trying to understand the following behavior.

gremlin> g = TinkerFactory.createModern()

gremlin> g.V(1).as('a').values('name')
==>marko

gremlin> g.V(1).as('a').out('knows').back('a').values('name')
==>marko
==>marko

gremlin> g.V(1).as('a').out('knows').back('a').out('knows').back('a').values('name')
==>marko
==>marko
==>marko
==>marko

gremlin> g.V(1).as('a').out('knows').back('a').as('a').out('knows').back('a').values('name')
java.util.ArrayList cannot be cast to com.tinkerpop.gremlin.structure.Element

Because there are two steps labeled 'a', the value converts to a list, so the 'values' step fails.  You can see that by leaving it off:

gremlin> g.V(1).as('a').out('knows').back('a').as('a').out('knows').back('a')
==>[v[1], v[1]]
==>[v[1], v[1]]
==>[v[1], v[1]]
==>[v[1], v[1]]

In the Java API, 'back' must know the type <E2> it will emit.

public default <E2> GraphTraversal<S, E2> back(final String stepLabel);

What should <E2> be?  You could say the first 'back' should be <Vertex> while the second should be <List<Vertex>>.  However, this example is contrived.  A slightly more realistic example uses 'as' within a 'repeat' loop.  I'd like for it to have either the most recent value or always a list, but it actually changes types after the first iteration.

gremlin> g.V(1).repeat(__.as('a').out().back('a')).times(2).values('name')
java.util.ArrayList cannot be cast to com.tinkerpop.gremlin.structure.Element


Benjamin McBride

unread,
Feb 27, 2015, 1:36:33 PM2/27/15
to gremli...@googlegroups.com
I'm also running into this problem...don't have a good solution yet.

Matt Frantz

unread,
Mar 5, 2015, 2:54:42 PM3/5/15
to gremli...@googlegroups.com
Another data point is the behavior of Traverser.path(String).  The javadoc says that it can return a single object or a list.  In the following examples, it returns a single object, with the "last" semantics that I desire:

gremlin> g.V(1).as('a').map{it.path('a')}
==>v[1]

gremlin> g.V(1).as('a').out().as('a').map{it.path('a')}
==>v[3]
==>v[2]
==>v[4]

gremlin> g.V(1).as('a').out().as('a').out().as('a').map{it.path('a')}
==>v[5]
==>v[3]

However, in other cases, e.g. in the presence of an intervening 'back' step, it returns a list:

gremlin> g.V(1).as('a').out().back('a').as('a').map{it.path('a')}

==>[v[1], v[1]]
==>[v[1], v[1]]
==>[v[1], v[1]]

Matt Frantz

unread,
Mar 5, 2015, 7:12:25 PM3/5/15
to gremli...@googlegroups.com
If I am looking for "most recent value at a particular named step", do I want "select" instead of "back"?  Contrast the behavior:


gremlin> g.V(1).as('a').out().as('a').out().as('a').back('a')
==>[v[1], v[4], v[5]]
==>[v[1], v[4], v[3]]

gremlin> g.V(1).as('a').out().as('a').out().as('a').select('a')


==>v[5]
==>v[3]

Reply all
Reply to author
Forward
0 new messages