From: lempinen Date: Mon, 18 Mar 2013 08:13:19 +0000 (+0000) Subject: Removed accessor from a regular simulation result: divided SysdynResult into MemoryRe... X-Git-Tag: simantics-1.10.1~3 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=fea3ca0c354562c252a677ee7a8e6150d6adb034;p=simantics%2Fsysdyn.git Removed accessor from a regular simulation result: divided SysdynResult into MemoryResult (without accessor, saveable to a file) and FileResult (read from a file and used with an accessor) (refs #4180) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27111 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 bb8f2dd9..fdfbbf93 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java +++ b/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java @@ -441,12 +441,13 @@ public class SimulationResult { * * @param file result file * @param interval the interval of results to be read. 1 reads all time steps, 2 reads every other time step, etc... + * @return The added dataset * @throws FileNotFoundException * @throws IOException */ - public void readVariable(String variable, File file) throws FileNotFoundException, IOException { + public DataSet readVariable(String variable, File file) throws FileNotFoundException, IOException { if(resultFileReader == null) - return; + return null; DataSet ds; List names = resultFileReader.getNames(); @@ -461,10 +462,12 @@ public class SimulationResult { // We should never need to go there unless some change in // the logic of reading variables are made. // Also helps in seeking memory leaks. - return; + return null; } variables.add(ds); + return ds; } + return null; } /** diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FileResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FileResult.java new file mode 100644 index 00000000..e685e8d4 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FileResult.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.manager; + +import java.io.File; + +import org.eclipse.core.runtime.IProgressMonitor; +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.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.reference.ChildReference; +import org.simantics.databoard.accessor.reference.ComponentReference; +import org.simantics.databoard.accessor.reference.IndexReference; +import org.simantics.databoard.accessor.reference.KeyReference; +import org.simantics.databoard.accessor.reference.NameReference; +import org.simantics.databoard.binding.mutable.Variant; + +public class FileResult extends SysdynResult { + + private RecordAccessor accessor; + + /** + * Open result from a file + * + * @param result Name of the result (seen in visualization) + * @param path file path + */ + public FileResult(String resultName, String path) { + super(resultName); + File file = new File(path); + try { + BinaryVariant bv = (BinaryVariant)Accessors.openAccessor(file); + BinaryRecord br = bv.getContentAccessor(); + accessor = br; + } catch (AccessorConstructionException e) { + e.printStackTrace(); + } + } + + /** + * Get the {@link RecordAccessor} of this result + * @return {@link RecordAccessor} for this result + */ + public Accessor getAccessor() { + return accessor; + } + + + /** + * Get dataset for a named variable from accessor + * @param variable The name of the variable + * @return {@link SysdynDataSet} + */ + @Override + public SysdynDataSet getDataSet(String variable) { + if(accessor != null) { + synchronized(accessor) { + ChildReference ref = ChildReference.compile( + new NameReference("recordings"), + new KeyReference( Bindings.VARIANT, Variant.ofInstance(variable) ), + new ComponentReference(), + new NameReference("segments"), + new IndexReference(0) + ); + MapAccessor ma; + try { + ma = accessor.getComponent( ref ); + } catch (AccessorConstructionException e) { + return null; + } + + try { + int size = ma.size(); + Double[] times = new Double[size]; + Double[] values = new Double[size]; + + ma.getAll(Bindings.DOUBLE, Bindings.DOUBLE, times, values); + + double[] times_ = new double[size]; + double[] values_ = new double[size]; + for (int i=0; i>>() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/MemoryResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/MemoryResult.java new file mode 100644 index 00000000..448e1786 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/MemoryResult.java @@ -0,0 +1,234 @@ +/******************************************************************************* + * Copyright (c) 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.manager; + +import gnu.trove.map.hash.THashMap; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; +import java.util.TreeMap; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.simantics.databoard.Accessors; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.Datatypes; +import org.simantics.databoard.Files; +import org.simantics.databoard.accessor.MapAccessor; +import org.simantics.databoard.accessor.RecordAccessor; +import org.simantics.databoard.accessor.error.AccessorConstructionException; +import org.simantics.databoard.accessor.java.JavaRecord; +import org.simantics.databoard.annotations.Arguments; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.RecordBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.type.Datatype; +import org.simantics.modelica.data.DataSet; +import org.simantics.modelica.data.SimulationResult; + +public class MemoryResult extends SysdynResult { + + private THashMap results; + private File resultFile; + private SimulationResult simulationResult; + + + /** + * Create a sysdynresult accessor using a {@link SimulationResult} + * @param result + */ + public MemoryResult(SimulationResult result, String resultName) { + super(resultName); + + if(result != null) + setResult(result); + + results = new THashMap(); + } + + public SysdynDataSet getDataSet(String variable) { + // Try to get cached result + SysdynDataSet result = results.get(variable); + if(result != null) + return result; + + // if not found, try to read it + SimulationResult simulationResult = getSimulationResult(); + try { + if (simulationResult != null) { + DataSet ds = simulationResult.getDataSet(variable); + if(ds == null) + ds = simulationResult.readVariable(variable, resultFile); + + if(ds != null) { + SysdynDataSet sds = new SysdynDataSet(variable, getResultName(), ds.times, ds.values); + results.put(variable, sds); + return sds; + } + } + } catch (FileNotFoundException e1) { + // Unable to read data from OpenModelica result file + System.err.println("Variable " + variable + " not found in OpenModelica result file"); + return null; + } catch (IOException e1) { + // IoFail + e1.printStackTrace(); + } + + return null; + } + + public void setResultFile(File resFile) { + this.resultFile = resFile; + } + + public int numberOfVariables() { + return simulationResult.getVariableDataSets().size(); + } + + public void setResult(SimulationResult result) { + this.simulationResult = result; + this.results.clear(); + if(result != null) + addAllInitialValues(result); // For legacy reasons + } + + public SimulationResult getSimulationResult() { + return this.simulationResult; + } + + + + /** + * Read all results from their DataSets. --- Legacy init file support. Not needed with mat files. + */ + private void addAllInitialValues(SimulationResult result) { + // Add initial values + for(DataSet ds : result.getInitialValueDataSets()){ + SysdynDataSet sds = new SysdynDataSet(ds.name, getResultName(), ds.times, ds.values); + results.put(ds.name, sds); + } + } + + + /** + * Save current {@link RecordAccessor} to a {@link File} + * + * @param file {@link File} where the {@link RecordAccessor} is saved + */ + public void saveToFile(File file, IProgressMonitor progressMonitor) { + try { + // Create Memory Historian + Datatype recordingSessionType = Datatypes.getDatatype("RecordingSession"); + RecordBinding sessionBinding = (RecordBinding) Bindings.getBinding( recordingSessionType ); + Object session = sessionBinding.createDefault(); + JavaRecord accessor = (JavaRecord) Accessors.getAccessor( sessionBinding, session ); + + // Get binding and value from memory + JavaRecord jr = (JavaRecord) accessor; + Binding binding = jr.getBinding(); + Object value = jr.getObject(); + + // Read the missing variables one by one. + getSimulationResult().readMissingVariables(this.resultFile); + + addAllDataSetsToAccessor(accessor, progressMonitor); + // Write to file + if (progressMonitor != null) { + progressMonitor.subTask("Save results"); + } + Files.createFile(file, binding, value); + + // The division is made so that 1 unit per variable and their sum for saving them. + progressMonitor.worked(numberOfVariables()); + } catch (IOException e) { + e.printStackTrace(); + } catch (BindingException e) { + e.printStackTrace(); + } catch (AccessorConstructionException e) { + e.printStackTrace(); + } + + } + + + /** + * Read all results from their DataSets. + * @param progressMonitor + */ + private void addAllDataSetsToAccessor(JavaRecord accessor, IProgressMonitor progressMonitor) { + try { + MapAccessor recordings = accessor.getFieldAccessor("recordings"); + + // There is a recording for each variable + List datasets = getSimulationResult().getVariableDataSets(); + RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class ); + if (progressMonitor != null) { + progressMonitor.subTask("Prepare saving"); + } + // Add variables + for(DataSet ds : datasets){ + if (progressMonitor != null) { + progressMonitor.worked(1); + } + // Add recording to session, if not already added + Variant dsNodeId = Variant.ofInstance( ds.name ); + if (recordings.containsKey(Bindings.VARIANT, dsNodeId)) + continue; + + Recording recording = createRecording(ds); + recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Create a recording for a variable to this result's {@link RecordAccessor} + * @param ds {@link DataSet} for the variable + * @return {@link Recording} + * @throws BindingException + */ + @SuppressWarnings("unchecked") + Recording createRecording(DataSet ds) throws BindingException { + RecordBinding recordingBinding = (RecordBinding) Bindings.getBindingUnchecked( Recording.class ); + Recording recording = (Recording) recordingBinding.createDefault(); + recording.nodeId = Variant.ofInstance( ds.name ); + recording.labels.put("en", ds.name); + + // Create one segment + TreeMap segment = new TreeMap(); + recording.segments = new TreeMap[] { segment }; + + int length = ds.values.length; + for (int i=0; i labels; + public @Arguments({TreeMap.class, Double.class, Double.class}) TreeMap[] segments; + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java index 04beb102..c36d5703 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 Association for Decentralized Information Management in + * Copyright (c) 2010, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy - initial API and implementation *******************************************************************************/ package org.simantics.sysdyn.manager; @@ -38,13 +39,13 @@ import org.simantics.sysdyn.SysdynResource; */ public class SaveResultJob extends Job { - private final SysdynResult sysdynResult; + private final MemoryResult sysdynResult; private final SysdynExperiment experiment; private final Session session; private IProgressMonitor monitor; private File file; - public SaveResultJob(final SysdynExperiment experiment, Session session, final SysdynResult result) { + public SaveResultJob(final SysdynExperiment experiment, Session session, final MemoryResult result) { super("Save Result"); this.experiment = experiment; this.sysdynResult = 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 40305285..98fc43ad 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 Association for Decentralized Information Management in + * Copyright (c) 2010, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy *******************************************************************************/ package org.simantics.sysdyn.manager; @@ -79,7 +80,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment, protected boolean canceled = false; protected ExperimentState sysdynExperimentState; - private SysdynResult result; + private MemoryResult result; private File simulationDir; protected String experimentName; @@ -96,9 +97,9 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment, return INSTANCE; } - public SysdynResult getCurrentResult() { + public MemoryResult getCurrentResult() { if(this.result == null) - this.result = new SysdynResult(); + this.result = new MemoryResult(null, null); return this.result; } @@ -141,7 +142,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment, @Override public void saveState() { - if(result == null) + if(result == null || !(result instanceof MemoryResult)) return; SaveResultJob saveResultJob = new SaveResultJob(SysdynExperiment.this, session, result); saveResultJob.schedule(); 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 bfe35fa1..448deccc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 Association for Decentralized Information Management in + * Copyright (c) 2010, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy *******************************************************************************/ package org.simantics.sysdyn.manager; @@ -197,10 +198,10 @@ public class SysdynModel implements IMappingListener, IModel, VariableSubscripti if(graph.isInstanceOf(result, sr.HistoryDataset)) { HistoryDatasetResult r = new HistoryDatasetResult(); listeningHistories.add(r); - sysdynResult = new SysdynResult(r, NameUtils.getSafeLabel(graph, result)); - r.read(sysdynResult, result); + sysdynResult = new MemoryResult(r, NameUtils.getSafeLabel(graph, result)); + r.read((MemoryResult)sysdynResult, result); } else { - sysdynResult = new SysdynResult( + sysdynResult = new FileResult( (String) graph.getPossibleRelatedValue(result, l0.HasLabel), (String) graph.getPossibleRelatedValue(result, sr.Result_resultFile)); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java index 4ad2d9b8..fe54118b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 Association for Decentralized Information Management in + * Copyright (c) 2010, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,360 +8,34 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy *******************************************************************************/ package org.simantics.sysdyn.manager; import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.List; -import java.util.TreeMap; import org.eclipse.core.runtime.IProgressMonitor; -import org.simantics.databoard.Accessors; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.Datatypes; -import org.simantics.databoard.Files; -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.MapAccessor; -import org.simantics.databoard.accessor.RecordAccessor; -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.accessor.reference.ChildReference; -import org.simantics.databoard.accessor.reference.ComponentReference; -import org.simantics.databoard.accessor.reference.IndexReference; -import org.simantics.databoard.accessor.reference.KeyReference; -import org.simantics.databoard.accessor.reference.NameReference; -import org.simantics.databoard.annotations.Arguments; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.RecordBinding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.mutable.Variant; -import org.simantics.databoard.type.Datatype; -import org.simantics.modelica.data.DataSet; -import org.simantics.modelica.data.SimulationResult; /** - * Class for containing the accessor for a sysdyn simulation result + * * @author Teemu Lempinen * */ -public class SysdynResult { +public abstract class SysdynResult { - protected RecordAccessor accessor; - protected String resultName; - private SimulationResult simulationResult; - private File resultFile; + private String resultName; - /** - * Create an empty result - */ - public SysdynResult() { - - } - - /** - * Create a sysdynresult accessor using a {@link SimulationResult} - * @param result - */ - public SysdynResult(SimulationResult result) { - if(result != null) - setResult(result); - } - - /** - * Create a sysdynresult accessor using a {@link SimulationResult} - * @param result - * @param resultName Name of the result (seen in visualization) - */ - public SysdynResult(SimulationResult result, String resultName) { - this.resultName = resultName; - if(result != null) - setResult(result); - } - - /** - * Open result from a file - * - * @param result Name of the result (seen in visualization) - * @param path file path - */ - public SysdynResult(String result, String path) { - this.resultName = result; - File file = new File(path); - try { - BinaryVariant bv = (BinaryVariant)Accessors.openAccessor(file); - BinaryRecord br = bv.getContentAccessor(); - accessor = br; - } catch (AccessorConstructionException e) { - e.printStackTrace(); - } - } - - /** - * Get the {@link RecordAccessor} of this result - * @return {@link RecordAccessor} for this result - */ - public Accessor getAccessor() { - return accessor; - } - - /** - * Create a {@link RecordAccessor} for this result based on a {@link SimulationResult} - * @param result {@link SimulationResult} - */ - public void setResult(SimulationResult result) { - try { - // Create Memory Historian - this.simulationResult = result; - Datatype recordingSessionType = Datatypes.getDatatype("RecordingSession"); - RecordBinding sessionBinding = (RecordBinding) Bindings.getBinding( recordingSessionType ); - Object session = sessionBinding.createDefault(); - accessor = (JavaRecord) Accessors.getAccessor( sessionBinding, session ); - - addAllInitialValues(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Read all results from their DataSets. - * @param progressMonitor - */ - private void addAllDataSets(IProgressMonitor progressMonitor) { - try { - MapAccessor recordings = accessor.getFieldAccessor("recordings"); - - // There is a recording for each variable - List datasets = simulationResult.getVariableDataSets(); - RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class ); - if (progressMonitor != null) { - progressMonitor.subTask("Prepare saving"); - } - // Add variables - for(DataSet ds : datasets){ - if (progressMonitor != null) { - progressMonitor.worked(1); - } - // Add recording to session, if not already added - Variant dsNodeId = Variant.ofInstance( ds.name ); - if (recordings.containsKey(Bindings.VARIANT, dsNodeId)) - continue; - - Recording recording = createRecording(ds); - recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording)); - } - } catch (Exception e) { - e.printStackTrace(); - } + public SysdynResult(String resultName) { + this.resultName = resultName; } - /** - * Read all results from their DataSets. - */ - private void addDataSet(String variable) { - try { - MapAccessor recordings = accessor.getFieldAccessor("recordings"); - - // There is a recording for each variable - if (simulationResult == null) return; - List datasets = simulationResult.getVariableDataSets(); - RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class ); - - // Add variables - for(DataSet ds : datasets){ - if (ds.name.equals(variable)) { - // Add recording to session - Recording recording = createRecording(ds); - recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording)); - break; - } - continue; - } - } catch (Exception e) { - e.printStackTrace(); - } - + public String getResultName() { + return this.resultName; } - - /** - * Read all results from their DataSets. - */ - private void addAllInitialValues() { - try { - MapAccessor recordings = accessor.getFieldAccessor("recordings"); - - RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class ); - - // Add initial values - for(DataSet ds : simulationResult.getInitialValueDataSets()){ - Variant nodeId = Variant.ofInstance( ds.name ); - if (recordings.containsKey(Bindings.VARIANT, nodeId)) continue; - // Add recording to session - Recording recording = createRecording(ds); - recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording)); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Create a recording for a variable to this result's {@link RecordAccessor} - * @param ds {@link DataSet} for the variable - * @return {@link Recording} - * @throws BindingException - */ - @SuppressWarnings("unchecked") - Recording createRecording(DataSet ds) throws BindingException { - RecordBinding recordingBinding = (RecordBinding) Bindings.getBindingUnchecked( Recording.class ); - Recording recording = (Recording) recordingBinding.createDefault(); - recording.nodeId = Variant.ofInstance( ds.name ); - recording.labels.put("en", ds.name); - // Create one segment - TreeMap segment = new TreeMap(); - recording.segments = new TreeMap[] { segment }; + public abstract SysdynDataSet getDataSet(String variable); - int length = ds.values.length; - for (int i=0; i labels; - public @Arguments({TreeMap.class, Double.class, Double.class}) TreeMap[] segments; - } - - /** - * Get dataset for a named variable - * @param variable The name of the variable - * @return {@link SysdynDataSet} - */ - public SysdynDataSet getDataSet(String variable) { - if(accessor != null) { - synchronized(accessor) { - ChildReference ref = ChildReference.compile( - new NameReference("recordings"), - new KeyReference( Bindings.VARIANT, Variant.ofInstance(variable) ), - new ComponentReference(), - new NameReference("segments"), - new IndexReference(0) - ); - MapAccessor ma; - try { - ma = accessor.getComponent( ref ); - } catch (AccessorConstructionException e) { - // Variable was not found, so read it from file - try { - if (simulationResult != null) { - simulationResult.readVariable(variable, resultFile); - } - } catch (FileNotFoundException e1) { - // Unable to read data from OpenModelica result file - System.err.println("Variable " + variable + " not found in OpenModelica result file"); - return null; - } catch (IOException e1) { - // IoFail - e1.printStackTrace(); - return null; - } - - // Add the read item - addDataSet(variable); - try { - ma = accessor.getComponent( ref ); - } catch (AccessorConstructionException e2) { - // Unable to read data from file - System.err.println("Variable " + variable + " not found"); - return null; - } - } - - try { - int size = ma.size(); - Double[] times = new Double[size]; - Double[] values = new Double[size]; - - ma.getAll(Bindings.DOUBLE, Bindings.DOUBLE, times, values); - - double[] times_ = new double[size]; - double[] values_ = new double[size]; - for (int i=0; i