From: <ha...@us...> - 2012-12-06 16:56:17
|
Revision: 17789 http://jmol.svn.sourceforge.net/jmol/?rev=17789&view=rev Author: hansonr Date: 2012-12-06 16:56:07 +0000 (Thu, 06 Dec 2012) Log Message: ----------- recoding TRY/CATCH out of ScriptParallelProcessor in preparation for JavaScript version Modified Paths: -------------- trunk/Jmol/applet.classes trunk/Jmol/build.xml trunk/Jmol/src/org/jmol/awt/JmolFileAdapter.java trunk/Jmol/src/org/jmol/script/ScriptCompiler.java trunk/Jmol/src/org/jmol/script/ScriptContext.java trunk/Jmol/src/org/jmol/script/ScriptEvaluator.java trunk/Jmol/src/org/jmol/script/ScriptFunction.java trunk/Jmol/src/org/jmol/script/ScriptMathProcessor.java trunk/Jmol/src/org/jmol/viewer/Viewer.java Added Paths: ----------- trunk/Jmol/src/org/jmol/api/JmolParallelProcessor.java trunk/Jmol/src/org/jmol/parallel/ trunk/Jmol/src/org/jmol/parallel/ScriptParallelProcessor.java trunk/Jmol/src/org/jmol/parallel/ScriptProcessRunnable.java Removed Paths: ------------- trunk/Jmol/src/org/jmol/thread/ScriptParallelProcessor.java trunk/Jmol/src/org/jmol/thread/ScriptProcessRunnable.java Modified: trunk/Jmol/applet.classes =================================================================== --- trunk/Jmol/applet.classes 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/applet.classes 2012-12-06 16:56:07 UTC (rev 17789) @@ -37,6 +37,7 @@ org/jmol/multitouch/*.java org/jmol/multitouch/sparshui/*.java org/jmol/navigate/*.java +org/jmol/parallel/*.java org/jmol/popup/*.java com/sparshui/client/*.java Modified: trunk/Jmol/build.xml =================================================================== --- trunk/Jmol/build.xml 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/build.xml 2012-12-06 16:56:07 UTC (rev 17789) @@ -738,6 +738,12 @@ </fileset> </jar> + <jar destfile="${jmol.applet.build.dir}/JmolApplet0_Parallel.jar" manifest="manifest/applet.txt"> + <fileset dir="${applet.classes.dir}"> + <include name="org/jmol/parallel/**" /> + </fileset> + </jar> + <jar destfile="${jmol.applet.build.dir}/JmolApplet0_Spectrum.jar" manifest="manifest/applet.txt"> <fileset dir="${applet.classes.dir}"> <include name="org/jmol/spectrum/**" /> @@ -787,6 +793,7 @@ <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Console.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Minimize.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Navigate.jar" /> + <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Parallel.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Popup.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_ReadersCifPdb.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_ReadersMolXyz.jar" /> @@ -799,6 +806,7 @@ <pathelement path="${jmol.applet.build.dir}/JmolApplet0_ShapeSpecial.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_ShapeSurface.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Smiles.jar" /> + <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Spectrum.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Symmetry.jar" /> <fileset dir="${jmol.applet.build.dir}" includes="JmolApplet0_i18n_*.jar" /> @@ -883,6 +891,11 @@ keystore="selfSignedCertificate/selfSignedCertificate.store" storepass="selfSignedCertificate" alias="selfSignedCertificate" /> + <signjar jar="${jmol.applet.build.dir}/JmolApplet0_Parallel.jar" + signedjar="${jmol.applet.build.dir}/JmolAppletSigned0_Parallel.jar" + keystore="selfSignedCertificate/selfSignedCertificate.store" + storepass="selfSignedCertificate" alias="selfSignedCertificate" /> + <signjar jar="${jmol.applet.build.dir}/JmolApplet0_Popup.jar" signedjar="${jmol.applet.build.dir}/JmolAppletSigned0_Popup.jar" keystore="selfSignedCertificate/selfSignedCertificate.store" @@ -1002,6 +1015,7 @@ <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_Minimize.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_MultiTouch.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_Navigate.jar" /> + <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_Parallel.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_Popup.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_ReadersCifPdb.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_ReadersMolXyz.jar" /> @@ -1057,6 +1071,7 @@ <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_Minimize.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_MultiTouch.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_Navigate.jar" /> + <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_Parallel.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_Popup.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_ReadersCifPdb.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolAppletSigned0_ReadersMolXyz.jar" /> @@ -1097,6 +1112,12 @@ --> + <jar destfile="${jmol.applet.build.dir}/JmolApplet0_Console.jar" manifest="manifest/applet.txt"> + <fileset dir="${applet.classes.dir}"> + <include name="org/jmol/console/**" /> + </fileset> + </jar> + <jar destfile="${jmol.applet.build.dir}/JmolApplet0_Minimize.jar" manifest="manifest/applet.txt"> <fileset dir="${applet.classes.dir}"> <include name="org/jmol/minimize/**/**" /> @@ -1111,9 +1132,9 @@ </fileset> </jar> - <jar destfile="${jmol.applet.build.dir}/JmolApplet0_Console.jar" manifest="manifest/applet.txt"> + <jar destfile="${jmol.applet.build.dir}/JmolApplet0_Parallel.jar" manifest="manifest/applet.txt"> <fileset dir="${applet.classes.dir}"> - <include name="org/jmol/console/**" /> + <include name="org/jmol/parallel/**" /> </fileset> </jar> @@ -1244,6 +1265,7 @@ <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Console.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Minimize.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Navigate.jar" /> + <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Parallel.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_Popup.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_ReadersCifPdb.jar" /> <pathelement path="${jmol.applet.build.dir}/JmolApplet0_ReadersMolXyz.jar" /> Added: trunk/Jmol/src/org/jmol/api/JmolParallelProcessor.java =================================================================== --- trunk/Jmol/src/org/jmol/api/JmolParallelProcessor.java (rev 0) +++ trunk/Jmol/src/org/jmol/api/JmolParallelProcessor.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -0,0 +1,16 @@ +package org.jmol.api; + +import org.jmol.script.ScriptContext; +import org.jmol.viewer.Viewer; + +public interface JmolParallelProcessor { + + Object getExecutor(); + + void runAllProcesses(Viewer viewer); + + void addProcess(String name, ScriptContext context); + + void set(String name, int tok); + +} Modified: trunk/Jmol/src/org/jmol/awt/JmolFileAdapter.java =================================================================== --- trunk/Jmol/src/org/jmol/awt/JmolFileAdapter.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/awt/JmolFileAdapter.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -55,7 +55,7 @@ try { return new BufferedInputStream(new FileInputStream(file)); } catch (IOException e) { - return e.getMessage(); + return e.toString();//e.getMessage(); } } Copied: trunk/Jmol/src/org/jmol/parallel/ScriptParallelProcessor.java (from rev 17786, trunk/Jmol/src/org/jmol/thread/ScriptParallelProcessor.java) =================================================================== --- trunk/Jmol/src/org/jmol/parallel/ScriptParallelProcessor.java (rev 0) +++ trunk/Jmol/src/org/jmol/parallel/ScriptParallelProcessor.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -0,0 +1,132 @@ +/* $Author: hansonr $ + * $Date: 2010-04-22 13:16:44 -0500 (Thu, 22 Apr 2010) $ + * $Revision: 12904 $ + * + * Copyright (C) 2002-2005 The Jmol Development Team + * + * Contact: jmo...@li... + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.jmol.parallel; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import org.jmol.api.JmolParallelProcessor; +import org.jmol.script.ScriptContext; +import org.jmol.script.ScriptFunction; +import org.jmol.script.ScriptProcess; +import org.jmol.util.Logger; +import org.jmol.viewer.ShapeManager; +import org.jmol.viewer.Viewer; + +public class ScriptParallelProcessor extends ScriptFunction implements JmolParallelProcessor { + + /** + * parallel operations + * + */ + + public ScriptParallelProcessor() { + } + + public Object getExecutor() { + return Executors.newCachedThreadPool(); + } + + Viewer viewer; + public volatile int counter = 0; + public volatile Error error = null; + Object lock = new Object() ; + + public void runAllProcesses(Viewer viewer) { + if (processes.size() == 0) + return; + this.viewer = viewer; + boolean inParallel = !viewer.isParallel() && viewer.setParallel(true); + List<ShapeManager> vShapeManagers = new ArrayList<ShapeManager>(); + error = null; + counter = 0; + if (Logger.debugging) + Logger.debug("running " + processes.size() + " processes on " + + Viewer.nProcessors + " processesors inParallel=" + inParallel); + + counter = processes.size(); + for (int i = processes.size(); --i >= 0;) { + ShapeManager shapeManager = null; + if (inParallel) { + shapeManager = new ShapeManager(viewer, viewer.getModelSet()); + vShapeManagers.add(shapeManager); + } + runProcess(processes.remove(0), shapeManager); + } + + synchronized (lock) { + while (counter > 0) { + try { + lock.wait(); + } catch (InterruptedException e) { + } + if (error != null) + throw error; + } + } + mergeResults(vShapeManagers); + viewer.setParallel(false); + } + + void mergeResults(List<ShapeManager> vShapeManagers) { + try { + for (int i = 0; i < vShapeManagers.size(); i++) + viewer.mergeShapes(vShapeManagers.get(i).getShapes()); + } catch (Error e) { + throw e; + } finally { + counter = -1; + vShapeManagers = null; + } + } + + public void clearShapeManager(Error er) { + synchronized (this) { + this.error = er; + this.notifyAll(); + } + } + + List<ScriptProcess> processes = new ArrayList<ScriptProcess>(); + + public void addProcess(String name, ScriptContext context) { + processes.add(new ScriptProcess(name, context)); + } + + private void runProcess(final ScriptProcess process, ShapeManager shapeManager) { + ScriptProcessRunnable r = new ScriptProcessRunnable(this, process, lock, shapeManager); + Executor exec = (shapeManager == null ? null : (Executor) viewer.getExecutor()); + if (exec != null) { + exec.execute(r); + } else { + r.run(); + } + } + + public void eval(ScriptContext context, ShapeManager shapeManager) { + viewer.evalParallel(context, shapeManager); + } +} Copied: trunk/Jmol/src/org/jmol/parallel/ScriptProcessRunnable.java (from rev 17601, trunk/Jmol/src/org/jmol/thread/ScriptProcessRunnable.java) =================================================================== --- trunk/Jmol/src/org/jmol/parallel/ScriptProcessRunnable.java (rev 0) +++ trunk/Jmol/src/org/jmol/parallel/ScriptProcessRunnable.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -0,0 +1,77 @@ +/* $RCSfile$ + * $Author$ + * $Date$ + * $Revision$ + * + * Copyright (C) 2011 The Jmol Development Team + * + * Contact: jmo...@li... + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package org.jmol.parallel; + +import org.jmol.script.ScriptProcess; +import org.jmol.script.Token; +import org.jmol.util.Logger; +import org.jmol.viewer.ShapeManager; + +public class ScriptProcessRunnable implements Runnable { + /** + * + */ + private final ScriptParallelProcessor parallelProcessor; + private final ScriptProcess process; + private Object processLock; + private ShapeManager shapeManager; + /** + * + * @param process + * @param lock + * @param shapeManager + * @param parallelProcessor TODO + */ + public ScriptProcessRunnable(ScriptParallelProcessor parallelProcessor, ScriptProcess process, Object lock, ShapeManager shapeManager) { + this.parallelProcessor = parallelProcessor; + this.process = process; + processLock = lock; + this.shapeManager = shapeManager; + } + + public void run() { + try { + if (this.parallelProcessor.error == null) { + if (Logger.debugging) + Logger.debug("Running process " + process.processName + " " + + process.context.pc + " - " + (process.context.pcEnd - 1)); + this.parallelProcessor.eval(process.context, shapeManager); + if (Logger.debugging) + Logger.debug("Process " + process.processName + " complete"); + } + } catch (Exception e) { + if (this.parallelProcessor.tok != Token.trycmd) + e.printStackTrace(); + } catch (Error er) { + this.parallelProcessor.clearShapeManager(er); + } finally { + synchronized (processLock) { + --this.parallelProcessor.counter; + processLock.notifyAll(); + } + } + } +} \ No newline at end of file Modified: trunk/Jmol/src/org/jmol/script/ScriptCompiler.java =================================================================== --- trunk/Jmol/src/org/jmol/script/ScriptCompiler.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/script/ScriptCompiler.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -23,7 +23,6 @@ package org.jmol.script; -import org.jmol.thread.ScriptParallelProcessor; import org.jmol.util.ArrayUtil; import org.jmol.util.BitSet; import org.jmol.util.Escape; @@ -35,10 +34,12 @@ import org.jmol.util.StringXBuilder; import org.jmol.viewer.JmolConstants; import org.jmol.viewer.Viewer; +import org.jmol.api.Interface; import org.jmol.i18n.GT; import org.jmol.io.JmolBinary; import org.jmol.modelset.Group; import org.jmol.modelset.Bond.BondSet; +import org.jmol.parallel.ScriptParallelProcessor; import java.util.ArrayList; import java.util.Hashtable; @@ -1519,7 +1520,7 @@ if (nTokens == 1) { if (thisFunction != null) vFunctionStack.add(0, thisFunction); - thisFunction = (tokCommand == Token.parallel ? new ScriptParallelProcessor(ident, tokCommand) : new ScriptFunction(ident, tokCommand)); + thisFunction = (tokCommand == Token.parallel ? newScriptParallelProcessor(ident, tokCommand) : new ScriptFunction(ident, tokCommand)); htUserFunctions.put(ident, Boolean.TRUE); flowContext.setFunction(thisFunction); break; // function f @@ -1736,6 +1737,12 @@ return OK; } + private static ScriptFunction newScriptParallelProcessor(String name, int tok) { + ScriptFunction jpp = (ScriptFunction) Interface.getOptionInterface("parallel.ScriptParallelProcessor"); + jpp.set(name, tok); + return jpp; + } + private Token setNewSetCommand(boolean isSetBrace, String ident) { tokCommand = Token.set; isNewSet = (!isSetBrace && !isUserFunction(ident)); @@ -1945,7 +1952,7 @@ flowContext = new ScriptFlowContext(this, ct, pt, flowContext); if (thisFunction != null) vFunctionStack.add(0, thisFunction); - thisFunction = new ScriptParallelProcessor("", tokCommand); + thisFunction = newScriptParallelProcessor("", tokCommand); flowContext.setFunction(thisFunction); pushCount++; vPush.add(ct); Modified: trunk/Jmol/src/org/jmol/script/ScriptContext.java =================================================================== --- trunk/Jmol/src/org/jmol/script/ScriptContext.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/script/ScriptContext.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -27,8 +27,7 @@ import java.util.Map; - -import org.jmol.thread.ScriptParallelProcessor; +import org.jmol.api.JmolParallelProcessor; import org.jmol.util.StringXBuilder; public class ScriptContext { @@ -37,7 +36,7 @@ */ public String fullpath = ""; public String scriptFileName; - public ScriptParallelProcessor parallelProcessor; + public JmolParallelProcessor parallelProcessor; public String functionName; public String script; public short[] lineNumbers; @@ -69,6 +68,8 @@ public boolean mustResumeEval; public boolean isJSThread; public boolean allowJSThreads; + public boolean displayLoadErrorsSave; + public int tryPt; ScriptContext() { } Modified: trunk/Jmol/src/org/jmol/script/ScriptEvaluator.java =================================================================== --- trunk/Jmol/src/org/jmol/script/ScriptEvaluator.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/script/ScriptEvaluator.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -57,7 +57,6 @@ import org.jmol.shape.Object2d; import org.jmol.shape.Shape; import org.jmol.thread.ScriptDelayThread; -import org.jmol.thread.ScriptParallelProcessor; import org.jmol.util.BitSetUtil; import org.jmol.util.ColorEncoder; import org.jmol.util.Escape; @@ -88,6 +87,7 @@ import org.jmol.util.Tuple3f; import org.jmol.util.Vector3f; import org.jmol.modelset.TickInfo; +import org.jmol.api.JmolParallelProcessor; import org.jmol.viewer.ActionManager; import org.jmol.viewer.FileManager; import org.jmol.viewer.JmolConstants; @@ -285,10 +285,10 @@ private void startEval() { viewer.pushHoldRepaintWhy("runEval"); setScriptExtensions(); - executeCommands(); + executeCommands(false); } - private void executeCommands() { + private void executeCommands(boolean isTry) { boolean haveError = false; try { if (!dispatchCommands(false)) @@ -309,6 +309,10 @@ // property so that it can be caught here. return; } + if (isTry) { + viewer.setStringProperty("_errormessage", "" + e); + return; + } setErrorMessage(e.toString()); errorMessageUntranslated = e.getErrorMessageUntranslated(); scriptStatusOrBuffer(errorMessage); @@ -395,7 +399,7 @@ if (sc.scriptLevel > 0) scriptLevel = sc.scriptLevel - 1; restoreScriptContext(sc, true, false, false); - executeCommands(); + executeCommands(sc.isTryCatch); } /** @@ -660,7 +664,7 @@ * @param shapeManager * @return true if successful; false if not */ - public static boolean evaluateContext(Viewer viewer, ScriptContext context, + public static boolean evaluateParallel(Viewer viewer, ScriptContext context, ShapeManager shapeManager) { ScriptEvaluator e = new ScriptEvaluator(viewer); e.historyDisabled = true; @@ -1505,7 +1509,7 @@ case Token.function: bsAtom.set(i); fv = ScriptVariable.fValue(runFunctionRet(null, userFunction, params, - tokenAtom, true, true)); + tokenAtom, true, true, false)); bsAtom.clear(i); break; case Token.property: @@ -2151,7 +2155,7 @@ return v; } - private ScriptParallelProcessor parallelProcessor; + private JmolParallelProcessor parallelProcessor; @SuppressWarnings("unchecked") public float evalFunctionFloat(Object func, Object params, float[] values) { @@ -2161,17 +2165,20 @@ p.get(i).value = new Float(values[i]); ScriptFunction f = (ScriptFunction) func; return ScriptVariable - .fValue(runFunctionRet(f, f.name, p, null, true, false)); + .fValue(runFunctionRet(f, f.name, p, null, true, false, false)); } catch (Exception e) { return Float.NaN; } } + static int tryPt; + ScriptVariable runFunctionRet(ScriptFunction function, String name, - List<ScriptVariable> params, - ScriptVariable tokenAtom, boolean getReturn, - boolean setContextPath) throws ScriptException { + List<ScriptVariable> params, + ScriptVariable tokenAtom, boolean getReturn, + boolean setContextPath, boolean allowThreads) + throws ScriptException { if (function == null) { // general function call function = viewer.getFunction(name); @@ -2183,43 +2190,42 @@ // "try"; not from evalFunctionFloat contextPath += " >> " + name; } - + pushContext(null); - allowJSThreads = false; + allowJSThreads = allowThreads; boolean isTry = (function.tok == Token.trycmd); thisContext.isTryCatch = isTry; thisContext.isFunction = !isTry; - allowJSThreads = false; functionName = name; - - if (function instanceof ScriptParallelProcessor) { + if (function.tok == Token.trycmd) { + viewer.resetError(); + thisContext.displayLoadErrorsSave = viewer.displayLoadErrors; + thisContext.tryPt = ++tryPt; + viewer.displayLoadErrors = false; + restoreFunction(function, params, tokenAtom); + contextVariables.put("_breakval", + new ScriptVariableInt(Integer.MAX_VALUE)); + contextVariables.put("_errorval", ScriptVariable.newVariable( + Token.string, "")); + Map<String, ScriptVariable> cv = contextVariables; + executeCommands(true); + //JavaScript will not return here after DELAY + while (thisContext.tryPt != tryPt) + popContext(false, false); + processTry(cv); + return null; + } else if (function instanceof JmolParallelProcessor) { synchronized (function) // can't do this -- too general { - parallelProcessor = (ScriptParallelProcessor) function; - vProcess = null; - runFunction(function, params, tokenAtom); - - ScriptContext sc = getScriptContext(); - if (isTry) { - contextVariables.put("_breakval", new ScriptVariableInt(Integer.MAX_VALUE)); - contextVariables.put("_errorval", ScriptVariable.newVariable(Token.string, "")); - viewer.resetError(); - parallelProcessor.addProcess("try", sc); - } - ((ScriptParallelProcessor) function).runAllProcesses(viewer, !isTry); - if (isTry) { - String err = (String) viewer.getParameter("_errormessage"); - if (err.length() > 0) { - contextVariables.put("_errorval", ScriptVariable.newVariable(Token.string, err)); - viewer.resetError(); - } - contextVariables.put("_tryret", contextVariables.get("_retval")); - contextVariables.put("_retval", ScriptVariable.newVariable(0, - contextVariables)); - } + parallelProcessor = (JmolParallelProcessor) function; + restoreFunction(function, params, tokenAtom); + dispatchCommands(false); // to load the processes + ((JmolParallelProcessor) function).runAllProcesses(viewer); } } else { - runFunction(function, params, tokenAtom); + restoreFunction(function, params, tokenAtom); + dispatchCommands(false); + //JavaScript will not return here after DELAY } ScriptVariable v = (getReturn ? getContextVariableAsVariable("_retval") : null); @@ -2227,6 +2233,40 @@ return v; } + private void processTry(Map<String, ScriptVariable> cv) throws ScriptException { + viewer.displayLoadErrors = thisContext.displayLoadErrorsSave; + popContext(false, false); + String err = (String) viewer.getParameter("_errormessage"); + if (err.length() > 0) { + cv.put("_errorval", ScriptVariable.newVariable( + Token.string, err)); + viewer.resetError(); + } + cv.put("_tryret", cv.get("_retval")); + ScriptVariable ret = cv.get("_tryret"); + if (ret.value != null || ret.intValue != Integer.MAX_VALUE) { + returnCmd(ret); + return; + } + String errMsg = (String) (cv.get("_errorval")).value; + if (errMsg.length() == 0) { + int iBreak = (cv.get("_breakval")).intValue; + if (iBreak != Integer.MAX_VALUE) { + breakCmd(pc - iBreak); + return; + } + } + // normal return will skip the catch + if (pc + 1 < aatoken.length && aatoken[pc + 1][0].tok == Token.catchcmd) { + // set the intValue positive to indicate "not done" for the IF evaluation + ContextToken ct = (ContextToken) aatoken[pc + 1][0]; + if (ct.contextVariables != null && ct.name0 != null) + ct.contextVariables.put(ct.name0, ScriptVariable + .newVariable(Token.string, errMsg)); + ct.intValue = (errMsg.length() > 0 ? 1 : -1) * Math.abs(ct.intValue); + } + } + /** * note that functions requiring motion cannot be run * in JavaScript @@ -2236,8 +2276,9 @@ * @param tokenAtom * @throws ScriptException */ - private void runFunction(ScriptFunction function, - List<ScriptVariable> params, ScriptVariable tokenAtom) + private void restoreFunction(ScriptFunction function, + List<ScriptVariable> params, + ScriptVariable tokenAtom) throws ScriptException { aatoken = function.aatoken; lineNumbers = function.lineNumbers; @@ -2250,8 +2291,6 @@ } if (tokenAtom != null) contextVariables.put("_x", tokenAtom); - if (function.tok != Token.trycmd) - dispatchCommands(false); } private void clearDefinedVariableAtomSets() { @@ -5349,7 +5388,6 @@ private boolean dispatchCommands(boolean isSpt) throws ScriptException { long timeBegin = 0; - vProcess = null; if (shapeManager == null) shapeManager = viewer.getShapeManager(); debugScript = logMessages = false; @@ -5390,7 +5428,7 @@ private void commandLoop() throws ScriptException { String lastCommand = ""; boolean isForCheck = false; // indicates the stage of the for command loop - + List<Token[]> vProcess = null; long lastTime = System.currentTimeMillis(); for (; pc < aatoken.length && pc < pcEnd; pc++) { if (!isSyntaxCheck && isJS && allowJSThreads) { @@ -5502,9 +5540,10 @@ case Token.switchcmd: case Token.casecmd: case Token.defaultcmd: - case Token.process: case Token.whilecmd: - isForCheck = flowControl(theToken.tok, isForCheck); + isForCheck = flowControl(theToken.tok, isForCheck, vProcess); + if (theTok == Token.process) + vProcess = null; // "end process" break; case Token.animation: animation(); @@ -5663,6 +5702,11 @@ case Token.print: print(); break; + case Token.process: + pushContext((ContextToken) theToken); + if (parallelProcessor != null) + vProcess = new ArrayList<Token[]>(); + break; case Token.prompt: prompt(); break; @@ -5994,8 +6038,7 @@ } } - @SuppressWarnings("unchecked") - private boolean flowControl(int tok, boolean isForCheck) + private boolean flowControl(int tok, boolean isForCheck, List<Token[]> vProcess) throws ScriptException { ContextToken ct; switch (tok) { @@ -6021,11 +6064,6 @@ contextVariables.put(ct.name0, ct.contextVariables.get(ct.name0)); isOK = !isDone; break; - case Token.process: - pushContext((ContextToken) theToken); - isDone = isOK = true; - addProcess(pc, pt, true); - break; case Token.switchcmd: case Token.defaultcmd: case Token.casecmd: @@ -6215,30 +6253,7 @@ ScriptFunction trycmd = (ScriptFunction) getToken(1).value; if (isSyntaxCheck) return false; - Map<String, ScriptVariable> cv = (Map<String, ScriptVariable>) runFunctionRet( - trycmd, "try", null, null, true, true).value; - ScriptVariable ret = cv.get("_tryret"); - if (ret.value != null || ret.intValue != Integer.MAX_VALUE) { - returnCmd(ret); - return false; - } - String errMsg = (String) (cv.get("_errorval")).value; - if (errMsg.length() == 0) { - int iBreak = (cv.get("_breakval")).intValue; - if (iBreak != Integer.MAX_VALUE) { - breakCmd(pc - iBreak); - return false; - } - } - // normal return will skip the catch - if (pc + 1 < aatoken.length && aatoken[pc + 1][0].tok == Token.catchcmd) { - // set the intValue positive to indicate "not done" for the IF evaluation - ct = (ContextToken) aatoken[pc + 1][0]; - if (ct.contextVariables != null && ct.name0 != null) - ct.contextVariables.put(ct.name0, ScriptVariable - .newVariable(Token.string, errMsg)); - ct.intValue = (errMsg.length() > 0 ? 1 : -1) * Math.abs(ct.intValue); - } + runFunctionRet(trycmd, "try", null, null, true, true, true); return false; case Token.catchcmd: popContext(true, false); @@ -6248,7 +6263,7 @@ viewer.addFunction((ScriptFunction) theToken.value); return isForCheck; case Token.process: - addProcess(pt, pc, false); + addProcess(vProcess, pt, pc); popContext(true, false); break; case Token.switchcmd: @@ -6357,26 +6372,19 @@ } } - private List<Token[]> vProcess; static int iProcess; - private void addProcess(int pc, int pt, boolean isStart) { + private void addProcess(List<Token[]> vProcess, int pc, int pt) { if (parallelProcessor == null) return; - if (isStart) { - vProcess = new ArrayList<Token[]>(); - } else { - - Token[][] statements = new Token[pt][]; - for (int i = 0; i < vProcess.size(); i++) - statements[i + 1 - pc] = vProcess.get(i); - ScriptContext context = getScriptContext(); - context.aatoken = statements; - context.pc = 1 - pc; - context.pcEnd = pt; - parallelProcessor.addProcess("p" + (++iProcess), context); - vProcess = null; - } + Token[][] statements = new Token[pt][]; + for (int i = 0; i < vProcess.size(); i++) + statements[i + 1 - pc] = vProcess.get(i); + ScriptContext context = getScriptContext(); + context.aatoken = statements; + context.pc = 1 - pc; + context.pcEnd = pt; + parallelProcessor.addProcess("p" + (++iProcess), context); } private int switchCmd(ContextToken c, int tok) throws ScriptException { @@ -10773,7 +10781,7 @@ : parameterExpressionList(1, -1, false)); if (isSyntaxCheck) return; - runFunctionRet(null, name, params, null, false, true); + runFunctionRet(null, name, params, null, false, true, true); } private void sync() throws ScriptException { Modified: trunk/Jmol/src/org/jmol/script/ScriptFunction.java =================================================================== --- trunk/Jmol/src/org/jmol/script/ScriptFunction.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/script/ScriptFunction.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -69,10 +69,18 @@ short[] lineNumbers; String script; + public ScriptFunction() { + // for reflection with ScriptParallelProcessor + } + protected ScriptFunction(String name, int tok) { + set(name, tok); + typeName = Token.nameOf(tok); + } + + public void set(String name, int tok) { this.name = name; this.tok = tok; - typeName = Token.nameOf(tok); } void setVariables(Map<String, ScriptVariable> contextVariables, List<ScriptVariable> params) { Modified: trunk/Jmol/src/org/jmol/script/ScriptMathProcessor.java =================================================================== --- trunk/Jmol/src/org/jmol/script/ScriptMathProcessor.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/script/ScriptMathProcessor.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -1371,7 +1371,7 @@ null, null, x1.value, new Object[] { name, params }, false, x1.index, false)); } - ScriptVariable var = eval.runFunctionRet(null, name, params, null, true, true); + ScriptVariable var = eval.runFunctionRet(null, name, params, null, true, true, false); return (var == null ? false : addXVar(var)); } Deleted: trunk/Jmol/src/org/jmol/thread/ScriptParallelProcessor.java =================================================================== --- trunk/Jmol/src/org/jmol/thread/ScriptParallelProcessor.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/thread/ScriptParallelProcessor.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -1,134 +0,0 @@ -/* $Author: hansonr $ - * $Date: 2010-04-22 13:16:44 -0500 (Thu, 22 Apr 2010) $ - * $Revision: 12904 $ - * - * Copyright (C) 2002-2005 The Jmol Development Team - * - * Contact: jmo...@li... - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package org.jmol.thread; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - -import org.jmol.script.ScriptContext; -import org.jmol.script.ScriptFunction; -import org.jmol.script.ScriptProcess; -import org.jmol.util.Logger; -import org.jmol.viewer.ShapeManager; -import org.jmol.viewer.Viewer; - -public class ScriptParallelProcessor extends ScriptFunction { - - /** - * included try/catch, not just parallel operations - * - * @param name - * @param tok - */ - - public ScriptParallelProcessor(String name, int tok) { - super(name, tok); - } - - public static Object getExecutor() { - return Executors.newCachedThreadPool(); - } - - Viewer viewer; - public volatile int counter = 0; - public volatile Error error = null; - Object lock = new Object() ; - - public void runAllProcesses(Viewer viewer, boolean inParallel) { - if (processes.size() == 0) - return; - this.viewer = viewer; - inParallel &= !viewer.isParallel() && viewer.setParallel(true); - List<ShapeManager> vShapeManagers = new ArrayList<ShapeManager>(); - error = null; - counter = 0; - if (Logger.debugging) - Logger.debug("running " + processes.size() + " processes on " - + Viewer.nProcessors + " processesors inParallel=" + inParallel); - - counter = processes.size(); - for (int i = processes.size(); --i >= 0;) { - ShapeManager shapeManager = null; - if (inParallel) { - shapeManager = new ShapeManager(viewer, viewer.getModelSet()); - vShapeManagers.add(shapeManager); - } - runProcess(processes.remove(0), shapeManager); - } - - synchronized (lock) { - while (counter > 0) { - try { - lock.wait(); - } catch (InterruptedException e) { - } - if (error != null) - throw error; - } - } - mergeResults(vShapeManagers); - viewer.setParallel(false); - } - - void mergeResults(List<ShapeManager> vShapeManagers) { - try { - for (int i = 0; i < vShapeManagers.size(); i++) - viewer.mergeShapes(vShapeManagers.get(i).getShapes()); - } catch (Error e) { - throw e; - } finally { - counter = -1; - vShapeManagers = null; - } - } - - public void clearShapeManager(Error er) { - synchronized (this) { - this.error = er; - this.notifyAll(); - } - } - - List<ScriptProcess> processes = new ArrayList<ScriptProcess>(); - - public void addProcess(String name, ScriptContext context) { - processes.add(new ScriptProcess(name, context)); - } - - private void runProcess(final ScriptProcess process, ShapeManager shapeManager) { - ScriptProcessRunnable r = new ScriptProcessRunnable(this, process, lock, shapeManager); - Executor exec = (shapeManager == null ? null : (Executor) viewer.getExecutor()); - if (exec != null) { - exec.execute(r); - } else { - r.run(); - } - } - - public void eval(ScriptContext context, ShapeManager shapeManager) { - viewer.evalContext(context, shapeManager); - } -} Deleted: trunk/Jmol/src/org/jmol/thread/ScriptProcessRunnable.java =================================================================== --- trunk/Jmol/src/org/jmol/thread/ScriptProcessRunnable.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/thread/ScriptProcessRunnable.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -1,77 +0,0 @@ -/* $RCSfile$ - * $Author$ - * $Date$ - * $Revision$ - * - * Copyright (C) 2011 The Jmol Development Team - * - * Contact: jmo...@li... - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - */ - -package org.jmol.thread; - -import org.jmol.script.ScriptProcess; -import org.jmol.script.Token; -import org.jmol.util.Logger; -import org.jmol.viewer.ShapeManager; - -public class ScriptProcessRunnable implements Runnable { - /** - * - */ - private final ScriptParallelProcessor parallelProcessor; - private final ScriptProcess process; - private Object processLock; - private ShapeManager shapeManager; - /** - * - * @param process - * @param lock - * @param shapeManager - * @param parallelProcessor TODO - */ - public ScriptProcessRunnable(ScriptParallelProcessor parallelProcessor, ScriptProcess process, Object lock, ShapeManager shapeManager) { - this.parallelProcessor = parallelProcessor; - this.process = process; - processLock = lock; - this.shapeManager = shapeManager; - } - - public void run() { - try { - if (this.parallelProcessor.error == null) { - if (Logger.debugging) - Logger.debug("Running process " + process.processName + " " - + process.context.pc + " - " + (process.context.pcEnd - 1)); - this.parallelProcessor.eval(process.context, shapeManager); - if (Logger.debugging) - Logger.debug("Process " + process.processName + " complete"); - } - } catch (Exception e) { - if (this.parallelProcessor.tok != Token.trycmd) - e.printStackTrace(); - } catch (Error er) { - this.parallelProcessor.clearShapeManager(er); - } finally { - synchronized (processLock) { - --this.parallelProcessor.counter; - processLock.notifyAll(); - } - } - } -} \ No newline at end of file Modified: trunk/Jmol/src/org/jmol/viewer/Viewer.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Viewer.java 2012-12-06 03:20:15 UTC (rev 17788) +++ trunk/Jmol/src/org/jmol/viewer/Viewer.java 2012-12-06 16:56:07 UTC (rev 17789) @@ -34,7 +34,6 @@ import org.jmol.shape.Shape; import org.jmol.thread.JmolThread; import org.jmol.thread.ScriptDelayThread; -import org.jmol.thread.ScriptParallelProcessor; import org.jmol.thread.TimeoutThread; import org.jmol.i18n.GT; import org.jmol.io.Base64; @@ -60,6 +59,7 @@ import org.jmol.api.JmolCallbackListener; import org.jmol.api.JmolImageCreatorInterface; import org.jmol.api.JmolMouseInterface; +import org.jmol.api.JmolParallelProcessor; import org.jmol.api.JmolRendererInterface; import org.jmol.api.JmolRepaintInterface; import org.jmol.api.JmolScriptEditorInterface; @@ -10050,7 +10050,7 @@ if (executor != null || nProcessors < 2) return executor; // note -- a Java 1.5 function try { - executor = ScriptParallelProcessor.getExecutor(); + executor = ((JmolParallelProcessor) Interface.getOptionInterface("parallel.ScriptParallelProcessor")).getExecutor(); } catch (Exception e) { executor = null; } catch (Error er) { @@ -10063,9 +10063,9 @@ public boolean displayLoadErrors = true; - public boolean evalContext(ScriptContext context, ShapeManager shapeManager) { + public boolean evalParallel(ScriptContext context, ShapeManager shapeManager) { displayLoadErrors = false; - boolean isOK = ScriptEvaluator.evaluateContext(this, context, + boolean isOK = ScriptEvaluator.evaluateParallel(this, context, (shapeManager == null ? this.shapeManager : shapeManager)); displayLoadErrors = true; return isOK; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |