]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/test/org/simantics/history/test/TestHistory.java
Update structure and value cache when refreshing variable
[simantics/platform.git] / bundles / org.simantics.history / test / org / simantics / history / test / TestHistory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.history.test;
13
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.File;
17 import java.io.IOException;
18
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.databoard.Datatypes;
25 import org.simantics.databoard.accessor.StreamAccessor;
26 import org.simantics.databoard.accessor.error.AccessorException;
27 import org.simantics.databoard.binding.Binding;
28 import org.simantics.databoard.binding.impl.DoubleArrayBinding;
29 import org.simantics.databoard.type.ArrayType;
30 import org.simantics.databoard.type.RecordType;
31 import org.simantics.databoard.util.Bean;
32 import org.simantics.databoard.util.Limit;
33 import org.simantics.databoard.util.Range;
34 import org.simantics.history.Collector;
35 import org.simantics.history.History;
36 import org.simantics.history.HistoryException;
37 import org.simantics.history.HistoryManager;
38 import org.simantics.history.csv.CSVFormatter;
39 import org.simantics.history.impl.CollectorImpl;
40 import org.simantics.history.impl.CollectorState;
41 import org.simantics.history.util.ProgressMonitor;
42 import org.simantics.history.util.StreamIterator;
43 import org.simantics.history.util.Stream;
44 import org.simantics.history.util.ValueBand;
45 import org.simantics.history.util.subscription.SubscriptionItem;
46 import org.simantics.history.util.subscription.SamplingFormat;
47 import org.simantics.utils.FileUtils;
48
49 public class TestHistory {
50         
51         static final double NaN = Double.NaN;
52         
53         // Time            0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0  1.1  1.2  1.3  1.4
54         double[] data1 = { 5.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0, 7.0, 8.0, 9.0, NaN,10.0, NaN, NaN, NaN }; 
55         double[] data2 = { 5.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0, 7.0, 8.0, 9.0,10.0, 9.0, 5.0, 4.0, 3.0 };
56         byte[] data3 = { 5, 6, 7, 8, 9, 10, 9, 5, 4, 3 };
57         
58         // Subscription formats
59         SamplingFormat simple, allfields, vector, minmax, byteformat, string;
60
61         // History
62         HistoryManager historian;
63         File workarea;
64         Collector collector;
65
66         @Before 
67         public void initSubscriptionFormats()
68         {
69                 simple = new SamplingFormat();
70                 simple.formatId = "simple";
71                 RecordType format;
72                 format = (RecordType) (simple.format = new RecordType());
73                 format.addComponent("time", Datatypes.FLOAT);
74                 format.addComponent("endTime", Datatypes.FLOAT);
75                 format.addComponent("value", Datatypes.DOUBLE );
76                 format.addComponent("quality", Datatypes.BYTE );
77                 simple.interval = NaN;
78                 simple.deadband = NaN;
79
80                 string = new SamplingFormat();
81                 string.formatId = "string";
82                 format = (RecordType) (string.format = new RecordType());
83                 format.addComponent("time", Datatypes.FLOAT);
84                 format.addComponent("endTime", Datatypes.FLOAT);
85                 format.addComponent("value", Datatypes.STRING );
86                 format.addComponent("quality", Datatypes.BYTE );
87                 string.interval = NaN;
88                 string.deadband = NaN;
89                 
90                 allfields = new SamplingFormat();
91                 allfields.formatId = "alldata"; 
92                 format = (RecordType) (allfields.format = new RecordType());
93                 format.addComponent("time", Datatypes.DOUBLE);
94                 format.addComponent("endTime", Datatypes.DOUBLE);
95                 format.addComponent("value", Datatypes.DOUBLE );
96                 format.addComponent("lastValue", Datatypes.DOUBLE);             
97                 format.addComponent("min", Datatypes.DOUBLE);
98                 format.addComponent("max", Datatypes.DOUBLE);
99                 format.addComponent("avg", Datatypes.DOUBLE);
100                 format.addComponent("median", Datatypes.DOUBLE);
101                 format.addComponent("quality", Datatypes.BYTE );
102                 format.addComponent("count", Datatypes.INTEGER);
103                 allfields.interval = NaN;
104                 allfields.deadband = NaN;               
105
106                 byteformat = new SamplingFormat();
107                 byteformat.formatId = "byte";
108                 byteformat.format = new RecordType();
109                 format = (RecordType) (byteformat.format = new RecordType());
110                 format.addComponent("time", Datatypes.DOUBLE);
111                 format.addComponent("endTime", Datatypes.DOUBLE);
112                 format.addComponent("value", Datatypes.BYTE);
113                 format.addComponent("lastValue", Datatypes.BYTE);               
114                 format.addComponent("min", Datatypes.BYTE);
115                 format.addComponent("max", Datatypes.BYTE);
116                 format.addComponent("avg", Datatypes.DOUBLE);
117                 format.addComponent("median", Datatypes.BYTE);
118                 format.addComponent("quality", Datatypes.BYTE);
119                 format.addComponent("count", Datatypes.INTEGER);
120                 byteformat.interval = NaN;
121                 byteformat.deadband = NaN;              
122                 
123                 vector = new SamplingFormat();
124                 vector.formatId = "vector";
125                 vector.format = new RecordType();
126                 format = (RecordType) (vector.format = new RecordType());
127                 format.addComponent("time", Datatypes.FLOAT);
128                 format.addComponent("endTime", Datatypes.FLOAT);
129                 format.addComponent("value", new ArrayType( Datatypes.DOUBLE, Range.between(Limit.inclusive(3), Limit.inclusive(3)) ));
130                 format.addComponent("count", Datatypes.INTEGER);
131                 vector.interval = NaN;
132                 vector.deadband = NaN;
133                 
134                 minmax = new SamplingFormat();
135                 minmax.formatId = "minmax";
136                 minmax.format = new RecordType();
137                 format = (RecordType) (minmax.format = new RecordType());
138                 format.addComponent("time", Datatypes.FLOAT);
139                 format.addComponent("endTime", Datatypes.FLOAT);
140                 format.addComponent("value", Datatypes.DOUBLE );
141                 format.addComponent("min", Datatypes.DOUBLE );
142                 format.addComponent("max", Datatypes.DOUBLE );
143                 minmax.interval = Double.MAX_VALUE;
144                 minmax.deadband = Double.MAX_VALUE;             
145         }
146
147         @Before
148         public void initHistory() {
149                 CollectorState cs = new CollectorState();
150                 workarea = FileUtils.createTmpDir();
151                 System.out.println(workarea);
152                 historian = History.openFileHistory( workarea );
153 //              historian = History.createMemoryHistory();
154                 collector = new CollectorImpl( historian );
155         }
156         
157         @After 
158         public void uninitHistory() {
159                 if ( collector != null ) {
160                         collector.close();                      
161                 }
162                 if ( historian != null ) {
163                         historian.close();
164                 }
165                 if ( workarea != null) {
166                         try {
167                                 FileUtils.deleteAll(workarea);
168                         } catch (IOException e) {
169                                 e.printStackTrace();
170                         }
171                 }
172         }
173         
174         @Test
175         public void testGetModifySubscription() throws Exception
176         {
177                 // 1. Create Item and Collector
178                 SubscriptionItem hi = SubscriptionItem.createItem("NotMyVariable", "MySubscription", allfields);
179                 historian.create(hi);
180                 collector.addItem(hi);
181                 
182                 // 2. Get Item
183                 SubscriptionItem[] items = collector.getItems();                
184                 Assert.assertEquals(1, items.length);
185                 SubscriptionItem hii = new SubscriptionItem();
186                 hii.readAvailableFields( items[0] );
187                 Assert.assertTrue( hii.equalContents( hi ) );
188                 
189                 // 3. Modify Item
190                 hi.variableId = "MyVariable";
191                 collector.setItem(hi);
192                 items = collector.getItems();           
193                 Assert.assertEquals(1, items.length);
194                 hii.readAvailableFields( items[0] );
195                 Assert.assertTrue( hii.equalContents( hi ) );
196                 
197                 // 4. Remove & Add
198                 collector.removeItem(hi.id);
199                 Assert.assertEquals(0, collector.getItems().length);
200                 collector.addItem(hi);
201                 
202                 // 5. Open & Modify subscription
203                 collector.beginStep(Bindings.DOUBLE, 5.0);
204                 hi.interval = 1.2;
205                 collector.setItem(hi);
206                 collector.endStep();
207                 collector.close();
208
209                 // 6. Read from history
210                 Bean bean = historian.getItem(hi.id);
211                 hi.interval = 0.0;
212                 hi.readAvailableFields(bean);
213                 Assert.assertEquals(1.2, hi.interval, 0.1);
214         }
215
216         @Test
217         public void failtestRecreateSubscription() throws HistoryException
218         {
219                 Collector collector2;
220                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", allfields);
221                 historian.create(hi);
222                 
223                 collector.addItem( hi );
224                 collector2 = new CollectorImpl( historian );
225                 collector2.addItem( hi );
226                 
227                 try {
228                         collector.beginStep(Bindings.DOUBLE, 0.0);
229                         collector2.beginStep(Bindings.DOUBLE, 0.0);
230                         Assert.fail("Recreate subscription should have failed");
231                 } catch (HistoryException e) {
232                         // expected exception
233                 }
234         
235                 collector2.close();
236         }
237
238         @Test
239         public void failtestReopenStream() throws HistoryException
240         {
241                 // 1. Create history and collector item
242                 SamplingFormat format = allfields.clone();
243                 format.deadband = 2.5;
244                 SubscriptionItem[] his = SubscriptionItem.createItems("MyVariable", "MySubscription", allfields, minmax);
245                 historian.create(his);
246                 StreamAccessor sa1=null, sa2=null;
247                 try {
248                         sa1 = historian.openStream(his[0].id, "r");
249                         sa2 = historian.openStream(his[1].id, "r");
250                         Assert.fail("HistoryManager must not allow to open two handles to same subscription");
251                 } catch (HistoryException he) {
252                         // Expected exception
253                 }
254                 try {
255                 if (sa1!=null)sa1.close();
256                 if (sa2!=null) sa2.close();
257                 } catch (AccessorException e) {
258                         throw new HistoryException(e);
259                 }
260                 
261                 historian.delete( his[0].id, his[1].id );
262         }
263         
264         @Test
265         public void testDisableItem() throws Exception {
266                 // 1. Create history and collector item
267                 SamplingFormat format = allfields.clone();
268                 format.deadband = 2.5;
269                 SubscriptionItem[] hi = SubscriptionItem.createItems("MyVariable", "MySubscription", allfields, minmax);
270                 historian.create(hi);           
271                 
272                 // 2. Create collector
273                 collector.addItems(hi);
274                 
275                 // Write data
276                 try {
277                         // Simulate
278                         double[] data = data1;
279                         for (int i=0; i<4; i++) 
280                         {
281                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
282                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
283                                 collector.endStep();
284                         }
285                         
286                         // Disable item
287                         hi[0].enabled = false;
288                         collector.setItem(hi[0]);
289                         for (int i=4; i<12; i++) 
290                         {
291                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
292                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
293                                 collector.endStep();
294                         }
295                         
296                         // Enable item
297                         hi[0].enabled = true;
298                         collector.setItem(hi[0]);
299                         for (int i=12; i<data.length; i++) 
300                         {
301                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
302                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
303                                 collector.endStep();
304                         }                       
305                         
306                 } finally {
307                         collector.close();
308                 }
309                 
310                 
311                 // 3. Verify data
312                 StreamAccessor aa = historian.openStream(hi[0].id, "r");
313                 try {
314                         Binding sampleBinding = Bindings.getBinding( allfields.format );
315                         
316                         // Read and assert the sample
317                         Object sample = aa.get(0, sampleBinding);
318                         ValueBand se = new ValueBand( sampleBinding, sample );
319                         if (se.supportsNullValue()) { 
320                                 Assert.assertEquals(0.0, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
321                                 Assert.assertEquals(0.3, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
322                                 Assert.assertFalse( se.isNullValue() );
323                                 
324                                 aa.get(1, sampleBinding, sample);
325                                 Assert.assertEquals(0.4, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
326                                 Assert.assertEquals(1.1, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
327                                 Assert.assertTrue( se.isNullValue() );
328         
329                                 aa.get(2, sampleBinding, sample);                       
330                                 Assert.assertEquals(1.2, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
331                         }
332                         
333                 } finally {
334                         aa.close();
335                 }
336
337                 // 4. Verify min-max has only 1 sample
338                 aa = historian.openStream(hi[1].id, "r");
339                 try {
340                         Assert.assertEquals(1, aa.size());
341                         Binding sampleBinding = Bindings.getBinding( minmax.format );
342                         
343                         // Read and assert the sample
344                         Object sample = aa.get(0, sampleBinding);
345                         ValueBand se = new ValueBand( sampleBinding, sample );
346                         Assert.assertEquals(0.0, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
347                         Assert.assertEquals(1.4, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
348                         Assert.assertEquals(5.0, (Double) se.getMin( Bindings.DOUBLE ), 0.01);
349                         Assert.assertEquals(10.0, (Double) se.getMax( Bindings.DOUBLE ), 0.01);
350                         
351                 } finally {
352                         aa.close();
353                 }
354         }
355
356         
357         @Test
358         public void testDeadband() throws Exception {
359                 // 1. Create history and collector item
360                 SamplingFormat format = allfields.clone();
361                 format.deadband = 2.5;
362                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
363                 historian.create(hi);           
364                 
365                 // 2. Create collector
366                 collector.addItem(hi);          
367                 try {
368                         // Add values, dt=0.1
369                         double[] data = data2;
370                         for (int i=0; i<data.length; i++) 
371                         {
372                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
373                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
374                                 collector.endStep();
375                         }                       
376                 } finally {
377                         collector.close();
378                 }
379
380                 // 3. Verify data
381                 StreamAccessor aa = historian.openStream(hi.id, "r");           
382                 try {
383                         Binding sampleBinding = Bindings.getBinding( format.format );
384                         // Read and assert the sample
385                         Object sample = aa.get(0, sampleBinding);
386                         ValueBand se = new ValueBand( sampleBinding, sample );
387                         Assert.assertEquals(0.0, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
388                         Assert.assertEquals(0.7, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
389                         Assert.assertEquals(5.0, (Double) se.getValue( Bindings.DOUBLE ), 0.01);
390                         Assert.assertEquals(5.0, (Double) se.getMedian( Bindings.DOUBLE ), 0.01);
391                         Assert.assertEquals(5.0, (Double) se.getMin( Bindings.DOUBLE ), 0.01);
392                         Assert.assertEquals(7.0, (Double) se.getMax( Bindings.DOUBLE ), 0.01);
393                         Assert.assertEquals(8, se.getCount());
394                         
395                         aa.get(1, sampleBinding, sample);
396                         Assert.assertEquals(0.8, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
397                         Assert.assertEquals(1.1, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
398                         Assert.assertEquals(8.0, (Double) se.getValue( Bindings.DOUBLE ), 0.01);
399                         Assert.assertEquals(9.0, (Double) se.getMedian( Bindings.DOUBLE ), 0.01);
400                         Assert.assertEquals(8.0, (Double) se.getMin( Bindings.DOUBLE ), 0.01);
401                         Assert.assertEquals(10.0, (Double) se.getMax( Bindings.DOUBLE ), 0.01);
402                         Assert.assertEquals(4, se.getCount());
403
404                         aa.get(2, sampleBinding, sample);
405                         Assert.assertEquals(1.2, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
406                         Assert.assertEquals(1.4, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);                      
407                         Assert.assertEquals(5.0, (Double) se.getValue( Bindings.DOUBLE ), 0.01);
408                         Assert.assertEquals(4.0, (Double) se.getMedian( Bindings.DOUBLE ), 0.01);
409                         Assert.assertEquals(3.0, (Double) se.getMin( Bindings.DOUBLE ), 0.01);
410                         Assert.assertEquals(5.0, (Double) se.getMax( Bindings.DOUBLE ), 0.01);
411                         Assert.assertEquals(3, se.getCount());
412                 } finally {
413                         aa.close();
414                 }
415         }
416
417         
418         @Test
419         public void testMedianAndInterval() throws Exception {
420
421                 // 1. Create history and collector item
422                 SamplingFormat format = allfields.clone();
423                 format.interval = 1.0;
424                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
425                 historian.create(hi);
426                 // Create binding for the sample format
427                 Binding sampleBinding = Bindings.getBinding( format.format );
428                 
429                 
430                 // 2. Create collector
431                 collector.addItem( hi );
432
433                 // Write data
434                 try {
435                         // Add values, dt=0.1
436                         double[] data = data2;
437                         for (int i=0; i<data.length; i++) 
438                         {
439                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
440                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
441                                 collector.endStep();
442                         }                       
443                 } finally {
444                         collector.close();
445                 }
446                 
447                 // Verify data
448                 StreamAccessor aa = historian.openStream(hi.id, "r");           
449                 try {
450                         // 10s interval. There should be only one entry
451                         Assert.assertEquals(2, aa.size());
452                         
453                         // Read and assert the sample
454                         Object sample = aa.get(0, sampleBinding);
455                         ValueBand se = new ValueBand( sampleBinding, sample );
456                         Assert.assertEquals(0.0, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
457                         Assert.assertEquals(0.9, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
458                         Assert.assertEquals(6.0, (Double) se.getMedian( Bindings.DOUBLE ), 0.01);
459                         
460                         aa.get(1, sampleBinding, sample);
461                         Assert.assertEquals(1.0, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
462                         Assert.assertEquals(1.4, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
463                         Assert.assertEquals(5.0, (Double) se.getMedian( Bindings.DOUBLE ), 0.01);
464                         
465                 } finally {
466                         aa.close();
467                 }
468                 
469                 // Reopen subscription
470                 // Write data
471                 collector = new CollectorImpl( historian );                             
472                 try {
473                         // Add values, dt=0.1
474                         double[] data = data2;
475                         for (int i=0; i<data.length; i++) 
476                         {
477                                 collector.beginStep(Bindings.DOUBLE, (i+data.length) * 0.1);
478                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
479                                 collector.endStep();
480                         }                       
481                 } finally {
482                         collector.close();
483                 }
484                 
485                 // Verify data
486                 aa = historian.openStream(hi.id, "r");          
487                 try {
488                         Assert.assertEquals(3, aa.size());
489                         
490                         // Read and assert the sample
491                         Object sample = aa.get(0, sampleBinding);
492                         ValueBand se = new ValueBand( sampleBinding, sample );
493                         Assert.assertEquals(0.0, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
494                         Assert.assertEquals(0.9, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
495                         Assert.assertEquals(6.0, (Double) se.getMedian( Bindings.DOUBLE ), 0.01);
496                         
497                         aa.get(1, sampleBinding, sample);
498                         Assert.assertEquals(1.0, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
499                         Assert.assertEquals(2.1, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
500                         Assert.assertEquals(5.0, (Double) se.getMedian( Bindings.DOUBLE ), 0.01);
501                         
502                         aa.get(2, sampleBinding, sample);
503                         Assert.assertEquals(2.2, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
504                         Assert.assertEquals(2.9, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
505                         Assert.assertEquals(7.0, (Double) se.getMedian( Bindings.DOUBLE ), 0.01);
506                         
507                 } finally {
508                         aa.close();
509                 }
510                                 
511                 historian.delete(hi.id);
512         }
513
514         @Test
515         public void testAccessor() throws Exception
516         {
517                 // 1. Create history and collector item
518                 SamplingFormat format = allfields.clone();
519                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
520                 historian.create(hi);
521                 
522                 // 2. Create collector
523                 collector.addItem(hi);
524                 StreamAccessor aa = historian.openStream(hi.id, "r");           
525                 try {
526                         // Write data
527                         double[] data = data2;
528                         for (int i=0; i<data.length; i++) 
529                         {
530                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
531                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
532                                 collector.endStep();
533                         }                       
534
535                         // Assert data
536                         aa.reset();
537                         Assert.assertEquals(10, aa.size());
538                         
539                         // Write some more
540                         for (int i=0; i<data.length; i++) 
541                         {
542                                 collector.beginStep(Bindings.DOUBLE, (i + data.length) * 0.1);
543                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
544                                 collector.endStep();
545                         }                       
546                         
547                         // Assert data
548                         aa.reset();
549                         Assert.assertEquals(20, aa.size());
550
551                         // Print
552                         Binding sampleBinding = Bindings.getBinding( format.format );                   
553                         for (int i=0; i<aa.size(); i++) {
554                                 System.out.println( i+": "+sampleBinding.toString( aa.get(i, sampleBinding) ) );
555                         }
556                         
557                         
558                 } finally {
559                         aa.close();
560                         collector.close();
561                 }
562                 
563         }
564
565         @Test
566         public void testArrayData() throws Exception
567         {
568                 // 1. Create history and collector item
569                 SamplingFormat format = vector.clone();
570                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
571                 historian.create(hi);
572                 
573                 // 2. Create collector
574                 collector.addItem( hi );
575                 ArrayType vectorType = (ArrayType) ((RecordType)format.format).getComponent("value").type;
576                 Binding vectorBinding = DoubleArrayBinding.createFrom( vectorType );
577                 
578                 // Write data
579                 int count = 20;
580                 try {
581                         // Simulate
582                         double[] vector = new double[] { 1.0, 2.0, 3.0 };
583                         
584                         for (int i=0; i<count; i++) 
585                         {
586                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
587                                 
588                                 vector[0] = i;
589                                 vector[1] = i*1.5+1;
590                                 vector[2] = i*-2.4+5;
591                                 
592                                 collector.setValue("MyVariable", vectorBinding, vector);
593                                 collector.endStep();
594                         }
595                         
596                 } finally {
597                         collector.close();
598                 }
599                 
600                 StreamAccessor aa = historian.openStream(hi.id, "r");
601                 try {
602                         Assert.assertEquals(count, aa.size());
603                 } finally {
604                         aa.close();
605                 }
606                 
607                 historian.delete(hi.id);
608         }
609
610         @Test
611         public void testStringData() throws Exception
612         {
613                 // 1. Create history and collector item
614                 SamplingFormat format = string.clone();
615                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
616                 historian.create(hi);
617                 
618                 // 2. Create collector
619                 collector.addItem( hi );
620                 Binding lineBinding = Bindings.STRING;
621                 
622                 // Write data
623                 int count = 20;
624                 try {
625                         // Simulate
626                         for (int i=0; i<count; i++) 
627                         {
628                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
629                                 collector.setValue("MyVariable", lineBinding, "Line: "+i);
630                                 collector.endStep();
631                         }
632                         
633                 } finally {
634                         collector.close();
635                 }
636                 
637                 StreamAccessor aa = historian.openStream(hi.id, "rw");
638                 try {
639                         // Assert data ok
640                         Binding beanBinding = Bindings.getBeanBinding(hi.format);
641                         Assert.assertEquals(count, aa.size());
642                         for (int i=0; i<count; i++) {
643                                 Bean bean = (Bean) aa.get(i, beanBinding);
644                                 String line = (String) bean.getField("value");
645                                 Assert.assertEquals("Line: "+i, line);
646                         }
647                         
648                         // Delete entries
649                         aa.remove(10, 2);
650                         Assert.assertEquals(count-2, aa.size());
651                         for (int i=0; i<10; i++) {
652                                 Bean bean = (Bean) aa.get(i, beanBinding);
653                                 String line = (String) bean.getField("value");
654                                 Assert.assertEquals("Line: "+i, line);
655                         }
656                         for (int i=10; i<18; i++) {
657                                 Bean bean = (Bean) aa.get(i, beanBinding);
658                                 String line = (String) bean.getField("value");
659                                 Assert.assertEquals("Line: "+(i+2), line);
660                         }
661                         
662                 } finally {
663                         aa.close();
664                 }
665                 
666                 historian.delete(hi.id);
667         }
668         
669
670         @Test
671         public void testMinMax() throws Exception {
672                 // 1. Create history and collector item
673                 SamplingFormat format = minmax.clone();
674                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
675                 historian.create(hi);
676                 
677                 // 2. Create collector
678                 collector.addItem( hi );
679
680                 // 3. Write data
681                 try {
682                         // Add values, dt=0.1
683                         double[] data = data1;
684                         for (int i=0; i<data.length; i++) 
685                         {
686                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
687                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
688                                 collector.endStep();
689                         }                       
690                 } finally {
691                         collector.close();
692                 }
693
694                 // 3. Verify data
695                 StreamAccessor aa = historian.openStream(hi.id, "r");
696                 try {
697                         Assert.assertEquals(1, aa.size());
698                         Binding sampleBinding = Bindings.getBinding( format.format );
699                         // Read and assert the sample
700                         Object sample = aa.get(0, sampleBinding);
701                         ValueBand se = new ValueBand( sampleBinding, sample );
702                         Assert.assertEquals(0.0, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
703                         Assert.assertEquals(1.4, (Double) se.getEndTime( Bindings.DOUBLE ), 0.01);
704                         Assert.assertEquals(5.0, (Double) se.getMin( Bindings.DOUBLE ), 0.01);
705                         Assert.assertEquals(10.0, (Double) se.getMax( Bindings.DOUBLE ), 0.01);
706                 } finally {
707                         aa.close();
708                 }
709                 
710         }
711         
712         
713         public @Test void testStream() throws Exception {
714                 // 1. Create history and collector item
715                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", SamplingFormat.allfields);
716                 historian.create(hi);
717                 
718                 // 2. Create collector
719                 collector.addItem( hi );
720
721                 // 3. Write and assert data
722                 StreamAccessor aa = historian.openStream(hi.id, "r");
723                 try {
724                         // Write data
725                         double[] data = data2;
726                         for (int i=0; i<data.length; i++) 
727                         {
728                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
729                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
730                                 collector.endStep();
731                         }                       
732                         
733                         // Assert data
734                         aa.reset();
735                         Stream stream = new Stream(aa);
736                         
737                         Object sample0 = aa.get(0, stream.sampleBinding);
738                         Object sample1 = aa.get(1, stream.sampleBinding);
739                         Object sample;
740                         
741                         sample = stream.getLowerSample(Bindings.DOUBLE, 0.0);
742                         Assert.assertNull(sample);
743                         
744                         sample = stream.getLowerSample(Bindings.DOUBLE, 0.05);
745                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
746
747                         sample = stream.getFloorSample(Bindings.DOUBLE, 0.05);
748                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
749
750                         sample = stream.getFloorSample(Bindings.DOUBLE, 0.00);
751                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
752                         
753                         sample = stream.getSample(Bindings.DOUBLE, 0.05);
754                         Assert.assertNull(sample);
755
756                         sample = stream.getSample(Bindings.DOUBLE, 0.00);
757                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
758
759                         sample = stream.getCeilingSample(Bindings.DOUBLE, -0.10);
760                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
761                         
762                         sample = stream.getCeilingSample(Bindings.DOUBLE, 0.00);
763                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
764
765                         sample = stream.getCeilingSample(Bindings.DOUBLE, 0.05);
766                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample1) );
767
768                         sample = stream.getHigherSample(Bindings.DOUBLE, -0.05);
769                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
770
771                         sample = stream.getHigherSample(Bindings.DOUBLE, 0.00);
772                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample1) );
773                         
774                         sample = stream.getHigherSample(Bindings.DOUBLE, 0.05);
775                         Assert.assertTrue( stream.sampleBinding.equals(sample, sample1) );
776                         
777                         sample = stream.getHigherSample(Bindings.DOUBLE, 1.45);
778                         Assert.assertNull( sample );
779                 } finally {
780                         aa.close();
781                         collector.close();
782                 }
783         }
784         
785
786         @Test
787         public void testByteData() throws Exception {
788                 // 1. Create stream
789                 SamplingFormat format = byteformat.clone();
790                 format.formatId = "test";
791                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
792                 historian.create( hi );
793                 
794                 // 2. Create collector
795                 collector.addItem(hi);
796
797                 // 3. Write and assert data
798                 try {
799                         // Write data
800                         double[] data = data2;
801                         for (int i=0; i<data.length; i++) 
802                         {
803                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
804                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
805                                 collector.endStep();
806                         }                       
807         
808                         // Verify data
809                         StreamAccessor aa = historian.openStream(hi.id, "r");                           
810                         try {
811                                 Assert.assertEquals(data3.length, aa.size());
812                                 Binding sampleBinding = Bindings.getBinding( format.format );
813                                 // Read and assert the sample
814                                 Object sample = aa.get(0, sampleBinding);
815                                 ValueBand se = new ValueBand( sampleBinding, sample );
816                                 for (int i=0; i<data3.length; i++) {
817                                         aa.get(i, sampleBinding, se.getSample());
818                                         Assert.assertEquals(data3[i], se.getValue(Bindings.BYTE));
819                                 }
820                         } finally {
821                                 aa.close();
822                         }                       
823                         
824                 } finally {
825                         collector.close();
826                 }
827                 
828         }
829         
830         @Test
831         public void testCSVData() throws Exception {
832                 SamplingFormat format = simple.clone();
833                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format );
834                 historian.create( hi );
835                 collector.addItem( hi );
836                 try {
837                         // Write data
838                         double[] data = data1;
839                         for (int i=0; i<data.length; i++) 
840                         {
841                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
842                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
843                                 collector.endStep();
844                         }                       
845                 } finally {
846                         collector.close();
847                 }
848                 
849                 CSVFormatter csv = new CSVFormatter();
850                 csv.addItem(historian, hi.id, "MyVariable", "MyVariable", "m");
851                 csv.setTimeRange(0.9, 1.5);
852                 csv.setTimeStep(0.1);
853                 StringBuilder sb = new StringBuilder();
854 //              csv.formulate1(new ProgressMonitor.Stub(), sb);
855 //              sb.append("\n----\n");
856                 csv.formulate2(new ProgressMonitor.Stub(), sb);
857                 System.out.println(sb);
858                 
859         }
860         
861         @Test
862         public void testExport() throws Exception {
863                 SamplingFormat format = byteformat.clone();
864                 
865                 // 2. Write data
866                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", byteformat );
867                 historian.create( hi );
868                 collector.addItem( hi );
869                 try {
870                         // Write data
871                         double[] data = data2;
872                         for (int i=0; i<data.length; i++) 
873                         {
874                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
875                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
876                                 collector.endStep();
877                         }                       
878                 } finally {
879                         collector.close();
880                 }
881                 
882                 // Export data
883                 ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
884                 History.exportHistory(historian, Bindings.DOUBLE, -Double.MAX_VALUE, Double.MAX_VALUE, os);
885         
886                 // Create another historian
887                 HistoryManager historian2 = History.createMemoryHistory();
888                 ByteArrayInputStream is = new ByteArrayInputStream( os.toByteArray() );
889                 History.importHistory(historian2, is);
890                 
891                 // Verify data
892                 StreamAccessor aa = historian2.openStream(hi.id, "r");                          
893                 try {
894                         Assert.assertEquals(data3.length, aa.size());
895                         Binding sampleBinding = Bindings.getBinding( format.format );
896                         // Read and assert the sample
897                         Object sample = aa.get(0, sampleBinding);
898                         ValueBand se = new ValueBand( sampleBinding, sample );
899                         for (int i=0; i<data3.length; i++) {
900                                 aa.get(i, sampleBinding, se.getSample());
901                                 Assert.assertEquals(data3[i], se.getValue(Bindings.BYTE));
902                         }
903                 } finally {
904                         aa.close();
905                 }                       
906         }
907         
908         
909         @Test
910         public void testStreamIterator() throws Exception {
911                 // 1. Create history and collector item
912                 SamplingFormat format = allfields.clone();
913                 format.deadband = 0.05;
914                 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
915                 historian.create(hi);           
916                 
917                 // 2. Create collector
918                 collector.addItem(hi);          
919                 try {
920                         // Add values, dt=0.1
921                         double[] data = data1;
922                         for (int i=0; i<data.length; i++) 
923                         {
924                                 collector.beginStep(Bindings.DOUBLE, i * 0.1);
925                                 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
926                                 collector.endStep();
927                         }                       
928                 } finally {
929                         collector.close();
930                 }
931
932                 // 3. Verify data
933                 StreamAccessor aa = historian.openStream(hi.id, "r");           
934                 try {
935                         StreamIterator si = new StreamIterator( aa );
936                         while (si.hasNext()) {
937                                 si.next();
938                                 System.out.println( si );
939                         }
940                         
941                         si.gotoTime( -1 );
942                         System.out.println( si );
943                         
944                         si.gotoTime( 0.55 );
945                         System.out.println( si );
946                         
947                         si.proceedToTime( 0.6 );
948                         System.out.println( si );
949                         
950                         si.proceedToTime( 1.11 );
951                         System.out.println( si );
952
953                         si.gotoTime( 1.4 );
954                         System.out.println( si );
955                         
956                         si.gotoTime( 1.5 );
957                         System.out.println( si );
958                         
959                         
960                 } finally {
961                         aa.close();
962                 }
963         }
964         
965 }