1 package org.simantics.history.examples;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.Datatypes;
8 import org.simantics.databoard.accessor.StreamAccessor;
9 import org.simantics.databoard.accessor.error.AccessorException;
10 import org.simantics.databoard.binding.Binding;
11 import org.simantics.databoard.binding.RecordBinding;
12 import org.simantics.databoard.binding.error.BindingException;
13 import org.simantics.databoard.type.RecordType;
14 import org.simantics.databoard.util.Bean;
15 import org.simantics.history.History;
16 import org.simantics.history.HistoryException;
17 import org.simantics.history.HistoryItem;
18 import org.simantics.history.HistoryManager;
19 import org.simantics.history.ItemManager;
21 public class HistoryExample1 {
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 hi = new HistoryItem(id, sampleFormat);
39 // Create the item in the history
42 /// Example 2: Write data to history item's stream (without Collector)
44 // Open item for writing
45 StreamAccessor sa = history.openStream(id, "rw");
46 // Create bean based binding for the sample
47 RecordBinding sampleBinding = (RecordBinding) Bindings.getBeanBinding( sampleFormat );
48 // Create sample object
49 Bean sample = (Bean) sampleBinding.createPartial();
53 sample.setField(2, Bindings.BYTE, (byte)0);
56 for (int i=0; i<10; i++) {
59 sample.setField(0, Bindings.DOUBLE, 1.0*i );
61 sample.setField(1, Bindings.DOUBLE, 100.0*i );
63 sa.add(sampleBinding, sample);
71 /// Example 3: Read & print data
73 // Open stream from history
74 StreamAccessor aa = history.openStream(id, "r");
76 RecordType sampleType = (RecordType) aa.type().componentType;
77 // Create bean binding
78 RecordBinding sampleBinding = (RecordBinding) Bindings.getBeanBinding( sampleType );
79 // Create sample object
80 Bean sample = (Bean) sampleBinding.createPartial();
83 for (int i=0; i<aa.size(); i++) {
84 aa.get(i, sampleBinding, sample);
85 System.out.println(i+": "+sample);
92 /// Example 4: To search and read history items
93 // Read items from history. Each item is a record with various fields.
94 // The fields depending on usage and initialization.
96 // If collector is used, it adds a field called "collectorState" that persists
97 // the state data and subscription parameters.
98 Bean[] items = history.getItems();
100 // ItemManager is a utility that helps in the management of items
101 ItemManager im = new ItemManager( items );
103 // Print all item Ids
104 System.out.println("Items Identifiers:");
105 for ( String itemId : im.toIdArray() )
107 System.out.println( itemId );
110 // To search an item by id, use
111 List<Bean> result = im.search("id", id);
113 // To search items of specific parameter, say file format, use
114 im.search("format", sampleFormat);
117 im.search("interval", 0.1);