From 0c4aa9304f5c93e658773192216be5aac0abf849 Mon Sep 17 00:00:00 2001 From: niemisto Date: Tue, 26 Jan 2010 19:37:39 +0000 Subject: [PATCH] SysdynModel and SysdynModelManager git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13655 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn/META-INF/MANIFEST.MF | 5 +- .../simantics/sysdyn/manager/SysdynModel.java | 124 ++++++++++++++++++ .../sysdyn/manager/SysdynModelManager.java | 39 ++++++ .../sysdyn/modelica/ModelicaWriter.java | 30 +++-- 4 files changed, 187 insertions(+), 11 deletions(-) create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index e7dfc071..42514cbb 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -5,9 +5,12 @@ Bundle-SymbolicName: org.simantics.sysdyn Bundle-Version: 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Require-Bundle: org.simantics.objmap;bundle-version="0.1.0", - org.simantics.db;bundle-version="0.6.2" + org.simantics.db;bundle-version="0.6.2", + org.simantics.modelica;bundle-version="1.0.0", + org.simantics.db.common;bundle-version="0.7.1" Export-Package: org.simantics.sysdyn, org.simantics.sysdyn.expressionParser, + org.simantics.sysdyn.manager, org.simantics.sysdyn.modelica, org.simantics.sysdyn.representation, org.simantics.sysdyn.representation.visitors diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java new file mode 100644 index 00000000..c6430001 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -0,0 +1,124 @@ +package org.simantics.sysdyn.manager; + +import java.io.IOException; +import java.util.concurrent.CopyOnWriteArrayList; + +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.modelica.ModelicaManager; +import org.simantics.modelica.data.SimulationResult; +import org.simantics.objmap.IMapping; +import org.simantics.objmap.IMappingListener; +import org.simantics.objmap.Mappings; +import org.simantics.sysdyn.modelica.ModelicaWriter; +import org.simantics.sysdyn.representation.Configuration; +import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.SysdynSchema; + +/** + * Maintains a Java representation of system dynamic model. + * @author Hannu Niemistö + */ +public class SysdynModel implements IMappingListener { + + Session session; + Resource configurationResource; + + IMapping mapping; + Configuration configuration; + SimulationResult result; + + CopyOnWriteArrayList modificationListeners = + new CopyOnWriteArrayList(); + CopyOnWriteArrayList resultListeners = + new CopyOnWriteArrayList(); + + private void createMapping() throws DatabaseException { + session.syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + SysdynSchema schema = new SysdynSchema(graph); + mapping = Mappings.createWithListening(schema); + mapping.addMappingListener(SysdynModel.this); + configuration = (Configuration)mapping.map(graph, configurationResource); + + } + + }); + } + + public SysdynModel(Session session, Resource configurationResource) { + this.session = session; + this.configurationResource = configurationResource; + + try { + createMapping(); + } catch(DatabaseException e) { + e.printStackTrace(); + } + } + + public void simulate() throws IOException { + try { + ModelicaWriter writer = new ModelicaWriter(); + writer.write(configuration); + + result = ModelicaManager.runModelica( + configuration.name, + writer.toString()); + } catch(Exception e) { + result = null; + } + for(Runnable listener : resultListeners) + listener.run(); + } + + public boolean update() throws DatabaseException { + if(mapping.isDomainModified()) { + session.syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + mapping.updateRange(graph); + } + }); + return true; + } + else + return false; + } + + public SimulationResult getResult() { + return result; + } + + public void addResultListener(Runnable listener) { + resultListeners.add(listener); + } + + public void addModificationListener(Runnable listener) { + modificationListeners.add(listener); + } + + @Override + public void domainModified() { + for(Runnable listener : modificationListeners) + listener.run(); + } + + @Override + public void rangeModified() { + } + + public Configuration getConfiguration() { + return configuration; + } + + public IElement getElement(Resource resource) { + return (IElement)mapping.get(resource); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java new file mode 100644 index 00000000..a3fb07c8 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java @@ -0,0 +1,39 @@ +package org.simantics.sysdyn.manager; + +import java.util.WeakHashMap; + +import org.simantics.db.Resource; +import org.simantics.db.Session; + +/** + * Manages system dynamic models. + * @author Hannu Niemistö + */ +public class SysdynModelManager { + WeakHashMap models = + new WeakHashMap(); + Session session; + + public SysdynModelManager(Session session) { + this.session = session; + } + + public SysdynModel getModel(Resource resource) { + SysdynModel model = models.get(resource); + if(model == null) { + model = new SysdynModel(session, resource); + models.put(resource, model); + } + return model; + } + + public static SysdynModelManager getInstance(Session session) { + SysdynModelManager manager = + session.getService(SysdynModelManager.class); + if(manager == null) { + manager = new SysdynModelManager(session); + session.registerService(SysdynModelManager.class, manager); + } + return manager; + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java index f990c4c2..a6259b14 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -4,6 +4,7 @@ import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Variable; +import org.simantics.sysdyn.representation.expressions.IExpression; public class ModelicaWriter { @@ -16,26 +17,35 @@ public class ModelicaWriter { b.append("model ").append(conf.name).append('\n'); for(IElement element : conf.elements) if(element instanceof Variable) { - app = ((Variable)element).expression.getDeclaration((Variable)element); - if (app != null) b.append(app); + IExpression expr = ((Variable)element).expression; + if(expr != null) { + app = expr.getDeclaration((Variable)element); + if (app != null) b.append(app); + } } for(IElement element : conf.elements) if(element instanceof Stock) { - app = ((Stock)element).expression.getInitialEquation((Stock)element); - if (app != null) { - if(initialEquations == false) { - initialEquations = true; - b.append("initial equation\n"); + IExpression expr = ((Stock)element).expression; + if(expr != null) { + app = expr.getInitialEquation((Stock)element); + if (app != null) { + if(initialEquations == false) { + initialEquations = true; + b.append("initial equation\n"); + } + b.append(app); } - b.append(app); } } b.append("equation\n"); for(IElement element : conf.elements) if(element instanceof Variable) { - app = ((Variable)element).expression.getEquation((Variable)element); - if (app != null) b.append(app); + IExpression expr = ((Variable)element).expression; + if(expr != null) { + app = expr.getEquation((Variable)element); + if (app != null) b.append(app); + } } b.append("end ").append(conf.name).append(";\n"); } -- 2.47.1