/** * Delegates calls to the {@link CachedGremlinScriptEngineManager} instance ensuring that the same instance * is returned for each {@code ScriptEngine} requested. */ public static GremlinScriptEngine get(final String scriptEngineName) { return cached.getEngineByName(scriptEngineName); } }
public ScriptRecordWriter(final DataOutputStream out, final TaskAttemptContext context) throws IOException { this.out = out; final Configuration configuration = context.getConfiguration(); this.engine = manager.getEngineByName(configuration.get(SCRIPT_ENGINE, "gremlin-groovy")); final FileSystem fs = FileSystem.get(configuration); try { this.engine.eval(new InputStreamReader(fs.open(new Path(configuration.get(SCRIPT_FILE))))); } catch (final ScriptException e) { throw new IOException(e.getMessage(),e); } }
@Test public void shouldBeSameInstance() { final Set<GremlinScriptEngine> scriptEngines = new HashSet<>(); IntStream.range(0,100).forEach(i -> scriptEngines.add(manager.getEngineByName(ENGINE_TO_TEST))); assertEquals(1, scriptEngines.size()); }
/** * Attempts to compile a script and cache it in the request {@link javax.script.ScriptEngine}. This is only * possible if the {@link javax.script.ScriptEngine} implementation implements {@link Compilable}. * In the event that the requested {@link javax.script.ScriptEngine} does not implement it, the method will * return empty. */ public Optional<CompiledScript> compile(final String script, final Optional<String> language) throws ScriptException { final String lang = language.orElse("gremlin-groovy"); try { final GremlinScriptEngine scriptEngine = gremlinScriptEngineManager.getEngineByName(lang); if (scriptEngine instanceof Compilable) return Optional.of(((Compilable) scriptEngine).compile(script)); else return Optional.empty(); } catch (UnsupportedOperationException uoe) { return Optional.empty(); } }
/** * 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())); } }
@Override public void initialize(final InputSplit genericSplit, final TaskAttemptContext context) throws IOException { this.lineRecordReader.initialize(genericSplit, context); final Configuration configuration = context.getConfiguration(); if (configuration.get(Constants.GREMLIN_HADOOP_GRAPH_FILTER, null) != null) this.graphFilter = VertexProgramHelper.deserialize(ConfUtil.makeApacheConfiguration(configuration), Constants.GREMLIN_HADOOP_GRAPH_FILTER); this.engine = manager.getEngineByName(configuration.get(SCRIPT_ENGINE, "gremlin-groovy")); final FileSystem fs = FileSystem.get(configuration); try (final InputStream stream = fs.open(new Path(configuration.get(SCRIPT_FILE))); final InputStreamReader reader = new InputStreamReader(stream)) { final String parse = String.join("\n", IOUtils.toString(reader), READ_CALL); script = ((Compilable) engine).compile(parse); } catch (ScriptException e) { throw new IOException(e.getMessage()); } }
private void registerMetrics(final String engineName) { final GremlinScriptEngine engine = gremlinExecutor.getScriptEngineManager().getEngineByName(engineName); MetricManager.INSTANCE.registerGremlinScriptEngineMetrics(engine, engineName, "session", session, "class-cache"); } }
private void registerMetrics(final String engineName) { final GremlinScriptEngine engine = gremlinExecutor.getScriptEngineManager().getEngineByName(engineName); MetricManager.INSTANCE.registerGremlinScriptEngineMetrics(engine, engineName, "sessionless", "class-cache"); }
@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 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"); }
@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"); }
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); }
@Test public void shouldGetEngineByName() throws Exception { final GremlinScriptEngine scriptEngine = manager.getEngineByName(ENGINE_TO_TEST); assertEquals(ENGINE_TO_TEST, scriptEngine.getFactory().getEngineName()); }
@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 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)); } }
/** * Delegates calls to the {@link CachedGremlinScriptEngineManager} instance ensuring that the same instance * is returned for each {@code ScriptEngine} requested. */ public static GremlinScriptEngine get(final String scriptEngineName) { return cached.getEngineByName(scriptEngineName); } }
@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); }