public Object eval(ScriptEngine engine, String script, ScriptContext context) throws ScriptException { if (engine instanceof Compilable && Config.SCRIPT_ALLOW_COMPILATION) { Compilable eng = (Compilable) engine; CompiledScript cs = eng.compile(script); return context != null ? cs.eval(context) : cs.eval(); } else return context != null ? engine.eval(script, context) : engine.eval(script); }
boolean init_functions() { GenericDialog gd = new GenericDialog("Fitting Options"); gd.addStringField("Extra Definitions", exdef, 50); gd.addCheckbox("Weight Using Plot Errors", false); gd.addStringField("Weighting Equation (y is for data)", weightfunction, 50); gd.addStringField("Fit_Equation", function, 50); gd.showDialog(); if (gd.wasCanceled()) { return false; } exdef = gd.getNextString(); boolean errweights = gd.getNextBoolean(); weightfunction = gd.getNextString(); function = gd.getNextString(); // first initialize the weights weights = new float[tempdata.length]; if (errweights || weightfunction.equals("") || weightfunction == null || weightfunction == "1.0") { if (errweights) { for (int i = 0; i < tempdata.length; i++) weights[i] = 1.0f / (errs[i] * errs[i]); } else { for (int i = 0; i < tempdata.length; i++) weights[i] = 1.0f; } } else { for (int i = 0; i < tempdata.length; i++) { String script = "y =" + tempdata[i] + "; " + "x =" + tempx[i] + "; " + "retval=" + weightfunction + ";"; Double temp = new Double(0.0); try { temp = (Double) engine.eval(script); } catch (Exception e) { IJ.log(e.getMessage()); } if (!(temp.isInfinite() || temp.isNaN())) { weights[i] = temp.floatValue(); } } } // now compile the function script try { String script1 = exdef + "; retval=" + function + ";"; cs = ce.compile(script1); } catch (Exception e) { IJ.log(e.toString()); return false; } return true; }
public void executeScript(ScriptEngine engine, File file) throws FileNotFoundException, ScriptException { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); if (Config.SCRIPT_DEBUG) { _log.info("Loading Script: " + file.getAbsolutePath()); } if (Config.SCRIPT_ERROR_LOG) { String name = file.getAbsolutePath() + ".error.log"; File errorLog = new File(name); if (errorLog.isFile()) { errorLog.delete(); } } if (engine instanceof Compilable && Config.SCRIPT_ALLOW_COMPILATION) { ScriptContext context = new SimpleScriptContext(); context.setAttribute( "mainClass", getClassForFile(file).replace('/', '.').replace('\\', '.'), ScriptContext.ENGINE_SCOPE); context.setAttribute(ScriptEngine.FILENAME, file.getName(), ScriptContext.ENGINE_SCOPE); context.setAttribute( "classpath", SCRIPT_FOLDER.getAbsolutePath(), ScriptContext.ENGINE_SCOPE); context.setAttribute( "sourcepath", SCRIPT_FOLDER.getAbsolutePath(), ScriptContext.ENGINE_SCOPE); context.setAttribute( JythonScriptEngine.JYTHON_ENGINE_INSTANCE, engine, ScriptContext.ENGINE_SCOPE); setCurrentLoadingScript(file); ScriptContext ctx = engine.getContext(); try { engine.setContext(context); if (Config.SCRIPT_CACHE) { CompiledScript cs = _cache.loadCompiledScript(engine, file); cs.eval(context); } else { Compilable eng = (Compilable) engine; CompiledScript cs = eng.compile(reader); cs.eval(context); } } finally { engine.setContext(ctx); setCurrentLoadingScript(null); context.removeAttribute(ScriptEngine.FILENAME, ScriptContext.ENGINE_SCOPE); context.removeAttribute("mainClass", ScriptContext.ENGINE_SCOPE); } } else { ScriptContext context = new SimpleScriptContext(); context.setAttribute( "mainClass", getClassForFile(file).replace('/', '.').replace('\\', '.'), ScriptContext.ENGINE_SCOPE); context.setAttribute(ScriptEngine.FILENAME, file.getName(), ScriptContext.ENGINE_SCOPE); context.setAttribute( "classpath", SCRIPT_FOLDER.getAbsolutePath(), ScriptContext.ENGINE_SCOPE); context.setAttribute( "sourcepath", SCRIPT_FOLDER.getAbsolutePath(), ScriptContext.ENGINE_SCOPE); setCurrentLoadingScript(file); try { engine.eval(reader, context); } finally { setCurrentLoadingScript(null); engine.getContext().removeAttribute(ScriptEngine.FILENAME, ScriptContext.ENGINE_SCOPE); engine.getContext().removeAttribute("mainClass", ScriptContext.ENGINE_SCOPE); } } }