org.scijava.script.ScriptLanguage Java Examples

The following examples show how to use org.scijava.script.ScriptLanguage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestScriptEngine.java    From scijava-jupyter-kernel with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws ScriptException {
    // Only for testing purpose

    Context context = new Context();
    ScriptService scriptService = context.getService(ScriptService.class);
    ScriptLanguage scriptLanguage = scriptService.getLanguageByName("python");
    ScriptEngine engine = scriptLanguage.getScriptEngine();

    Object result = engine.eval("p=999\n555");
    System.out.println(result);

    scriptService = context.getService(ScriptService.class);
    scriptLanguage = scriptService.getLanguageByName("python");
    engine = scriptLanguage.getScriptEngine();
    
    result = engine.eval("555");
    System.out.println(result);

    context.dispose();
}
 
Example #2
Source File: ScriptEval.java    From imagej-server with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
	// Make an honest effort to figure out what language they mean. :-)
	final String ext;
	final ScriptLanguage langByExt = //
		scriptService.getLanguageByExtension(language);
	if (langByExt != null) ext = language;
	else {
		final ScriptLanguage langByName = scriptService.getLanguageByName(
			language);
		if (langByName != null) ext = langByName.getExtensions().get(0);
		else throw new IllegalArgumentException("Unknown language: " + language);
	}
	final String fakePath = "script." + ext;

	try {
		final Map<String, Object> safeArgs = //
			args == null ? Collections.emptyMap() : args;
		outputs = scriptService.run(fakePath, script, process, safeArgs).get()
			.getOutputs();
	}
	catch (final InterruptedException | ExecutionException exc) {
		throw new IllegalArgumentException(exc);
	}
}
 
Example #3
Source File: Worker.java    From scijava-jupyter-kernel with Apache License 2.0 5 votes vote down vote up
private void syncBindings(ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) {

	Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
	this.scriptEngines.forEach((String name, ScriptEngine engine) -> {
	    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
	    currentBindings.keySet().forEach((String key) -> {
		bindings.put(key, scriptLanguage.decode(currentBindings.get(key)));
	    });
	});

    }
 
Example #4
Source File: ScijavaEvaluator.java    From scijava-jupyter-kernel with Apache License 2.0 5 votes vote down vote up
private void addLanguage(String langName) {

        if (scriptService.getLanguageByName(langName) == null) {
            log.error("Script Language for '" + langName + "' not found.");
            System.exit(1);
        }

        if (!this.scriptLanguages.keySet().contains(langName)) {

            Bindings bindings = null;
            if (!this.scriptEngines.isEmpty()) {
                String firstLanguage = this.scriptEngines.keySet().iterator().next();
                bindings = this.scriptEngines.get(firstLanguage).getBindings(ScriptContext.ENGINE_SCOPE);
            }

            log.info("Script Language for '" + langName + "' found.");
            ScriptLanguage scriptLanguage = scriptService.getLanguageByName(langName);
            this.scriptLanguages.put(langName, scriptLanguage);

            ScriptEngine engine = this.scriptLanguages.get(langName).getScriptEngine();
            this.scriptEngines.put(langName, engine);

            AutoCompleter completer = scriptLanguage.getAutoCompleter();
            this.completers.put(languageName, completer);

            // Not implemented yet
            //engine.setBindings(this.bindings, ScriptContext.ENGINE_SCOPE);
            if (bindings != null) {
                this.initBindings(bindings, engine, scriptLanguage);
            }

        }

        log.debug("Script Language found for '" + langName + "'");
    }
 
Example #5
Source File: ScijavaEvaluator.java    From scijava-jupyter-kernel with Apache License 2.0 5 votes vote down vote up
private void initBindings(Bindings bindings, ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) {

        Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
        bindings.keySet().forEach((String key) -> {
            currentBindings.put(key, scriptLanguage.decode(bindings.get(key)));
        });

    }
 
Example #6
Source File: REPLEditor.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void setREPLLanguage(String language) {
  System.out.println("Resetting language to " + language);
  if(!repl.getInterpreter().getLanguage().getNames().contains(language)) {
    repl.lang(language);
  }
  ScriptLanguage l = scriptService.getLanguageByName(language);
  setLanguage(scriptService.getLanguageByName(language));

  final ScriptContext ctx = repl.getInterpreter().getEngine().getContext();
  ctx.setErrorWriter(outputPane.getOutputWriter());
  ctx.setWriter(outputPane.getOutputWriter());
}
 
Example #7
Source File: Worker.java    From scijava-jupyter-kernel with Apache License 2.0 4 votes vote down vote up
Worker(Context context, Map<String, ScriptEngine> scriptEngines, Map<String, ScriptLanguage> scriptLanguages) {
context.inject(this);
this.scriptEngines = scriptEngines;
this.scriptLanguages = scriptLanguages;
   }
 
Example #8
Source File: DefaultEquation.java    From imagej-ops with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void compute(final String input, final IterableInterval<T> output) {
	final String equation = input + ";";

	// evaluate the equation using Javascript!
	final ScriptLanguage js = scriptService.getLanguageByName("javascript");
	final ScriptEngine engine = js.getScriptEngine();
	final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);

	final Cursor<T> c = output.localizingCursor();
	final long[] pos = new long[output.numDimensions()];
	bindings.put("p", pos);
	bindings.put("c", c);

	if (engine instanceof Compilable) try {
		final String script = "importClass(Packages.java.lang.Double);\n" +
			"while (c.hasNext()) {\n" + "  c.fwd();\n" + "  c.localize(p);\n" +
			"  o = " + equation + ";\n" + "  try {\n" +
			"    c.get().setReal(o);\n" + "  } catch(e) {" +
			"    c.get().setReal(Double.NaN);\n" + "  }\n" + "}";
		final Compilable compiler = (Compilable) engine;
		final CompiledScript compiled = compiler.compile(script);
		compiled.eval(bindings);
	}
	catch (final ScriptException e) {
		log.warn(e);
		// fallthru
	}

	try {
		while (c.hasNext()) {
			c.fwd();
			c.localize(pos);
			final Object o = engine.eval(equation);
			final double d = o instanceof Number ? ((Number) o).doubleValue()
				: Double.NaN;
			c.get().setReal(d);
		}
	}
	catch (final ScriptException exc) {
		log.error(exc);
	}
}