AbstractFunc.hh
8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* AbstractFunc.hh
*
* Created on: Jun 21, 2018
* Author: benjamin
*/
#ifndef ABSTRACTFUNC_HH_
#define ABSTRACTFUNC_HH_
#include "Parameter.hh"
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "Operation.hh"
#include <iterator>
namespace AMDA {
namespace Parameters {
namespace StatisticFunctions {
/**
* @class AbstractFunc
* @brief
* @details This class implement the interface Operation.
*/
class AbstractFuncBase : public Operation {
public:
AbstractFuncBase(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, double windowtime) : Operation(pProcess), _timeIntervalList(pTimeIntervalList), _currentTimeInterval(pTimeIntervalList->begin()), _windowtime(windowtime), _needInit(true) {
}
virtual ~AbstractFuncBase() {
}
double getWindowTime() {
return _windowtime;
}
bool inWindow(double time) {
return (time >= _minWindow && time <= _maxWindow);
}
double getIntStartTime() {
return _currentTimeInterval->_startTime;
}
double getIntStopTime() {
return _currentTimeInterval->_stopTime;
}
bool inInt(double time) {
return (time >= _currentTimeInterval->_startTime && time <= _currentTimeInterval->_stopTime);
}
double getTarget() {
return _target;
}
bool needInit() {
return _needInit;
}
void setNeedInit(bool needInit) {
_needInit = needInit;
}
bool setTarget(double time) {
if (!inInt(time)) {
return false;
}
_target = time;
_minWindow = _target - _windowtime / 2.;
if (_minWindow < _currentTimeInterval->_startTime) {
_minWindow = _currentTimeInterval->_startTime;
}
_maxWindow = _target + _windowtime / 2.;
if (_maxWindow > _currentTimeInterval->_stopTime) {
_maxWindow = _currentTimeInterval->_stopTime;
}
return true;
}
/**
* @overload Operation::reset(double pStartTime, double pTimeInt)
* @brief reset static data to process another TimeInterval
*/
virtual void reset() {
Operation::reset();
if (_currentTimeInterval == _timeIntervalList->end()) {
return;
}
++_currentTimeInterval;
_needInit = true;
resetFunc();
}
virtual bool nextTarget() = 0;
virtual bool needToChangeTarget(double crtTime) = 0;
virtual double getSampling() = 0;
virtual void init() = 0;
virtual void resetFunc() = 0;
private:
TimeIntervalListSPtr _timeIntervalList;
TimeIntervalList::iterator _currentTimeInterval;
double _windowtime;
double _minWindow;
double _maxWindow;
double _target;
bool _needInit;
};
template <typename InputElemType, typename OutputElemType>
class AbstractFunc : public AbstractFuncBase {
public:
/**
* @brief Constructor.
* @details Create the ParamData type of the input ParamData.
*/
AbstractFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& paramInput, double windowtime)
: AbstractFuncBase(pProcess, pTimeIntervalList, windowtime),
_paramInput(paramInput),
_paramOutput(new ParamDataSpec<OutputElemType>) {
_paramDataOutput=_paramOutput;
}
virtual ~AbstractFunc() {
}
virtual void pushData(double time, InputElemType& elem) = 0;
virtual OutputElemType compute() = 0;
/**
* @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
*/
void write(ParamDataIndexInfo &pParamDataIndexInfo) {
if ((pParamDataIndexInfo._nbDataToProcess > 0)) {
if (pParamDataIndexInfo._startIndex == 0) {
_nanVal = _paramInput.get(0);
_nanVal << NotANumber();
}
for (unsigned int _index = pParamDataIndexInfo._startIndex ;
_index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess;
++_index)
{
double crtTime = _paramInput.getTime(_index);
InputElemType crtVal = _paramInput.get(_index);
if (needToChangeTarget(crtTime)) {
_paramOutput->pushTime(getTarget());
_paramOutput->push(compute());
pushData(crtTime, crtVal);
nextTarget();
bool skip = false;
while (!skip && needToChangeTarget(crtTime)) {
_paramOutput->pushTime(getTarget());
_paramOutput->push(compute());
skip = nextTarget();
}
}
else {
pushData(crtTime, crtVal);
if (needInit()) {
init();
}
}
}
}
if (pParamDataIndexInfo._timeIntToProcessChanged || pParamDataIndexInfo._noMoreTimeInt) {
if (!needInit()) {
do {
if (inInt(getTarget())) {
_paramOutput->pushTime(getTarget());
_paramOutput->push(compute());
}
} while (nextTarget());
}
}
}
double getInputParamSampling() {
return _paramInput.getMinSampling();
}
private:
ParamDataSpec<InputElemType>& _paramInput;
ParamDataSpec<OutputElemType>* _paramOutput;
protected:
OutputElemType _nanVal;
};
template <typename InputElemType, typename OutputElemType>
class ClassicAbstractFunc : public AbstractFunc<InputElemType,OutputElemType> {
public:
ClassicAbstractFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& paramInput, double windowtime)
: AbstractFunc<InputElemType,OutputElemType>(pProcess, pTimeIntervalList, paramInput, windowtime) {
}
virtual ~ClassicAbstractFunc() {
}
virtual void init() {
AbstractFunc<InputElemType,OutputElemType>::setTarget(AbstractFunc<InputElemType,OutputElemType>::getIntStartTime());
AbstractFunc<InputElemType,OutputElemType>::setNeedInit(false);
}
virtual bool nextTarget() {
double target = AbstractFunc<InputElemType,OutputElemType>::getTarget() + AbstractFunc<InputElemType,OutputElemType>::getWindowTime();
bool res = AbstractFunc<InputElemType,OutputElemType>::setTarget(target);
while (!_mem.empty() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(_mem.front().first)) {
_mem.pop_front();
}
return res;
}
virtual bool needToChangeTarget(double crtTime) {
return !AbstractFunc<InputElemType,OutputElemType>::needInit() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(crtTime);
}
virtual double getSampling() {
return AbstractFunc<InputElemType,OutputElemType>::getWindowTime();
}
virtual void pushData(double time, InputElemType& elem) {
_mem.push_back(std::make_pair(time, elem));
}
virtual void resetFunc() {
_mem.clear();
}
protected:
std::list<std::pair<double,InputElemType> > _mem;
};
template <typename InputElemType, typename OutputElemType>
class SmAbstractFunc : public AbstractFunc<InputElemType,OutputElemType> {
public:
SmAbstractFunc(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList, ParamDataSpec<InputElemType>& paramInput, double windowtime)
: AbstractFunc<InputElemType,OutputElemType>(pProcess, pTimeIntervalList, paramInput, windowtime) {
}
virtual ~SmAbstractFunc() {
}
virtual void init() {
if (!_targets.empty()) {
AbstractFunc<InputElemType,OutputElemType>::setTarget(_targets.front());
AbstractFunc<InputElemType,OutputElemType>::setNeedInit(false);
_targets.pop_front();
}
}
virtual bool nextTarget() {
if (!_targets.empty()) {
bool res = AbstractFunc<InputElemType,OutputElemType>::setTarget(_targets.front());
_targets.pop_front();
while (!_mem.empty() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(_mem.front().first)) {
_mem.pop_front();
}
return res;
}
return false;
}
virtual bool needToChangeTarget(double crtTime) {
return !AbstractFunc<InputElemType,OutputElemType>::needInit() && !AbstractFunc<InputElemType,OutputElemType>::inWindow(crtTime) && !_targets.empty() ;
}
virtual double getSampling() {
return AbstractFunc<InputElemType,OutputElemType>::getInputParamSampling();
}
virtual void pushData(double time, InputElemType& elem) {
_mem.push_back(std::make_pair(time, elem));
_targets.push_back(time);
}
virtual void resetFunc() {
_mem.clear();
_targets.clear();
}
protected:
std::list<double> _targets;
std::list<std::pair<double,OutputElemType> > _mem;
};
} /* namespace StatisticFunctions */
} /* namespace Parameters */
} /* namespace AMDA */
#endif /* ABSTRACTFUNC_HH_ */