]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/examples/org/simantics/history/examples/CollectorExample1.java
Removed graph compilation nature from org.simantics.jdbc
[simantics/platform.git] / bundles / org.simantics.history / examples / org / simantics / history / examples / CollectorExample1.java
1 package org.simantics.history.examples;
2
3 import java.util.UUID;
4
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;
20
21 public class CollectorExample1 {
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 historyItem = new HistoryItem(id, sampleFormat);
38                 
39                 // Create the item in the history
40                 history.create( historyItem );
41                 
42                 
43                 
44                 
45
46                 /// Example2: Prepare Collector
47                 Collector collector = History.createCollector(history, FlushPolicy.NoFlush);
48                 
49                 // Read item configuration data from history ( id and sampleFormat )
50                 Bean hi = history.getItem( id );
51                 
52                 // Create collector item
53                 HistoryAndCollectorItem ci = new HistoryAndCollectorItem();
54                 ci.readAvailableFields( hi ); // Read: id, sampleFormat
55                 ci.interval = 0.5;
56                 //ci.deadband = 10;
57                 ci.enabled = true;
58                 ci.gain = 10.0;
59                 ci.variableId = "MyItem";
60                 
61                 // Initialize Collector (Item)
62                 collector.addItem( ci );
63                 
64                 try {
65                         // Simulate 100 steps
66                         for ( int i = 0; i<100; i++ ) {
67                                 
68                                 // Our "simulation" results 
69                                 double time = i * 0.1;
70                                 double myItemValue = -20 + i*0.1;
71                                 
72                                 // Begin collector writing step
73                                 collector.beginStep(Bindings.DOUBLE, time);
74                                 
75                                 // Enter values 
76                                 collector.setValue("MyItem", Bindings.DOUBLE, myItemValue);
77                                 
78                                 // End collector step
79                                 collector.endStep();
80                         }
81                         
82                 } finally {
83                         // Flush values
84                         collector.flush();
85                         collector.close();
86                 }
87                 
88                 
89                 
90                 /// Print results
91                 // Open stream from history
92                 StreamAccessor aa = history.openStream(id, "r");
93                 // Read sample type
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();
99                 
100                 try {
101                         for (int i=0; i<aa.size(); i++) {
102                                 aa.get(i, sampleBinding, sample);
103                                 System.out.println(i+": "+sample);
104                         }
105                 } finally {
106                         aa.close();
107                 }
108                 
109         }
110         
111 }