public boolean containsKey(Object key) { for (Resolver scriptResolver : scriptResolvers) { if (scriptResolver.containsKey(key)) { return true; } } return defaultBindings.containsKey(key); }
/** * {@inheritDoc} */ @Override public int getAttributesScope(final String name) { checkName(name); if (engineScope.containsKey(name)) return ENGINE_SCOPE; else if (globalScope != null && globalScope.containsKey(name)) return GLOBAL_SCOPE; else return -1; }
public boolean containsKey(Object key) { for (Resolver scriptResolver: scriptResolvers) { if (scriptResolver.containsKey(key)) { return true; } } return wrappedBindings.containsKey(key); }
public boolean containsKey(Object key) { for (Resolver scriptResolver: scriptResolvers) { if (scriptResolver.containsKey(key)) { return true; } } return wrappedBindings.containsKey(key); }
/** * Dedicated implementation which does not fall back on the {@link #calculateBindingMap()} for performance reasons */ public boolean containsKey(Object key) { if(wrappedBindings.containsKey(key)) { return true; } if (key instanceof String) { return variableContext.containsVariable((String) key); } else { return false; } }
/** * {@inheritDoc} */ @Override public Object getAttribute(final String name) { checkName(name); if (engineScope.containsKey(name)) { return getAttribute(name, ENGINE_SCOPE); } else if (globalScope != null && globalScope.containsKey(name)) { return getAttribute(name, GLOBAL_SCOPE); } return null; }
@Override public Traversal.Admin eval(final Bytecode bytecode, final Bindings bindings, final String traversalSource) throws ScriptException { // these validations occur before merging in bytecode bindings which will override existing ones. need to // extract the named traversalsource prior to that happening so that bytecode bindings can share the same // namespace as global bindings (e.g. traversalsources and graphs). if (traversalSource.equals(HIDDEN_G)) throw new IllegalArgumentException("The traversalSource cannot have the name " + HIDDEN_G + " - it is reserved"); if (bindings.containsKey(HIDDEN_G)) throw new IllegalArgumentException("Bindings cannot include " + HIDDEN_G + " - it is reserved"); if (!bindings.containsKey(traversalSource)) throw new IllegalArgumentException("The bindings available to the ScriptEngine do not contain a traversalSource named: " + traversalSource); final Object b = bindings.get(traversalSource); if (!(b instanceof TraversalSource)) throw new IllegalArgumentException(traversalSource + " is of type " + b.getClass().getSimpleName() + " and is not an instance of TraversalSource"); final Bindings inner = new SimpleBindings(); inner.putAll(bindings); inner.putAll(bytecode.getBindings()); inner.put(HIDDEN_G, b); return (Traversal.Admin) this.eval(GroovyTranslator.of(HIDDEN_G, typeTranslator).translate(bytecode), inner); }
@Override public Traversal.Admin eval(final Bytecode bytecode, final Bindings bindings, final String traversalSource) throws ScriptException { // these validations occur before merging in bytecode bindings which will override existing ones. need to // extract the named traversalsource prior to that happening so that bytecode bindings can share the same // namespace as global bindings (e.g. traversalsources and graphs). if (traversalSource.equals(HIDDEN_G)) throw new IllegalArgumentException("The traversalSource cannot have the name " + HIDDEN_G + " - it is reserved"); if (!bindings.containsKey(traversalSource)) throw new IllegalArgumentException("The bindings available to the ScriptEngine do not contain a traversalSource named: " + traversalSource); final Object b = bindings.get(traversalSource); if (!(b instanceof TraversalSource)) throw new IllegalArgumentException(traversalSource + " is of type " + b.getClass().getSimpleName() + " and is not an instance of TraversalSource"); final Bindings inner = new SimpleBindings(); inner.putAll(bindings); inner.putAll(bytecode.getBindings()); inner.put(HIDDEN_G, b); return (Traversal.Admin) this.eval(JythonTranslator.of(HIDDEN_G).translate(bytecode), inner); }
public Object get(Object key) { Object result = null; if(wrappedBindings.containsKey(key)) { result = wrappedBindings.get(key); } else { for (Resolver scriptResolver: scriptResolvers) { if (scriptResolver.containsKey(key)) { result = scriptResolver.get(key); } } } return result; }
public Object get(Object key) { Object result = null; if(wrappedBindings.containsKey(key)) { result = wrappedBindings.get(key); } else { for (Resolver scriptResolver: scriptResolvers) { if (scriptResolver.containsKey(key)) { result = scriptResolver.get(key); } } } return result; }
/** * Dedicated implementation which does not fall back on the {@link #calculateBindingMap()} for performance reasons */ public Object get(Object key) { Object result = null; if(wrappedBindings.containsKey(key)) { result = wrappedBindings.get(key); } else { if (key instanceof String) { TypedValue resolvedValue = variableContext.resolve((String) key); result = unpack(resolvedValue); } } return result; }
.flatMap(bc -> bc.getBindings().entrySet().stream()) .forEach(kv -> { if (globalScope.containsKey(kv.getKey())) { logger.warn("Overriding the global binding [{}] - was [{}] and is now [{}]", kv.getKey(), globalScope.get(kv.getKey()), kv.getValue());
@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 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)); } }
/** {@inheritDoc} */ public int getAttributesScope(String name) { checkName(name); if (engineScope.containsKey(name)) { return ENGINE_SCOPE; } else if(globalScope != null && globalScope.containsKey(name)) { return GLOBAL_SCOPE; } return -1; }
public Object getAttribute(String name) { if (name == null) throw new IllegalArgumentException("Name is null"); if (engineScope != null && engineScope.containsKey(name)) return engineScope.get(name); if (globalScope != null && globalScope.containsKey(name)) return globalScope.get(name); return null; }
@Override public Object remove(Object key) { if (systemBindings.containsKey(key)) { throw new IllegalArgumentException(); } else { return userBindings.remove(key); } } }
public Object getAttribute(String name) { if (engineScope.containsKey(name)) { return getAttribute(name, ENGINE_SCOPE); } else if (globalScope != null && globalScope.containsKey(name)) { return getAttribute(name, GLOBAL_SCOPE); } return null; }
@Override public Object get(Object key) { if (systemBindings.containsKey(key)) { return systemBindings.get(key); } else { return userBindings.get(key); } }
public int getAttributesScope(String name) { if (name == null) throw new IllegalArgumentException("Name is null"); for (int scope : getScopes()) { if (getBindings(scope) != null && getBindings(scope).containsKey(name)) { return scope; } } return -1; }