From: lempinen Date: Tue, 10 Aug 2010 14:35:53 +0000 (+0000) Subject: New simulation results handling: new accessors, constant values visible X-Git-Tag: simantics-1.2.0~123 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=c24973903ea8de04917f42898c8c3d801694ff26;p=simantics%2Fsysdyn.git New simulation results handling: new accessors, constant values visible git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17078 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 6fdd8217..f3d22d02 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java +++ b/org.simantics.modelica/src/org/simantics/modelica/data/SimulationResult.java @@ -152,6 +152,10 @@ public class SimulationResult { return variables; } + public List getInitialValueDataSets() { + return initials; + } + /** * Gets DataSet for variable. Loops first the variables and then initials. * @param name the name of the variable 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 e17a47ef..db40e219 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java @@ -3,37 +3,41 @@ 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; import org.simantics.databoard.Accessors; import org.simantics.databoard.Bindings; import org.simantics.databoard.Files; +import org.simantics.databoard.Historian; 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.accessor.reference.AccessorReference; +import org.simantics.databoard.accessor.reference.ArrayIndexReference; +import org.simantics.databoard.accessor.reference.FieldNameReference; +import org.simantics.databoard.accessor.reference.KeyReference; +import org.simantics.databoard.accessor.reference.VariantValueReference; +import org.simantics.databoard.annotations.KeyType; +import org.simantics.databoard.annotations.ValueType; import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.error.BindingConstructionException; +import org.simantics.databoard.binding.RecordBinding; import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.java.StringBindingDefault; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.binding.mutable.MutableVariantBinding; +import org.simantics.databoard.binding.java.DoubleBindingDefault; +import org.simantics.databoard.binding.mutable.ImmutableVariantBinding; import org.simantics.databoard.binding.mutable.Variant; -import org.simantics.databoard.binding.mutable.VariantContainerBinding; import org.simantics.databoard.serialization.SerializationException; +import org.simantics.databoard.type.DataType; import org.simantics.modelica.data.DataSet; import org.simantics.modelica.data.SimulationResult; public class SysdynResult { - RecordingSession recordingSession; RecordAccessor accessor; String resultName; @@ -46,6 +50,12 @@ public class SysdynResult { 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); @@ -59,187 +69,133 @@ public class SysdynResult { } public Accessor getAccessor() { - try { - return Accessors.getAccessor(recordingSession); - } catch (AccessorConstructionException e) { - e.printStackTrace(); - } - return null; + return accessor; } public void setResult(SimulationResult result) { - try { - Binding stringBinding = StringBindingDefault.INSTANCE; - Binding recordBinding = Bindings.getBinding( Recording.class ); - - recordingSession = new RecordingSession(); - recordingSession.recordings = new TreeMap(); - MutableVariant timeVariableId = new MutableVariant(); - 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); + try { + // Create Memory Historian + DataType recordingSessionType = Historian.getRecordingSessionType(); + RecordBinding sessionBinding = (RecordBinding) Bindings.getBinding( recordingSessionType ); + RecordBinding recordingBinding = (RecordBinding) Bindings.getBinding( Recording.class ); + Object session = sessionBinding.createDefault(); + accessor = (JavaRecord) Accessors.getAccessor( sessionBinding, session ); + MapAccessor recordings = accessor.getFieldAccessor("recordings"); + + // There is a recording for each variable + List datasets = result.getVariableDataSets(); + + // Add variables + for(DataSet ds : datasets){ + // Add recording to session + Recording recording = createRecording(ds); + recordings.put(ImmutableVariantBinding.INSTANCE, recording.nodeId, ImmutableVariantBinding.INSTANCE, new Variant(recordingBinding, recording)); + } - MutableVariant tmpVariant = new MutableVariant(); - MutableVariant tmpVariant2 = new MutableVariant(); - tmpVariant.setValue(recordBinding, rec); - tmpVariant2.setValue(stringBinding, name); - recordingSession.recordings.put(tmpVariant2, tmpVariant); + // Add initial values + for(DataSet ds : result.getInitialValueDataSets()){ + Variant nodeId = Variant.ofInstance( ds.name ); + if (recordings.containsKey(ImmutableVariantBinding.INSTANCE, nodeId)) continue; + // Add recording to session + Recording recording = createRecording(ds); + recordings.put(ImmutableVariantBinding.INSTANCE, recording.nodeId, ImmutableVariantBinding.INSTANCE, new Variant(recordingBinding, recording)); } + } catch (Exception e) { e.printStackTrace(); } + } - if(recordingSession == null) return; - - - try { - Binding recordBinding = Bindings.getBinding( RecordingSession.class ); - if(accessor == null) { - accessor = (JavaRecord)Accessors.getAccessor(recordBinding, recordingSession); - } else { - accessor.setValue(recordBinding, recordingSession); - } - } catch (AccessorConstructionException e) { - e.printStackTrace(); - } catch (BindingConstructionException e) { - e.printStackTrace(); - } catch (AccessorException e) { - e.printStackTrace(); + @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 samples; - - @Override - public String toString() { - return "[startTime="+startTime+", endTime="+endTime+", samples="+samples+"]"; + } catch (AccessorException e) { + e.printStackTrace(); } } 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 Variant nodeId; + public @KeyType(String.class) @ValueType(String.class) TreeMap labels; + public @KeyType(Double.class) @ValueType(Double.class) TreeMap[] segments; } public SysdynDataSet getDataSet(String variable) { if(accessor != null) { - MapAccessor ma; try { - ma = accessor.getFieldAccessor("recordings"); - } catch (AccessorConstructionException e1) { - e1.printStackTrace(); - return null; - } - Binding stringBinding = StringBindingDefault.INSTANCE; - MutableVariant tmpVariant = new MutableVariant(); - tmpVariant.setValue(stringBinding, variable); - MutableVariant tmpVariant2 = new MutableVariant(); - tmpVariant2.setValue(stringBinding, "time"); - Recording rec; - Recording time; - try { - if(!ma.containsKey(MutableVariantBinding.INSTANCE, tmpVariant)) return null; - VariantAccessor va = ma.getValueAccessor( MutableVariantBinding.INSTANCE, tmpVariant); - Accessor valueAccessor = va.getContentAccessor(); - rec = (Recording)valueAccessor.getValue( Bindings.getBinding(Recording.class)); - - va = ma.getValueAccessor( MutableVariantBinding.INSTANCE, tmpVariant2); - valueAccessor = va.getContentAccessor(); - 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; + AccessorReference ref = AccessorReference.compile( + new FieldNameReference("recordings"), + new KeyReference( ImmutableVariantBinding.INSTANCE, Variant.ofInstance(variable) ), + new VariantValueReference(), + new FieldNameReference("segments"), + new ArrayIndexReference(0) + ); + + MapAccessor ma = accessor.getAccessor( ref ); + int size = ma.size(); + Double[] times = new Double[size]; + Double[] values = new Double[size]; + + ma.getAll(DoubleBindingDefault.INSTANCE, DoubleBindingDefault.INSTANCE, times, values); + + double[] times_ = new double[size]; + double[] values_ = new double[size]; + for (int i=0; i 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 6490741a..6d153106 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -523,6 +523,8 @@ _ @ "WorkModel" : SysdynModel L0.HasLabel "Experiment" : L0.String WorkModule WorkforceModule + WorkModuleConfiguration + WorkforceModuleConfiguration WorkModelConfiguration @@ -1157,7 +1159,9 @@ _ @ "CustomerModel" : SysdynModel _ @ "Experiment" : Experiment L0.HasLabel "Experiment" : L0.String CustomerModule + CustomerModuleConfiguration CapacityModule + CapacityModuleConfiguration RootConfiguration RootConfigurationDiagram : ConfigurationDiagram