ParameterInfo.cc
5.54 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
/*
* ParameterInfo.cc
*
* Created on: Mar 18 2016
* Author: b.renard
*/
#include "ParameterInfo.hh"
#include "TimeInterval.hh"
#include <boost/algorithm/string.hpp>
#include <string>
namespace AMDA {
namespace Parameters {
ParameterInfo::ParameterInfo(ParameterManager& pParameterManager) :
ParamOutput(pParameterManager),
_parameter(NULL) {
}
ParameterInfo::~ParameterInfo() {
}
void ParameterInfo::establishConnection() {
_parameter = _parameterManager.getSampledParameter(_paramName, _samplingMode, _samplingValue, _gapThreshold).get();
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);
}
void ParameterInfo::writeInfoFile() {
AMDA::ParameterInfo::ParameterInfoFileWriter fileWriter;
std::stringstream filePath;
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() );
fileWriter.addAttribute("name",tableSPtr->getName().c_str());
fileWriter.addAttribute("units",tableSPtr->getUnits().c_str());
if (tableSPtr->isVariable()) {
fileWriter.addAttribute("variable","true");
}
else {
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 */
}
void ParameterInfo::apply() {
//Intervals loop
while (_currentTimeInterval != _timeIntervalList->end())
{
if (_currentTimeInterval->_stopTime - _currentTimeInterval->_startTime == 0) {
++_currentTimeInterval;
continue;
}
_parameterManager.getParameter(_parameter->getId())->getAsync(this).get();
++_currentTimeInterval;
}
writeInfoFile();
}
} /* namespace Parameters */
} /* namespace AMDA */