* \r
* @param file result file\r
* @param interval the interval of results to be read. 1 reads all time steps, 2 reads every other time step, etc...\r
+ * @return The added dataset\r
* @throws FileNotFoundException\r
* @throws IOException\r
*/\r
- public void readVariable(String variable, File file) throws FileNotFoundException, IOException {\r
+ public DataSet readVariable(String variable, File file) throws FileNotFoundException, IOException {\r
if(resultFileReader == null)\r
- return;\r
+ return null;\r
\r
DataSet ds;\r
List<String> names = resultFileReader.getNames();\r
// We should never need to go there unless some change in\r
// the logic of reading variables are made.\r
// Also helps in seeking memory leaks.\r
- return;\r
+ return null;\r
}\r
variables.add(ds);\r
+ return ds;\r
}\r
+ return null;\r
}\r
\r
/**\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.manager;\r
+\r
+import java.io.File;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.simantics.databoard.Accessors;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.Accessor;\r
+import org.simantics.databoard.accessor.MapAccessor;\r
+import org.simantics.databoard.accessor.RecordAccessor;\r
+import org.simantics.databoard.accessor.binary.BinaryRecord;\r
+import org.simantics.databoard.accessor.binary.BinaryVariant;\r
+import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.accessor.reference.ComponentReference;\r
+import org.simantics.databoard.accessor.reference.IndexReference;\r
+import org.simantics.databoard.accessor.reference.KeyReference;\r
+import org.simantics.databoard.accessor.reference.NameReference;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+\r
+public class FileResult extends SysdynResult {\r
+ \r
+ private RecordAccessor accessor;\r
+\r
+ /**\r
+ * Open result from a file\r
+ * \r
+ * @param result Name of the result (seen in visualization)\r
+ * @param path file path\r
+ */\r
+ public FileResult(String resultName, String path) {\r
+ super(resultName);\r
+ File file = new File(path);\r
+ try {\r
+ BinaryVariant bv = (BinaryVariant)Accessors.openAccessor(file);\r
+ BinaryRecord br = bv.getContentAccessor();\r
+ accessor = br;\r
+ } catch (AccessorConstructionException e) {\r
+ e.printStackTrace();\r
+ } \r
+ }\r
+ \r
+ /**\r
+ * Get the {@link RecordAccessor} of this result\r
+ * @return {@link RecordAccessor} for this result\r
+ */\r
+ public Accessor getAccessor() {\r
+ return accessor;\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Get dataset for a named variable from accessor\r
+ * @param variable The name of the variable\r
+ * @return {@link SysdynDataSet}\r
+ */\r
+ @Override\r
+ public SysdynDataSet getDataSet(String variable) {\r
+ if(accessor != null) {\r
+ synchronized(accessor) {\r
+ ChildReference ref = ChildReference.compile(\r
+ new NameReference("recordings"),\r
+ new KeyReference( Bindings.VARIANT, Variant.ofInstance(variable) ),\r
+ new ComponentReference(),\r
+ new NameReference("segments"),\r
+ new IndexReference(0)\r
+ );\r
+ MapAccessor ma;\r
+ try {\r
+ ma = accessor.getComponent( ref );\r
+ } catch (AccessorConstructionException e) {\r
+ return null;\r
+ } \r
+ \r
+ try {\r
+ int size = ma.size();\r
+ Double[] times = new Double[size];\r
+ Double[] values = new Double[size];\r
+ \r
+ ma.getAll(Bindings.DOUBLE, Bindings.DOUBLE, times, values);\r
+ \r
+ double[] times_ = new double[size];\r
+ double[] values_ = new double[size];\r
+ for (int i=0; i<size; i++) {\r
+ times_[i] = (times[i]!=null ? times[i] : 0);\r
+ values_[i] = (values[i]!=null ? values[i] : 0);\r
+ }\r
+ \r
+ return new SysdynDataSet(variable, getResultName(), times_, values_);\r
+ } catch (AccessorException e) {\r
+ // Unable to read data from file\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void saveToFile(File file, IProgressMonitor progressMonitor) {\r
+ // Already a saved result. No need for action.\r
+ }\r
+\r
+}\r
/*******************************************************************************\r
- * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
* Industry THTH ry.\r
* All rights reserved. This program and the accompanying materials\r
* are made available under the terms of the Eclipse Public License v1.0\r
*\r
* Contributors:\r
* VTT Technical Research Centre of Finland - initial API and implementation\r
+ * Semantum Oy\r
*******************************************************************************/\r
package org.simantics.sysdyn.manager;\r
\r
/**\r
* Reads history data to this result. Location of the data is described in historyData resource.\r
* \r
- * @param sysdynResult SysdynResult to which this simulation result is read\r
+ * @param sysdynResult MemoryResult to which this simulation result is read\r
* @param historyData Resource describing the history data to be read from a spreadsheet\r
*/\r
- public void read(final SysdynResult sysdynResult, final Resource historyData) {\r
+ public void read(final MemoryResult sysdynResult, final Resource historyData) {\r
\r
Simantics.getSession().asyncRequest(new Read<Pair<SysdynModel, List<DataSet>>>() {\r
\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.manager;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.util.List;\r
+import java.util.TreeMap;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.simantics.databoard.Accessors;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.accessor.MapAccessor;\r
+import org.simantics.databoard.accessor.RecordAccessor;\r
+import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
+import org.simantics.databoard.accessor.java.JavaRecord;\r
+import org.simantics.databoard.annotations.Arguments;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.RecordBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.modelica.data.DataSet;\r
+import org.simantics.modelica.data.SimulationResult;\r
+\r
+public class MemoryResult extends SysdynResult {\r
+ \r
+ private THashMap<String, SysdynDataSet> results;\r
+ private File resultFile;\r
+ private SimulationResult simulationResult;\r
+\r
+\r
+ /**\r
+ * Create a sysdynresult accessor using a {@link SimulationResult}\r
+ * @param result\r
+ */\r
+ public MemoryResult(SimulationResult result, String resultName) {\r
+ super(resultName);\r
+ \r
+ if(result != null)\r
+ setResult(result);\r
+ \r
+ results = new THashMap<String, SysdynDataSet>();\r
+ }\r
+ \r
+ public SysdynDataSet getDataSet(String variable) {\r
+ // Try to get cached result\r
+ SysdynDataSet result = results.get(variable);\r
+ if(result != null)\r
+ return result;\r
+ \r
+ // if not found, try to read it\r
+ SimulationResult simulationResult = getSimulationResult();\r
+ try {\r
+ if (simulationResult != null) {\r
+ DataSet ds = simulationResult.getDataSet(variable);\r
+ if(ds == null)\r
+ ds = simulationResult.readVariable(variable, resultFile);\r
+\r
+ if(ds != null) {\r
+ SysdynDataSet sds = new SysdynDataSet(variable, getResultName(), ds.times, ds.values);\r
+ results.put(variable, sds);\r
+ return sds;\r
+ }\r
+ }\r
+ } catch (FileNotFoundException e1) {\r
+ // Unable to read data from OpenModelica result file\r
+ System.err.println("Variable " + variable + " not found in OpenModelica result file");\r
+ return null;\r
+ } catch (IOException e1) {\r
+ // IoFail\r
+ e1.printStackTrace();\r
+ }\r
+ \r
+ return null;\r
+ }\r
+\r
+ public void setResultFile(File resFile) {\r
+ this.resultFile = resFile;\r
+ }\r
+ \r
+ public int numberOfVariables() {\r
+ return simulationResult.getVariableDataSets().size();\r
+ }\r
+ \r
+ public void setResult(SimulationResult result) {\r
+ this.simulationResult = result;\r
+ this.results.clear();\r
+ if(result != null)\r
+ addAllInitialValues(result); // For legacy reasons\r
+ }\r
+ \r
+ public SimulationResult getSimulationResult() {\r
+ return this.simulationResult;\r
+ }\r
+\r
+ \r
+ \r
+ /**\r
+ * Read all results from their DataSets. --- Legacy init file support. Not needed with mat files.\r
+ */\r
+ private void addAllInitialValues(SimulationResult result) {\r
+ // Add initial values\r
+ for(DataSet ds : result.getInitialValueDataSets()){\r
+ SysdynDataSet sds = new SysdynDataSet(ds.name, getResultName(), ds.times, ds.values);\r
+ results.put(ds.name, sds);\r
+ }\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Save current {@link RecordAccessor} to a {@link File}\r
+ * \r
+ * @param file {@link File} where the {@link RecordAccessor} is saved\r
+ */\r
+ public void saveToFile(File file, IProgressMonitor progressMonitor) {\r
+ try {\r
+ // Create Memory Historian\r
+ Datatype recordingSessionType = Datatypes.getDatatype("RecordingSession");\r
+ RecordBinding sessionBinding = (RecordBinding) Bindings.getBinding( recordingSessionType );\r
+ Object session = sessionBinding.createDefault(); \r
+ JavaRecord accessor = (JavaRecord) Accessors.getAccessor( sessionBinding, session );\r
+\r
+ // Get binding and value from memory\r
+ JavaRecord jr = (JavaRecord) accessor;\r
+ Binding binding = jr.getBinding();\r
+ Object value = jr.getObject();\r
+ \r
+ // Read the missing variables one by one.\r
+ getSimulationResult().readMissingVariables(this.resultFile);\r
+\r
+ addAllDataSetsToAccessor(accessor, progressMonitor);\r
+ // Write to file\r
+ if (progressMonitor != null) {\r
+ progressMonitor.subTask("Save results");\r
+ }\r
+ Files.createFile(file, binding, value);\r
+ \r
+ // The division is made so that 1 unit per variable and their sum for saving them.\r
+ progressMonitor.worked(numberOfVariables());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ } catch (BindingException e) {\r
+ e.printStackTrace();\r
+ } catch (AccessorConstructionException e) {\r
+ e.printStackTrace();\r
+ }\r
+\r
+ }\r
+ \r
+ \r
+ /**\r
+ * Read all results from their DataSets.\r
+ * @param progressMonitor \r
+ */\r
+ private void addAllDataSetsToAccessor(JavaRecord accessor, IProgressMonitor progressMonitor) {\r
+ try {\r
+ MapAccessor recordings = accessor.getFieldAccessor("recordings");\r
+ \r
+ // There is a recording for each variable\r
+ List<DataSet> datasets = getSimulationResult().getVariableDataSets();\r
+ RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class );\r
+ if (progressMonitor != null) {\r
+ progressMonitor.subTask("Prepare saving");\r
+ }\r
+ // Add variables\r
+ for(DataSet ds : datasets){\r
+ if (progressMonitor != null) {\r
+ progressMonitor.worked(1);\r
+ }\r
+ // Add recording to session, if not already added\r
+ Variant dsNodeId = Variant.ofInstance( ds.name );\r
+ if (recordings.containsKey(Bindings.VARIANT, dsNodeId)) \r
+ continue;\r
+ \r
+ Recording recording = createRecording(ds);\r
+ recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording));\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ } \r
+ }\r
+ \r
+ /**\r
+ * Create a recording for a variable to this result's {@link RecordAccessor} \r
+ * @param ds {@link DataSet} for the variable\r
+ * @return {@link Recording}\r
+ * @throws BindingException\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ Recording createRecording(DataSet ds) throws BindingException {\r
+ RecordBinding recordingBinding = (RecordBinding) Bindings.getBindingUnchecked( Recording.class );\r
+ Recording recording = (Recording) recordingBinding.createDefault(); \r
+ recording.nodeId = Variant.ofInstance( ds.name );\r
+ recording.labels.put("en", ds.name);\r
+\r
+ // Create one segment\r
+ TreeMap<Double, Double> segment = new TreeMap<Double, Double>();\r
+ recording.segments = new TreeMap[] { segment };\r
+\r
+ int length = ds.values.length;\r
+ for (int i=0; i<length; i++) {\r
+ double time = ds.times[i];\r
+ double value = ds.values[i];\r
+ segment.put(time, value); \r
+ }\r
+ return recording;\r
+ }\r
+ \r
+ /**\r
+ * Class representing a recording for a variable\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+ public static class Recording {\r
+ public Variant nodeId;\r
+ public @Arguments({String.class, String.class}) TreeMap<String, String> labels;\r
+ public @Arguments({TreeMap.class, Double.class, Double.class}) TreeMap<Double, Double>[] segments;\r
+ }\r
+}\r
/*******************************************************************************\r
- * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2010, 2013 Association for Decentralized Information Management in\r
* Industry THTH ry.\r
* All rights reserved. This program and the accompanying materials\r
* are made available under the terms of the Eclipse Public License v1.0\r
*\r
* Contributors:\r
* VTT Technical Research Centre of Finland - initial API and implementation\r
+ * Semantum Oy - initial API and implementation\r
*******************************************************************************/\r
package org.simantics.sysdyn.manager;\r
\r
*/\r
public class SaveResultJob extends Job {\r
\r
- private final SysdynResult sysdynResult;\r
+ private final MemoryResult sysdynResult;\r
private final SysdynExperiment experiment;\r
private final Session session;\r
private IProgressMonitor monitor;\r
private File file;\r
\r
- public SaveResultJob(final SysdynExperiment experiment, Session session, final SysdynResult result) {\r
+ public SaveResultJob(final SysdynExperiment experiment, Session session, final MemoryResult result) {\r
super("Save Result");\r
this.experiment = experiment;\r
this.sysdynResult = result;\r
/*******************************************************************************\r
- * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2010, 2013 Association for Decentralized Information Management in\r
* Industry THTH ry.\r
* All rights reserved. This program and the accompanying materials\r
* are made available under the terms of the Eclipse Public License v1.0\r
*\r
* Contributors:\r
* VTT Technical Research Centre of Finland - initial API and implementation\r
+ * Semantum Oy\r
*******************************************************************************/\r
package org.simantics.sysdyn.manager;\r
\r
protected boolean canceled = false;\r
protected ExperimentState sysdynExperimentState;\r
\r
- private SysdynResult result;\r
+ private MemoryResult result;\r
private File simulationDir;\r
\r
protected String experimentName;\r
return INSTANCE;\r
}\r
\r
- public SysdynResult getCurrentResult() {\r
+ public MemoryResult getCurrentResult() {\r
if(this.result == null)\r
- this.result = new SysdynResult();\r
+ this.result = new MemoryResult(null, null);\r
return this.result;\r
}\r
\r
\r
@Override\r
public void saveState() {\r
- if(result == null) \r
+ if(result == null || !(result instanceof MemoryResult)) \r
return;\r
SaveResultJob saveResultJob = new SaveResultJob(SysdynExperiment.this, session, result);\r
saveResultJob.schedule();\r
/*******************************************************************************\r
- * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2010, 2013 Association for Decentralized Information Management in\r
* Industry THTH ry.\r
* All rights reserved. This program and the accompanying materials\r
* are made available under the terms of the Eclipse Public License v1.0\r
*\r
* Contributors:\r
* VTT Technical Research Centre of Finland - initial API and implementation\r
+ * Semantum Oy\r
*******************************************************************************/\r
package org.simantics.sysdyn.manager;\r
\r
if(graph.isInstanceOf(result, sr.HistoryDataset)) {\r
HistoryDatasetResult r = new HistoryDatasetResult();\r
listeningHistories.add(r);\r
- sysdynResult = new SysdynResult(r, NameUtils.getSafeLabel(graph, result));\r
- r.read(sysdynResult, result);\r
+ sysdynResult = new MemoryResult(r, NameUtils.getSafeLabel(graph, result));\r
+ r.read((MemoryResult)sysdynResult, result);\r
} else {\r
- sysdynResult = new SysdynResult(\r
+ sysdynResult = new FileResult(\r
(String) graph.getPossibleRelatedValue(result, l0.HasLabel),\r
(String) graph.getPossibleRelatedValue(result, sr.Result_resultFile));\r
}\r
/*******************************************************************************\r
- * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2010, 2013 Association for Decentralized Information Management in\r
* Industry THTH ry.\r
* All rights reserved. This program and the accompanying materials\r
* are made available under the terms of the Eclipse Public License v1.0\r
*\r
* Contributors:\r
* VTT Technical Research Centre of Finland - initial API and implementation\r
+ * Semantum Oy\r
*******************************************************************************/\r
package org.simantics.sysdyn.manager;\r
\r
\r
import java.io.File;\r
-import java.io.FileNotFoundException;\r
-import java.io.IOException;\r
-import java.util.List;\r
-import java.util.TreeMap;\r
\r
import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.simantics.databoard.Accessors;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Datatypes;\r
-import org.simantics.databoard.Files;\r
-import org.simantics.databoard.accessor.Accessor;\r
-import org.simantics.databoard.accessor.MapAccessor;\r
-import org.simantics.databoard.accessor.RecordAccessor;\r
-import org.simantics.databoard.accessor.binary.BinaryRecord;\r
-import org.simantics.databoard.accessor.binary.BinaryVariant;\r
-import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
-import org.simantics.databoard.accessor.error.AccessorException;\r
-import org.simantics.databoard.accessor.java.JavaRecord;\r
-import org.simantics.databoard.accessor.reference.ChildReference;\r
-import org.simantics.databoard.accessor.reference.ComponentReference;\r
-import org.simantics.databoard.accessor.reference.IndexReference;\r
-import org.simantics.databoard.accessor.reference.KeyReference;\r
-import org.simantics.databoard.accessor.reference.NameReference;\r
-import org.simantics.databoard.annotations.Arguments;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.RecordBinding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.databoard.type.Datatype;\r
-import org.simantics.modelica.data.DataSet;\r
-import org.simantics.modelica.data.SimulationResult;\r
\r
/**\r
- * Class for containing the accessor for a sysdyn simulation result\r
+ * \r
* @author Teemu Lempinen\r
*\r
*/\r
-public class SysdynResult {\r
+public abstract class SysdynResult {\r
\r
- protected RecordAccessor accessor;\r
- protected String resultName;\r
- private SimulationResult simulationResult;\r
- private File resultFile;\r
+ private String resultName;\r
\r
- /**\r
- * Create an empty result\r
- */\r
- public SysdynResult() {\r
-\r
- }\r
-\r
- /**\r
- * Create a sysdynresult accessor using a {@link SimulationResult}\r
- * @param result\r
- */\r
- public SysdynResult(SimulationResult result) {\r
- if(result != null)\r
- setResult(result);\r
- }\r
-\r
- /**\r
- * Create a sysdynresult accessor using a {@link SimulationResult}\r
- * @param result\r
- * @param resultName Name of the result (seen in visualization)\r
- */\r
- public SysdynResult(SimulationResult result, String resultName) {\r
- this.resultName = resultName;\r
- if(result != null)\r
- setResult(result);\r
- }\r
-\r
- /**\r
- * Open result from a file\r
- * \r
- * @param result Name of the result (seen in visualization)\r
- * @param path file path\r
- */\r
- public SysdynResult(String result, String path) {\r
- this.resultName = result;\r
- File file = new File(path);\r
- try {\r
- BinaryVariant bv = (BinaryVariant)Accessors.openAccessor(file);\r
- BinaryRecord br = bv.getContentAccessor();\r
- accessor = br;\r
- } catch (AccessorConstructionException e) {\r
- e.printStackTrace();\r
- } \r
- }\r
-\r
- /**\r
- * Get the {@link RecordAccessor} of this result\r
- * @return {@link RecordAccessor} for this result\r
- */\r
- public Accessor getAccessor() {\r
- return accessor;\r
- }\r
-\r
- /**\r
- * Create a {@link RecordAccessor} for this result based on a {@link SimulationResult}\r
- * @param result {@link SimulationResult}\r
- */\r
- public void setResult(SimulationResult result) {\r
- try {\r
- // Create Memory Historian\r
- this.simulationResult = result;\r
- Datatype recordingSessionType = Datatypes.getDatatype("RecordingSession");\r
- RecordBinding sessionBinding = (RecordBinding) Bindings.getBinding( recordingSessionType );\r
- Object session = sessionBinding.createDefault(); \r
- accessor = (JavaRecord) Accessors.getAccessor( sessionBinding, session );\r
- \r
- addAllInitialValues();\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- } \r
- }\r
-\r
- /**\r
- * Read all results from their DataSets.\r
- * @param progressMonitor \r
- */\r
- private void addAllDataSets(IProgressMonitor progressMonitor) {\r
- try {\r
- MapAccessor recordings = accessor.getFieldAccessor("recordings");\r
- \r
- // There is a recording for each variable\r
- List<DataSet> datasets = simulationResult.getVariableDataSets();\r
- RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class );\r
- if (progressMonitor != null) {\r
- progressMonitor.subTask("Prepare saving");\r
- }\r
- // Add variables\r
- for(DataSet ds : datasets){\r
- if (progressMonitor != null) {\r
- progressMonitor.worked(1);\r
- }\r
- // Add recording to session, if not already added\r
- Variant dsNodeId = Variant.ofInstance( ds.name );\r
- if (recordings.containsKey(Bindings.VARIANT, dsNodeId)) \r
- continue;\r
- \r
- Recording recording = createRecording(ds);\r
- recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording));\r
- }\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- } \r
+ public SysdynResult(String resultName) {\r
+ this.resultName = resultName;\r
}\r
\r
- /**\r
- * Read all results from their DataSets.\r
- */\r
- private void addDataSet(String variable) {\r
- try {\r
- MapAccessor recordings = accessor.getFieldAccessor("recordings");\r
- \r
- // There is a recording for each variable\r
- if (simulationResult == null) return;\r
- List<DataSet> datasets = simulationResult.getVariableDataSets();\r
- RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class );\r
- \r
- // Add variables\r
- for(DataSet ds : datasets){\r
- if (ds.name.equals(variable)) { \r
- // Add recording to session\r
- Recording recording = createRecording(ds);\r
- recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording));\r
- break;\r
- }\r
- continue;\r
- }\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- } \r
- \r
+ public String getResultName() {\r
+ return this.resultName;\r
}\r
- \r
- /**\r
- * Read all results from their DataSets.\r
- */\r
- private void addAllInitialValues() {\r
- try {\r
- MapAccessor recordings = accessor.getFieldAccessor("recordings");\r
- \r
- RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class );\r
- \r
- // Add initial values\r
- for(DataSet ds : simulationResult.getInitialValueDataSets()){\r
- Variant nodeId = Variant.ofInstance( ds.name );\r
- if (recordings.containsKey(Bindings.VARIANT, nodeId)) continue;\r
- // Add recording to session\r
- Recording recording = createRecording(ds);\r
- recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording));\r
- }\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- } \r
- }\r
-\r
- /**\r
- * Create a recording for a variable to this result's {@link RecordAccessor} \r
- * @param ds {@link DataSet} for the variable\r
- * @return {@link Recording}\r
- * @throws BindingException\r
- */\r
- @SuppressWarnings("unchecked")\r
- Recording createRecording(DataSet ds) throws BindingException {\r
- RecordBinding recordingBinding = (RecordBinding) Bindings.getBindingUnchecked( Recording.class );\r
- Recording recording = (Recording) recordingBinding.createDefault(); \r
- recording.nodeId = Variant.ofInstance( ds.name );\r
- recording.labels.put("en", ds.name);\r
\r
- // Create one segment\r
- TreeMap<Double, Double> segment = new TreeMap<Double, Double>();\r
- recording.segments = new TreeMap[] { segment };\r
+ public abstract SysdynDataSet getDataSet(String variable);\r
\r
- int length = ds.values.length;\r
- for (int i=0; i<length; i++) {\r
- double time = ds.times[i];\r
- double value = ds.values[i];\r
- segment.put(time, value); \r
- }\r
- return recording;\r
- }\r
+ public abstract void saveToFile(File file, IProgressMonitor progressMonitor);\r
\r
- /**\r
- * Save current {@link RecordAccessor} to a {@link File}\r
- * \r
- * @param file {@link File} where the {@link RecordAccessor} is saved\r
- */\r
- public void saveToFile(File file, IProgressMonitor progressMonitor) {\r
- if(accessor == null) return;\r
- try {\r
- Binding binding;\r
- Object value;\r
- if (accessor instanceof JavaRecord) {\r
- // Get binding and value from memory\r
- JavaRecord jr = (JavaRecord) accessor;\r
- binding = jr.getBinding();\r
- value = jr.getObject();\r
- } else {\r
- // Read value from another file\r
- binding = Bindings.getBinding( accessor.type() );\r
- value = accessor.getValue(binding);\r
- }\r
- // Read the missing variables one by one.\r
- simulationResult.readMissingVariables(this.resultFile);\r
-\r
- addAllDataSets(progressMonitor);\r
- // Write to file\r
- if (progressMonitor != null) {\r
- progressMonitor.subTask("Save results");\r
- }\r
- Files.createFile(file, binding, value);\r
- \r
- // The division is made so that 1 unit per variable and their sum for saving them.\r
- progressMonitor.worked(numberOfVariables());\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- } catch (AccessorException e) {\r
- e.printStackTrace();\r
- }\r
-\r
- }\r
-\r
- /**\r
- * Class representing a recording for a variable\r
- * @author Teemu Lempinen\r
- *\r
- */\r
- public static class Recording {\r
- public Variant nodeId;\r
- public @Arguments({String.class, String.class}) TreeMap<String, String> labels;\r
- public @Arguments({TreeMap.class, Double.class, Double.class}) TreeMap<Double, Double>[] segments;\r
- }\r
-\r
- /**\r
- * Get dataset for a named variable\r
- * @param variable The name of the variable\r
- * @return {@link SysdynDataSet}\r
- */\r
- public SysdynDataSet getDataSet(String variable) {\r
- if(accessor != null) {\r
- synchronized(accessor) {\r
- ChildReference ref = ChildReference.compile(\r
- new NameReference("recordings"),\r
- new KeyReference( Bindings.VARIANT, Variant.ofInstance(variable) ),\r
- new ComponentReference(),\r
- new NameReference("segments"),\r
- new IndexReference(0)\r
- );\r
- MapAccessor ma;\r
- try {\r
- ma = accessor.getComponent( ref );\r
- } catch (AccessorConstructionException e) {\r
- // Variable was not found, so read it from file\r
- try {\r
- if (simulationResult != null) {\r
- simulationResult.readVariable(variable, resultFile);\r
- }\r
- } catch (FileNotFoundException e1) {\r
- // Unable to read data from OpenModelica result file\r
- System.err.println("Variable " + variable + " not found in OpenModelica result file");\r
- return null;\r
- } catch (IOException e1) {\r
- // IoFail\r
- e1.printStackTrace();\r
- return null;\r
- }\r
- \r
- // Add the read item\r
- addDataSet(variable);\r
- try {\r
- ma = accessor.getComponent( ref );\r
- } catch (AccessorConstructionException e2) {\r
- // Unable to read data from file\r
- System.err.println("Variable " + variable + " not found");\r
- return null;\r
- }\r
- } \r
- \r
- try {\r
- int size = ma.size();\r
- Double[] times = new Double[size];\r
- Double[] values = new Double[size];\r
- \r
- ma.getAll(Bindings.DOUBLE, Bindings.DOUBLE, times, values);\r
- \r
- double[] times_ = new double[size];\r
- double[] values_ = new double[size];\r
- for (int i=0; i<size; i++) {\r
- times_[i] = (times[i]!=null ? times[i] : 0);\r
- values_[i] = (values[i]!=null ? values[i] : 0);\r
- }\r
- \r
- return new SysdynDataSet(variable, resultName, times_, values_);\r
- } catch (AccessorException e) {\r
- // Unable to read data from file\r
- e.printStackTrace();\r
- }\r
- }\r
- }\r
- return null;\r
- }\r
-\r
- public int numberOfVariables() {\r
- return simulationResult.getVariableDataSets().size();\r
- }\r
-\r
- public void setResultFile(File resFile) {\r
- this.resultFile = resFile;\r
- }\r
}\r