]> gerrit.simantics Code Review - simantics/platform.git/blob - examples/org/simantics/history/examples/CollectorExample2.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / examples / org / simantics / history / examples / CollectorExample2.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 CollectorExample2 {
22
23         public static void main(String[] args) throws HistoryException, BindingException, AccessorException {
24                 
25                 /// Example2: Record variable "MyVariable" to two history  
26                 ///           items "MyVariable-interval=0.5" and "MyVariable-deadband=5"
27                 
28                 // Create History Manager
29                 HistoryManager history = History.createMemoryHistory();
30                 
31                 // Create a sample format
32                 RecordType sampleFormat = new RecordType();
33                 sampleFormat.addComponent("time", Datatypes.DOUBLE );
34                 sampleFormat.addComponent("value", Datatypes.DOUBLE );
35                 sampleFormat.addComponent("quality", Datatypes.BYTE );
36                 
37                 // Create two History Item and Collector Item configurations
38                 HistoryAndCollectorItem i1 = new HistoryAndCollectorItem();
39                 i1.id = "MyVariable-interval=0.5";
40                 i1.format = sampleFormat;
41                 i1.interval = 0.5;
42                 i1.variableId = "MyVariable";
43                 
44                 HistoryAndCollectorItem i2 = new HistoryAndCollectorItem();
45                 i2.id = "MyVariable-deadband=5";
46                 i2.format = sampleFormat;
47                 i2.deadband = 5.0;
48                 i2.variableId = "MyVariable";
49
50                 // Create the item in the history
51                 history.create( i1, i2 );
52
53                 /// Prepare Collector
54                 Collector collector = History.createCollector(history, FlushPolicy.NoFlush);
55                 
56                 // Initialize Collector (Items)
57                 collector.addItem( i1 );
58                 collector.addItem( i2 );
59                 
60                 try {
61                         // Simulate 100 steps
62                         for ( int i = 0; i<100; i++ ) {
63                                 
64                                 // Our "simulation" results 
65                                 double time = i * 0.1;
66                                 double myItemValue = -20 + i*0.1;
67                                 
68                                 // Begin collector writing step
69                                 collector.beginStep(Bindings.DOUBLE, time);
70                                 
71                                 // Enter values for one variable. 
72                                 collector.setValue("MyVariable", Bindings.DOUBLE, myItemValue);
73                                 
74                                 // End collector step - MyVariable is written to two HistoryItems
75                                 collector.endStep();
76                         }
77                         
78                 } finally {
79                         // Flush values
80                         collector.flush();
81                         collector.close();
82                 }
83                                 
84         }
85
86 }