/** * 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")); }
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE); .map(p -> ((BindingsCustomizer) p)) .filter(bc -> bc.getScope() == ScriptContext.ENGINE_SCOPE) .forEach(bc -> engine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(bc.getBindings())); final Object initializedBindings = engine.eval(initScript); if (initializedBindings != null && initializedBindings instanceof Map) ((Map<String,Object>) initializedBindings).forEach((k,v) -> put(k,v));
final Bindings bindings = scriptEngine.createBindings(); 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);
private void registerLookUpInfo(final GremlinScriptEngine engine, final String shortName) { if (null == engine) throw new IllegalArgumentException(String.format("%s is not an available GremlinScriptEngine", shortName)); cache.putIfAbsent(shortName, engine); engine.getFactory().getExtensions().forEach(ext -> extensionToName.putIfAbsent(ext, shortName)); engine.getFactory().getMimeTypes().forEach(mime -> mimeToName.putIfAbsent(mime, shortName)); } }
final Bindings bindings = scriptEngine.createBindings(); 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);
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE); .map(p -> ((BindingsCustomizer) p)) .filter(bc -> bc.getScope() == ScriptContext.ENGINE_SCOPE) .forEach(bc -> engine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(bc.getBindings())); final Object initializedBindings = engine.eval(initScript); if (initializedBindings != null && initializedBindings instanceof Map) ((Map<String,Object>) initializedBindings).forEach((k,v) -> put(k,v));
/** * Gets a {@link GremlinScriptEngine} from cache or creates a new one from the {@link GremlinScriptEngineFactory}. * <p/> * {@inheritDoc} */ @Override public GremlinScriptEngine getEngineByExtension(final String extension) { if (!extensionToName.containsKey(extension)) { final GremlinScriptEngine engine = super.getEngineByExtension(extension); registerLookUpInfo(engine, engine.getFactory().getEngineName()); return engine; } return cache.get(extensionToName.get(extension)); }
@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())); } }
@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)); }
/** * 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); }
/** * Gets a {@link GremlinScriptEngine} from cache or creates a new one from the {@link GremlinScriptEngineFactory}. * <p/> * {@inheritDoc} */ @Override public GremlinScriptEngine getEngineByMimeType(final String mimeType) { if (!mimeToName.containsKey(mimeType)) { final GremlinScriptEngine engine = super.getEngineByMimeType(mimeType); registerLookUpInfo(engine, engine.getFactory().getEngineName()); return engine; } return cache.get(mimeToName.get(mimeType)); }
@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 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)); } }
@Test public void shouldGetEngineByName() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); assertEquals(ENGINE_TO_TEST, scriptEngine.getFactory().getEngineName()); }
@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 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")); }
private void registerLookUpInfo(final GremlinScriptEngine engine, final String shortName) { if (null == engine) throw new IllegalArgumentException(String.format("%s is not an available GremlinScriptEngine", shortName)); cache.putIfAbsent(shortName, engine); engine.getFactory().getExtensions().forEach(ext -> extensionToName.putIfAbsent(ext, shortName)); engine.getFactory().getMimeTypes().forEach(mime -> mimeToName.putIfAbsent(mime, shortName)); } }
@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"); }