0

In my java project, I can create in-memory "GraphTraversalSource" and I can create traversal queries easily. But I wonder how can I create script queries like client.submit(query)?

public GraphTraversalSource gremlinGraph()
{
    final Graph graph = TinkerGraph.open();
    return graph.traversal();
}

I need a create Client object from TinkerGraph. Then I want to call client.submit(query) queries.

Is there any suggestion?

Sha
  • 921
  • 17
  • 46

1 Answers1

1

I'd say that the easiest way is to reference the gremlin-groovy module and create a GremlinGroovyScriptEngine instance (source code). Then you can just do:

Graph graph = TinkerGraph.open();
ScriptEngine engine = new GremlinGroovyScriptEngine();
Bindings b = new SimpleBindings();
b.put("g", graph);
Traversal t = (Traversal) engine.eval("g.V()", b);

Note that client.submit() is meant to submit Gremlin to a Gremlin Server compliant system. To use TinkerGraph in that context would mean hosting TinkerGraph in Gremlin Server.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • This does not solve my problem. But it looks a good suggestion. – Sha Feb 13 '21 at 11:36
  • there is no way to "create a `Client` object from TinkerGraph". i read your question as wanting a way to process Gremlin scripts in your applications. i'm not sure how else to interpret your problem. perhaps you should restate it? – stephen mallette Feb 14 '21 at 13:31
  • stephen, can I convert traversal(coming from engine.eval) to ResultSet or Result like `client.submit() return type`(coming from typical client...)? – Sha Feb 19 '21 at 13:56
  • `Client.submit(Traversal)` - but i can't envision why you would do this. it sounds like have a more complicated approach than you need. – stephen mallette Feb 19 '21 at 15:38
  • In here, my aim is so simple; to write unit tests for Client. I want to use engine.eval("query") on test; use client.submit("query") on production. In here, query will be same execution will be different for test and production. – Sha Feb 21 '21 at 07:21
  • By the way, I dont ask `Client.submit(Traversal)` function; I ask that; is there a function like `engine.eval("query") => return ResultSet`. Why ResultSet; because I want to return same results on `test(engine.eval)` and `production(client.submit)`. – Sha Feb 21 '21 at 07:23
  • i understand now. thank you. sadly no, there is no good way to do what you want. we have an open ticket for this problem though: https://issues.apache.org/jira/browse/TINKERPOP-2428 Note that `ResultSet` does return `Iterable` - perhaps you can use that interface for now somehow? – stephen mallette Feb 21 '21 at 14:31