]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/src/org/simantics/history/impl/CollectorState.java
Tested and fixed utility for truncating history data.
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / impl / CollectorState.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.history.impl;
13
14 import java.util.Map;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.annotations.Identifier;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.binding.error.BindingConstructionException;
20 import org.simantics.databoard.binding.mutable.MutableVariant;
21 import org.simantics.databoard.binding.reflection.BindingRequest;
22 import org.simantics.databoard.type.Datatype;
23 import org.simantics.databoard.util.Bean;
24 import org.simantics.history.util.MedianBindingProvider;
25 import org.simantics.history.util.WeightedMedian;
26
27 /**
28  * This is the state that s persisted.
29  *  
30  * @author toni.kalajainen
31  */
32 public class CollectorState extends Bean {
33
34         public static final Binding BINDING = Bindings.getBindingUnchecked( CollectorState.class );
35         public static final Binding BINDING_ITEM = Bindings.getBindingUnchecked( CollectorState.Item.class );
36         
37         public @Identifier String id;
38         
39         /** Last known time value */
40         public MutableVariant time;
41
42         /** Time stepped between the two last collector steps. Used for time stamp epsilon calculations. */
43         public double dT = 1.0;
44
45         /** Last known values for each variable */
46         public Map<String, VariableState> values;
47          
48         /** Item states - corresponds to subscription.items */
49         public Map<String, Item> itemStates;
50
51         public static class VariableState {
52                 
53                 /** The actual value, type is Void if no value */
54                 public MutableVariant value;
55                 
56                 /** Set to true if is valid value */
57                 public boolean isValid; 
58                 
59                 /** True, if double or float and is NaN */
60                 public boolean isNan;
61         }
62         
63         public static class Item extends Bean {
64                 
65                 // Subscription info
66                 /** History item reference */
67                 public @Identifier String id;
68                 /** Datasource reference */
69                 public String variableId;
70                 /** Dataformat */
71                 public Datatype format;
72                 /** Deadband */
73                 public double deadband = Double.NaN;
74                 /** Interval */
75                 public double interval = Double.NaN;
76                 /** Gain */
77                 public double gain = 1.0;
78                 /** Bias */
79                 public double bias = 0.0;
80                 /** Enabled */
81                 public boolean enabled = true;
82                 /** Format id, group id */
83                 public String groupId = "", groupItemId = "", formatId = "";
84                 
85                 // - Valueband  
86                 /** Time of the first sample in the value band. NAN, if there is no value */
87                 public double firstTime = Double.NaN;
88                 /** Value of the first sample in the value band. null, if there is no value */
89                 public MutableVariant firstValue;
90                 /** Time of the prev added sample. NAN, if there is no value */
91                 public double currentTime;
92                 /** Value of the prev added sample. null, if there is no value */
93                 public MutableVariant currentValue;
94                 /** Current value is NaN */
95                 public boolean isNaN;
96                 /** Current value is valid */
97                 public boolean isValid;
98                 /** Time weighted sum (area), NAN, if not accumulated */
99                 public double sum;
100                 /** Sample Count of acquired samples in current band */
101                 public int count;
102                 /** Out of dead band, set true if a sample has crossed the deadband region in this value band */
103                 public boolean ooDeadband = false;
104                 /** First time after disabled */
105                 public double firstDisabledTime;
106                 /** Last time while disabled*/
107                 public double lastDisabledTime;
108                 /** Is item currently disabled */
109                 public boolean isDisabled;
110                 
111                 /** Median values of the value band */
112                 public WeightedMedian median;
113                 
114                 /**
115                  * Interval is the time between two samples, a step length.  
116                  * Distribution contains classification of their variation. It is of 
117                  * interest if the datasource contains varying sizes and a mipmap interval 
118                  * needs to be decided. Null, if not breakdown is not constructed. 
119                  */     
120                 //public @Optional ClassDistribution intervalDistribution;
121                 
122                 /**
123                  * Value distribution contains clasification of values. 
124                  */
125                 //public @Optional ClassDistribution valueDistribution;
126
127         }
128         
129         public CollectorState() {
130                 super(BINDING);
131         }
132         
133         static {
134         Bindings.classBindingFactory.addFactory( MedianBindingProvider.INSTANCE );
135         
136                 try {
137                 BindingRequest br = new BindingRequest( CollectorState.Item.class ); 
138                 Binding datatypeBinding = Bindings.getBinding( br );
139                 Bindings.typeClassFactory.getRepository().put(datatypeBinding.type(), br);      
140                 
141                 br = new BindingRequest( CollectorState.class ); 
142                 datatypeBinding = Bindings.getBinding( br );
143                 Bindings.typeClassFactory.getRepository().put(datatypeBinding.type(), br);
144                 
145                 } catch (BindingConstructionException e) {
146                         System.err.println(e);
147                 }
148         }
149         
150 }