From b071749dbff3405611f23f047a2ab48d6bed98c6 Mon Sep 17 00:00:00 2001 From: jplaine Date: Thu, 26 Aug 2010 06:19:31 +0000 Subject: [PATCH] DataSet changed to use List instead of plain array, to make it work with Accessors (caused crash because data couldn't be appended to plain array) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17633 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../org/simantics/modelica/data/DataSet.java | 8 ++- .../modelica/data/SimulationResult.java | 14 ++-- .../sysdyn/ui/project/DefaultVariable.java | 8 +++ .../sysdyn/ui/project/HistoryVariable.java | 70 ++++++++++++++++--- .../simantics/sysdyn/ui/trend/TrendView.java | 6 +- .../sysdyn/manager/SysdynDataSet.java | 4 +- .../sysdyn/manager/SysdynResult.java | 15 ++-- 7 files changed, 96 insertions(+), 29 deletions(-) diff --git a/org.simantics.modelica/src/org/simantics/modelica/data/DataSet.java b/org.simantics.modelica/src/org/simantics/modelica/data/DataSet.java index 9edc7933..1f35c897 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/data/DataSet.java +++ b/org.simantics.modelica/src/org/simantics/modelica/data/DataSet.java @@ -11,16 +11,18 @@ *******************************************************************************/ package org.simantics.modelica.data; +import java.util.List; + /** * Simulation result for one variable. * @author Hannu Niemistö */ public class DataSet { public String name; - public double[] times; - public double[] values; + public List times; + public List values; - public DataSet(String name, double[] times, double[] values) { + public DataSet(String name, List times, List values) { this.name = name; this.times = times; this.values = values; 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 f3d22d02..89ce4c9b 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java +++ b/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java @@ -89,10 +89,14 @@ public class SimulationResult { double startTime = mappedInitials.get("start value"); double stopTime = mappedInitials.get("stop value"); - double[] times = {startTime, stopTime}; + List times = new ArrayList(2); + times.add(startTime); + times.add(stopTime); for(String key : mappedInitials.keySet()) { - double[] values = {mappedInitials.get(key), mappedInitials.get(key)}; + List values = new ArrayList(2); + values.add(mappedInitials.get(key)); + values.add(mappedInitials.get(key)); initials.add(new DataSet(key, times , values)); } } @@ -122,8 +126,8 @@ public class SimulationResult { return; String name = matcher.group(1); - TDoubleArrayList times = new TDoubleArrayList(); - TDoubleArrayList values = new TDoubleArrayList(); + List times = new ArrayList(); + List values = new ArrayList(); while(true) { String line = getLine(stream); if(line == null) @@ -135,7 +139,7 @@ public class SimulationResult { values.add(Double.parseDouble(nn[1])); } - variables.add(new DataSet(name, times.toNativeArray(), values.toNativeArray())); + variables.add(new DataSet(name, times, values)); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/DefaultVariable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/DefaultVariable.java index 909cab6d..0f859dd2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/DefaultVariable.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/DefaultVariable.java @@ -3,6 +3,8 @@ package org.simantics.sysdyn.ui.project; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.java.DoubleBindingDefault; import org.simantics.databoard.binding.java.StringBindingDefault; +import org.simantics.databoard.type.DataType; +import org.simantics.databoard.type.DoubleType; import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -52,4 +54,10 @@ public class DefaultVariable extends ResourceVariable { graph.claimValue(expression, sr.HasEquation, value.toString(), StringBindingDefault.INSTANCE); } + @Override + public T getInterface(ReadGraph graph, Class clazz) + throws DatabaseException { + if(DataType.class.equals(clazz)) return (T)DoubleType.INSTANCE; + return super.getInterface(graph, clazz); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/HistoryVariable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/HistoryVariable.java index cd4162ad..035f2020 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/HistoryVariable.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/HistoryVariable.java @@ -3,7 +3,9 @@ package org.simantics.sysdyn.ui.project; import org.simantics.databoard.Accessors; import org.simantics.databoard.Bindings; import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.RecordAccessor; import org.simantics.databoard.accessor.error.AccessorConstructionException; +import org.simantics.databoard.accessor.error.AccessorException; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; @@ -23,22 +25,71 @@ public class HistoryVariable extends ResourceVariable { super(resource); } +// @Override +// public T getInterface(ReadGraph graph, Class clazz) throws DatabaseException { +// if(Accessor.class.equals(clazz)) { +// +// SimulationResource SIMU = SimulationResource.getInstance(graph); +// Resource model = Variables.getModel(graph, this); +// Resource configuration = graph.getPossibleObject(model, SIMU.HasConfiguration); +// SysdynModel sm = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); +// SysdynResult sr = sm.getSysdynResult(); +// String rvi = Variables.getRVI(graph, this); +// System.out.println("HistoryVariable rvi='" + rvi + "'"); +// rvi = rvi.substring(1).replace("/", "."); +// SysdynDataSet ds = sr.getDataSet(rvi); +// +// try { +// return (T)Accessors.getAccessor(Bindings.getBindingUnchecked(SysdynDataSet.class), ds); +// } catch (RuntimeBindingConstructionException e) { +// e.printStackTrace(); +// } catch (AccessorConstructionException e) { +// e.printStackTrace(); +// } +// return null; +// +// } +// return super.getInterface(graph, clazz); +// } + @Override public T getInterface(ReadGraph graph, Class clazz) throws DatabaseException { - if(Accessor.class.equals(clazz)) { - + if(RecordAccessor.class.equals(clazz) || Accessor.class.equals(clazz)) { SimulationResource SIMU = SimulationResource.getInstance(graph); + Builtins b = graph.getBuiltins(); Resource model = Variables.getModel(graph, this); Resource configuration = graph.getPossibleObject(model, SIMU.HasConfiguration); - SysdynModel sm = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); - SysdynResult sr = sm.getSysdynResult(); - String rvi = Variables.getRVI(graph, this); - System.out.println("HistoryVariable rvi='" + rvi + "'"); - rvi = rvi.substring(1).replace("/", "."); - SysdynDataSet ds = sr.getDataSet(rvi); + final SysdynModel sm = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); + SysdynResult sr = new SysdynResult(sm.getSimulationResult()); // TODO: copy or not to copy ... + String tmp = Variables.getRVI(graph, this); + System.out.println("HistoryVariable rvi='" + tmp + "'"); + final String rvi = tmp.substring(1).replace("/", "."); + SysdynDataSet ds = sr.getDataSet(rvi); + assert(ds != null); try { - return (T)Accessors.getAccessor(Bindings.getBindingUnchecked(SysdynDataSet.class), ds); + final RecordAccessor ac = (RecordAccessor)Accessors.getAccessor(Bindings.getBindingUnchecked(SysdynDataSet.class), ds); + + sm.addResultListener(new Runnable() { // FIXME: remove listener at some point.. + @Override + public void run() { + SysdynResult sr = new SysdynResult(sm.getSimulationResult()); + SysdynDataSet ds = sr.getDataSet(rvi); + if(ds == null) return; + try { + if(ds.result == null) ds.result = ""; + ac.setValue(Bindings.getBindingUnchecked(SysdynDataSet.class), ds); + } catch (RuntimeBindingConstructionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (AccessorException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// System.out.println("===================================================== NEW RESULT ====================================================="); + + }}); + return (T)ac; } catch (RuntimeBindingConstructionException e) { e.printStackTrace(); } catch (AccessorConstructionException e) { @@ -49,5 +100,4 @@ public class HistoryVariable extends ResourceVariable { } return super.getInterface(graph, clazz); } - } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java index 6fd478ba..cc6b6077 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java @@ -74,17 +74,17 @@ public class TrendView extends ViewPart { @Override public Number getY(int series, int item) { - return sets[series].values[item]; + return sets[series].values.get(item); } @Override public Number getX(int series, int item) { - return sets[series].times[item]; + return sets[series].times.get(item); } @Override public int getItemCount(int series) { - return sets[series].times.length; + return sets[series].times.size(); } @Override diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynDataSet.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynDataSet.java index 88ca787a..e810fe7a 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynDataSet.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynDataSet.java @@ -1,12 +1,14 @@ package org.simantics.sysdyn.manager; +import java.util.List; + import org.simantics.modelica.data.DataSet; public class SysdynDataSet extends DataSet { public String result; - public SysdynDataSet(String name, String result, double[] times, double[] values) { + public SysdynDataSet(String name, String result, List times, List values) { super(name, times, values); this.result = result; } 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 db40e219..766d5f13 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java @@ -3,6 +3,7 @@ package org.simantics.sysdyn.manager; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.TreeMap; @@ -117,10 +118,10 @@ public class SysdynResult { TreeMap segment = new TreeMap(); recording.segments = new TreeMap[] { segment }; - int length = ds.values.length; + int length = ds.values.size(); for (int i=0; i times_ = new ArrayList(size); + List values_ = new ArrayList(size); for (int i=0; i