protected Object evaluateExpression(Object expression, Map<String, Object> parameters) { try { Object result = null; StringWriter writer = new StringWriter(); ScriptContext context = new SimpleScriptContext(); for (Map.Entry<String, Object> property : engineProperties.entrySet()) { context.setAttribute(property.getKey(), property.getValue(), ScriptContext.ENGINE_SCOPE); } Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); bindings.putAll(parameters); context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); context.setWriter(writer); if (expression instanceof CompiledScript) { logger.debug("About to evaluate compiled expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = ((CompiledScript) expression).eval(context); } else { logger.debug("About to evaluate expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = scriptEngine.eval(expression.toString(), context); } if (result == null) { result = writer.toString(); } return result; } catch (ScriptException e) { throw new RuntimeException("Error when evaluating script", 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); }
@Override public Object eval(final String script, final Bindings n) throws ScriptException { this.pyScriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(n); // TODO: groovy and jython act different return this.pyScriptEngine.eval(script); }
@Override public Object eval(final Reader reader, final Bindings n) throws ScriptException { this.pyScriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(n); // TODO: groovy and jython act different return this.pyScriptEngine.eval(reader); }
private Bindings createBindings(final Map<String,Object> bindingMap, final Map<String,String> rebindingMap) { final Bindings bindings = new SimpleBindings(); // rebind any global bindings to a different variable. if (!rebindingMap.isEmpty()) { for (Map.Entry<String, String> kv : rebindingMap.entrySet()) { boolean found = false; final Graph g = this.graphManager.getGraph(kv.getValue()); if (null != g) { bindings.put(kv.getKey(), g); found = true; } if (!found) { final TraversalSource ts = this.graphManager.getTraversalSource(kv.getValue()); if (null != ts) { bindings.put(kv.getKey(), ts); found = true; } } if (!found) { final String error = String.format("Could not rebind [%s] to [%s] as [%s] not in the Graph or TraversalSource global bindings", kv.getKey(), kv.getValue(), kv.getValue()); throw new IllegalStateException(error); } } } bindings.putAll(bindingMap); return bindings; }
@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); }
@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); }
/** * 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); }
@Override public Object evaluateExpression(String expression, JadeModel model) throws ExpressionException { try{ Bindings bindings = jsEngine.createBindings(); bindings.putAll(model); Object eval; if(expression.startsWith("{")){ eval = ((Map)jsEngine.eval("["+expression+"]", bindings)).get("0"); }else{ eval = jsEngine.eval(expression, bindings); } for (Map.Entry<String, Object> stringObjectEntry : bindings.entrySet()) { model.put(stringObjectEntry.getKey(),convertToJadeModelValue(stringObjectEntry.getValue())); } return convertToJadeModelValue(eval); } catch (ScriptException ex){ // return expression; throw new ExpressionException(expression, ex); } }
.map(p -> ((BindingsCustomizer) p)) .filter(bc -> bc.getScope() == ScriptContext.ENGINE_SCOPE) .forEach(bc -> engine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(bc.getBindings()));
bindings.putAll(this.bindings);
.beforeEval(b -> { try { b.putAll(bindingsSupplier.get()); } catch (OpProcessorException ope) {
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());
@SuppressWarnings("unchecked") @Override public T execute(String script, Map<String, Object> parameters) throws ScriptException { ScriptEngine scriptEngine = manager.getEngineByName("js"); Bindings binding = scriptEngine.createBindings(); if (parameters != null) { binding.putAll(parameters); } Object val = scriptEngine.eval(script, binding); return ((T) val); }
protected Bindings createLocal(Bindings globalBindings) throws ScriptException { Bindings localBindings = jsEngine.createBindings(); localBindings.putAll(globalBindings); return localBindings; }
private Bindings createBinding(final NodeModel node, ScriptContext scriptContext) { final Bindings binding = engine.createBindings(); binding.put("c", ProxyFactory.createController(scriptContext)); binding.put("node", ProxyFactory.createNode(node, scriptContext)); binding.putAll(ScriptingConfiguration.getStaticProperties()); return binding; }
@Override public T execute(String script,Map<String, Object> parameters) throws ScriptException { Bindings bindings = compiledScript.getEngine().createBindings(); if (parameters != null) { bindings.putAll(parameters); } T result = (T) compiledScript.eval(bindings); return result; }
/** * 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); }