]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.simulation/src/org/simantics/simulation/history/HistoryUtil.java
Tested and fixed utility for truncating history data.
[simantics/platform.git] / bundles / org.simantics.simulation / src / org / simantics / simulation / history / HistoryUtil.java
index 53a76d1360d783011320f451eb86838aa170d1bb..27f2bf895bff382b0ae05cf251cd160425249739 100644 (file)
@@ -52,14 +52,15 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ServiceNotFoundException;
 import org.simantics.db.request.Read;
 import org.simantics.fastlz.FastLZ;
 import org.simantics.db.exception.ServiceNotFoundException;
 import org.simantics.db.request.Read;
 import org.simantics.fastlz.FastLZ;
-import org.simantics.history.Collector;
 import org.simantics.history.HistoryException;
 import org.simantics.history.HistoryManager;
 import org.simantics.history.ItemManager;
 import org.simantics.history.HistoryException;
 import org.simantics.history.HistoryManager;
 import org.simantics.history.ItemManager;
+import org.simantics.history.impl.CollectorImpl;
 import org.simantics.history.impl.CollectorState;
 import org.simantics.history.impl.CollectorState.VariableState;
 import org.simantics.history.util.Stream;
 import org.simantics.history.util.ValueBand;
 import org.simantics.history.impl.CollectorState;
 import org.simantics.history.impl.CollectorState.VariableState;
 import org.simantics.history.util.Stream;
 import org.simantics.history.util.ValueBand;
+import org.simantics.history.util.WeightedMedian;
 import org.simantics.layer0.Layer0;
 import org.simantics.simulation.Activator;
 import org.simantics.simulation.ontology.HistoryResource;
 import org.simantics.layer0.Layer0;
 import org.simantics.simulation.Activator;
 import org.simantics.simulation.ontology.HistoryResource;
@@ -949,17 +950,16 @@ public class HistoryUtil {
                return 0L;
        }
 
                return 0L;
        }
 
-       public static void truncateHistory(double toBeforeTime, HistoryManager history, Collector collector) throws AccessorException, BindingException, HistoryException {
+       public static CollectorState truncateHistory(double toBeforeTime, HistoryManager history, CollectorState state) throws AccessorException, BindingException, HistoryException {
                Double t = toBeforeTime;
                Binding timeBinding = null;
                Double t = toBeforeTime;
                Binding timeBinding = null;
-               CollectorState state = collector != null ? (CollectorState) collector.getState() : null;
 
                Bean[] items = history.getItems();
                //System.out.println("truncating all samples after t=" + toBeforeTime + " for " + items.length + " history items");
 
                for (Bean item : items) {
                        String id = (String) item.getField("id");
 
                Bean[] items = history.getItems();
                //System.out.println("truncating all samples after t=" + toBeforeTime + " for " + items.length + " history items");
 
                for (Bean item : items) {
                        String id = (String) item.getField("id");
-                       StreamAccessor sa = history.openStream(id, "w");
+                       StreamAccessor sa = history.openStream(id, "rw");
                        try {
                                Stream s = new Stream(sa);
                                timeBinding = s.timeBinding;
                        try {
                                Stream s = new Stream(sa);
                                timeBinding = s.timeBinding;
@@ -972,7 +972,8 @@ public class HistoryUtil {
 
                                        if (state != null) {
                                                Object prevTime  = newSize > 0 ? s.getItemTime(s.timeBinding, newSize - 1) : null;
 
                                        if (state != null) {
                                                Object prevTime  = newSize > 0 ? s.getItemTime(s.timeBinding, newSize - 1) : null;
-                                               Object prevValue = newSize > 0 ? sa.get(newSize - 1, s.valueBinding) : null;
+                                               Bean prevSample = newSize > 0 ? (Bean) sa.get(newSize - 1, s.sampleBinding) : null;
+                                               Object prevValue = prevSample != null ? prevSample.getField(s.valueIndex) : null;
                                                boolean isNan = isNaN(prevValue);
 
                                                VariableState vs = state.values.get(id);
                                                boolean isNan = isNaN(prevValue);
 
                                                VariableState vs = state.values.get(id);
@@ -984,18 +985,18 @@ public class HistoryUtil {
 
                                                CollectorState.Item is = state.itemStates.get(id);
                                                if (is != null) {
 
                                                CollectorState.Item is = state.itemStates.get(id);
                                                if (is != null) {
-                                                       is.firstTime = Double.NaN;
-                                                       is.firstValue = null;
-                                                       is.currentTime = toTime(prevTime);
-                                                       is.currentValue = prevValue != null ? new MutableVariant(s.valueBinding, prevValue) : null;
-                                                       is.isNaN = isNaN(is.currentValue);
-                                                       is.isValid = is.currentValue != null;
+                                                       is.firstTime = toTime(prevTime);
+                                                       is.firstValue = toValue(s.valueBinding, prevValue);
+                                                       is.currentTime = is.firstTime;
+                                                       is.currentValue = toValue(s.valueBinding, prevValue);
+                                                       is.isNaN = isNan;
+                                                       is.isValid = prevValue != null;
                                                        is.sum = Double.NaN;
                                                        is.count = 0;
                                                        is.ooDeadband = false;
                                                        is.firstDisabledTime = Double.NaN;
                                                        is.lastDisabledTime = Double.NaN;
                                                        is.sum = Double.NaN;
                                                        is.count = 0;
                                                        is.ooDeadband = false;
                                                        is.firstDisabledTime = Double.NaN;
                                                        is.lastDisabledTime = Double.NaN;
-                                                       is.median = null;
+                                                       is.median = new WeightedMedian( CollectorImpl.MEDIAN_LIMIT );
                                                }
                                        }
                                }
                                                }
                                        }
                                }
@@ -1007,14 +1008,19 @@ public class HistoryUtil {
                if (timeBinding != null && state != null) {
                        state.time.setValue(timeBinding, t);
                        state.dT = 1.0;
                if (timeBinding != null && state != null) {
                        state.time.setValue(timeBinding, t);
                        state.dT = 1.0;
-                       collector.setState(state);
                }
                }
+
+               return state;
        }
 
        private static double toTime(Object time) {
                return time instanceof Number ? ((Number) time).doubleValue() : Double.NaN;
        }
 
        }
 
        private static double toTime(Object time) {
                return time instanceof Number ? ((Number) time).doubleValue() : Double.NaN;
        }
 
+       private static MutableVariant toValue(Binding valueBinding, Object value) {
+               return new MutableVariant(valueBinding, value != null ? value : valueBinding.createDefaultUnchecked());
+       }
+
        private static boolean isNaN(Object value) {
                return value instanceof Number ? Double.isNaN(((Number) value).doubleValue()) : false;
        }
        private static boolean isNaN(Object value) {
                return value instanceof Number ? Double.isNaN(((Number) value).doubleValue()) : false;
        }