0

The line: g.V('1').out('knows').hasId('2').hasNext()

This exact line works in the Gremlin console.

I have not read in the documentation that hasNext does not exist in Gremlin.NET. Am I missing something, or is there simply another way to do this in Gremlin.NET?

Deinonychus
  • 135
  • 1
  • 9

2 Answers2

1

It does not exist yet:

https://issues.apache.org/jira/browse/TINKERPOP-1921

The main reason is related to the fact that hasNext() is a Java Iterator semantic that was not applied to .NET. Gremlin Language Variants (GLVs) like .NET are given some latitude in terms of how they interpret the language so as to provide the most at-home feel for developers using it. In other words, if you are using the .NET GLV you shouldn't feel as though you are coding in Java, but should instead feel write at home with the standard .NET semantics.

That said, it could be argued, as it is in the issue I've referenced above, that something like hasNext() is a common form of Gremlin as a query language and should thus be available in all GLVs. So, we will consider those options as we come across them.

For .NET I guess you would try to check Current as discussed here.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
1

This method is really missing in Gremlin.Net right now. While this is not explicitly stated in the documentation, the documentation does list all terminal steps implemented by Gremlin.Net:

  • ITraversal.Next()
  • ITraversal.NextTraverser()
  • ITraversal.ToList()
  • ITraversal.ToSet()
  • ITraversal.Iterate()

hasNext is also such a terminal step but as you can see it is missing in this list.

The only workaround I can think of for situations like this is to use the count step and then check in your application whether the returned count is greater than zero:

var count = g.V("1").Out("knows").HasId("2").Count().Next();
var exists = count > 0;

In some cases it could also make sense to limit the number of vertices going into the Count step as you aren't interested in the exact count but only want to know whether at least one vertex exists:

g.V("1").Out("knows").HasId("2").Limit<Vertex>(1).Count().Next();

This is also the proposed workaround in the ticket for this feature: TINKERPOP-1921.

Florian Hockmann
  • 2,634
  • 13
  • 24
  • why does checking `Current` not work? Isn't `ITraversal` just an `IEnumerator` and thus follows those same iteration semantics? – stephen mallette Jul 03 '18 at 19:07
  • I wrote my answer without seeing yours first and to be honest, I simply haven't thought of manual enumeration. But `MoveNext()` is probably what should be used in those cases as `Current` doesn't start the enumeration and will therefore be `null` at first. `MoveNext()` moves the enumerator to the next element and returns `true` if there was an element. So, it looks like this: `var exists = g.V("1").Out("knows").HasId("2").MoveNext();`. – Florian Hockmann Jul 03 '18 at 19:25
  • Thanks to both of you. I feel like I'm doing something wrong here, I get a null reference exception as soon as I add `MoveNext()`or `Next()`. This is the line: `var test = g.V("1").Out("test").HasId("2").MoveNext();` Your sample code in your answer also gives me a NullRef. – Deinonychus Jul 03 '18 at 22:28
  • yeah - `MoveNext()` + `Current` is what allows the concept of `hasNext()` is what I meant. cool – stephen mallette Jul 03 '18 at 23:59
  • Did you construct the graph traversal source `g` correctly with a remote connection? And are you sure that the vertex ids are strings in your graph database? In the end, a stack trace for the null reference exception would be helpful. Maybe create a thread for that in the [Gremlin Users group](https://groups.google.com/forum/#!forum/gremlin-users) as StackOverflow comments aren't that suitable to debug a problem. – Florian Hockmann Jul 04 '18 at 06:26