]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/examples/org/simantics/history/examples/HistoryExample1.java
Removed graph compilation nature from org.simantics.jdbc
[simantics/platform.git] / bundles / org.simantics.history / examples / org / simantics / history / examples / HistoryExample1.java
1 package org.simantics.history.examples;
2
3 import java.util.List;
4 import java.util.UUID;
5
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;
20
21 public class HistoryExample1 {
22
23         public static void main(String[] args) throws HistoryException, BindingException, AccessorException {
24                 
25                 /// Example1: Create History Manager & Item
26                 // Create History Manager
27                 HistoryManager history = History.createMemoryHistory();
28                 
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 );
34                 
35                 // Create History Item configuration
36                 String id = UUID.randomUUID().toString();
37                 HistoryItem hi = new HistoryItem(id, sampleFormat);
38                 
39                 // Create the item in the history
40                 history.create( hi );
41                 
42                 /// Example 2: Write data to history item's stream (without Collector)
43                 {
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();
50                         try {
51                                 
52                                 // Set quality
53                                 sample.setField(2, Bindings.BYTE, (byte)0);
54                                 
55                                 // Write 10 samples
56                                 for (int i=0; i<10; i++) {
57                                         
58                                         // Set time
59                                         sample.setField(0, Bindings.DOUBLE, 1.0*i );
60                                         // Set Value
61                                         sample.setField(1, Bindings.DOUBLE, 100.0*i );
62                                         
63                                         sa.add(sampleBinding, sample);                          
64                                 }
65                         } finally {
66                                 sa.flush();
67                                 sa.close();
68                         }
69                 }
70                 
71                 /// Example 3: Read & print data
72                 {
73                         // Open stream from history
74                         StreamAccessor aa = history.openStream(id, "r");
75                         // Read sample type
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();
81                         
82                         try {
83                                 for (int i=0; i<aa.size(); i++) {
84                                         aa.get(i, sampleBinding, sample);
85                                         System.out.println(i+": "+sample);
86                                 }
87                         } finally {
88                                 aa.close();
89                         }
90                 }
91                 
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.
95                 //
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();
99                 
100                 // ItemManager is a utility that helps in the management of items 
101                 ItemManager im = new ItemManager( items );
102                         
103                 // Print all item Ids
104                 System.out.println("Items Identifiers:");
105                 for ( String itemId : im.toIdArray() )
106                 {
107                         System.out.println( itemId );
108                 }
109                 
110                 // To search an item by id, use
111                 List<Bean> result = im.search("id", id);
112                 
113                 // To search items of specific parameter, say file format, use
114                 im.search("format", sampleFormat);
115                 
116                 // Or interval
117                 im.search("interval", 0.1);
118                 
119         }
120         
121 }