package org.simantics.sysdyn.manager;\r
\r
import java.io.File;\r
-import java.util.ArrayList;\r
-import java.util.TreeMap;\r
import java.util.UUID;\r
import java.util.concurrent.locks.Lock;\r
\r
import org.eclipse.core.runtime.Platform;\r
-import org.simantics.databoard.annotations.Optional;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.Session;\r
\r
public class SysdynExperiment extends Experiment implements IDynamicExperiment {\r
\r
- Session session;\r
- Runnable modificationListener;\r
- Runnable modificationListener2;\r
- Resource experiment;\r
- SysdynModel sysdynModel;\r
-\r
- public SysdynExperiment(Resource experiment, Resource model) {\r
- super(model);\r
- this.experiment = experiment;\r
- // TODO Auto-generated constructor stub\r
- }\r
- \r
- public Resource getResource() {\r
- return this.experiment;\r
- }\r
-\r
- @Override\r
- public void rewindTo(double time) {\r
- // TODO Auto-generated method stub\r
- System.out.println("rewindTo");\r
- }\r
-\r
- @Override\r
- public void saveState() {\r
- // TODO Auto-generated method stub\r
- System.out.println("saveState");\r
- if(sysdynModel.getSysdynResult() == null) return;\r
-\r
- try {\r
- session.syncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- Resource model = graph.getSingleObject(experiment, l0.PartOf);\r
- Resource project = graph.getSingleObject(model, l0.PartOf);\r
- String projectName = graph.getPossibleRelatedValue(project, l0.HasName);\r
- File root = new File(Platform.getLocation().toOSString(), "www.simantics.org");\r
- if(!root.isDirectory()) root.mkdir();\r
- File projectRoot = new File(root, projectName);\r
- if(!projectRoot.isDirectory()) projectRoot.mkdir();\r
- File file = new File( projectRoot, UUID.randomUUID().toString() + ".dbb");\r
- \r
- String name = GraphUtils.findFreshName(graph, "Result", model, l0.ConsistsOf, "%s%d");\r
- \r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Resource res = GraphUtils.create2(graph, sr.Result,\r
- l0.HasLabel, name,\r
- l0.HasName, name,\r
- l0.PartOf, model,\r
- sr.HasResultFile, file.getAbsolutePath());\r
- graph.claim(experiment, sr.HasResult, res);\r
- sysdynModel.getSysdynResult().saveToFile(file);\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
-\r
-\r
- @Override\r
- public void simulate(boolean enabled) {\r
- System.out.println("simulate");\r
- if(enabled) {\r
- changeState(ExperimentState.STOPPED);\r
- startSimulation();\r
- }\r
- }\r
-\r
-\r
- private void startSimulation() {\r
- try {\r
- session.syncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- SimulationScheduler.start(sysdynModel);\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
-\r
- public void toggleSimulation(boolean enabled) {\r
- if(enabled) {\r
- changeState(ExperimentState.RUNNING);\r
- if(modificationListener == null) { \r
- \r
- modificationListener = new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- session.asyncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- if(getState() == ExperimentState.RUNNING) {\r
- SimulationScheduler.start(sysdynModel);\r
- }\r
-\r
- }\r
- });\r
-\r
- };\r
- };\r
- sysdynModel.addModificationListener(modificationListener);\r
- } \r
- }\r
- else {\r
- changeState(ExperimentState.STOPPED); \r
- }\r
-\r
- }\r
-\r
- @Override\r
- public void simulateDuration(double duration) {\r
- // TODO Auto-generated method stub\r
- System.out.println("simulateDuartion");\r
- }\r
-\r
- @Override\r
- public void refresh(Session session) {\r
- System.out.println("refresh");\r
- session.asyncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- init(graph);\r
- }\r
- \r
- });\r
- }\r
-\r
- public void init(ReadGraph g) {\r
- System.out.println("init");\r
- changeState(ExperimentState.STOPPED);\r
- this.session = g.getSession();\r
- \r
- session.asyncRequest(new ReadRequest() {\r
- \r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- final Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);\r
- sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration);\r
- toggleActivation(graph, true);\r
- \r
- if (modificationListener2 == null) {\r
- modificationListener2 = new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- refresh(session);\r
- };\r
- };\r
- sysdynModel.addModificationListener(modificationListener2);\r
- }\r
- }\r
- });\r
- \r
- }\r
-\r
- @Override\r
- protected void localStateChange() {\r
- ExperimentState state = getState();\r
- switch(state) {\r
- case DISPOSED:\r
- System.out.println("disposed");\r
- session.asyncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- sysdynModel.removeModificationListener(modificationListener);\r
- sysdynModel.removeModificationListener(modificationListener2);\r
- modificationListener = null;\r
- toggleActivation(graph, false);\r
- }\r
- });\r
- break;\r
- }\r
- }\r
- \r
- private void toggleActivation(ReadGraph graph, final boolean activate) {\r
- VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
- final Session session = graph.getSession();\r
- session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("experiments")) {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- VirtualGraph runtime = graph.getService(VirtualGraph.class);\r
-\r
- session.asyncRequest(new WriteRequest(runtime) {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- SimulationResource SIMU = SimulationResource.getInstance(graph);\r
- if(activate)\r
- graph.claim(experiment, SIMU.IsActive, experiment);\r
- else\r
- graph.denyStatement(experiment, SIMU.IsActive, experiment);\r
- }\r
-\r
- });\r
- }\r
- });\r
- }\r
- \r
- public static class Sample {\r
- public double value;\r
- \r
- @Override\r
- public String toString() {\r
- return ""+value;\r
- }\r
- }\r
- \r
- public static class Segment {\r
- public int startTime, endTime;\r
- public ArrayList<Sample> samples;\r
- \r
- @Override\r
- public String toString() {\r
- return "[startTime="+startTime+", endTime="+endTime+", samples="+samples+"]";\r
- }\r
- \r
- }\r
-\r
- public static class Recording {\r
- public String id;\r
- public TreeMap<String, String> labels;\r
- public ArrayList<Segment> segments;\r
- \r
- @Override\r
- public String toString() {\r
- return "id="+id+", labels="+labels+", segments="+segments;\r
- }\r
- \r
- }\r
-\r
- public static class RecordingSession {\r
- public @Optional Variant datasource;\r
- public Variant timeVarialbeId;\r
- public TreeMap<Variant, Variant> recordings;\r
- \r
- @Override\r
- public String toString() {\r
- return recordings.toString();\r
- }\r
- }\r
- \r
- public static class NodeId {\r
- public String nodeId;\r
- }\r
+ Session session;\r
+ Runnable modificationListener;\r
+ Resource experiment;\r
+ SysdynModel sysdynModel;\r
+ boolean toggled = false;\r
+\r
+ public SysdynExperiment(Resource experiment, Resource model) {\r
+ super(model);\r
+ this.experiment = experiment;\r
+ // TODO Auto-generated constructor stub\r
+ }\r
+\r
+ public Resource getResource() {\r
+ return this.experiment;\r
+ }\r
+\r
+ @Override\r
+ public void rewindTo(double time) {\r
+ // TODO Auto-generated method stub\r
+ System.out.println("rewindTo");\r
+ }\r
+\r
+ @Override\r
+ public void saveState() {\r
+ if(sysdynModel.getSysdynResult() == null) return;\r
+\r
+ try {\r
+ session.syncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ Resource model = graph.getSingleObject(experiment, l0.PartOf);\r
+ Resource project = graph.getSingleObject(model, l0.PartOf);\r
+ String projectName = graph.getPossibleRelatedValue(project, l0.HasName);\r
+ File root = new File(Platform.getLocation().toOSString(), "www.simantics.org");\r
+ if(!root.isDirectory()) root.mkdir();\r
+ File projectRoot = new File(root, projectName);\r
+ if(!projectRoot.isDirectory()) projectRoot.mkdir();\r
+ File file = new File( projectRoot, UUID.randomUUID().toString() + ".dbb");\r
+\r
+ String name = GraphUtils.findFreshName(graph, "Result", model, l0.ConsistsOf, "%s%d");\r
+\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Resource res = GraphUtils.create2(graph, sr.Result,\r
+ l0.HasLabel, name,\r
+ l0.HasName, name,\r
+ l0.PartOf, model,\r
+ sr.HasResultFile, file.getAbsolutePath());\r
+ graph.claim(experiment, sr.HasResult, res);\r
+ sysdynModel.getSysdynResult().saveToFile(file);\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+\r
+ @Override\r
+ public void simulate(boolean enabled) {\r
+ if(enabled) {\r
+ changeState(ExperimentState.RUNNING);\r
+ startSimulation();\r
+ } else if (!toggled){\r
+ changeState(ExperimentState.STOPPED);\r
+ }\r
+ }\r
+\r
+\r
+ private void startSimulation() {\r
+ session.asyncRequest(new ReadRequest() {\r
+\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ SimulationScheduler.start(sysdynModel, SysdynExperiment.this);\r
+ }\r
+ });\r
+ }\r
+\r
+ public void toggleSimulation(boolean enabled) {\r
+ if(enabled) {\r
+ this.toggled = true;\r
+ changeState(ExperimentState.RUNNING);\r
+ if(modificationListener == null) { \r
+\r
+ modificationListener = new Runnable() {\r
+\r
+ @Override\r
+ public void run() {\r
+ session.asyncRequest(new ReadRequest() {\r
+\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ if(getState() == ExperimentState.RUNNING) {\r
+ SimulationScheduler.start(sysdynModel, SysdynExperiment.this);\r
+ }\r
+\r
+ }\r
+ });\r
+\r
+ };\r
+ };\r
+ sysdynModel.addModificationListener(modificationListener);\r
+ } \r
+ }\r
+ else {\r
+ changeState(ExperimentState.STOPPED);\r
+ this.toggled = false;\r
+ }\r
+\r
+ }\r
+\r
+ @Override\r
+ public void simulateDuration(double duration) {\r
+ // TODO Auto-generated method stub\r
+ System.out.println("simulateDuartion");\r
+ }\r
+\r
+ @Override\r
+ public void refresh(Session session) {\r
+ session.asyncRequest(new ReadRequest() {\r
+\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ init(graph);\r
+ }\r
+\r
+ });\r
+ }\r
+\r
+ public void init(ReadGraph g) {\r
+ changeState(ExperimentState.STOPPED);\r
+ this.session = g.getSession();\r
+\r
+ session.asyncRequest(new ReadRequest() {\r
+\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ final Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);\r
+ sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration);\r
+ toggleActivation(graph, true);\r
+ }\r
+ });\r
+\r
+ }\r
+\r
+ @Override\r
+ protected void localStateChange() {\r
+ ExperimentState state = getState();\r
+ switch(state) {\r
+ case DISPOSED:\r
+ System.out.println("disposed");\r
+ session.asyncRequest(new ReadRequest() {\r
+\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ sysdynModel.cancelSimulation();\r
+ sysdynModel.removeModificationListener(modificationListener);\r
+ modificationListener = null;\r
+ toggleActivation(graph, false);\r
+ }\r
+ });\r
+ break;\r
+ }\r
+ }\r
+\r
+ private void toggleActivation(ReadGraph graph, final boolean activate) {\r
+ VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
+ final Session session = graph.getSession();\r
+ session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("experiments")) {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ VirtualGraph runtime = graph.getService(VirtualGraph.class);\r
+\r
+ session.asyncRequest(new WriteRequest(runtime) {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ if(activate)\r
+ graph.claim(experiment, SIMU.IsActive, experiment);\r
+ else\r
+ graph.denyStatement(experiment, SIMU.IsActive, experiment);\r
+ }\r
+\r
+ });\r
+ }\r
+ });\r
+ }\r
\r
@Override\r
public Lock getDatasourceLock() {\r
import org.simantics.objmap.IMapping;\r
import org.simantics.objmap.IMappingListener;\r
import org.simantics.objmap.Mappings;\r
+import org.simantics.simulation.experiment.Experiment;\r
+import org.simantics.simulation.experiment.ExperimentState;\r
import org.simantics.simulation.experiment.IExperiment;\r
import org.simantics.simulation.model.IModel;\r
import org.simantics.simulation.ontology.SimulationResource;\r
*/\r
public class SysdynModel implements IMappingListener, IModel {\r
\r
- Session session;\r
+ Session session;\r
\r
- IMapping mapping;\r
+ IMapping mapping;\r
\r
- Resource configurationResource;\r
- Configuration configuration;\r
+ Resource configurationResource;\r
+ Configuration configuration;\r
\r
- Set<Configuration> modules = new HashSet<Configuration>();\r
+ Set<Configuration> modules = new HashSet<Configuration>();\r
\r
- Process process;\r
- boolean canceled;\r
- SimulationResult result;\r
- SysdynResult sysdynResult;\r
+ Process process;\r
+ boolean canceled;\r
+ SimulationResult result;\r
+ SysdynResult sysdynResult;\r
\r
- CopyOnWriteArrayList<Runnable> modificationListeners =\r
- new CopyOnWriteArrayList<Runnable>();\r
- CopyOnWriteArrayList<Runnable> resultListeners =\r
- new CopyOnWriteArrayList<Runnable>();\r
+ CopyOnWriteArrayList<Runnable> modificationListeners =\r
+ new CopyOnWriteArrayList<Runnable>();\r
+ CopyOnWriteArrayList<Runnable> resultListeners =\r
+ new CopyOnWriteArrayList<Runnable>();\r
\r
- @SuppressWarnings("rawtypes")\r
+ @SuppressWarnings("rawtypes")\r
Map<Class, Object> services = new HashMap<Class, Object>();\r
\r
- String previousModelStructure;\r
- HashMap<String, String> previousImportantInits = new HashMap<String, String>();\r
-\r
- File simulationDir;\r
-\r
- void readModules(ReadGraph graph, Resource configResource, Set<Resource> result) throws DatabaseException {\r
-\r
- if(!result.add(configResource)) return;\r
-\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- StructuralResource2 str = StructuralResource2.getInstance(graph);\r
-\r
- for(Resource part : graph.getObjects(configResource, l0.ConsistsOf)) {\r
- if(graph.isInstanceOf(part, sr.Module)) {\r
- Resource type = graph.getPossibleType(part, sr.Module);\r
- Resource config = graph.getPossibleObject(type, str.IsDefinedBy);\r
- readModules(graph, config, result);\r
- }\r
- }\r
-\r
- }\r
-\r
- Set<Resource> readModules(ReadGraph graph, Resource configResource) throws DatabaseException {\r
- HashSet<Resource> result = new HashSet<Resource>();\r
- readModules(graph, configResource, result);\r
- return result;\r
- }\r
-\r
- private void createMapping(ReadGraph g) throws DatabaseException {\r
- SysdynSchema schema = new SysdynSchema(g);\r
- mapping = Mappings.createWithListening(schema);\r
- mapping.addMappingListener(SysdynModel.this);\r
- configuration = (Configuration)mapping.map(g, configurationResource);\r
- for(Resource config : readModules(g, configurationResource)) {\r
- modules.add((Configuration)mapping.map(g, config));\r
- }\r
- System.out.println("Loaded model with " + modules.size() + " modules.");\r
- }\r
-\r
- public SysdynModel(ReadGraph g, Resource configurationResource) {\r
- this.session = g.getSession();\r
- this.configurationResource = configurationResource;\r
-\r
- try {\r
- createMapping(g);\r
- } catch(DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
- sysdynResult = new SysdynResult();\r
- sysdynResult.setResult(new SimulationResult());\r
-\r
- previousModelStructure = "";\r
-\r
- File modelsDir = Activator.getBundleContext().getDataFile("models");\r
- String configName = configuration.getName();\r
- List<String> files = Arrays.asList(modelsDir.list());\r
- if (files.contains(configName)) {\r
- int i = 2;\r
- while (files.contains(configName + "_" + i)){\r
- i++;\r
- }\r
- configName += "_" + i;\r
- }\r
-\r
- simulationDir = Activator.getBundleContext().getDataFile("models/" + configName);\r
- if (!simulationDir.exists()) {\r
- simulationDir.mkdir();\r
- }\r
- }\r
-\r
-\r
- /*\r
- * some dummy(?) stuff for experiments\r
- */\r
- public SysdynModel(Resource configurationResource) {\r
- this.configurationResource = configurationResource;\r
- }\r
-\r
- public synchronized void simulate(IModelicaMonitor monitor, final IProgressMonitor progressMonitor) throws IOException {\r
- canceled = false;\r
- \r
- ModelicaWriter writer = new ModelicaWriter();\r
- //writer.write(configuration);\r
- for(Configuration c : modules) {\r
- writer.write(c);\r
- }\r
-\r
- progressMonitor.worked(20);\r
-\r
- String modelText = writer.toString();\r
-\r
- HashMap<String, String> inits = getInits(configuration, "");\r
-\r
- Model model = configuration.getModel();\r
-\r
- String startTime = model.getStartTime().toString();\r
- String stopTime = model.getStopTime().toString();\r
- inits.put("start value", startTime);\r
- inits.put("stop value", stopTime);\r
- if(model.getSolver() != null)\r
- inits.put("solver", model.getSolver());\r
- if(model.getTolerance() != null)\r
- inits.put("tolerance", model.getTolerance().toString());\r
-\r
- final SimulationLocation simulationLocation = ModelicaManager.createInputFiles(\r
- simulationDir,\r
- configuration.getName(),\r
- writer.toString(),\r
- inits);\r
-\r
- progressMonitor.worked(20);\r
-\r
- if (hasStructureChanged(modelText, inits)) {\r
- previousModelStructure = modelText;\r
- System.out.println("== Modelica == ");\r
- System.out.println(writer.toString());\r
- System.out.println("== Modelica ends == ");\r
-\r
- try {\r
+ String previousModelStructure;\r
+ HashMap<String, String> previousImportantInits = new HashMap<String, String>();\r
+\r
+ File simulationDir;\r
+\r
+ void readModules(ReadGraph graph, Resource configResource, Set<Resource> result) throws DatabaseException {\r
+\r
+ if(!result.add(configResource)) return;\r
+\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ StructuralResource2 str = StructuralResource2.getInstance(graph);\r
+\r
+ for(Resource part : graph.getObjects(configResource, l0.ConsistsOf)) {\r
+ if(graph.isInstanceOf(part, sr.Module)) {\r
+ Resource type = graph.getPossibleType(part, sr.Module);\r
+ Resource config = graph.getPossibleObject(type, str.IsDefinedBy);\r
+ readModules(graph, config, result);\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ Set<Resource> readModules(ReadGraph graph, Resource configResource) throws DatabaseException {\r
+ HashSet<Resource> result = new HashSet<Resource>();\r
+ readModules(graph, configResource, result);\r
+ return result;\r
+ }\r
+\r
+ private void createMapping(ReadGraph g) throws DatabaseException {\r
+ SysdynSchema schema = new SysdynSchema(g);\r
+ mapping = Mappings.createWithListening(schema);\r
+ mapping.addMappingListener(SysdynModel.this);\r
+ configuration = (Configuration)mapping.map(g, configurationResource);\r
+ for(Resource config : readModules(g, configurationResource)) {\r
+ modules.add((Configuration)mapping.map(g, config));\r
+ }\r
+ System.out.println("Loaded model with " + modules.size() + " modules.");\r
+ }\r
+\r
+ public SysdynModel(ReadGraph g, Resource configurationResource) {\r
+ this.session = g.getSession();\r
+ this.configurationResource = configurationResource;\r
+\r
+ try {\r
+ createMapping(g);\r
+ } catch(DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ sysdynResult = new SysdynResult();\r
+ sysdynResult.setResult(new SimulationResult());\r
+\r
+ previousModelStructure = "";\r
+\r
+ File modelsDir = Activator.getBundleContext().getDataFile("models");\r
+ String configName = configuration.getName();\r
+ List<String> files = Arrays.asList(modelsDir.list());\r
+ if (files.contains(configName)) {\r
+ int i = 2;\r
+ while (files.contains(configName + "_" + i)){\r
+ i++;\r
+ }\r
+ configName += "_" + i;\r
+ }\r
+\r
+ simulationDir = Activator.getBundleContext().getDataFile("models/" + configName);\r
+ if (!simulationDir.exists()) {\r
+ simulationDir.mkdir();\r
+ }\r
+ }\r
+\r
+\r
+ /*\r
+ * some dummy(?) stuff for experiments\r
+ */\r
+ public SysdynModel(Resource configurationResource) {\r
+ this.configurationResource = configurationResource;\r
+ }\r
+\r
+ public synchronized void simulate(IModelicaMonitor monitor, final IProgressMonitor progressMonitor, final Experiment experiment) throws IOException {\r
+ canceled = false;\r
+ progressMonitor.subTask("Write modelica classes");\r
+ ModelicaWriter writer = new ModelicaWriter();\r
+ for(Configuration c : modules) {\r
+ writer.write(c);\r
+ }\r
+ progressMonitor.worked(1);\r
+\r
+ progressMonitor.subTask("Write initial files");\r
+ String modelText = writer.toString();\r
+ HashMap<String, String> inits = getInits(configuration, "");\r
+ Model model = configuration.getModel();\r
+ String startTime = model.getStartTime().toString();\r
+ String stopTime = model.getStopTime().toString();\r
+ inits.put("start value", startTime);\r
+ inits.put("stop value", stopTime);\r
+ if(model.getSolver() != null)\r
+ inits.put("solver", model.getSolver());\r
+ if(model.getTolerance() != null)\r
+ inits.put("tolerance", model.getTolerance().toString());\r
+\r
+ final SimulationLocation simulationLocation = ModelicaManager.createInputFiles(\r
+ simulationDir,\r
+ configuration.getName(),\r
+ writer.toString(),\r
+ inits);\r
+\r
+ progressMonitor.worked(1);\r
+\r
+\r
+ if (!simulationLocation.exeFile.isFile() || hasStructureChanged(modelText, inits)) {\r
+ progressMonitor.subTask("Build model");\r
+ previousModelStructure = modelText;\r
+ System.out.println("== Modelica == ");\r
+ System.out.println(writer.toString());\r
+ System.out.println("== Modelica ends == ");\r
+\r
+ try {\r
+ simulationLocation.exeFile.delete();\r
ModelicaManager.buildModel(simulationLocation, monitor);\r
- previousImportantInits.clear();\r
- previousImportantInits.put("start value", startTime);\r
- previousImportantInits.put("stop value", stopTime);\r
+ previousImportantInits.clear();\r
+ previousImportantInits.put("start value", startTime);\r
+ previousImportantInits.put("stop value", stopTime);\r
} catch (ModelicaException e) {\r
monitor.showConsole();\r
canceled = true;\r
previousModelStructure = "";\r
}\r
- }\r
-\r
- progressMonitor.worked(20);\r
-\r
- if(simulationLocation != null && !canceled) {\r
- process = ModelicaManager.runModelica(\r
- simulationLocation,\r
- monitor,\r
- inits\r
- );\r
- ModelicaManager.printProcessOutput(process, monitor);\r
-\r
- Thread resultThread = new Thread() {\r
- @Override\r
- public void run() {\r
- try {\r
- process.waitFor();\r
-\r
- if(!canceled) {\r
- result = new SimulationResult();\r
- result.read(simulationLocation.outputFile);\r
- result.readInits(simulationLocation.initFile);\r
- result.filter();\r
- progressMonitor.worked(20);\r
- sysdynResult.setResult(result);\r
- progressMonitor.worked(20);\r
- synchronized(resultListeners) {\r
- for(Runnable listener : resultListeners)\r
- listener.run();\r
- }\r
- }\r
- } catch (FileNotFoundException e) {\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- };\r
- resultThread.run();\r
- }\r
- process = null;\r
- }\r
-\r
- private boolean hasStructureChanged(String modelText, Map<String, String> inits) {\r
- if (!modelText.equals(previousModelStructure))\r
- return true;\r
- for(String key : previousImportantInits.keySet()) {\r
- if(!inits.containsKey(key) || !previousImportantInits.get(key).equals(inits.get(key)))\r
- return true;\r
- }\r
- return false;\r
- }\r
- \r
- public void cancelSimulation() {\r
- canceled = true;\r
- if(process != null) {\r
- process.destroy();\r
- }\r
- }\r
-\r
- public synchronized boolean update() throws DatabaseException {\r
- if(mapping.isDomainModified()) {\r
- session.syncRequest(new ReadRequest() {\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- mapping.updateRange(graph);\r
- }\r
- });\r
- return true;\r
- }\r
- else\r
- return false;\r
- }\r
-\r
- public SimulationResult getSimulationResult() {\r
- return result;\r
- }\r
-\r
- public SysdynResult getSysdynResult() {\r
- return sysdynResult;\r
- }\r
-\r
- public void addResultListener(Runnable listener) {\r
- synchronized(resultListeners) {\r
- resultListeners.add(listener);\r
- }\r
- }\r
-\r
- public void removeResultListener(Runnable listener) {\r
- synchronized(resultListeners) {\r
- resultListeners.add(listener);\r
- }\r
- }\r
-\r
- public void addModificationListener(Runnable listener) {\r
- synchronized(modificationListeners) {\r
- modificationListeners.add(listener);\r
- }\r
- }\r
-\r
- public void removeModificationListener(Runnable listener) {\r
- synchronized(modificationListeners) {\r
- modificationListeners.remove(listener);\r
- }\r
- }\r
-\r
- @Override\r
- public void domainModified() {\r
- synchronized(modificationListeners) {\r
- for(Runnable listener : modificationListeners)\r
- listener.run();\r
- }\r
- }\r
-\r
- @Override\r
- public void rangeModified() {\r
- }\r
-\r
- public Configuration getConfiguration() {\r
- return configuration;\r
- }\r
-\r
- public IMapping getMapping() {\r
- return mapping;\r
- }\r
-\r
- public synchronized IElement getElement(Resource resource) {\r
- return (IElement)mapping.get(resource);\r
- }\r
-\r
- public <T> T getService(Class<T> clazz) {\r
- synchronized(services) {\r
- return clazz.cast(services.get(clazz));\r
- }\r
- }\r
-\r
- public <T> void addService(Class<T> clazz, T service) {\r
- synchronized(services) {\r
- services.put(clazz, service);\r
- }\r
- }\r
-\r
- @Override\r
- public IExperiment loadExperiment(ReadGraph g, Resource experiment, IExperimentActivationListener listener) {\r
- SysdynExperiment exp = new SysdynExperiment(experiment, configurationResource);\r
- try {\r
- exp.init(g);\r
- listener.onExperimentActivated(exp);\r
- return exp;\r
- } catch(Exception e) {\r
- listener.onFailure(e);\r
- return null;\r
- }\r
- }\r
-\r
- public Collection<SysdynResult> getActiveResults(ReadGraph graph) {\r
- ArrayList<SysdynResult> results = new ArrayList<SysdynResult>();\r
-\r
- try {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- SimulationResource SIMU = SimulationResource.getInstance(graph);\r
- Resource model = graph.getSingleObject(configurationResource, SIMU.IsConfigurationOf);\r
- Collection<Resource> experiments = graph.getObjects(model, l0.ConsistsOf);\r
- for(Resource experiment : experiments) {\r
- Collection<Resource> experimentResults = graph.getObjects(experiment, sr.HasResult);\r
- for(Resource result : experimentResults) {\r
- if(graph.hasStatement(result, SIMU.IsActive)) {\r
- SysdynResult sysdynResult = new SysdynResult(\r
- (String) graph.getPossibleRelatedValue(result, l0.HasLabel),\r
- (String) graph.getPossibleRelatedValue(result, sr.HasResultFile));\r
- results.add(sysdynResult);\r
- }\r
- }\r
- }\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- }\r
-\r
- if(getSysdynResult() != null)\r
- results.add(0, getSysdynResult() );\r
-\r
-\r
-\r
- return results;\r
- }\r
-\r
- private HashMap<String, String> getInits(Configuration configuration, String prefix) {\r
- HashMap<String, String> inits = new HashMap<String, String>();\r
- for (IElement element : configuration.getElements()) {\r
- if (element instanceof Module) {\r
- Module module = (Module) element;\r
- Configuration conf = module.getType().getConfiguration();\r
- inits.putAll(getInits(conf, prefix + module.getName() + "."));\r
- } else if (element instanceof IndependentVariable) {\r
- IndependentVariable variable = (IndependentVariable) element;\r
- if (variable.getExpression() instanceof ParameterExpression) {\r
- inits.put(prefix + variable.getName(), "" + ((ParameterExpression)variable.getExpression()).getValue());\r
- }\r
- }\r
- }\r
- return inits;\r
- }\r
+ }\r
+\r
+ progressMonitor.worked(1);\r
+\r
+ if(simulationLocation != null && !canceled) {\r
+ progressMonitor.subTask("Simulate model");\r
+ process = ModelicaManager.runModelica(\r
+ simulationLocation,\r
+ monitor,\r
+ inits\r
+ );\r
+ ModelicaManager.printProcessOutput(process, monitor);\r
+\r
+ Thread resultThread = new Thread() {\r
+ @Override\r
+ public void run() {\r
+ try {\r
+ process.waitFor();\r
+\r
+ if(!canceled) {\r
+ progressMonitor.worked(1);\r
+ progressMonitor.subTask("Read results");\r
+ result = new SimulationResult();\r
+ result.read(simulationLocation.outputFile);\r
+ result.readInits(simulationLocation.initFile);\r
+ result.filter();\r
+ sysdynResult.setResult(result);\r
+ progressMonitor.worked(1);\r
+ synchronized(resultListeners) {\r
+ for(Runnable listener : resultListeners)\r
+ listener.run();\r
+ }\r
+ setExperimentStopped(experiment);\r
+ }\r
+ } catch (FileNotFoundException e) {\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ } catch (InterruptedException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ };\r
+ resultThread.run();\r
+ }\r
+ if(canceled)\r
+ setExperimentStopped(experiment);\r
+ process = null;\r
+ }\r
+\r
+ private void setExperimentStopped(Experiment experiment) {\r
+ if(experiment instanceof SysdynExperiment) {\r
+ SysdynExperiment e = (SysdynExperiment)experiment;\r
+ if(e.getState() != ExperimentState.DISPOSED)\r
+ e.simulate(false);\r
+ }\r
+ }\r
+\r
+ private boolean hasStructureChanged(String modelText, Map<String, String> inits) {\r
+ if (!modelText.equals(previousModelStructure))\r
+ return true;\r
+ for(String key : previousImportantInits.keySet()) {\r
+ if(!inits.containsKey(key) || !previousImportantInits.get(key).equals(inits.get(key)))\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ public void cancelSimulation() {\r
+ canceled = true;\r
+ if(process != null) {\r
+ process.destroy();\r
+ }\r
+ }\r
+\r
+ public synchronized boolean update() throws DatabaseException {\r
+ if(mapping.isDomainModified()) {\r
+ session.syncRequest(new ReadRequest() {\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ mapping.updateRange(graph);\r
+ }\r
+ });\r
+ return true;\r
+ }\r
+ else\r
+ return false;\r
+ }\r
+\r
+ public SimulationResult getSimulationResult() {\r
+ return result;\r
+ }\r
+\r
+ public SysdynResult getSysdynResult() {\r
+ return sysdynResult;\r
+ }\r
+\r
+ public void addResultListener(Runnable listener) {\r
+ synchronized(resultListeners) {\r
+ resultListeners.add(listener);\r
+ }\r
+ }\r
+\r
+ public void removeResultListener(Runnable listener) {\r
+ synchronized(resultListeners) {\r
+ resultListeners.add(listener);\r
+ }\r
+ }\r
+\r
+ public void addModificationListener(Runnable listener) {\r
+ synchronized(modificationListeners) {\r
+ modificationListeners.add(listener);\r
+ }\r
+ }\r
+\r
+ public void removeModificationListener(Runnable listener) {\r
+ synchronized(modificationListeners) {\r
+ modificationListeners.remove(listener);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void domainModified() {\r
+ synchronized(modificationListeners) {\r
+ for(Runnable listener : modificationListeners)\r
+ listener.run();\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void rangeModified() {\r
+ }\r
+\r
+ public Configuration getConfiguration() {\r
+ return configuration;\r
+ }\r
+\r
+ public IMapping getMapping() {\r
+ return mapping;\r
+ }\r
+\r
+ public synchronized IElement getElement(Resource resource) {\r
+ return (IElement)mapping.get(resource);\r
+ }\r
+\r
+ public <T> T getService(Class<T> clazz) {\r
+ synchronized(services) {\r
+ return clazz.cast(services.get(clazz));\r
+ }\r
+ }\r
+\r
+ public <T> void addService(Class<T> clazz, T service) {\r
+ synchronized(services) {\r
+ services.put(clazz, service);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public IExperiment loadExperiment(ReadGraph g, Resource experiment, IExperimentActivationListener listener) {\r
+ SysdynExperiment exp = new SysdynExperiment(experiment, configurationResource);\r
+ try {\r
+ exp.init(g);\r
+ listener.onExperimentActivated(exp);\r
+ return exp;\r
+ } catch(Exception e) {\r
+ listener.onFailure(e);\r
+ return null;\r
+ }\r
+ }\r
+\r
+ public Collection<SysdynResult> getActiveResults(ReadGraph graph) {\r
+ ArrayList<SysdynResult> results = new ArrayList<SysdynResult>();\r
+\r
+ try {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ Resource model = graph.getSingleObject(configurationResource, SIMU.IsConfigurationOf);\r
+ Collection<Resource> experiments = graph.getObjects(model, l0.ConsistsOf);\r
+ for(Resource experiment : experiments) {\r
+ Collection<Resource> experimentResults = graph.getObjects(experiment, sr.HasResult);\r
+ for(Resource result : experimentResults) {\r
+ if(graph.hasStatement(result, SIMU.IsActive)) {\r
+ SysdynResult sysdynResult = new SysdynResult(\r
+ (String) graph.getPossibleRelatedValue(result, l0.HasLabel),\r
+ (String) graph.getPossibleRelatedValue(result, sr.HasResultFile));\r
+ results.add(sysdynResult);\r
+ }\r
+ }\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+\r
+ if(getSysdynResult() != null)\r
+ results.add(0, getSysdynResult() );\r
+\r
+\r
+\r
+ return results;\r
+ }\r
+\r
+ private HashMap<String, String> getInits(Configuration configuration, String prefix) {\r
+ HashMap<String, String> inits = new HashMap<String, String>();\r
+ for (IElement element : configuration.getElements()) {\r
+ if (element instanceof Module) {\r
+ Module module = (Module) element;\r
+ Configuration conf = module.getType().getConfiguration();\r
+ inits.putAll(getInits(conf, prefix + module.getName() + "."));\r
+ } else if (element instanceof IndependentVariable) {\r
+ IndependentVariable variable = (IndependentVariable) element;\r
+ if (variable.getExpression() instanceof ParameterExpression) {\r
+ inits.put(prefix + variable.getName(), "" + ((ParameterExpression)variable.getExpression()).getValue());\r
+ }\r
+ }\r
+ }\r
+ return inits;\r
+ }\r
\r
}\r