将 gremlin 查询从 gremlin 控制台转换为字节码

Converting gremlin query from gremlin console to Bytecode

我正在尝试将从 gremlin 控制台收到的 gremlin 查询转换为字节码以便提取 StepInstructions。我正在使用下面的代码来做到这一点,但对我来说它看起来很古怪和丑陋。有没有更好的方法将 gremlin 查询从 gremlin 控制台转换为字节码?

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();

如果您需要获取 Gremlin 字符串并将其转换为 Bytecode,我认为没有更好的方法可以做到这一点。您必须通过 GremlinGroovyScriptEngine 传递字符串以将其评估为您可以操作的实际 Traversal 对象。我能想到的唯一改进是更直接地调用 eval()

// 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();