Blame view

src/amdaParameterInfo/ParameterInfo.cc 5.79 KB
ad920fd6   Benjamin Renard   First implementat...
1
2
3
4
5
6
7
8
/*
 * ParameterInfo.cc
 *
 *  Created on: Mar 18 2016
 *      Author: b.renard
 */

#include "ParameterInfo.hh"
2f05d991   RENARD Benjamin   Generate param in...
9
#include "TimeInterval.hh"
ad920fd6   Benjamin Renard   First implementat...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <boost/algorithm/string.hpp>
#include <string>

namespace AMDA {
namespace Parameters {

ParameterInfo::ParameterInfo(ParameterManager& pParameterManager) :
							ParamOutput(pParameterManager),
		_parameter(NULL) {
}

ParameterInfo::~ParameterInfo() {
}

void ParameterInfo::establishConnection() {
854a7fcf   Benjamin Renard   First implementat...
26
	_parameter = _parameterManager.getSampledParameter(_paramName, _samplingMode, _samplingValue, _gapThreshold, true).get();
ad920fd6   Benjamin Renard   First implementat...
27
28
29
30
31
32
33
34
35
36
	if(_parameter == NULL)  {
		LOG4CXX_ERROR(_logger,"ParamOutput::init parameter : \""<< _paramName <<"\" Not Exist" );
		BOOST_THROW_EXCEPTION( ParamOutput_exception());
	}
	_parameter->openConnection(this);
}


void ParameterInfo::init() {
	_parameter->init(this, _timeIntervalList);
2f05d991   RENARD Benjamin   Generate param in...
37
}
ad920fd6   Benjamin Renard   First implementat...
38

2f05d991   RENARD Benjamin   Generate param in...
39
40
void ParameterInfo::writeInfoFile() {
        AMDA::ParameterInfo::ParameterInfoFileWriter fileWriter;
ad920fd6   Benjamin Renard   First implementat...
41

2f05d991   RENARD Benjamin   Generate param in...
42
        std::stringstream filePath;
ad920fd6   Benjamin Renard   First implementat...
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
	filePath << "info_" << _paramName.c_str() << ".xml";
	fileWriter.initWriter(filePath.str().c_str());
	fileWriter.addAttribute("id", _parameter->getId().c_str());

	unsigned int dim1 = _parameter->getDataWriterTemplate()->getParamData()->getDim1();
	unsigned int dim2 = _parameter->getDataWriterTemplate()->getParamData()->getDim2();

	//Write dimensions info
	fileWriter.startElement("dimensions"); /* dimensions */
	std::string dim_1 = std::to_string(dim1);
	std::string dim_2 = std::to_string(dim2);
	fileWriter.addAttribute("dim_1",dim_1.c_str());
	fileWriter.addAttribute("dim_2",dim_2.c_str());
	fileWriter.endElement(); /* dimensions */

	//Write components info
	std::vector<std::string> components = this->getComponents();
	if (!components.empty()) {
		fileWriter.startElement("components"); /* components */

		for (int i = 0; i < (int)dim1; ++i) {
			for (int j = 0; j < (int)dim2; ++j) {
				fileWriter.startElement("component"); /* component */
				std::string index_1 = std::to_string(i);
				std::string index_2 = std::to_string(j);

				fileWriter.addAttribute("index_1",index_1.c_str());
				if (dim2 > 1)
					fileWriter.addAttribute("index_2",index_2.c_str());
				fileWriter.addAttribute("name",(components[i*(int)dim2+j]).c_str());
				fileWriter.endElement(); /* component */
			}
		}

		fileWriter.endElement(); /* components */
	}

	// Write tables info
	AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(_parameter->getInfoId());

	fileWriter.startElement("tables"); /* tables */
	boost::shared_ptr<AMDA::Info::ParamTable> table1SPtr = paramInfo->getTable(0);
	if (table1SPtr != nullptr) {
		this->writeTable(table1SPtr, fileWriter, "dim_1");
	}

	boost::shared_ptr<AMDA::Info::ParamTable> table2SPtr = paramInfo->getTable(1);
	if (table2SPtr != nullptr) {
		this->writeTable(table2SPtr, fileWriter, "dim_2");
	}

	fileWriter.endElement(); /* tables */

	fileWriter.closeWriter();
}

std::vector<std::string> ParameterInfo::getComponents() {
	unsigned int dim1 = _parameter->getDataWriterTemplate()->getParamData()->getDim1();
	unsigned int dim2 = _parameter->getDataWriterTemplate()->getParamData()->getDim2();

	AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(_parameter->getInfoId());

	std::vector<std::string> components;

	if ( dim1 * dim2 <= 1 ) //scalar => no components
		return components;

	std::string componentsStr = paramInfo->getComponents();

	//Split labels
	boost::split(components,componentsStr,boost::is_any_of(","));

	if (components.size() != dim1 * dim2) {
		//If labels not defined (or misdefined)
		components.clear();
		for (int i = 0; i < (int)dim1; ++i) {
			for (int j = 0; j < (int)dim2; ++j) {
				std::string crtComp;
				crtComp += (!paramInfo->getShortName().empty() ? paramInfo->getShortName() : paramInfo->getId());
				crtComp += "[";
				if (dim1 > 1)
					crtComp += std::to_string(i);
				if (dim1 > 1 && dim2 > 1)
					crtComp += ",";
				if (dim2 > 1)
					crtComp += std::to_string(j);
				crtComp += "]";
				components.push_back(crtComp);
			}
		}
	}

	return components;
}

void ParameterInfo::writeTable(boost::shared_ptr<AMDA::Info::ParamTable> tableSPtr, AMDA::ParameterInfo::ParameterInfoFileWriter& fileWriter, std::string relatedDim) {
	if (tableSPtr == nullptr)
		return;
	fileWriter.startElement("table"); /* table */
	fileWriter.addAttribute("relatedDim", relatedDim.c_str() );
e7ea756d   Benjamin Renard   Implements tables...
143
144
145
	fileWriter.addAttribute("name",tableSPtr->getName(&_parameter->getParameterManager()).c_str());
	fileWriter.addAttribute("units",tableSPtr->getUnits(&_parameter->getParameterManager()).c_str());
	if (tableSPtr->isVariable(&_parameter->getParameterManager())) {
ad920fd6   Benjamin Renard   First implementat...
146
147
148
		fileWriter.addAttribute("variable","true");
	}
	else {
ad920fd6   Benjamin Renard   First implementat...
149
150
151
152
153
154
155
156
157
158
159
160
161
		for (int i = 0; i < tableSPtr->getSize(&_parameter->getParameterManager()); ++i) {
			AMDA::Info::t_TableBound bound = tableSPtr->getBound(&_parameter->getParameterManager(),i);
			fileWriter.startElement("channel"); /* channel */
			std::string min = std::to_string(bound.min);
			std::string max = std::to_string(bound.max);
			fileWriter.addAttribute("min",min.c_str());
			fileWriter.addAttribute("max",max.c_str());
			fileWriter.endElement(); /* channel */
		}
	}
	fileWriter.endElement(); /* table */
}

2f05d991   RENARD Benjamin   Generate param in...
162
163
void ParameterInfo::apply() {
    //Intervals loop
b03f2eca   Benjamin Renard   Improve amdaParam...
164
165
    bool skip = false;
    while (!skip && (_currentTimeInterval != _timeIntervalList->end()))
2f05d991   RENARD Benjamin   Generate param in...
166
167
168
169
170
171
    {
        if (_currentTimeInterval->_stopTime - _currentTimeInterval->_startTime == 0) {
            ++_currentTimeInterval;
            continue;
        }
    
b03f2eca   Benjamin Renard   Improve amdaParam...
172
173
174
175
        ParamDataIndexInfo paramIndexInfo = _parameterManager.getParameter(_parameter->getId())->getAsync(this).get();
        if (paramIndexInfo._nbDataToProcess > 0) {
            skip = true;
        }
2f05d991   RENARD Benjamin   Generate param in...
176
177
178
179
180
181
                
        ++_currentTimeInterval;
    }
    
    writeInfoFile();
}
ad920fd6   Benjamin Renard   First implementat...
182
183
184

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