I'm parsing a Gremlin query in Java (well, actually I'm writing Scala, and using the Groovy compiled JARs like it was Java).
The query is a String variable that is given by user input. In other words - I cannot tell what the query will be, I'm only assuming it's a valid Gremlin query (syntactically and logically).
I started with a simple Gremlin.compile(query)
that returns Pipe
on which I'm iterating. However, according to the example, one must invoke .setStarts
prior to iterating the Pipe. And I must know what the is the runtime type S
in my Pipe<S,E>
.
It feels like this API isn't generic enough, the following line from the example
pipe.setStarts(new SingleIterator<Vertex>(graph.getVertex(1)));
will work for some cases, but for Vertex Iteration for one example (g.V()
) it will throw a CastException.
Is there a way to work-around it?
Perhaps using the underlying Script Engine (like the next examples in the link above) will help me to achieve more generic code?
I found a workaround. It feels a bit ugly but it does the job.
I'm using
ScriptEngine
withbindings
of 'g' for the Graph, so the user can start his/her queries withg.
. (not helps for generics, but makes it more user-friendly by not making the user use the Identity Pipe (_()
) at the beginning of his/her queries).(kind of ugly, I know) I'm extracting from the query string (using RegEx) the starting vertex (if exists), finding it programatically and (if found) invoking setStarts with it. If it's not found I'm giving the Graph itself as the parameter for setStarts, assuming its a Vertex Iteration query.