Blame view

src/InternLib/MultiParamProcess.cc 10.5 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
/*
 * 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) {
a9f0f983   Benjamin Renard   Fix copy construc...
28
	establishConnection();
1eac3ad2   brenard   Fix tests used to...
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
}

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;

b9779b5b   Benjamin Renard   Add missing file ...
68
69
	unsigned int nbDataBeforeCallProcess = _paramData->getDataNumber();

e9b02dbf   Benjamin Renard   Fix parameters sy...
70
71
72
73
	if (_paramDataIndexInfo._timeIntToProcessChanged) {
		for (ParameterList::iterator itParam = _paramNameList.begin(); itParam != _paramNameList.end(); ++itParam) {
			LOG4CXX_DEBUG(_logger, "MultiParamProcess::write - New interval - Need more data for: " << itParam->first );
			(*itParam).second.second = (*itParam->second.first).getAsync(this).get();
1eac3ad2   brenard   Fix tests used to...
74
75
		}
	}
e9b02dbf   Benjamin Renard   Fix parameters sy...
76
77
78
79
80
81
82
83
84
85
86
	else {
		for (ParameterList::iterator itParam = _paramNameList.begin(); itParam != _paramNameList.end(); ++itParam) {
			if (itParam->second.second._timeIntToProcessChanged) {
				continue;
			}
			 lNbTotalAvalaible = itParam->second.second._startIndex + itParam->second.second._nbDataToProcess;
	               	if ( _paramDataIndexInfo._startIndex >= lNbTotalAvalaible) {
				LOG4CXX_DEBUG(_logger, "MultiParamProcess::write - Need more data for: " << itParam->first );
				(*itParam).second.second = (*itParam->second.first).getAsync(this).get();
			}
		}
1eac3ad2   brenard   Fix tests used to...
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
	}

	//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();
        
b9779b5b   Benjamin Renard   Add missing file ...
143
	return _paramData->getDataNumber() - nbDataBeforeCallProcess;
1eac3ad2   brenard   Fix tests used to...
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
}

/*
 * @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);
72d1e9e7   Hacene SI HADJ MOHAND   moitie de l'us
191
192
193
194
195
196
                   
                    // adding tableParams into linked params 
                    std::map<int, boost::shared_ptr<AMDA::Info::ParamTable>> tables =paramInfo->getTables();
                    
                    if(! tables.empty()){
                        for(auto table : tables){
5d98ad1c   Hacene SI HADJ MOHAND   us ok tests ok
197
                             if (table.second != nullptr){
f49cb7a4   Benjamin Renard   Fix bug in base p...
198
199
                            if(table.second->isVariable(&parameter.getParameterManager())){
                            std::map<std::string, std::string> tableParams = table.second->getTableParams(&parameter.getParameterManager());
72d1e9e7   Hacene SI HADJ MOHAND   moitie de l'us
200
201
202
203
204
205
206
207
                            if(! tableParams.empty())
                            for(auto tableParam : tableParams){
                                paramInfo->addLinkedParamId(tableParam.second);
                            }
                         }
                        
                      }
                    }
5d98ad1c   Hacene SI HADJ MOHAND   us ok tests ok
208
                    }
72d1e9e7   Hacene SI HADJ MOHAND   moitie de l'us
209
                     
1eac3ad2   brenard   Fix tests used to...
210
	paramInfo->setProcessInfo(processInfo);
f91abde1   Benjamin Renard   Write data mining...
211
212
213
214
215

	std::string description = getDescription();
	if (description.empty()) {
		description = getExpression();
	}
f1326d28   Benjamin Renard   Some fixes
216

6b9babe7   Benjamin Renard   Some fix for Fitn...
217
	if ((_paramNameList.size() == 1) && paramInfo->getTables().empty()) {
f1326d28   Benjamin Renard   Some fixes
218
219
220
221
222
223
224
225
226
227
		AMDA::Info::ParamInfoSPtr inputParamInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(_paramNameList.begin()->second.first->getInfoId());
		if (inputParamInfo != nullptr) {
			//Copy tables
			std::map<int, boost::shared_ptr<AMDA::Info::ParamTable>> inputTables = inputParamInfo->getTables();
			for (std::map<int, boost::shared_ptr<AMDA::Info::ParamTable>>::iterator it = inputTables.begin(); it != inputTables.end(); ++it) {
				paramInfo->addTable(it->first, it->second);
			}
		}
	}

f91abde1   Benjamin Renard   Write data mining...
228
	paramInfo->setProcessDescription(description);
1eac3ad2   brenard   Fix tests used to...
229

eddfb311   Erdogan Furkan   9326 - Modificita...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
	if ((_paramNameList.size() == 1) && paramInfo->getAdditionInfo().empty())
	{
		AMDA::Info::ParamInfoSPtr inputParamInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(_paramNameList.begin()->second.first->getInfoId());
		if (inputParamInfo != nullptr)
		{
			// Copy tables
			std::map<std::string, std::string> inputAdditionals = inputParamInfo->getAdditionInfo();
			for (std::map<std::string, std::string>::iterator it = inputAdditionals.begin(); it != inputAdditionals.end(); ++it)
			{
				paramInfo->addAdditionalInfo(it->first, it->second);
			}
		}
	}
}
1eac3ad2   brenard   Fix tests used to...
244
245
246
247
248
249
250
251
252
253
/*
 * @brief Get min sampling
 */
double MultiParamProcess::getMinSampling()
{
	double minSampling = 0;
	for (auto param : _paramNameList)
	{
		ParameterSPtr& parameter = param.second.first;
		double paramMinSampling = 0;
deefda79   Benjamin Renard   Addapt resampling...
254
255
256
257
258
259
		if (!parameter->getReferenceParameter().empty()) {
			std::string refParamId = parameter->getReferenceParameter();
			ParameterSPtr refParam = parameter->getParameterManager().getParameter(refParamId);
			paramMinSampling = refParam->getDataWriterTemplate()->getMinSampling();
		}
		else if (parameter->getTimeResolution() > 0)
1eac3ad2   brenard   Fix tests used to...
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
			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 */