banjara
banjara

Reputation: 3890

Converting gremlin query from gremlin console to Bytecode

I am trying to convert gremlin query received from gremlin console to bytecode in order to extract StepInstructions. I am using the below code to do that but it looks hacky and ugly to me. Is there any better way of converting gremlin query from gremlin console to Bytecode?

String query = (String) requestMessage.getArgs().get(Tokens.ARGS_GREMLIN);
final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
CompiledScript compiledScript = engine.compile(query);

final Graph graph = EmptyGraph.instance();
final GraphTraversalSource g = graph.traversal();

final Bindings bindings = engine.createBindings();
bindings.put("g", g);

DefaultGraphTraversal graphTraversal = (DefaultGraphTraversal) compiledScript.eval(bindings);

Bytecode bytecode = graphTraversal.getBytecode();

Upvotes: 0

Views: 418

Answers (1)

stephen mallette
stephen mallette

Reputation: 46216

If you need to take a Gremlin string and convert it to Bytecode I don't think there is a much better way to do that. You must pass the string through a GremlinGroovyScriptEngine to evaluate it into an actual Traversal object that you can manipulate. The only improvement that I can think of would be to call eval() more directly:

// construct all of this once and re-use it for your application
final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
final Graph graph = EmptyGraph.instance();
final GraphTraversalSource g = graph.traversal();
final Bindings bindings = engine.createBindings();
bindings.put("g", g);

//////////////

String query = (String) requestMessage.getArgs().get(Tokens.ARGS_GREMLIN);
DefaultGraphTraversal graphTraversal = (DefaultGraphTraversal) engine.eval(query, bindings);    
Bytecode bytecode = graphTraversal.getBytecode();

Upvotes: 2

Related Questions