Blame view

src/InternLib/ProcessSamplingUnderRefParam.cc 7.32 KB
fbe3c2bb   Benjamin Renard   First commit
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
/**
 * ProcessSamplingUnderTimeList.cc
 *
 *  Created on: Jul 25, 2014
 *      Author: AKKA IS
 */
#include <stdlib.h>
#include <string>

#include "ProcessSamplingUnderRefParam.hh"
#include "Parameter.hh"
#include "ParameterManager.hh"
#include "ParameterCreatorFromExpression.hh"
#include "Resampling.hh"
#include "ParamMgr.hh"

using namespace std;
using namespace boost;
using namespace log4cxx;

namespace AMDA {
namespace Parameters {

ProcessSamplingUnderRefParam::ProcessSamplingUnderRefParam(Parameter &parameter) :
		SingleParamProcess_CRTP(parameter), _paramRefInput(NULL) {
}

ProcessSamplingUnderRefParam::ProcessSamplingUnderRefParam(const ProcessSamplingUnderRefParam& pProcess, Parameter &parameter)
: SingleParamProcess_CRTP(pProcess,parameter), _paramRefInput(pProcess._paramRefInput) {
}

ProcessSamplingUnderRefParam::~ProcessSamplingUnderRefParam() {
	if (_refParameterSPtr != nullptr)
	{
		_refParameterSPtr->closeConnection(this);
	}
}


void ProcessSamplingUnderRefParam::establishConnection() {
	if (_refParameterSPtr != nullptr)
		_refParameterSPtr->openConnection(this);

	SingleParamProcess_CRTP::establishConnection();
}

TimeStamp ProcessSamplingUnderRefParam::init() {
	/// Init input parameter
	TimeStamp timeStamp = _parameterInput->init( this, _timeIntervalList);
	/// Calibration information copy
	Parameter::InfoList lInfoList = _parameterInput->getInfoList();
	_parameter.getInfoList().insert(lInfoList.begin(), lInfoList.end());
	_paramInput = _parameterInput->getParamData(this).get();

	// check reference parameter
	if (_refParameterSPtr == nullptr) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("ProcessSamplingUnderRefParam : cannot retrieve reference parameter ")));
	}

	timeStamp = std::max(timeStamp, _refParameterSPtr->init( this, _timeIntervalList));

	_paramRefInput = _refParameterSPtr->getParamData(this).get();

	double computedGapSize = _refParameterSPtr->getParameterManager().getComputedGapSize(_parameterInput->getGapThreshold(), _paramInput->getMinSampling());

	// ProcessSamplingClassic _operation creation
	Resampling::CreateResampling lCreateResampling(*this, _timeIntervalList,
			*_paramInput, *_paramRefInput, computedGapSize, _parameterInput->getDataWriterTemplate()->useNearestValue());
	_operation = lCreateResampling.getResampling();
	static_cast<Resampling::ResamplingAbstract*>(_operation)->init();

	/// Get result ParamData
	_paramData = ParamDataSPtr(_operation->getParamOutput());

	_paramData->setMinSampling(static_cast<Resampling::ResamplingAbstract*>(_operation)->getSampling());

	return timeStamp;
}

unsigned int ProcessSamplingUnderRefParam::write()
{
	_paramRefInput = _refParameterSPtr->getParamData(this).get();

a6490f4d   Benjamin Renard   Do not throw an e...
84
	//ParamDataIndexInfo lRefParamDataIndexInfo = _paramRefInput->getIndexInfo();
fbe3c2bb   Benjamin Renard   First commit
85

a6490f4d   Benjamin Renard   Do not throw an e...
86
87
88
89
90
	/*if (!lRefParamDataIndexInfo._timeIntToProcessChanged) {
		do {
			lRefParamDataIndexInfo = _refParameterSPtr->getAsync(this).get();
		} while(!lRefParamDataIndexInfo._timeIntToProcessChanged && (lRefParamDataIndexInfo._nbDataToProcess != 0));
	}*/
fbe3c2bb   Benjamin Renard   First commit
91

fbe3c2bb   Benjamin Renard   First commit
92
	int ret = 0;
a6490f4d   Benjamin Renard   Do not throw an e...
93
94
95
96
97
	unsigned int nbDataBeforeCallProcess = _paramData->getDataNumber();
	ParamDataIndexInfo lParamDataIndexInfo = _parameterInput->getParamData(this).get()->getIndexInfo();

	this->_treatTerminated = false;

fbe3c2bb   Benjamin Renard   First commit
98
	do {
a6490f4d   Benjamin Renard   Do not throw an e...
99
100
		//if (!lParamDataIndexInfo._timeIntToProcessChanged && (lParamDataIndexInfo._nbDataToProcess == 0))
			lParamDataIndexInfo =_parameterInput->getAsync(this).get();
fbe3c2bb   Benjamin Renard   First commit
101

a6490f4d   Benjamin Renard   Do not throw an e...
102
103
104
		if ( ! this->_treatTerminated ) {
			_operation->write(lParamDataIndexInfo);
			ret = _paramData->getDataNumber() - nbDataBeforeCallProcess;
fbe3c2bb   Benjamin Renard   First commit
105
106
			// As we do not have output data, continue processing the input data
		}
a6490f4d   Benjamin Renard   Do not throw an e...
107
108
109
110
111
112
		if ( lParamDataIndexInfo._nbDataToProcess == 0) {
			this->_treatTerminated = true;
		}
	} while(ret == 0 && lParamDataIndexInfo._nbDataToProcess != 0);


c2fa3b5d   Benjamin Renard   Rework of legend ...
113
114
115
116
	bool updateDims = (nbDataBeforeCallProcess == 0) && (ret > 0);
	if (updateDims)
		_paramData->updateDims();

a6490f4d   Benjamin Renard   Do not throw an e...
117
118
119
120
121
122
	// Reset operation to prepare static data for the next TimeInterval.
	if (lParamDataIndexInfo._timeIntToProcessChanged) {
		_paramData->getIndexInfo()._endTimeIntIndexList.push_back(_paramData->getDataNumber());
		_operation->reset();
	}
	// There is no more time interval to process
fa4b7852   Benjamin Renard   Fix bug around ti...
123
	else if (lParamDataIndexInfo._noMoreTimeInt) {
a6490f4d   Benjamin Renard   Do not throw an e...
124
125
126
127
128
129
130
		_paramData->getIndexInfo()._endTimeIntIndexList.push_back(_paramData->getDataNumber());
	} else {
		// Nothing to do.
	}

	// Pull up information on which time interval changed.
	_paramData->getIndexInfo()._timeIntToProcessChanged = lParamDataIndexInfo._timeIntToProcessChanged;
fa4b7852   Benjamin Renard   Fix bug around ti...
131
	_paramData->getIndexInfo()._noMoreTimeInt = lParamDataIndexInfo._noMoreTimeInt;
a6490f4d   Benjamin Renard   Do not throw an e...
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

	return ret;






	/*while (lRefParamDataIndexInfo._nbDataToProcess > 0)
	{
		lRefParamDataIndexInfo = _refParameterSPtr->getAsync(this).get();
	}*/


	//if (!_paramRefInput->getIndexInfo()._timeIntToProcessChanged) {

		/*unsigned int nbDataBeforeCallProcess = _paramRefInput->getDataNumber();

		ParamDataIndexInfo lRefParamDataIndexInfo;
		bool _treatTerminated = false;
		int ret = 0;
		do {
			lRefParamDataIndexInfo = _refParameterSPtr->getAsync(this).get();

			if ( ! _treatTerminated ) {
				ret = _paramRefInput->getDataNumber() - nbDataBeforeCallProcess;
				// As we do not have output data, continue processing the input data
			}
			if ( lRefParamDataIndexInfo._nbDataToProcess == 0) {
				_treatTerminated = true;
			}
		} while(ret == 0 && lRefParamDataIndexInfo._nbDataToProcess != 0);*/

		// Reset operation to prepare static data for the next TimeInterval.
		/*if (lRefParamDataIndexInfo._timeIntToProcessChanged) {
			_paramRefInput->getIndexInfo()._endTimeIntIndexList.push_back(_paramRefInput->getDataNumber());
		}
		// There is no more time interval to process
		else if (!lRefParamDataIndexInfo._timeIntToProcessChanged && lRefParamDataIndexInfo._nbDataToProcess == 0) {
			_paramRefInput->getIndexInfo()._endTimeIntIndexList.push_back(_paramRefInput->getDataNumber());
		} else {
			// Nothing to do.
fbe3c2bb   Benjamin Renard   First commit
174
		}
fbe3c2bb   Benjamin Renard   First commit
175

a6490f4d   Benjamin Renard   Do not throw an e...
176
177
178
179
180
181
182
183
184
		_paramRefInput->getIndexInfo()._timeIntToProcessChanged = true;*/
	//}

	/*int ret = SingleParamProcess_CRTP::write();

	_paramRefInput->getIndexInfo()._timeIntToProcessChanged = _paramData->getIndexInfo()._timeIntToProcessChanged;

	return ret;*/
	//return lRefParamDataIndexInfo._nbDataToProcess;
fbe3c2bb   Benjamin Renard   First commit
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
}

/*
 * @brief Get min sampling
 */
double ProcessSamplingUnderRefParam::getMinSampling()
{
	if (_refParameterSPtr == nullptr)
		return 0;
	if (_refParameterSPtr->getDataWriterTemplate() == nullptr)
		return 0;
	return _refParameterSPtr->getDataWriterTemplate()->getMinSampling();
}

/**
 * @overload SingleParamProcess_CRTP::updateInfo update parameter info in relation to the sampling process
 */
void ProcessSamplingUnderRefParam::updateInfo(Parameter & parameter)
{
	LOG4CXX_DEBUG(_logger, "ProcessSamplingUnderRefParam::updateInfo - " << parameter.getId());

	SingleParamProcess::updateInfo(parameter);

	//Param info
	AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(parameter.getInfoId());

	if (paramInfo == nullptr)
		return;

	std::string processInfo = "Resampling of '";
	processInfo += _parameterInput->getId();
	processInfo += "' under times list of '";
	processInfo += _refParameterSPtr->getId();
	processInfo += "'";

	if (_parameterInput->getDataWriterTemplate()->useNearestValue())
			processInfo += ". Use nearest value.";

	paramInfo->setProcessInfo(processInfo);
}

} /* namespace Parameters */
} /* namespace AMDA */