From: lempinen Date: Thu, 10 Jun 2010 11:26:26 +0000 (+0000) Subject: Simulation results using databoard X-Git-Tag: simantics-1.2.0~225 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=3fa8b4016b33ad6bf22d889dee86a9d453304c7e;p=simantics%2Fsysdyn.git Simulation results using databoard git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16106 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.modelica/META-INF/MANIFEST.MF b/org.simantics.modelica/META-INF/MANIFEST.MF index 3a07eb40..17b60bc2 100644 --- a/org.simantics.modelica/META-INF/MANIFEST.MF +++ b/org.simantics.modelica/META-INF/MANIFEST.MF @@ -4,6 +4,7 @@ Bundle-Name: Modelica Bundle-SymbolicName: org.simantics.modelica Bundle-Version: 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.6 -Require-Bundle: gnu.trove2;bundle-version="2.0.4" +Require-Bundle: gnu.trove2;bundle-version="2.0.4", + org.simantics.databoard;bundle-version="0.5.2" Export-Package: org.simantics.modelica, org.simantics.modelica.data diff --git a/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java b/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java index 888d2cee..6b0b4475 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java +++ b/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java @@ -30,7 +30,8 @@ import java.util.regex.Pattern; */ public class SimulationResult { - List dataSets = new ArrayList(); + List variables = new ArrayList(); + List initials = new ArrayList(); static String getLine(InputStream stream) { if(stream == null) @@ -90,7 +91,7 @@ public class SimulationResult { for(String key : mappedInitials.keySet()) { double[] values = {mappedInitials.get(key), mappedInitials.get(key)}; - dataSets.add(new DataSet(key, times , values)); + initials.add(new DataSet(key, times , values)); } } @@ -130,25 +131,33 @@ public class SimulationResult { values.add(Double.parseDouble(nn[1])); } - dataSets.add(new DataSet(name, times.toNativeArray(), values.toNativeArray())); + variables.add(new DataSet(name, times.toNativeArray(), values.toNativeArray())); } } public void filter() { - ArrayList newDataSets = new ArrayList(); - for(DataSet dataSet : dataSets) { - if(!dataSet.name.equals("time") && !dataSet.name.contains("$") && !dataSet.name.contains("der(")) - newDataSets.add(dataSet); + ArrayList newVariables = new ArrayList(); + for(DataSet dataSet : variables) { + if(!dataSet.name.contains("$") && !dataSet.name.contains("der(")) + newVariables.add(dataSet); } - dataSets = newDataSets; + variables = newVariables; } - public List getDataSets() { - return dataSets; + public List getVariableDataSets() { + return variables; } + /** + * Gets DataSet for variable. Loops first the variables and then initials. + * @param name the name of the variable + * @return DataSet for the variable or null if DataSet not found + */ public DataSet getDataSet(String name) { - for(DataSet set : dataSets) + for(DataSet set : variables) + if(set.name.equals(name)) + return set; + for(DataSet set : initials) if(set.name.equals(name)) return set; return null; diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index 7518623b..4b552959 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -25,5 +25,5 @@ - + \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index e7a97cbd..402d10a1 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -199,6 +199,22 @@ + + + + + + + + @@ -364,6 +380,11 @@ id="org.simantics.sysdyn.ui.saveResults" name="Save Results"> + + @@ -543,6 +564,16 @@ + + + + + + { @Override public String getLabel(ReadGraph graph, ExperimentNode experiment) throws DatabaseException { - String name = graph.getPossibleRelatedValue(experiment.resource, graph.getBuiltins().HasName); + String name = graph.getPossibleRelatedValue(experiment.resource, graph.getBuiltins().HasLabel); return name == null ? "Experiment (no name)" : name; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java index d91d306b..7133bd17 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java @@ -9,6 +9,7 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.browser.nodes.AbstractNode; import org.simantics.sysdyn.ui.browser.nodes.ExperimentNode; @@ -18,7 +19,8 @@ public class SimulationResult extends ViewpointContributor { public Collection getContribution(ReadGraph graph, ExperimentNode experiment) throws DatabaseException { ArrayList result = new ArrayList(); Builtins b = graph.getBuiltins(); - for(Resource r : graph.syncRequest(new ObjectsWithType(experiment.resource, b.ConsistsOf, b.Run))) { + SysdynResource sr = SysdynResource.getInstance(graph); + for(Resource r : graph.syncRequest(new ObjectsWithType(experiment.resource, sr.HasResult, sr.Result))) { try { result.add(graph.adapt(r, AbstractNode.class)); } catch(DatabaseException e) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java new file mode 100644 index 00000000..78333c56 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java @@ -0,0 +1,26 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import org.simantics.browsing.ui.content.LabelDecorator; +import org.simantics.browsing.ui.graph.contributor.labeler.LabelDecoratorContributor; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.SimulationResultNode; + +public class SimulationResultDecorator extends LabelDecoratorContributor{ + + @Override + public LabelDecorator getDecorator(ReadGraph graph, SimulationResultNode result) throws DatabaseException { + Builtins b = graph.getBuiltins(); + if (graph.hasStatement(result.resource, b.IsActive)) { + return new LabelDecorator.Stub() { + @Override + public String decorateLabel(String label, String column, int itemIndex) { + return label + " [ACTIVE]"; + } + }; + } + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultLabeler.java index cf5b808c..c74bb404 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultLabeler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultLabeler.java @@ -9,7 +9,7 @@ public class SimulationResultLabeler extends LabelerContributor ress = ISelectionUtils.filterSetSelection(selection, Resource.class); + Set ress = ISelectionUtils.filterSetSelection(selection, Resource.class); // Object[] els = ((IStructuredSelection) selection).toArray(); // Resource[] resources = new Resource[els.length]; // for(int i=0;i datasets = new ArrayList(); + for(Resource resource : resources) { + Collection set = load(g, resource); + if(set == null) return; + datasets.addAll(load(g, resource)); + } + SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - sysdynDataset.setDatasets(sets); + sysdynDatasets.setDatasets(datasets.toArray(new SysdynDataSet[datasets.size()])); } - + }); - + } }); } - + Runnable updater = new Runnable() { @Override public void run() { updateView(); } }; - + HashMap models = new HashMap(); protected SysdynModel getModel(ReadGraph g, Resource configuration) { synchronized(models) { @@ -215,28 +217,32 @@ public class TrendView extends ViewPart { return model; } } - - protected DataSet load(ReadGraph g, Resource resource) throws DatabaseException { + + protected Collection load(ReadGraph g, Resource resource) throws DatabaseException { Builtins b = g.getBuiltins(); SysdynResource sr = SysdynResource.getInstance(g); - + ModelingResources mr = ModelingResources.getInstance(g); Resource map = g.getPossibleObject(resource, mr.ElementToComponent); if(map != null) resource = map; - + if(g.isInstanceOf(resource, sr.Variable)) { Resource configuration = g.getPossibleObject(resource, b.PartOf); if(g.isInstanceOf(configuration, sr.Configuration)) { SysdynModel model = getModel(g, configuration); if(model == null) return null; + IElement element = model.getElement(resource); if(element instanceof Variable) { - Variable var = (Variable)element; - SimulationResult result = model.getResult(); - if(result == null) - return null; - return result.getDataSet(var.getName()); + Variable var = (Variable)element; + + Collection activeResults = model.getActiveResults(g); + ArrayList datasets = new ArrayList(); + for(SysdynResult r : activeResults) { + datasets.add(r.getDataSet(var.getName())); + } + return datasets; } } diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index 47301654..801b2fe2 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -13,7 +13,8 @@ Require-Bundle: org.simantics.objmap;bundle-version="0.1.0", org.eclipse.ui.console;bundle-version="3.4.0", org.eclipse.core.runtime;bundle-version="3.5.0", org.eclipse.jface;bundle-version="3.5.2", - org.simantics.project;bundle-version="1.0.0" + org.simantics.project;bundle-version="1.0.0", + org.simantics.layer0.utils;bundle-version="0.8.0" Export-Package: org.simantics.sysdyn, org.simantics.sysdyn.expressionParser, org.simantics.sysdyn.manager, diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index 668279e1..3be2eda9 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -44,6 +44,9 @@ public class SysdynResource { public final Resource HasMaxY; public final Resource HasMinX; public final Resource HasMinY; + public final Resource HasParameterFile; + public final Resource HasResult; + public final Resource HasResultFile; public final Resource HasStartTime; public final Resource HasStopTime; public final Resource HasTail; @@ -61,6 +64,7 @@ public class SysdynResource { public final Resource ParameterExpression; public final Resource PhantomTerminal; public final Resource RefersTo; + public final Resource Result; public final Resource SimulateOnChangeExperiment; public final Resource Stock; public final Resource StockExpression; @@ -101,6 +105,9 @@ public class SysdynResource { public static final String HasMaxY = "http://www.simantics.org/Sysdyn-1.0/HasMaxY"; public static final String HasMinX = "http://www.simantics.org/Sysdyn-1.0/HasMinX"; public static final String HasMinY = "http://www.simantics.org/Sysdyn-1.0/HasMinY"; + public static final String HasParameterFile = "http://www.simantics.org/Sysdyn-1.0/HasParameterFile"; + public static final String HasResult = "http://www.simantics.org/Sysdyn-1.0/HasResult"; + public static final String HasResultFile = "http://www.simantics.org/Sysdyn-1.0/HasResultFile"; public static final String HasStartTime = "http://www.simantics.org/Sysdyn-1.0/HasStartTime"; public static final String HasStopTime = "http://www.simantics.org/Sysdyn-1.0/HasStopTime"; public static final String HasTail = "http://www.simantics.org/Sysdyn-1.0/HasTail"; @@ -118,6 +125,7 @@ public class SysdynResource { public static final String ParameterExpression = "http://www.simantics.org/Sysdyn-1.0/ParameterExpression"; public static final String PhantomTerminal = "http://www.simantics.org/Sysdyn-1.0/PhantomTerminal"; public static final String RefersTo = "http://www.simantics.org/Sysdyn-1.0/RefersTo"; + public static final String Result = "http://www.simantics.org/Sysdyn-1.0/Result"; public static final String SimulateOnChangeExperiment = "http://www.simantics.org/Sysdyn-1.0/SimulateOnChangeExperiment"; public static final String Stock = "http://www.simantics.org/Sysdyn-1.0/Stock"; public static final String StockExpression = "http://www.simantics.org/Sysdyn-1.0/StockExpression"; @@ -168,6 +176,9 @@ public class SysdynResource { HasMaxY = getResourceOrNull(graph, URIs.HasMaxY); HasMinX = getResourceOrNull(graph, URIs.HasMinX); HasMinY = getResourceOrNull(graph, URIs.HasMinY); + HasParameterFile = getResourceOrNull(graph, URIs.HasParameterFile); + HasResult = getResourceOrNull(graph, URIs.HasResult); + HasResultFile = getResourceOrNull(graph, URIs.HasResultFile); HasStartTime = getResourceOrNull(graph, URIs.HasStartTime); HasStopTime = getResourceOrNull(graph, URIs.HasStopTime); HasTail = getResourceOrNull(graph, URIs.HasTail); @@ -185,6 +196,7 @@ public class SysdynResource { ParameterExpression = getResourceOrNull(graph, URIs.ParameterExpression); PhantomTerminal = getResourceOrNull(graph, URIs.PhantomTerminal); RefersTo = getResourceOrNull(graph, URIs.RefersTo); + Result = getResourceOrNull(graph, URIs.Result); SimulateOnChangeExperiment = getResourceOrNull(graph, URIs.SimulateOnChangeExperiment); Stock = getResourceOrNull(graph, URIs.Stock); StockExpression = getResourceOrNull(graph, URIs.StockExpression); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynDataSet.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynDataSet.java new file mode 100644 index 00000000..88ca787a --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynDataSet.java @@ -0,0 +1,14 @@ +package org.simantics.sysdyn.manager; + +import org.simantics.modelica.data.DataSet; + +public class SysdynDataSet extends DataSet { + + public String result; + + public SysdynDataSet(String name, String result, double[] times, double[] values) { + super(name, times, values); + this.result = result; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java index 1297dba9..c89d0a63 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java @@ -1,24 +1,39 @@ package org.simantics.sysdyn.manager; +import java.io.File; +import java.util.ArrayList; +import java.util.TreeMap; +import java.util.UUID; + +import org.eclipse.core.runtime.Platform; +import org.simantics.databoard.annotations.Optional; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; +import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.request.Read; +import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.simulation.experiment.Experiment; import org.simantics.simulation.experiment.ExperimentState; import org.simantics.simulation.experiment.IDynamicExperiment; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.simulation.SimulationScheduler; public class SysdynExperiment extends Experiment implements IDynamicExperiment { Session session; Runnable modificationListener; + Resource experiment; + SysdynModel sysdynModel; + SysdynResult result; - public SysdynExperiment(Resource model) { + public SysdynExperiment(Resource experiment, Resource model) { super(model); - System.out.println(this.getIdentifier()); + this.experiment = experiment; // TODO Auto-generated constructor stub } @@ -32,11 +47,33 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { public void saveState() { // TODO Auto-generated method stub System.out.println("saveState"); + + try { + session.syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + File root = new File(Platform.getLocation().toOSString(), "www.simantics.org"); + if(!root.isDirectory()) + root.mkdir(); + File file = new File( root, UUID.randomUUID().toString() + ".dbb"); + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + Resource res = GraphUtils.create2(graph, sr.Result, + b.HasLabel, "Result x", + b.HasName, "Result x", + sr.HasResultFile, file.getAbsolutePath()); + graph.claim(experiment, sr.HasResult, res); + result.saveToFile(file); + + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } } - /** - * enabled: is simulate on change enabled - */ + @Override public void simulate(boolean enabled) { System.out.println("simulate"); @@ -47,31 +84,25 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { } - private SysdynModel startSimulation() { + private void startSimulation() { try { - return session.syncRequest(new Read() { + session.syncRequest(new ReadRequest() { @Override - public SysdynModel perform(ReadGraph graph) throws DatabaseException { - final Resource configuration = graph.getPossibleObject(model, graph.getBuiltins().HasConfiguration); - SysdynModel sysdynmodel = - SysdynModelManager.getInstance(session).getModel(graph, configuration); - SimulationScheduler.start(sysdynmodel); - return sysdynmodel; + public void run(ReadGraph graph) throws DatabaseException { + SimulationScheduler.start(sysdynModel); } }); } catch (DatabaseException e) { e.printStackTrace(); } - return null; } public void toggleSimulation(boolean enabled) { if(enabled) { changeState(ExperimentState.RUNNING); - if(modificationListener == null) { - SysdynModel sysdynModel = startSimulation(); - + if(modificationListener == null) { + modificationListener = new Runnable() { @Override @@ -81,10 +112,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { @Override public void run(ReadGraph graph) throws DatabaseException { if(getState() == ExperimentState.RUNNING) { - Resource configuration = graph.getPossibleObject(model, graph.getBuiltins().HasConfiguration); - SysdynModel sysdynmodel = - SysdynModelManager.getInstance(session).getModel(graph, configuration); - SimulationScheduler.start(sysdynmodel); + SimulationScheduler.start(sysdynModel); } } @@ -117,6 +145,25 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { System.out.println("init"); changeState(ExperimentState.STOPPED); this.session = g.getSession(); + result = new SysdynResult(); + + session.asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + final Resource configuration = graph.getPossibleObject(model, graph.getBuiltins().HasConfiguration); + sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration); + sysdynModel.addResultListener(new Runnable() { + + @Override + public void run() { + result.setResult(sysdynModel.getSimulationResult()); + } + }); + } + }); + + } @Override @@ -128,10 +175,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { @Override public void run(ReadGraph graph) throws DatabaseException { - final Resource configuration = graph.getPossibleObject(model, graph.getBuiltins().HasConfiguration); - SysdynModel sysdynmodel = - SysdynModelManager.getInstance(session).getModel(graph, configuration); - sysdynmodel.removeModificationListener(modificationListener); + sysdynModel.removeModificationListener(modificationListener); modificationListener = null; } @@ -139,6 +183,54 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { break; } } + + public static class Sample { + public double value; + + @Override + public String toString() { + return ""+value; + } + } + + public static class Segment { + public int startTime, endTime; + public ArrayList samples; + + @Override + public String toString() { + return "[startTime="+startTime+", endTime="+endTime+", samples="+samples+"]"; + } + + } + + public static class Recording { + public String id; + public TreeMap labels; + public ArrayList segments; + + @Override + public String toString() { + return "id="+id+", labels="+labels+", segments="+segments; + } + + } + + public static class RecordingSession { + public @Optional Variant datasource; + public Variant timeVarialbeId; + public TreeMap recordings; + + @Override + public String toString() { + return recordings.toString(); + } + } + + public static class NodeId { + public String nodeId; + } } + 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 65d31d32..0d888a64 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -12,10 +12,13 @@ package org.simantics.sysdyn.manager; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; +import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; @@ -30,6 +33,7 @@ import org.simantics.objmap.Mappings; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.model.IModel; import org.simantics.simulation.project.IExperimentActivationListener; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.modelica.ModelicaWriter; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; @@ -72,8 +76,8 @@ public class SysdynModel implements IMappingListener, IModel { e.printStackTrace(); } } - - + + /* * some dummy(?) stuff for experiments */ @@ -116,7 +120,7 @@ public class SysdynModel implements IMappingListener, IModel { return false; } - public SimulationResult getResult() { + public SimulationResult getSimulationResult() { return result; } @@ -182,7 +186,7 @@ public class SysdynModel implements IMappingListener, IModel { @Override public IExperiment loadExperiment(ReadGraph g, Resource experiment, IExperimentActivationListener listener) { - SysdynExperiment exp = new SysdynExperiment(configurationResource); + SysdynExperiment exp = new SysdynExperiment(experiment, configurationResource); try { exp.init(g); listener.onExperimentActivated(exp); @@ -192,4 +196,36 @@ public class SysdynModel implements IMappingListener, IModel { return null; } } + + public Collection getActiveResults(ReadGraph graph) { + ArrayList results = new ArrayList(); + + try { + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + Resource model = graph.getSingleObject(configurationResource, b.IsConfigurationOf); + Collection experiments = graph.getObjects(model, b.ConsistsOf); + for(Resource experiment : experiments) { + Collection experimentResults = graph.getObjects(experiment, sr.HasResult); + for(Resource result : experimentResults) { + if(graph.hasStatement(result, b.IsActive)) { + SysdynResult sysdynResult = new SysdynResult( + (String) graph.getPossibleRelatedValue(result, b.HasLabel), + (String) graph.getPossibleRelatedValue(result, sr.HasResultFile)); + results.add(sysdynResult); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + if(getSimulationResult() != null) + results.add(0, new SysdynResult(getSimulationResult())); + + + + return results; + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java new file mode 100644 index 00000000..e0783bb7 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java @@ -0,0 +1,233 @@ +package org.simantics.sysdyn.manager; + + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.TreeMap; + +import org.simantics.databoard.Accessors; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.MapAccessor; +import org.simantics.databoard.accessor.RecordAccessor; +import org.simantics.databoard.accessor.VariantAccessor; +import org.simantics.databoard.accessor.binary.BinaryRecord; +import org.simantics.databoard.accessor.binary.BinaryVariant; +import org.simantics.databoard.accessor.error.AccessorConstructionException; +import org.simantics.databoard.accessor.error.AccessorException; +import org.simantics.databoard.accessor.java.JavaRecord; +import org.simantics.databoard.annotations.Optional; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingConstructionException; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.java.StringJavaBinding; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.binding.mutable.VariantContainerBinding; +import org.simantics.databoard.serialization.SerializationException; +import org.simantics.modelica.data.DataSet; +import org.simantics.modelica.data.SimulationResult; + +public class SysdynResult { + + RecordingSession recordingSession; + RecordAccessor accessor; + String resultName; + + public SysdynResult() { + + } + + public SysdynResult(SimulationResult result) { + if(result != null) + setResult(result); + } + + public SysdynResult(String result, String path) { + this.resultName = result; + File file = new File(path); + try { + BinaryVariant bv = (BinaryVariant)Accessors.openFile(file); + BinaryRecord br = bv.getValueAccessor(); + accessor = br; + } catch (AccessorConstructionException e) { + e.printStackTrace(); + } + } + + public Accessor getAccessor() { + try { + return Accessors.getAccessor(recordingSession); + } catch (AccessorConstructionException e) { + e.printStackTrace(); + } + return null; + } + + public void setResult(SimulationResult result) { + try { + Binding stringBinding = StringJavaBinding.INSTANCE; + Binding recordBinding = Bindings.getBinding( Recording.class ); + + recordingSession = new RecordingSession(); + recordingSession.recordings = new TreeMap(); + Variant timeVariableId = new Variant(); + timeVariableId.setValue(stringBinding, "timeVariableId"); + recordingSession.timeVariableId = timeVariableId; + + + + for(DataSet ds : result.getVariableDataSets()){ + Recording rec = new Recording(); + rec.labels = new TreeMap(); + String name = ds.name; + rec.id = name; + rec.labels.put("en", name); + rec.segments = new ArrayList(); + + Segment seg = new Segment(); + seg.startTime = 0; + seg.endTime = ds.values.length; + seg.samples = new ArrayList(); + for(double value : ds.values) { + Sample samp = new Sample(); + samp.value = value; + seg.samples.add(samp); + } + + rec.segments.add(seg); + + Variant tmpVariant = new Variant(); + Variant tmpVariant2 = new Variant(); + tmpVariant.setValue(recordBinding, rec); + tmpVariant2.setValue(stringBinding, name); + recordingSession.recordings.put(tmpVariant2, tmpVariant); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void saveToFile(File file) { + try { + Binding binding = Bindings.getBinding( recordingSession.getClass() ); + Accessors.createFile(file, binding, recordingSession); + } catch (IOException e) { + e.printStackTrace(); + } catch (BindingConstructionException e) { + e.printStackTrace(); + } catch (SerializationException e) { + e.printStackTrace(); + } catch (BindingException e) { + e.printStackTrace(); + } + + } + + + public static class Sample { + public double value; + + @Override + public String toString() { + return ""+value; + } + } + + public static class Segment { + public int startTime, endTime; + public ArrayList samples; + + @Override + public String toString() { + return "[startTime="+startTime+", endTime="+endTime+", samples="+samples+"]"; + } + + } + + public static class Recording { + public String id; + public TreeMap labels; + public ArrayList segments; + + @Override + public String toString() { + return "id="+id+", labels="+labels+", segments="+segments; + } + + } + + public static class RecordingSession { + public @Optional Variant datasource; + public Variant timeVariableId; + public TreeMap recordings; + + @Override + public String toString() { + return recordings.toString(); + } + } + + public static class NodeId { + public String nodeId; + } + + public SysdynDataSet getDataSet(String variable) { + if(accessor == null && recordingSession != null) { + try { + Binding recordBinding = Bindings.getBinding( RecordingSession.class ); + accessor = (JavaRecord)Accessors.getAccessor(recordBinding, recordingSession); + } catch (AccessorConstructionException e) { + e.printStackTrace(); + } catch (BindingConstructionException e) { + e.printStackTrace(); + } + } + + if(accessor != null) { + MapAccessor ma; + try { + ma = accessor.getFieldAccessor("recordings"); + } catch (AccessorConstructionException e1) { + e1.printStackTrace(); + return null; + } + Binding stringBinding = StringJavaBinding.INSTANCE; + Variant tmpVariant = new Variant(); + tmpVariant.setValue(stringBinding, variable); + Variant tmpVariant2 = new Variant(); + tmpVariant2.setValue(stringBinding, "time"); + Recording rec; + Recording time; + try { + VariantAccessor va = ma.getValueAccessor( VariantContainerBinding.INSTANCE, tmpVariant); + Accessor valueAccessor = va.getValueAccessor(); + rec = (Recording)valueAccessor.getValue( Bindings.getBinding(Recording.class)); + + va = ma.getValueAccessor( VariantContainerBinding.INSTANCE, tmpVariant2); + valueAccessor = va.getValueAccessor(); + time = (Recording)valueAccessor.getValue( Bindings.getBinding(Recording.class)); + if( rec == null || time == null) return null; + } catch (AccessorException e) { + e.printStackTrace(); + return null; + } catch (BindingConstructionException e) { + e.printStackTrace(); + return null; + } catch (AccessorConstructionException e) { + e.printStackTrace(); + return null; + } + ArrayList samples = rec.segments.get(0).samples; + double[] values = new double[samples.size()]; + ArrayList timeSamples = time.segments.get(0).samples; + double[] times = new double[samples.size()]; + for(int i = 0; i < samples.size(); i++) { + values[i] = samples.get(i).value; + times[i] = timeSamples.get(i).value; + } + return new SysdynDataSet(variable, resultName, times, values); + } + return null; + } +} diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index 69216fb4..326a0f44 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -18,6 +18,10 @@ L0.Asserts _ : L0.Assertion L0.HasPredicate $pred L0.HasObject $obj + +%define tag($pred) + $subject + $pred $subject ###################################################################### # Defines ontology and attaches it to SimanticsDomain @@ -32,8 +36,7 @@ Sysdyn @ "Sysdyn-1.0" : L0.Ontology Symbols : L0.Library SysdynProject : PROJ.Feature - PROJ.LifecycleFeature - SysdynProject + tag(PROJ.LifecycleFeature) L0.HasLabel "System Dynamics Project" : L0.String L0.HasDescription """System dynamics modelling project. Create system dynamics models and simulate them with OpenModelica.""" : L0.String L0.PartOf Sysdyn @@ -126,7 +129,7 @@ HasUnit