Java Code Examples for org.scijava.script.ScriptLanguage#getScriptEngine()

The following examples show how to use org.scijava.script.ScriptLanguage#getScriptEngine() . 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: 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);
	}
}