From e0e8b899b33439a5c6d0eff41d194586ca3597ea Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 13 Aug 2010 13:21:30 +0000 Subject: [PATCH] Simulation can be canceled git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17201 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/modelica/ModelicaManager.java | 66 ++++---- .../ui/properties/InputVariableTab.java | 21 ++- .../simantics/sysdyn/manager/SysdynModel.java | 154 +++++++++++------- .../sysdyn/simulation/SimulationJob.java | 5 + 4 files changed, 143 insertions(+), 103 deletions(-) diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java index 51e9ca7f..94ad2a09 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -19,8 +19,6 @@ import java.io.InputStream; import java.io.PrintStream; import java.util.HashMap; -import org.simantics.modelica.data.SimulationResult; - public class ModelicaManager { @@ -57,24 +55,40 @@ public class ModelicaManager { return new String(buffer); } - protected static void printProcessOutput(Process process, IModelicaMonitor monitor) throws IOException { - InputStream stream = process.getInputStream(); - StringBuilder b = new StringBuilder(); - while(true) { - int c = stream.read(); - if(c <= 0) - break; - if((char)c != '\n') - b.append((char)c); - else { - System.out.println("OMC output: " + b.toString()); - monitor.message(b.toString()); - b.delete(0, b.length()); - } - } + public static void printProcessOutput(final Process process, final IModelicaMonitor monitor) { + Thread thread = new Thread() { + @Override + public void run() { + InputStream stream = process.getInputStream(); + StringBuilder b = new StringBuilder(); + while(true) { + try { + int c; + + c = stream.read(); + + if(c <= 0) + break; + if((char)c != '\n') + b.append((char)c); + else { + System.out.println("OMC output: " + b.toString()); + monitor.message(b.toString()); + b.delete(0, b.length()); + } + } catch (IOException e) { + System.err.println("Not able to read simulation output"); + e.printStackTrace(); + } + } + } + }; + thread.run(); + } public static SimulationLocation createInputFiles(File simulationDir, String modelName, String modelText, HashMap inits) throws IOException { + System.out.println(simulationDir.getAbsolutePath()); modelName = modelName.replace(" ", ""); File modelFile = new File(simulationDir, modelName + ".mo"); File scriptFile = new File(simulationDir, modelName + ".mos"); @@ -89,10 +103,10 @@ public class ModelicaManager { PrintStream s = new PrintStream(scriptFile); s.println("loadFile(\"" + modelName + ".mo\");"); s.print("buildModel("+modelName+ - ",startTime="+inits.get("startTime")+ - ",stopTime="+inits.get("stopTime")+ + ",startTime="+inits.get("start value")+ + ",stopTime="+inits.get("stop value")+ ",method=\"euler\"" - ); + ); if(inits.containsKey("tolerance")) { s.print(",tolerance="+inits.get("tolerance")); } @@ -131,7 +145,7 @@ public class ModelicaManager { } - public static SimulationResult runModelica(SimulationLocation simulationLocation, IModelicaMonitor monitor, HashMap inits) throws IOException { + public static Process runModelica(SimulationLocation simulationLocation, IModelicaMonitor monitor, HashMap inits) throws IOException { try { @@ -143,17 +157,11 @@ public class ModelicaManager { .directory(simulationLocation.simulationDir.getAbsoluteFile()) .redirectErrorStream(true) .start(); - printProcessOutput(process, monitor); - SimulationResult result = new SimulationResult(); - result.read(simulationLocation.outputFile); - result.readInits(simulationLocation.initFile); - result.filter(); - - return result; + return process; } catch(IOException e) { e.printStackTrace(); - } + } return null; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java index 4cba9abb..5ef2c912 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java @@ -11,7 +11,6 @@ import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; -import org.simantics.browsing.ui.swt.widgets.TrackedCombo; import org.simantics.browsing.ui.swt.widgets.TrackedText; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.Builtins; @@ -23,32 +22,32 @@ import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifi public class InputVariableTab extends PropertyTabContributorImpl { - TrackedCombo expressionTypeCombo; - IsOutputWidget isOutput; @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { Composite composite = new Composite(body, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); - GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(composite); + GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).grab(false, true).applyTo(composite); + GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(composite); TrackedText nameText = new TrackedText(composite, support, SWT.BORDER); nameText.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName)); nameText.addModifyListener(new StringPropertyModifier(context, Builtins.URIs.HasName)); GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget()); - isOutput = new IsOutputWidget(composite, support, SWT.NULL); - - Label label = new Label(composite, SWT.NULL); - label.setText("Default Value"); + Composite defaultValueComposite = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().grab(false, true).applyTo(defaultValueComposite); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(defaultValueComposite); + + Label label = new Label(defaultValueComposite, SWT.NULL); + label.setText("Default Value:"); - TrackedText defaultValue = new TrackedText(composite, support, SWT.RIGHT | SWT.BORDER); + TrackedText defaultValue = new TrackedText(defaultValueComposite, support, SWT.RIGHT | SWT.BORDER); defaultValue.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasDefaultInputValue)); defaultValue.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasDefaultInputValue)); defaultValue.setInputValidator(new DoubleValidator()); GridDataFactory.fillDefaults().grab(true, false).applyTo(defaultValue.getWidget()); - + new IsOutputWidget(composite, support, SWT.NULL); } private class DoubleValidator implements IInputValidator { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index 594cc680..fbb8df79 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -12,6 +12,7 @@ package org.simantics.sysdyn.manager; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -67,6 +68,8 @@ public class SysdynModel implements IMappingListener, IModel { Set modules = new HashSet(); + Process process; + boolean canceled; SimulationResult result; SysdynResult sysdynResult; @@ -140,7 +143,7 @@ public class SysdynModel implements IMappingListener, IModel { } configName += "_" + i; } - + simulationDir = Activator.getBundleContext().getDataFile("models/" + configName); if (!simulationDir.exists()) { simulationDir.mkdir(); @@ -155,70 +158,95 @@ public class SysdynModel implements IMappingListener, IModel { this.configurationResource = configurationResource; } - public synchronized void simulate(IModelicaMonitor monitor, IProgressMonitor progressMonitor) throws IOException { + public synchronized void simulate(IModelicaMonitor monitor, final IProgressMonitor progressMonitor) throws IOException { + canceled = false; - try { - ModelicaWriter writer = new ModelicaWriter(); - //writer.write(configuration); - for(Configuration c : modules) { - writer.write(c); - } - - progressMonitor.worked(20); - - String modelText = writer.toString(); - - HashMap inits = getInits(configuration, ""); - - Model model = configuration.getModel(); - - inits.put("startTime", model.getStartTime().toString()); - inits.put("stopTime", model.getStopTime().toString()); - if(model.getSolver() != null) - inits.put("solver", model.getSolver()); - if(model.getTolerance() != null) - inits.put("tolerance", model.getTolerance().toString()); - - SimulationLocation simulationLocation = ModelicaManager.createInputFiles( - simulationDir, - configuration.getName(), - writer.toString(), - inits); - - progressMonitor.worked(20); - - if (simulationLocation == null || !modelText.equals(previousModelStructure)) { - previousModelStructure = modelText; - System.out.println("== Modelica == "); - System.out.println(writer.toString()); - System.out.println("== Modelica ends == "); - - ModelicaManager.buildModel(simulationLocation, monitor); - - } - - progressMonitor.worked(20); - - if(simulationLocation != null) - result = ModelicaManager.runModelica( - simulationLocation, - monitor, - inits - ); - - progressMonitor.worked(20); - - sysdynResult.setResult(result); - - progressMonitor.worked(20); - - } catch(Exception e) { - e.printStackTrace(); - result = null; + ModelicaWriter writer = new ModelicaWriter(); + //writer.write(configuration); + for(Configuration c : modules) { + writer.write(c); } - synchronized(resultListeners) { - for(Runnable listener : resultListeners) - listener.run(); + + progressMonitor.worked(20); + + String modelText = writer.toString(); + + HashMap inits = getInits(configuration, ""); + + Model model = configuration.getModel(); + + inits.put("start value", model.getStartTime().toString()); + inits.put("stop value", model.getStopTime().toString()); + if(model.getSolver() != null) + inits.put("solver", model.getSolver()); + if(model.getTolerance() != null) + inits.put("tolerance", model.getTolerance().toString()); + + final SimulationLocation simulationLocation = ModelicaManager.createInputFiles( + simulationDir, + configuration.getName(), + writer.toString(), + inits); + + progressMonitor.worked(20); + + if (!modelText.equals(previousModelStructure)) { + previousModelStructure = modelText; + System.out.println("== Modelica == "); + System.out.println(writer.toString()); + System.out.println("== Modelica ends == "); + + ModelicaManager.buildModel(simulationLocation, monitor); + + } + + progressMonitor.worked(20); + + if(simulationLocation != null && !canceled) { + process = ModelicaManager.runModelica( + simulationLocation, + monitor, + inits + ); + ModelicaManager.printProcessOutput(process, monitor); + + Thread resultThread = new Thread() { + @Override + public void run() { + try { + process.waitFor(); + + if(!canceled) { + result = new SimulationResult(); + result.read(simulationLocation.outputFile); + result.readInits(simulationLocation.initFile); + result.filter(); + progressMonitor.worked(20); + sysdynResult.setResult(result); + progressMonitor.worked(20); + synchronized(resultListeners) { + for(Runnable listener : resultListeners) + listener.run(); + } + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }; + resultThread.run(); + } + process = null; + } + + public void cancelSimulation() { + canceled = true; + if(process != null) { + process.destroy(); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java index a81688a3..1bb019bb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java @@ -88,5 +88,10 @@ public class SimulationJob extends Job { } } + + @Override + protected void canceling() { + model.cancelSimulation(); + } } -- 2.47.1