Blame view

src/InternLib/MultiParamProcess.cc 7.77 KB
1eac3ad2   brenard   Fix tests used to...
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
/*
 * MultiParamProcess.cc
 *
 *  Created on: Jan 4, 2013
 *      Author: f.casimir
 */

#include <algorithm>
#include <limits>

#include "ParamData.hh"
#include "Operation.hh"
#include "ParamMgr.hh"

#include "MultiParamProcess.hh"

namespace AMDA {
namespace Parameters {

MultiParamProcess::MultiParamProcess(Parameter &parameter) :
		Process(parameter), _minSampling(std::numeric_limits<double>::max()) {
}

MultiParamProcess::MultiParamProcess(const MultiParamProcess &pProcess, Parameter &parameter) :
		Process(pProcess,parameter),
		 _paramNameList(pProcess._paramNameList),
		 _minSampling(pProcess._minSampling) {

}

MultiParamProcess::~MultiParamProcess() {
	for (ParameterList::iterator it = _paramNameList.begin(); it != _paramNameList.end(); ++it) {
		ParameterSPtr& parameter = it->second.first;
		if ( parameter) { parameter->closeConnection(this); }
	}
}

/**
 *  @brief Connection to Parameter server.
 */
void MultiParamProcess::establishConnection() {

	for (ParameterList::iterator it = _paramNameList.begin(); it != _paramNameList.end(); ++it) {
		ParameterSPtr& parameter = it->second.first;
		parameter->openConnection(this);
	}
}

 TimeStamp MultiParamProcess::init() {

	 TimeStamp time = 0;
	for (ParameterList::iterator it = _paramNameList.begin(); it != _paramNameList.end(); ++it) {
		ParameterSPtr& parameter = it->second.first;
		time = std::max(time, parameter->init(this, _timeIntervalList));
		Parameter::InfoList lInfoList = parameter->getInfoList();
		_parameter.getInfoList().insert(lInfoList.begin(), lInfoList.end());
		_minSampling = std::min(parameter->getParamData(this)->getMinSampling(), _minSampling);
	}
	return time;
}

unsigned int MultiParamProcess::write() {
	typedef std::list<std::pair< ParameterList::iterator,boost::shared_future<ParamDataIndexInfo> > > ParamDataIndexInfoFutureList;

	ParamDataIndexInfoFutureList lResultList;
	unsigned int lNbTotalAvalaible = 0;

	for (ParameterList::iterator itParam = _paramNameList.begin(); itParam != _paramNameList.end(); ++itParam) {
		if (itParam->second.second._timeIntToProcessChanged && !_paramDataIndexInfo._timeIntToProcessChanged)
			continue;
		lNbTotalAvalaible = itParam->second.second._startIndex + itParam->second.second._nbDataToProcess;
		if ( _paramDataIndexInfo._startIndex >= lNbTotalAvalaible)
		{
			LOG4CXX_DEBUG(_logger, "Need more data Parameter name: " << itParam->first );
			lResultList.push_back(make_pair(itParam,(*itParam->second.first).getAsync(this)));
		}
	}

	// Wait data
	for ( ParamDataIndexInfoFutureList::iterator it= lResultList.begin();it != lResultList.end(); ++it) {
		LOG4CXX_DEBUG(_logger, "Wait data Parameter name: " << it->first->first );
		it->first->second.second=it->second.get();
	}

	//compute minimum data available
	ParameterList::iterator it = _paramNameList.begin();
	if( it != _paramNameList.end()) {
		ParamDataIndexInfo &lParamDataIndexInfoTmp= (*it).second.second;
		LOG4CXX_DEBUG(_logger, "Parameter name: " << it->first << " Index: " << lParamDataIndexInfoTmp._startIndex  <<" nbDataToProcess " << lParamDataIndexInfoTmp._nbDataToProcess);
		lNbTotalAvalaible = lParamDataIndexInfoTmp._nbDataToProcess + lParamDataIndexInfoTmp._startIndex ;
		++it;
		for ( ;it != _paramNameList.end(); ++it) {
			ParamDataIndexInfo &lParamDataIndexInfoTmp= (*it).second.second;
			LOG4CXX_DEBUG(_logger, "Parameter name: " << it->first << " Index: " << lParamDataIndexInfoTmp._startIndex  <<" nbDataToProcess " << lParamDataIndexInfoTmp._nbDataToProcess);
			lNbTotalAvalaible =std::min(lNbTotalAvalaible, lParamDataIndexInfoTmp._nbDataToProcess + lParamDataIndexInfoTmp._startIndex) ;
		}
	}

	_paramDataIndexInfo._nbDataToProcess = lNbTotalAvalaible - _paramDataIndexInfo._startIndex;

	_operation->write(_paramDataIndexInfo);
        
        bool updateDims = (_paramDataIndexInfo._nbDataToProcess > 0) && (_paramDataIndexInfo._startIndex == 0);

	_paramDataIndexInfo._startIndex +=_paramDataIndexInfo._nbDataToProcess ;
        
        // Reset operation instance to prepare it for the next time interval.
	bool lTimeIntToProcessChanged = true;
	bool lNoMoreTimeInt = true;

	for (ParameterList::iterator itParam = _paramNameList.begin(); itParam != _paramNameList.end(); ++itParam) {
		lTimeIntToProcessChanged &= itParam->second.second._timeIntToProcessChanged;
		lNoMoreTimeInt &= itParam->second.second._noMoreTimeInt;
	}

	if(lTimeIntToProcessChanged) {
		LOG4CXX_DEBUG(_logger, "MultiParamProcess => Need to change interval");
		_operation->reset();
		_paramDataIndexInfo._endTimeIntIndexList.push_back(lNbTotalAvalaible);
		_paramData->getIndexInfo()._endTimeIntIndexList.push_back(lNbTotalAvalaible);
	}
	// There is no more time interval to process
	else if (lNoMoreTimeInt) {
		_paramDataIndexInfo._endTimeIntIndexList.push_back(lNbTotalAvalaible);
		_paramData->getIndexInfo()._endTimeIntIndexList.push_back(lNbTotalAvalaible);
		LOG4CXX_DEBUG(_logger, "MultiParamProcess => No more time interval to process; end index for this interval is " << _paramDataIndexInfo._endTimeIntIndexList.back());
	} else {
		// Nothing to do.
	}

	_paramDataIndexInfo._timeIntToProcessChanged = lTimeIntToProcessChanged;
	_paramDataIndexInfo._noMoreTimeInt = lNoMoreTimeInt;
	_paramData->getIndexInfo()._noMoreTimeInt = lNoMoreTimeInt;
	_paramData->getIndexInfo()._timeIntToProcessChanged = lTimeIntToProcessChanged;
        
        if (updateDims)
            _paramData->updateDims();
        
        return _paramDataIndexInfo._nbDataToProcess;
}

/*
 * @brief @ to know the resampling strategy to use. If it's true, the nearest value will be use
 */
bool MultiParamProcess::useNearestValue()
{
	bool useNearestValue = true;
	for (ParameterList::iterator it = _paramNameList.begin(); it != _paramNameList.end(); ++it)
	{
		ParameterSPtr& parameter = it->second.first;
		useNearestValue = useNearestValue && parameter->getDataWriterTemplate()->useNearestValue();
	}
	return useNearestValue;
}

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

	if (parameter.getInfoId().empty())
		parameter.setInfoId(parameter.getId());

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

	if (paramInfo == nullptr)
		return;

	//Derived parameter => no comes from a dataset
	paramInfo->setDatasetId("");

	//set name and short name = param id
	if (paramInfo->getName().empty())
		paramInfo->setName(parameter.getId());
	if (paramInfo->getShortName().empty())
		paramInfo->setShortName(parameter.getId());

	std::string processInfo = "Derived parameter from expression '";
	processInfo += getExpression();
	processInfo += "'";

	for (ParameterList::iterator itParam = _paramNameList.begin(); itParam != _paramNameList.end(); ++itParam)
		paramInfo->addLinkedParamId(itParam->first);

	paramInfo->setProcessInfo(processInfo);
	paramInfo->setProcessDescription(getDescription());
}

/*
 * @brief Get min sampling
 */
double MultiParamProcess::getMinSampling()
{
	double minSampling = 0;
	for (auto param : _paramNameList)
	{
		ParameterSPtr& parameter = param.second.first;
		double paramMinSampling = 0;
		if (parameter->getTimeResolution() > 0)
			paramMinSampling = parameter->getTimeResolution();
		else if (parameter->getDataWriterTemplate() != nullptr)
			paramMinSampling = parameter->getDataWriterTemplate()->getMinSampling();

		if (minSampling == 0)
			minSampling = paramMinSampling;
		else if (paramMinSampling != 0)
			minSampling = std::min(minSampling,paramMinSampling);
	}
	return minSampling;
}


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