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