--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.history.util;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.math.BigDecimal;\r
+import java.math.MathContext;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.StreamAccessor;\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.RecordBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.serialization.Serializer;\r
+import org.simantics.databoard.serialization.SerializerConstructionException;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.util.Bean;\r
+import org.simantics.databoard.util.binary.BinaryReadable;\r
+import org.simantics.databoard.util.binary.InputStreamReadable;\r
+import org.simantics.history.HistoryAndCollectorItem;\r
+import org.simantics.history.HistoryException;\r
+import org.simantics.history.HistoryManager;\r
+\r
+/**\r
+ * Util for import/export.\r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class HistoryExportUtil {\r
+\r
+ Binding beanArrayBinding = Bindings.getBindingUnchecked(Bean[].class);\r
+ Serializer beanArraySerializer = Bindings.getSerializerUnchecked( beanArrayBinding );\r
+ \r
+ Serializer intSerializer = Bindings.getSerializerUnchecked( Bindings.INTEGER );\r
+ \r
+ /**\r
+ * Export history to an output stream.\r
+ * \r
+ * @param history\r
+ * @param timeBinding\r
+ * @param from\r
+ * @param end\r
+ * @param out\r
+ * @throws IOException\r
+ * @throws HistoryException\r
+ */\r
+ public void exportHistory( HistoryManager history, Binding timeBinding, Object from, Object end, OutputStream out )\r
+ throws IOException, HistoryException\r
+ {\r
+ try {\r
+ Bean[] items = history.getItems();\r
+ // Serialize items as Variants\r
+ beanArraySerializer.serialize(out, items);\r
+ for (Bean item : items) {\r
+ Datatype format = (Datatype) item.getField("format");\r
+ String id = (String) item.getField("id");\r
+ RecordBinding sampleBinding = Bindings.getBinding( format );\r
+ Serializer sampleSerializer = Bindings.getSerializer(sampleBinding);\r
+ Object sample = sampleBinding.createDefault();\r
+ StreamAccessor sa = history.openStream(id, "r");\r
+ Stream stream = new Stream(sa, sampleBinding);\r
+ try {\r
+ // Start index\r
+ int startIndex = stream.binarySearch(Bindings.DOUBLE, from); \r
+ if ( startIndex < -stream.count() ) {\r
+ intSerializer.serialize(0, out);\r
+ continue;\r
+ }\r
+ if ( startIndex<0 ) startIndex = -2-startIndex;\r
+ if ( startIndex == -1 ) startIndex = 0;\r
+ \r
+ // End index\r
+ int endIndex = stream.binarySearch(Bindings.DOUBLE, end);\r
+ if ( endIndex == -1 ) {\r
+ intSerializer.serialize(0, out);\r
+ continue;\r
+ }\r
+ if ( endIndex<0 ) endIndex = -1-endIndex;\r
+ if ( endIndex == sa.size() ) endIndex = sa.size()-1;\r
+ if ( endIndex<startIndex ) {\r
+ intSerializer.serialize(0, out);\r
+ continue;\r
+ }\r
+ \r
+ // Write sample count\r
+ int count = endIndex - startIndex + 1;\r
+ intSerializer.serialize(count, out);\r
+ \r
+ for (int i=0; i<count; i++) {\r
+ // Read sample\r
+ sa.get(i, sampleBinding, sample);\r
+ sampleSerializer.serialize(sample, out);\r
+ } \r
+ } finally {\r
+ sa.close();\r
+ } \r
+ }\r
+ } catch (AccessorException ae) {\r
+ throw new HistoryException( ae );\r
+ } catch (BindingException e) {\r
+ throw new HistoryException( e );\r
+ } catch (SerializerConstructionException e) {\r
+ throw new HistoryException( e );\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Import history from input stream \r
+ * \r
+ * @param history\r
+ * @param is\r
+ * @throws IOException\r
+ * @throws HistoryException\r
+ */\r
+ public void importHistory( HistoryManager history, InputStream is )\r
+ throws IOException, HistoryException\r
+ { \r
+ try {\r
+ List<Object> ids = new ArrayList<Object>();\r
+ BinaryReadable in = new InputStreamReadable( is, Long.MAX_VALUE );\r
+ Bean[] items = (Bean[]) beanArraySerializer.deserialize( in );\r
+ for (Bean item : items) {\r
+ StreamAccessor sa = null;\r
+ try {\r
+ Datatype format = (Datatype) item.getField("format");\r
+ String id = (String) item.getField("id");\r
+ history.create(item);\r
+ RecordBinding sampleBinding = Bindings.getBinding( format );\r
+ Serializer sampleSerializer = Bindings.getSerializer(sampleBinding);\r
+ Object sample = sampleBinding.createDefault();\r
+ sa = history.openStream(id, "rw");\r
+ int count = (Integer) intSerializer.deserialize(in);\r
+ ids.clear();\r
+ for (int i=0; i<count; i++) {\r
+ sample = sampleSerializer.deserializeToTry(in, ids, sample);\r
+ sa.add(sampleBinding, sample);\r
+ }\r
+ }\r
+ finally {\r
+ try {\r
+ if (sa != null)\r
+ sa.close();\r
+ } catch (AccessorException e) {\r
+ }\r
+ }\r
+ }\r
+ } catch (AccessorException ae) {\r
+ throw new HistoryException( ae );\r
+ } catch (BindingException e) {\r
+ throw new HistoryException( e );\r
+ } catch (SerializerConstructionException e) {\r
+ throw new HistoryException( e );\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * An export of the history \r
+ */\r
+ static public class HistoryExport {\r
+ public HistoryAndCollectorItem[] subscriptionItems;\r
+ public Variant[] items;\r
+ /* Variant = {\r
+ int count;\r
+ Sample[] samples;\r
+ }\r
+ */\r
+ }\r
+ \r
+ private static MathContext mc = new MathContext(15); \r
+\r
+ /**\r
+ * Performs a linear interpolation with the specified times and values.\r
+ * @param t1\r
+ * @param v1\r
+ * @param t2\r
+ * @param v2\r
+ * @param ts\r
+ * @return\r
+ */\r
+ public static double biglerp(double t1, double v1, double t2, double v2, double ts) {\r
+ assert (t1 < ts) && (ts < t2);\r
+\r
+ // #11585: Safety for Inf/NaN numbers.\r
+ // BigDecimal throws NumberFormatException for non-finite values.\r
+ if (!Double.isFinite(v1) || !Double.isFinite(v2)) {\r
+ double d1 = ts-t1;\r
+ double d2 = t2-ts;\r
+ return (d1 > d2) ? v1 : v2;\r
+ }\r
+\r
+ BigDecimal T1 = new BigDecimal(-t1, mc);\r
+ BigDecimal T2 = new BigDecimal(t2, mc);\r
+ BigDecimal TS = new BigDecimal(ts, mc);\r
+ BigDecimal T = (TS.add(T1, mc)).divide(T2.add(T1, mc), mc);\r
+\r
+ BigDecimal V1 = new BigDecimal(v1, mc);\r
+ BigDecimal V2 = new BigDecimal(v2, mc);\r
+ BigDecimal VS = V1.add(V2.subtract(V1, mc).multiply(T, mc), mc);\r
+\r
+ return VS.doubleValue();\r
+ }\r
+ \r
+ public static boolean contains(StreamIterator iter, double time) {\r
+ double start = iter.getStartTime();\r
+ double end = iter.getEndTime();\r
+ // A special case, where start == end => accept\r
+ if(time == start) return true;\r
+ else if(time < start) return false;\r
+ else if(time >= end) return false;\r
+ else return true;\r
+ }\r
+\r
+}\r