1 #ifndef _ots_ConfigurationView_h_
2 #define _ots_ConfigurationView_h_
4 #include "otsdaq-core/ConfigurationDataFormats/ViewColumnInfo.h"
5 #include "otsdaq-core/MessageFacility/MessageFacility.h"
6 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
7 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationVersion.h"
19 std::string ots_demangle(
const char* name);
29 static const unsigned int INVALID;
30 typedef std::vector<std::vector<std::string> > DataView;
31 typedef DataView::iterator iterator;
32 typedef DataView::const_iterator const_iterator;
45 unsigned int findRow(
unsigned int col,
const T& value,
46 unsigned int offsetRow=0)
const
48 std::istringstream s(value);
49 return findRow(col,s.str(),offsetRow);
51 unsigned int findRow (
unsigned int col,
const std::string& value,
unsigned int offsetRow=0)
const;
54 unsigned int findRowInGroup(
unsigned int col,
const T& value,
55 const std::string &groupId,
const std::string &childLinkIndex,
unsigned int offsetRow=0)
const
57 std::istringstream s(value);
58 return findRowInGroup(col,s.str(),groupId,childLinkIndex,offsetRow);
60 unsigned int findRowInGroup (
unsigned int col,
const std::string &value,
const std::string &groupId,
const std::string &childLinkIndex,
unsigned int offsetRow=0)
const;
61 unsigned int findCol (
const std::string &name)
const;
62 unsigned int findColByType (
const std::string &type,
int startingCol = 0)
const;
65 const std::string& getUniqueStorageIdentifier (
void)
const;
66 const std::string& getTableName (
void)
const;
68 const std::string& getComment (
void)
const;
69 const std::string& getAuthor (
void)
const;
70 const time_t& getCreationTime (
void)
const;
71 const time_t& getLastAccessTime (
void)
const;
72 const bool& getLooseColumnMatching (
void)
const;
73 const unsigned int getDataColumnSize (
void)
const;
74 const unsigned int& getSourceColumnMismatch (
void)
const;
75 const unsigned int& getSourceColumnMissing (
void)
const;
76 const std::set<std::string>& getSourceColumnNames (
void)
const;
77 std::set<std::string> getColumnNames (
void)
const;
78 std::set<std::string> getColumnStorageNames (
void)
const;
79 std::vector<std::string> getDefaultRowValues (
void)
const;
81 unsigned int getNumberOfRows (
void)
const;
82 unsigned int getNumberOfColumns (
void)
const;
83 const unsigned int getColUID (
void)
const;
89 bool isEntryInGroupCol (
const unsigned int& row,
const unsigned int& groupCol,
const std::string& groupNeedle, std::set<std::string>* groupIDList = 0)
const;
92 std::set<std::string> getSetOfGroupIDs (
const std::string& childLinkIndex,
unsigned int row = -1)
const;
93 bool isEntryInGroup (
const unsigned int& row,
const std::string& childLinkIndex,
const std::string& groupNeedle)
const;
94 const bool getChildLink (
const unsigned int& col,
bool& isGroup, std::pair<unsigned int /*link col*/, unsigned int /*link id col*/>& linkPair)
const;
95 const unsigned int getColLinkGroupID (
const std::string& childLinkIndex)
const;
96 void addRowToGroup (
const unsigned int& row,
const unsigned int& col,
const std::string& groupID,
const std::string& colDefault);
97 void removeRowFromGroup (
const unsigned int& row,
const unsigned int& col,
const std::string& groupID);
104 void getValue(T& value,
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const
106 if(!(col < columnsInfo_.size() && row < getNumberOfRows()))
108 __SS__ <<
"Invalid row col requested" << std::endl;
109 __MOUT_ERR__ <<
"\n" << ss.str();
110 throw std::runtime_error(ss.str());
113 value = validateValueForColumn<T>(theDataView_[row][col],col,convertEnvironmentVariables);
199 void getValue(std::string& value,
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
208 T validateValueForColumn(
const std::string& value,
unsigned int col,
209 bool convertEnvironmentVariables=
true)
const
211 if(col >= columnsInfo_.size())
213 __SS__ <<
"Invalid col requested" << std::endl;
214 __MOUT_ERR__ <<
"\n" << ss.str();
215 throw std::runtime_error(ss.str());
220 if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_NUMBER)
222 std::string data = convertEnvironmentVariables?convertEnvVariables(value):
227 __SS__ << (data +
" is not a number!") << std::endl;
228 __MOUT__ <<
"\n" << ss.str();
229 throw std::runtime_error(ss.str());
232 if(
typeid(
double) ==
typeid(retValue))
233 retValue = strtod(data.c_str(),0);
234 else if(
typeid(
float) ==
typeid(retValue))
235 retValue = strtof(data.c_str(),0);
236 else if(data.size() > 2 && data[1] ==
'x')
237 retValue = strtol(data.c_str(),0,16);
238 else if(data.size() > 1 && data[0] ==
'b')
239 retValue = strtol(data.substr(1).c_str(),0,2);
241 retValue = strtol(data.c_str(),0,10);
243 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_FIXED_CHOICE_DATA &&
244 columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
245 (
typeid(int) ==
typeid(retValue) ||
246 typeid(
unsigned int) ==
typeid(retValue)))
253 if(value == ViewColumnInfo::DATATYPE_STRING_DEFAULT)
257 std::vector<std::string> choices = columnsInfo_[col].getDataChoices();
258 for(retValue=1;retValue-1<(T)choices.size();++retValue)
259 if(value == choices[retValue-1])
262 __SS__ <<
"\tInvalid value for column data type: " << columnsInfo_[col].getDataType()
263 <<
" in configuration " << tableName_
264 <<
" at column=" << columnsInfo_[col].getName()
265 <<
" for getValue with type '" << ots_demangle(
typeid(retValue).name())
267 <<
"Attempting to get index of '" << value
268 <<
" in fixed choice array, but was not found in array. "
269 <<
"Here are the valid choices:\n";
270 ss <<
"\t" << ViewColumnInfo::DATATYPE_STRING_DEFAULT <<
"\n";
271 for(
const auto &choice:choices)
272 ss <<
"\t" << choice <<
"\n";
273 __MOUT__ <<
"\n" << ss.str();
274 throw std::runtime_error(ss.str());
277 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
278 typeid(bool) ==
typeid(retValue))
280 if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_ON_OFF)
281 retValue = (value == ViewColumnInfo::TYPE_VALUE_ON) ?
true:
false;
282 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_TRUE_FALSE)
283 retValue = (value == ViewColumnInfo::TYPE_VALUE_TRUE) ?
true:
false;
284 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_YES_NO)
285 retValue = (value == ViewColumnInfo::TYPE_VALUE_YES) ?
true:
false;
289 if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_FIXED_CHOICE_DATA)
290 __MOUT_WARN__ <<
"For column type " << ViewColumnInfo::TYPE_FIXED_CHOICE_DATA
291 <<
" the only valid numeric types are 'int' and 'unsigned int.'";
293 __SS__ <<
"\tUnrecognized column data type: " << columnsInfo_[col].getDataType()
294 <<
" and column type: " << columnsInfo_[col].getType()
295 <<
", in configuration " << tableName_
296 <<
" at column=" << columnsInfo_[col].getName()
297 <<
" for getValue with type '" << ots_demangle(
typeid(retValue).name())
299 throw std::runtime_error(ss.str());
306 std::string validateValueForColumn(
const std::string& value,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
308 std::string getValueAsString(
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
309 std::string getEscapedValueAsString(
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
310 bool isURIEncodedCommentTheSame(
const std::string &comment)
const;
312 const DataView& getDataView (
void)
const;
313 const std::vector<ViewColumnInfo>& getColumnsInfo (
void)
const;
314 std::vector<ViewColumnInfo>* getColumnsInfoP(
void);
319 void setUniqueStorageIdentifier (
const std::string &storageUID);
320 void setTableName (
const std::string &name );
321 void setComment (
const std::string &comment );
322 void setURIEncodedComment (
const std::string &uriComment );
323 void setAuthor (
const std::string &author );
324 void setCreationTime (time_t t );
325 void setLastAccessTime (time_t t = time(0) );
326 void setLooseColumnMatching (
bool setValue );
330 void setVersion (
const T &version)
339 void setValue(
const T &value,
unsigned int row,
unsigned int col)
341 if(!(col < columnsInfo_.size() && row < getNumberOfRows()))
343 __SS__ <<
"Invalid row (" << row <<
") col (" << col <<
") requested!" << std::endl;
344 throw std::runtime_error(ss.str());
347 if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_NUMBER)
349 std::stringstream ss;
351 theDataView_[row][col] = ss.str();
353 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_TIME &&
354 typeid(time_t) ==
typeid(value))
357 std::stringstream ss;
359 theDataView_[row][col] = ss.str();
363 __SS__ <<
"\tUnrecognized view data type: " << columnsInfo_[col].getDataType()
364 <<
" in configuration " << tableName_
365 <<
" at column=" << columnsInfo_[col].getName()
366 <<
" for setValue with type '" << ots_demangle(
typeid(value).name())
368 throw std::runtime_error(ss.str());
371 void setValue (
const std::string &value,
unsigned int row,
unsigned int col);
372 void setValue (
const char *value,
unsigned int row,
unsigned int col);
375 void setValueAsString (
const std::string &value,
unsigned int row,
unsigned int col);
378 void resizeDataView (
unsigned int nRows,
unsigned int nCols);
379 int addRow (
const std::string &author =
"");
380 void deleteRow (
int r);
385 iterator begin (
void) {
return theDataView_.begin();}
386 iterator end (
void) {
return theDataView_.end();}
387 const_iterator begin (
void)
const {
return theDataView_.begin();}
388 const_iterator end (
void)
const {
return theDataView_.end();}
390 void print (std::ostream &out = std::cout)
const;
391 void printJSON (std::ostream &out = std::cout)
const;
392 int fillFromJSON (
const std::string &json);
393 int fillFromCSV (
const std::string &data,
const int &dataOffset = 0,
const std::string &author =
"")
throw(std::runtime_error);
394 bool setURIEncodedValue (
const std::string &value,
const unsigned int &row,
const unsigned int &col,
const std::string &author =
"");
396 static std::string decodeURIComponent (
const std::string& data);
399 const unsigned int getOrInitColUID (
void);
404 std::string convertEnvVariables (
const std::string& data)
const;
405 bool isNumber (
const std::string& s)
const;
407 std::string uniqueStorageIdentifier_;
408 std::string tableName_ ;
410 std::string comment_ ;
411 std::string author_ ;
412 time_t creationTime_ ;
413 time_t lastAccessTime_ ;
414 unsigned int colUID_ ;
415 std::map<std::string, unsigned int> colLinkGroupIDs_;
417 bool fillWithLooseColumnMatching_;
418 unsigned int sourceColumnMismatchCount_, sourceColumnMissingCount_;
419 std::set<std::string> sourceColumnNames_;
421 std::vector<ViewColumnInfo> columnsInfo_ ;
422 DataView theDataView_ ;