/** * Evaluates bytecode with bindings for a specific language into a {@link Traversal}. */ public Traversal.Admin eval(final Bytecode bytecode, final Bindings boundVars, final String language, final String traversalSource) throws ScriptException { final String lang = Optional.ofNullable(language).orElse("gremlin-groovy"); final Bindings bindings = new SimpleBindings(); bindings.putAll(globalBindings); bindings.putAll(boundVars); return gremlinScriptEngineManager.getEngineByName(lang).eval(bytecode, bindings, traversalSource); }
@Test public void shouldHaveCoreImportsInPlace() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final List<Class> classesToCheck = Arrays.asList(Vertex.class, Edge.class, Graph.class, VertexProperty.class); for (Class clazz : classesToCheck) { assertEquals(clazz, scriptEngine.eval(clazz.getSimpleName())); } }
/** * Evaluates {@link Traversal} {@link Bytecode} against a traversal source in the global bindings of the * {@code ScriptEngine}. * * @param bytecode of the traversal to execute * @param traversalSource to execute the bytecode against which should be in the available bindings. */ public default Traversal.Admin eval(final Bytecode bytecode, final String traversalSource) throws ScriptException { final Bindings bindings = this.createBindings(); bindings.putAll(bytecode.getBindings()); return eval(bytecode, bindings, traversalSource); }
@Test public void shouldIncludeGlobalBindings() throws ScriptException { final GremlinScriptEngineManager manager = new DefaultGremlinScriptEngineManager(); final Bindings b = new SimpleBindings(); b.put("x", 1); b.put("y", 2); manager.addPlugin(BindingsGremlinPlugin.build(). bindings(b).create()); final GremlinScriptEngine engine = manager.getEngineByName(ENGINE_TO_TEST); assertEquals(1, engine.eval("x")); assertEquals(2, engine.eval("y")); }
@Test public void shouldAddLazyGlobalBindingsViaPlugin() throws Exception { final Bindings bindings = new SimpleBindings(); bindings.put("x", 100); final BindingsGremlinPlugin plugin = new BindingsGremlinPlugin(() -> bindings); final DefaultGremlinScriptEngineManager manager = new DefaultGremlinScriptEngineManager(); manager.addPlugin(plugin); final GremlinScriptEngine engine = manager.getEngineByName(ENGINE_TO_TEST); assertThat(engine.getBindings(ScriptContext.GLOBAL_SCOPE).size(), is(1)); assertEquals(101, (int) engine.eval("x+1")); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithInvalidBinding() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("z", g); bindings.put("x", "invalid-binding-for-x-given-x-should-be-traversal-source"); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, "x"); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithAliasInBindings() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("x", g); bindings.put(GremlinScriptEngine.HIDDEN_G, g); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, "x"); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithMissingBinding() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("z", g); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, "x"); }
final Object o = gremlinScriptEngineManager.getEngineByName(lang).eval(script, bindings);
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithAliasAsTraversalSource() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("x", g); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, GremlinScriptEngine.HIDDEN_G); }
final Object initializedBindings = engine.eval(initScript); if (initializedBindings != null && initializedBindings instanceof Map) ((Map<String,Object>) initializedBindings).forEach((k,v) -> put(k,v));
@Test public void shouldEvalBytecode() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("x", g); final Traversal evald = scriptEngine.eval(t.asAdmin().getBytecode(), bindings, "x"); assertTraversals(t, evald); assertThat(manager.getBindings().containsKey(GremlinScriptEngine.HIDDEN_G), is(false)); }
@Test public void shouldRemoveGlobalBindings() throws Exception { final DefaultGremlinScriptEngineManager manager = new DefaultGremlinScriptEngineManager(); manager.put("x", 100); final GremlinScriptEngine engine1 = manager.getEngineByName(ENGINE_TO_TEST); assertThat(engine1.getBindings(ScriptContext.GLOBAL_SCOPE).size(), is(1)); assertEquals(101, (int) engine1.eval("x+1")); manager.getBindings().remove("x"); final GremlinScriptEngine engine2 = manager.getEngineByName(ENGINE_TO_TEST); assertThat(engine2.getBindings(ScriptContext.GLOBAL_SCOPE).size(), is(0)); }
@Test public void shouldExtractGlobalBindingsAfterScriptExecution() throws Exception { assumeThat("Only works with gremlin-groovy", ENGINE_TO_TEST, is("gremlin-groovy")); final GremlinScriptEngineManager manager = new DefaultGremlinScriptEngineManager(); final Bindings b1 = new SimpleBindings(); b1.put("x", 1); b1.put("y", 2); manager.addPlugin(BindingsGremlinPlugin.build().bindings(b1).create()); final Bindings b2 = new SimpleBindings(); b2.put("x", 100); b2.put("y", 200); manager.addPlugin(BindingsGremlinPlugin.build().bindings(b2).create()); final File scriptFile = TestHelper.generateTempFileFromResource(BindingsScriptEngineTest.class, "bindings-init.groovy", ".groovy"); final List<String> files = new ArrayList<>(); files.add(scriptFile.getAbsolutePath()); manager.addPlugin(ScriptFileGremlinPlugin.build().files(files).create()); final GremlinScriptEngine engine = manager.getEngineByName(ENGINE_TO_TEST); assertEquals(100, engine.eval("x")); assertEquals(200, engine.eval("y")); assertEquals(300, engine.eval("z")); assertEquals(600, engine.eval("addItUp(z, addItUp(x,y))")); assertEquals(300, engine.getBindings(ScriptContext.GLOBAL_SCOPE).get("z")); assertThat(engine.getBindings(ScriptContext.ENGINE_SCOPE).containsKey("z"), is(false)); } }
bindings.putAll(scriptEngine.getContext().getBindings(ScriptContext.ENGINE_SCOPE)); bindings.put(this.translator.getTraversalSource().toString(), this.traversalSource); translatedTraversal = (Traversal.Admin<?, ?>) scriptEngine.eval(bytecode, bindings, this.translator.getTraversalSource().toString()); } catch (final Exception e) { throw new IllegalArgumentException(e.getMessage(), e);
/** * Evaluates bytecode with bindings for a specific language into a {@link Traversal}. */ public Traversal.Admin eval(final Bytecode bytecode, final Bindings boundVars, final String language, final String traversalSource) throws ScriptException { final String lang = Optional.ofNullable(language).orElse("gremlin-groovy"); final Bindings bindings = new SimpleBindings(); bindings.putAll(globalBindings); bindings.putAll(boundVars); return gremlinScriptEngineManager.getEngineByName(lang).eval(bytecode, bindings, traversalSource); }
/** * Evaluates {@link Traversal} {@link Bytecode} against a traversal source in the global bindings of the * {@code ScriptEngine}. * * @param bytecode of the traversal to execute * @param traversalSource to execute the bytecode against which should be in the available bindings. */ public default Traversal.Admin eval(final Bytecode bytecode, final String traversalSource) throws ScriptException { final Bindings bindings = this.createBindings(); bindings.putAll(bytecode.getBindings()); return eval(bytecode, bindings, traversalSource); }
@Test public void shouldAddLazyGlobalBindingsViaPlugin() throws Exception { final Bindings bindings = new SimpleBindings(); bindings.put("x", 100); final BindingsGremlinPlugin plugin = new BindingsGremlinPlugin(() -> bindings); final DefaultGremlinScriptEngineManager manager = new DefaultGremlinScriptEngineManager(); manager.addPlugin(plugin); final GremlinScriptEngine engine = manager.getEngineByName(ENGINE_TO_TEST); assertThat(engine.getBindings(ScriptContext.GLOBAL_SCOPE).size(), is(1)); assertEquals(101, (int) engine.eval("x+1")); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowBytecodeEvalWithAliasAsTraversalSource() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); final Graph graph = EmptyGraph.instance(); final GraphTraversalSource g = graph.traversal(); // purposefully use "x" to match the name of the traversal source binding for "x" below and // thus tests the alias added for "x" final GraphTraversal t = getTraversalWithLambda(g); final Bindings bindings = new SimpleBindings(); bindings.put("x", g); scriptEngine.eval(t.asAdmin().getBytecode(), bindings, GremlinScriptEngine.HIDDEN_G); }
@Test public void shouldRemoveGlobalBindings() throws Exception { final DefaultGremlinScriptEngineManager manager = new DefaultGremlinScriptEngineManager(); manager.put("x", 100); final GremlinScriptEngine engine1 = manager.getEngineByName(ENGINE_TO_TEST); assertThat(engine1.getBindings(ScriptContext.GLOBAL_SCOPE).size(), is(1)); assertEquals(101, (int) engine1.eval("x+1")); manager.getBindings().remove("x"); final GremlinScriptEngine engine2 = manager.getEngineByName(ENGINE_TO_TEST); assertThat(engine2.getBindings(ScriptContext.GLOBAL_SCOPE).size(), is(0)); }