1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.history.test;
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
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;
49 public class TestHistory {
51 static final double NaN = Double.NaN;
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 };
58 // Subscription formats
59 SamplingFormat simple, allfields, vector, minmax, byteformat, string;
62 HistoryManager historian;
67 public void initSubscriptionFormats()
69 simple = new SamplingFormat();
70 simple.formatId = "simple";
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;
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;
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;
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;
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;
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;
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 );
158 public void uninitHistory() {
159 if ( collector != null ) {
162 if ( historian != null ) {
165 if ( workarea != null) {
167 FileUtils.deleteAll(workarea);
168 } catch (IOException e) {
175 public void testGetModifySubscription() throws Exception
177 // 1. Create Item and Collector
178 SubscriptionItem hi = SubscriptionItem.createItem("NotMyVariable", "MySubscription", allfields);
179 historian.create(hi);
180 collector.addItem(hi);
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 ) );
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 ) );
198 collector.removeItem(hi.id);
199 Assert.assertEquals(0, collector.getItems().length);
200 collector.addItem(hi);
202 // 5. Open & Modify subscription
203 collector.beginStep(Bindings.DOUBLE, 5.0);
205 collector.setItem(hi);
209 // 6. Read from history
210 Bean bean = historian.getItem(hi.id);
212 hi.readAvailableFields(bean);
213 Assert.assertEquals(1.2, hi.interval, 0.1);
217 public void failtestRecreateSubscription() throws HistoryException
219 Collector collector2;
220 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", allfields);
221 historian.create(hi);
223 collector.addItem( hi );
224 collector2 = new CollectorImpl( historian );
225 collector2.addItem( hi );
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
239 public void failtestReopenStream() throws HistoryException
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;
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
255 if (sa1!=null)sa1.close();
256 if (sa2!=null) sa2.close();
257 } catch (AccessorException e) {
258 throw new HistoryException(e);
261 historian.delete( his[0].id, his[1].id );
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);
272 // 2. Create collector
273 collector.addItems(hi);
278 double[] data = data1;
279 for (int i=0; i<4; i++)
281 collector.beginStep(Bindings.DOUBLE, i * 0.1);
282 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
287 hi[0].enabled = false;
288 collector.setItem(hi[0]);
289 for (int i=4; i<12; i++)
291 collector.beginStep(Bindings.DOUBLE, i * 0.1);
292 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
297 hi[0].enabled = true;
298 collector.setItem(hi[0]);
299 for (int i=12; i<data.length; i++)
301 collector.beginStep(Bindings.DOUBLE, i * 0.1);
302 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
312 StreamAccessor aa = historian.openStream(hi[0].id, "r");
314 Binding sampleBinding = Bindings.getBinding( allfields.format );
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() );
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() );
329 aa.get(2, sampleBinding, sample);
330 Assert.assertEquals(1.2, (Double) se.getTime( Bindings.DOUBLE ), 0.01);
337 // 4. Verify min-max has only 1 sample
338 aa = historian.openStream(hi[1].id, "r");
340 Assert.assertEquals(1, aa.size());
341 Binding sampleBinding = Bindings.getBinding( minmax.format );
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);
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);
365 // 2. Create collector
366 collector.addItem(hi);
368 // Add values, dt=0.1
369 double[] data = data2;
370 for (int i=0; i<data.length; i++)
372 collector.beginStep(Bindings.DOUBLE, i * 0.1);
373 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
381 StreamAccessor aa = historian.openStream(hi.id, "r");
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());
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());
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());
419 public void testMedianAndInterval() throws Exception {
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 );
430 // 2. Create collector
431 collector.addItem( hi );
435 // Add values, dt=0.1
436 double[] data = data2;
437 for (int i=0; i<data.length; i++)
439 collector.beginStep(Bindings.DOUBLE, i * 0.1);
440 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
448 StreamAccessor aa = historian.openStream(hi.id, "r");
450 // 10s interval. There should be only one entry
451 Assert.assertEquals(2, aa.size());
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);
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);
469 // Reopen subscription
471 collector = new CollectorImpl( historian );
473 // Add values, dt=0.1
474 double[] data = data2;
475 for (int i=0; i<data.length; i++)
477 collector.beginStep(Bindings.DOUBLE, (i+data.length) * 0.1);
478 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
486 aa = historian.openStream(hi.id, "r");
488 Assert.assertEquals(3, aa.size());
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);
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);
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);
511 historian.delete(hi.id);
515 public void testAccessor() throws Exception
517 // 1. Create history and collector item
518 SamplingFormat format = allfields.clone();
519 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
520 historian.create(hi);
522 // 2. Create collector
523 collector.addItem(hi);
524 StreamAccessor aa = historian.openStream(hi.id, "r");
527 double[] data = data2;
528 for (int i=0; i<data.length; i++)
530 collector.beginStep(Bindings.DOUBLE, i * 0.1);
531 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
537 Assert.assertEquals(10, aa.size());
540 for (int i=0; i<data.length; i++)
542 collector.beginStep(Bindings.DOUBLE, (i + data.length) * 0.1);
543 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
549 Assert.assertEquals(20, aa.size());
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) ) );
566 public void testArrayData() throws Exception
568 // 1. Create history and collector item
569 SamplingFormat format = vector.clone();
570 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
571 historian.create(hi);
573 // 2. Create collector
574 collector.addItem( hi );
575 ArrayType vectorType = (ArrayType) ((RecordType)format.format).getComponent("value").type;
576 Binding vectorBinding = DoubleArrayBinding.createFrom( vectorType );
582 double[] vector = new double[] { 1.0, 2.0, 3.0 };
584 for (int i=0; i<count; i++)
586 collector.beginStep(Bindings.DOUBLE, i * 0.1);
590 vector[2] = i*-2.4+5;
592 collector.setValue("MyVariable", vectorBinding, vector);
600 StreamAccessor aa = historian.openStream(hi.id, "r");
602 Assert.assertEquals(count, aa.size());
607 historian.delete(hi.id);
611 public void testStringData() throws Exception
613 // 1. Create history and collector item
614 SamplingFormat format = string.clone();
615 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
616 historian.create(hi);
618 // 2. Create collector
619 collector.addItem( hi );
620 Binding lineBinding = Bindings.STRING;
626 for (int i=0; i<count; i++)
628 collector.beginStep(Bindings.DOUBLE, i * 0.1);
629 collector.setValue("MyVariable", lineBinding, "Line: "+i);
637 StreamAccessor aa = historian.openStream(hi.id, "rw");
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);
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);
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);
666 historian.delete(hi.id);
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);
677 // 2. Create collector
678 collector.addItem( hi );
682 // Add values, dt=0.1
683 double[] data = data1;
684 for (int i=0; i<data.length; i++)
686 collector.beginStep(Bindings.DOUBLE, i * 0.1);
687 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
695 StreamAccessor aa = historian.openStream(hi.id, "r");
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);
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);
718 // 2. Create collector
719 collector.addItem( hi );
721 // 3. Write and assert data
722 StreamAccessor aa = historian.openStream(hi.id, "r");
725 double[] data = data2;
726 for (int i=0; i<data.length; i++)
728 collector.beginStep(Bindings.DOUBLE, i * 0.1);
729 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
735 Stream stream = new Stream(aa);
737 Object sample0 = aa.get(0, stream.sampleBinding);
738 Object sample1 = aa.get(1, stream.sampleBinding);
741 sample = stream.getLowerSample(Bindings.DOUBLE, 0.0);
742 Assert.assertNull(sample);
744 sample = stream.getLowerSample(Bindings.DOUBLE, 0.05);
745 Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
747 sample = stream.getFloorSample(Bindings.DOUBLE, 0.05);
748 Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
750 sample = stream.getFloorSample(Bindings.DOUBLE, 0.00);
751 Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
753 sample = stream.getSample(Bindings.DOUBLE, 0.05);
754 Assert.assertNull(sample);
756 sample = stream.getSample(Bindings.DOUBLE, 0.00);
757 Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
759 sample = stream.getCeilingSample(Bindings.DOUBLE, -0.10);
760 Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
762 sample = stream.getCeilingSample(Bindings.DOUBLE, 0.00);
763 Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
765 sample = stream.getCeilingSample(Bindings.DOUBLE, 0.05);
766 Assert.assertTrue( stream.sampleBinding.equals(sample, sample1) );
768 sample = stream.getHigherSample(Bindings.DOUBLE, -0.05);
769 Assert.assertTrue( stream.sampleBinding.equals(sample, sample0) );
771 sample = stream.getHigherSample(Bindings.DOUBLE, 0.00);
772 Assert.assertTrue( stream.sampleBinding.equals(sample, sample1) );
774 sample = stream.getHigherSample(Bindings.DOUBLE, 0.05);
775 Assert.assertTrue( stream.sampleBinding.equals(sample, sample1) );
777 sample = stream.getHigherSample(Bindings.DOUBLE, 1.45);
778 Assert.assertNull( sample );
787 public void testByteData() throws Exception {
789 SamplingFormat format = byteformat.clone();
790 format.formatId = "test";
791 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", format);
792 historian.create( hi );
794 // 2. Create collector
795 collector.addItem(hi);
797 // 3. Write and assert data
800 double[] data = data2;
801 for (int i=0; i<data.length; i++)
803 collector.beginStep(Bindings.DOUBLE, i * 0.1);
804 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
809 StreamAccessor aa = historian.openStream(hi.id, "r");
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));
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 );
838 double[] data = data1;
839 for (int i=0; i<data.length; i++)
841 collector.beginStep(Bindings.DOUBLE, i * 0.1);
842 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
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);
862 public void testExport() throws Exception {
863 SamplingFormat format = byteformat.clone();
866 SubscriptionItem hi = SubscriptionItem.createItem("MyVariable", "MySubscription", byteformat );
867 historian.create( hi );
868 collector.addItem( hi );
871 double[] data = data2;
872 for (int i=0; i<data.length; i++)
874 collector.beginStep(Bindings.DOUBLE, i * 0.1);
875 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
883 ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
884 History.exportHistory(historian, Bindings.DOUBLE, -Double.MAX_VALUE, Double.MAX_VALUE, os);
886 // Create another historian
887 HistoryManager historian2 = History.createMemoryHistory();
888 ByteArrayInputStream is = new ByteArrayInputStream( os.toByteArray() );
889 History.importHistory(historian2, is);
892 StreamAccessor aa = historian2.openStream(hi.id, "r");
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));
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);
917 // 2. Create collector
918 collector.addItem(hi);
920 // Add values, dt=0.1
921 double[] data = data1;
922 for (int i=0; i<data.length; i++)
924 collector.beginStep(Bindings.DOUBLE, i * 0.1);
925 collector.setValue("MyVariable", Bindings.DOUBLE, data[i]);
933 StreamAccessor aa = historian.openStream(hi.id, "r");
935 StreamIterator si = new StreamIterator( aa );
936 while (si.hasNext()) {
938 System.out.println( si );
942 System.out.println( si );
945 System.out.println( si );
947 si.proceedToTime( 0.6 );
948 System.out.println( si );
950 si.proceedToTime( 1.11 );
951 System.out.println( si );
954 System.out.println( si );
957 System.out.println( si );