1 package org.simantics.history.examples;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.databoard.Datatypes;
7 import org.simantics.databoard.accessor.StreamAccessor;
8 import org.simantics.databoard.accessor.error.AccessorException;
9 import org.simantics.databoard.binding.RecordBinding;
10 import org.simantics.databoard.binding.error.BindingException;
11 import org.simantics.databoard.type.RecordType;
12 import org.simantics.databoard.util.Bean;
13 import org.simantics.history.Collector;
14 import org.simantics.history.History;
15 import org.simantics.history.HistoryAndCollectorItem;
16 import org.simantics.history.HistoryException;
17 import org.simantics.history.HistoryItem;
18 import org.simantics.history.HistoryManager;
19 import org.simantics.history.impl.FlushPolicy;
21 public class CollectorExample1 {
23 public static void main(String[] args) throws HistoryException, BindingException, AccessorException {
25 /// Example1: Create History Manager & Item
26 // Create History Manager
27 HistoryManager history = History.createMemoryHistory();
29 // Create a sample format
30 RecordType sampleFormat = new RecordType();
31 sampleFormat.addComponent("time", Datatypes.DOUBLE );
32 sampleFormat.addComponent("value", Datatypes.DOUBLE );
33 sampleFormat.addComponent("quality", Datatypes.BYTE );
35 // Create History Item configuration
36 String id = UUID.randomUUID().toString();
37 HistoryItem historyItem = new HistoryItem(id, sampleFormat);
39 // Create the item in the history
40 history.create( historyItem );
46 /// Example2: Prepare Collector
47 Collector collector = History.createCollector(history, FlushPolicy.NoFlush);
49 // Read item configuration data from history ( id and sampleFormat )
50 Bean hi = history.getItem( id );
52 // Create collector item
53 HistoryAndCollectorItem ci = new HistoryAndCollectorItem();
54 ci.readAvailableFields( hi ); // Read: id, sampleFormat
59 ci.variableId = "MyItem";
61 // Initialize Collector (Item)
62 collector.addItem( ci );
66 for ( int i = 0; i<100; i++ ) {
68 // Our "simulation" results
69 double time = i * 0.1;
70 double myItemValue = -20 + i*0.1;
72 // Begin collector writing step
73 collector.beginStep(Bindings.DOUBLE, time);
76 collector.setValue("MyItem", Bindings.DOUBLE, myItemValue);
91 // Open stream from history
92 StreamAccessor aa = history.openStream(id, "r");
94 RecordType sampleType = (RecordType) aa.type().componentType;
95 // Create bean binding
96 RecordBinding sampleBinding = (RecordBinding) Bindings.getBeanBinding( sampleType );
97 // Create sample object
98 Bean sample = (Bean) sampleBinding.createPartial();
101 for (int i=0; i<aa.size(); i++) {
102 aa.get(i, sampleBinding, sample);
103 System.out.println(i+": "+sample);