\r
import java.io.File;\r
import java.io.IOException;\r
-import java.util.ArrayList;\r
+import java.util.List;\r
import java.util.TreeMap;\r
\r
import org.simantics.databoard.Accessors;\r
import org.simantics.databoard.Bindings;\r
import org.simantics.databoard.Files;\r
+import org.simantics.databoard.Historian;\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.VariantAccessor;\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.annotations.Optional;\r
+import org.simantics.databoard.accessor.reference.AccessorReference;\r
+import org.simantics.databoard.accessor.reference.ArrayIndexReference;\r
+import org.simantics.databoard.accessor.reference.FieldNameReference;\r
+import org.simantics.databoard.accessor.reference.KeyReference;\r
+import org.simantics.databoard.accessor.reference.VariantValueReference;\r
+import org.simantics.databoard.annotations.KeyType;\r
+import org.simantics.databoard.annotations.ValueType;\r
import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.RecordBinding;\r
import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.java.StringBindingDefault;\r
-import org.simantics.databoard.binding.mutable.MutableVariant;\r
-import org.simantics.databoard.binding.mutable.MutableVariantBinding;\r
+import org.simantics.databoard.binding.java.DoubleBindingDefault;\r
+import org.simantics.databoard.binding.mutable.ImmutableVariantBinding;\r
import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.databoard.binding.mutable.VariantContainerBinding;\r
import org.simantics.databoard.serialization.SerializationException;\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 SysdynResult {\r
\r
- RecordingSession recordingSession;\r
RecordAccessor accessor;\r
String resultName;\r
\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
}\r
\r
public Accessor getAccessor() {\r
- try {\r
- return Accessors.getAccessor(recordingSession);\r
- } catch (AccessorConstructionException e) {\r
- e.printStackTrace();\r
- }\r
- return null;\r
+ return accessor;\r
}\r
\r
public void setResult(SimulationResult result) {\r
- try { \r
- Binding stringBinding = StringBindingDefault.INSTANCE;\r
- Binding recordBinding = Bindings.getBinding( Recording.class );\r
-\r
- recordingSession = new RecordingSession();\r
- recordingSession.recordings = new TreeMap<Variant, Variant>();\r
- MutableVariant timeVariableId = new MutableVariant();\r
- timeVariableId.setValue(stringBinding, "timeVariableId");\r
- recordingSession.timeVariableId = timeVariableId;\r
-\r
-\r
-\r
- for(DataSet ds : result.getVariableDataSets()){\r
- Recording rec = new Recording();\r
- rec.labels = new TreeMap<String, String>();\r
- String name = ds.name;\r
- rec.id = name;\r
- rec.labels.put("en", name); \r
- rec.segments = new ArrayList<Segment>(); \r
-\r
- Segment seg = new Segment();\r
- seg.startTime = 0;\r
- seg.endTime = ds.values.length;\r
- seg.samples = new ArrayList<Sample>();\r
- for(double value : ds.values) {\r
- Sample samp = new Sample();\r
- samp.value = value;\r
- seg.samples.add(samp); \r
- }\r
-\r
- rec.segments.add(seg);\r
+ try {\r
+ // Create Memory Historian\r
+ DataType recordingSessionType = Historian.getRecordingSessionType();\r
+ RecordBinding sessionBinding = (RecordBinding) Bindings.getBinding( recordingSessionType );\r
+ RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class );\r
+ Object session = sessionBinding.createDefault(); \r
+ accessor = (JavaRecord) Accessors.getAccessor( sessionBinding, session );\r
+ MapAccessor recordings = accessor.getFieldAccessor("recordings");\r
+\r
+ // There is a recording for each variable\r
+ List<DataSet> datasets = result.getVariableDataSets();\r
+\r
+ // Add variables\r
+ for(DataSet ds : datasets){\r
+ // Add recording to session\r
+ Recording recording = createRecording(ds);\r
+ recordings.put(ImmutableVariantBinding.INSTANCE, recording.nodeId, ImmutableVariantBinding.INSTANCE, new Variant(recordingBinding, recording));\r
+ }\r
\r
- MutableVariant tmpVariant = new MutableVariant();\r
- MutableVariant tmpVariant2 = new MutableVariant();\r
- tmpVariant.setValue(recordBinding, rec);\r
- tmpVariant2.setValue(stringBinding, name);\r
- recordingSession.recordings.put(tmpVariant2, tmpVariant);\r
+ // Add initial values\r
+ for(DataSet ds : result.getInitialValueDataSets()){\r
+ Variant nodeId = Variant.ofInstance( ds.name );\r
+ if (recordings.containsKey(ImmutableVariantBinding.INSTANCE, nodeId)) continue;\r
+ // Add recording to session\r
+ Recording recording = createRecording(ds);\r
+ recordings.put(ImmutableVariantBinding.INSTANCE, recording.nodeId, ImmutableVariantBinding.INSTANCE, new Variant(recordingBinding, recording));\r
}\r
+\r
} catch (Exception e) {\r
e.printStackTrace();\r
} \r
+ }\r
\r
- if(recordingSession == null) return;\r
-\r
-\r
- try {\r
- Binding recordBinding = Bindings.getBinding( RecordingSession.class );\r
- if(accessor == null) {\r
- accessor = (JavaRecord)Accessors.getAccessor(recordBinding, recordingSession);\r
- } else {\r
- accessor.setValue(recordBinding, recordingSession);\r
- }\r
- } catch (AccessorConstructionException e) {\r
- e.printStackTrace();\r
- } catch (BindingConstructionException e) {\r
- e.printStackTrace();\r
- } catch (AccessorException e) {\r
- e.printStackTrace();\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
-\r
+ return recording;\r
}\r
\r
public void saveToFile(File file) {\r
- if(recordingSession == null) return;\r
+ if(accessor == null) return;\r
try {\r
- Binding binding = Bindings.getBinding( recordingSession.getClass() );\r
- Files.createFile(file, binding, recordingSession);\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
+\r
+ // Write to file\r
+ Files.createFile(file, binding, value);\r
} catch (IOException e) {\r
e.printStackTrace();\r
- } catch (BindingConstructionException e) {\r
- e.printStackTrace();\r
} catch (SerializationException e) {\r
e.printStackTrace();\r
} catch (BindingException e) {\r
e.printStackTrace();\r
- }\r
-\r
- }\r
-\r
-\r
- public static class Sample {\r
- public double value;\r
-\r
- @Override\r
- public String toString() {\r
- return ""+value;\r
- }\r
- }\r
-\r
- public static class Segment {\r
- public int startTime, endTime;\r
- public ArrayList<Sample> samples;\r
-\r
- @Override\r
- public String toString() {\r
- return "[startTime="+startTime+", endTime="+endTime+", samples="+samples+"]";\r
+ } catch (AccessorException e) {\r
+ e.printStackTrace();\r
}\r
\r
}\r
\r
public static class Recording {\r
- public String id;\r
- public TreeMap<String, String> labels;\r
- public ArrayList<Segment> segments;\r
-\r
- @Override\r
- public String toString() {\r
- return "id="+id+", labels="+labels+", segments="+segments;\r
- }\r
-\r
- }\r
-\r
- public static class RecordingSession {\r
- public @Optional Variant datasource;\r
- public Variant timeVariableId;\r
- public TreeMap<Variant, Variant> recordings;\r
-\r
- @Override\r
- public String toString() {\r
- return recordings.toString();\r
- }\r
- }\r
-\r
- public static class NodeId {\r
- public String nodeId;\r
+ public Variant nodeId;\r
+ public @KeyType(String.class) @ValueType(String.class) TreeMap<String, String> labels;\r
+ public @KeyType(Double.class) @ValueType(Double.class) TreeMap<Double, Double>[] segments;\r
}\r
\r
public SysdynDataSet getDataSet(String variable) {\r
if(accessor != null) {\r
- MapAccessor ma;\r
try {\r
- ma = accessor.getFieldAccessor("recordings");\r
- } catch (AccessorConstructionException e1) {\r
- e1.printStackTrace();\r
- return null;\r
- }\r
- Binding stringBinding = StringBindingDefault.INSTANCE;\r
- MutableVariant tmpVariant = new MutableVariant();\r
- tmpVariant.setValue(stringBinding, variable);\r
- MutableVariant tmpVariant2 = new MutableVariant();\r
- tmpVariant2.setValue(stringBinding, "time");\r
- Recording rec;\r
- Recording time;\r
- try {\r
- if(!ma.containsKey(MutableVariantBinding.INSTANCE, tmpVariant)) return null;\r
- VariantAccessor va = ma.getValueAccessor( MutableVariantBinding.INSTANCE, tmpVariant);\r
- Accessor valueAccessor = va.getContentAccessor();\r
- rec = (Recording)valueAccessor.getValue( Bindings.getBinding(Recording.class));\r
-\r
- va = ma.getValueAccessor( MutableVariantBinding.INSTANCE, tmpVariant2);\r
- valueAccessor = va.getContentAccessor();\r
- time = (Recording)valueAccessor.getValue( Bindings.getBinding(Recording.class));\r
- if( rec == null || time == null) return null;\r
- } catch (AccessorException e) {\r
- e.printStackTrace();\r
- return null;\r
- } catch (BindingConstructionException e) {\r
- e.printStackTrace();\r
- return null;\r
+ AccessorReference ref = AccessorReference.compile(\r
+ new FieldNameReference("recordings"),\r
+ new KeyReference( ImmutableVariantBinding.INSTANCE, Variant.ofInstance(variable) ),\r
+ new VariantValueReference(),\r
+ new FieldNameReference("segments"),\r
+ new ArrayIndexReference(0)\r
+ );\r
+\r
+ MapAccessor ma = accessor.getAccessor( ref );\r
+ int size = ma.size();\r
+ Double[] times = new Double[size];\r
+ Double[] values = new Double[size];\r
+\r
+ ma.getAll(DoubleBindingDefault.INSTANCE, DoubleBindingDefault.INSTANCE, 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];\r
+ values_[i] = values[i];\r
+ }\r
+\r
+ return new SysdynDataSet(variable, resultName, times_, values_);\r
+\r
} catch (AccessorConstructionException e) {\r
+ // Variable was not found\r
+ System.err.println("Variable "+variable+" is not available in the history.");\r
+ } catch (AccessorException e) {\r
+ // Unable to read data from file\r
e.printStackTrace();\r
- return null;\r
- }\r
- ArrayList<Sample> samples = rec.segments.get(0).samples;\r
- double[] values = new double[samples.size()];\r
- ArrayList<Sample> timeSamples = time.segments.get(0).samples;\r
- double[] times = new double[samples.size()];\r
- for(int i = 0; i < samples.size(); i++) {\r
- values[i] = samples.get(i).value;\r
- times[i] = timeSamples.get(i).value;\r
}\r
- return new SysdynDataSet(variable, resultName, times, values);\r
}\r
return null;\r
}\r