From 9376eb0d7931626cfa953ef3327bb75489ce6dd6 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 31 Aug 2010 08:02:15 +0000 Subject: [PATCH] Some error handling for simulations git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17708 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/modelica/IModelicaMonitor.java | 4 +- .../simantics/modelica/ModelicaException.java | 11 +++++ .../simantics/modelica/ModelicaManager.java | 5 +- .../simantics/sysdyn/manager/SysdynModel.java | 49 ++++++++++++++----- .../sysdyn/simulation/SimulationJob.java | 8 ++- 5 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 org.simantics.modelica/src/org/simantics/modelica/ModelicaException.java diff --git a/org.simantics.modelica/src/org/simantics/modelica/IModelicaMonitor.java b/org.simantics.modelica/src/org/simantics/modelica/IModelicaMonitor.java index 5a24d20d..f215031c 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/IModelicaMonitor.java +++ b/org.simantics.modelica/src/org/simantics/modelica/IModelicaMonitor.java @@ -12,5 +12,7 @@ package org.simantics.modelica; public interface IModelicaMonitor { - void message(String message); + public void message(String message); + public void clearConsole(); + public void showConsole(); } diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaException.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaException.java new file mode 100644 index 00000000..a71942c6 --- /dev/null +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaException.java @@ -0,0 +1,11 @@ +package org.simantics.modelica; + +public class ModelicaException extends Exception { + + private static final long serialVersionUID = 2712433918044442927L; + + public ModelicaException(String message) { + super(message); + } + +} diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java index 3a31c382..1bddcf45 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -124,7 +124,7 @@ public class ModelicaManager { ); } - public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) { + public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) throws ModelicaException { try { File modelicaHome = getOpenModelicaHome(); File omc = new File(modelicaHome, "bin/omc.exe"); @@ -139,6 +139,9 @@ public class ModelicaManager { .redirectErrorStream(true) .start(); printProcessOutput(process, monitor); + + if(!simulationLocation.exeFile.isFile()) + throw new ModelicaException("exe file not created"); } catch(IOException e) { e.printStackTrace(); 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 fe7747a0..c7a7e88e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -25,13 +25,14 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import org.eclipse.core.runtime.IProgressMonitor; -import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; import org.simantics.modelica.IModelicaMonitor; +import org.simantics.modelica.ModelicaException; import org.simantics.modelica.ModelicaManager; import org.simantics.modelica.SimulationLocation; import org.simantics.modelica.data.SimulationResult; @@ -79,9 +80,11 @@ public class SysdynModel implements IMappingListener, IModel { CopyOnWriteArrayList resultListeners = new CopyOnWriteArrayList(); - Map services = new HashMap(); + @SuppressWarnings("rawtypes") + Map services = new HashMap(); String previousModelStructure; + HashMap previousImportantInits = new HashMap(); File simulationDir; @@ -89,11 +92,11 @@ public class SysdynModel implements IMappingListener, IModel { if(!result.add(configResource)) return; - Builtins b = graph.getBuiltins(); + Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); StructuralResource2 str = StructuralResource2.getInstance(graph); - for(Resource part : graph.getObjects(configResource, b.ConsistsOf)) { + for(Resource part : graph.getObjects(configResource, l0.ConsistsOf)) { if(graph.isInstanceOf(part, sr.Module)) { Resource type = graph.getPossibleType(part, sr.Module); Resource config = graph.getPossibleObject(type, str.IsDefinedBy); @@ -161,7 +164,7 @@ public class SysdynModel implements IMappingListener, IModel { public synchronized void simulate(IModelicaMonitor monitor, final IProgressMonitor progressMonitor) throws IOException { canceled = false; - + ModelicaWriter writer = new ModelicaWriter(); //writer.write(configuration); for(Configuration c : modules) { @@ -176,8 +179,10 @@ public class SysdynModel implements IMappingListener, IModel { Model model = configuration.getModel(); - inits.put("start value", model.getStartTime().toString()); - inits.put("stop value", model.getStopTime().toString()); + String startTime = model.getStartTime().toString(); + String stopTime = model.getStopTime().toString(); + inits.put("start value", startTime); + inits.put("stop value", stopTime); if(model.getSolver() != null) inits.put("solver", model.getSolver()); if(model.getTolerance() != null) @@ -191,14 +196,22 @@ public class SysdynModel implements IMappingListener, IModel { progressMonitor.worked(20); - if (!modelText.equals(previousModelStructure)) { + if (hasStructureChanged(modelText, inits)) { previousModelStructure = modelText; System.out.println("== Modelica == "); System.out.println(writer.toString()); System.out.println("== Modelica ends == "); - ModelicaManager.buildModel(simulationLocation, monitor); - + try { + ModelicaManager.buildModel(simulationLocation, monitor); + previousImportantInits.clear(); + previousImportantInits.put("start value", startTime); + previousImportantInits.put("stop value", stopTime); + } catch (ModelicaException e) { + monitor.showConsole(); + canceled = true; + previousModelStructure = ""; + } } progressMonitor.worked(20); @@ -244,6 +257,16 @@ public class SysdynModel implements IMappingListener, IModel { process = null; } + private boolean hasStructureChanged(String modelText, Map inits) { + if (!modelText.equals(previousModelStructure)) + return true; + for(String key : previousImportantInits.keySet()) { + if(!inits.containsKey(key) || !previousImportantInits.get(key).equals(inits.get(key))) + return true; + } + return false; + } + public void cancelSimulation() { canceled = true; if(process != null) { @@ -350,17 +373,17 @@ public class SysdynModel implements IMappingListener, IModel { ArrayList results = new ArrayList(); try { - Builtins b = graph.getBuiltins(); + Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); SimulationResource SIMU = SimulationResource.getInstance(graph); Resource model = graph.getSingleObject(configurationResource, SIMU.IsConfigurationOf); - Collection experiments = graph.getObjects(model, b.ConsistsOf); + Collection experiments = graph.getObjects(model, l0.ConsistsOf); for(Resource experiment : experiments) { Collection experimentResults = graph.getObjects(experiment, sr.HasResult); for(Resource result : experimentResults) { if(graph.hasStatement(result, SIMU.IsActive)) { SysdynResult sysdynResult = new SysdynResult( - (String) graph.getPossibleRelatedValue(result, b.HasLabel), + (String) graph.getPossibleRelatedValue(result, l0.HasLabel), (String) graph.getPossibleRelatedValue(result, sr.HasResultFile)); results.add(sysdynResult); } 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 1bb019bb..02166691 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java @@ -62,7 +62,7 @@ public class SimulationJob extends Job { private class ModelicaMonitor implements IModelicaMonitor { MessageConsole console; - + public ModelicaMonitor() { ConsolePlugin plugin = ConsolePlugin.getDefault(); IConsoleManager conMan = plugin.getConsoleManager(); @@ -86,6 +86,12 @@ public class SimulationJob extends Job { public void clearConsole() { this.console.clearConsole(); } + + public void showConsole() { + ConsolePlugin plugin = ConsolePlugin.getDefault(); + IConsoleManager conMan = plugin.getConsoleManager(); + conMan.showConsoleView(console); + } } -- 2.47.1