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.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.util.WeightedMedian;
import org.simantics.layer0.Layer0;
import org.simantics.simulation.Activator;
import org.simantics.simulation.ontology.HistoryResource;
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;
- 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");
- StreamAccessor sa = history.openStream(id, "w");
+ StreamAccessor sa = history.openStream(id, "rw");
try {
Stream s = new Stream(sa);
timeBinding = s.timeBinding;
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);
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.median = null;
+ is.median = new WeightedMedian( CollectorImpl.MEDIAN_LIMIT );
}
}
}
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 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;
}