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;
84 const unsigned int getColStatus (
void)
const;
90 bool isEntryInGroupCol (
const unsigned int& row,
const unsigned int& groupCol,
const std::string& groupNeedle, std::set<std::string>* groupIDList = 0)
const;
93 std::set<std::string> getSetOfGroupIDs (
const std::string& childLinkIndex,
unsigned int row = -1)
const;
94 bool isEntryInGroup (
const unsigned int& row,
const std::string& childLinkIndex,
const std::string& groupNeedle)
const;
95 const bool getChildLink (
const unsigned int& col,
bool& isGroup, std::pair<unsigned int /*link col*/, unsigned int /*link id col*/>& linkPair)
const;
96 const unsigned int getColLinkGroupID (
const std::string& childLinkIndex)
const;
97 void addRowToGroup (
const unsigned int& row,
const unsigned int& col,
const std::string& groupID);
98 bool removeRowFromGroup (
const unsigned int& row,
const unsigned int& col,
const std::string& groupID,
bool deleteRowIfNoGroupLeft=
false);
105 void getValue(T& value,
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const
107 if(!(col < columnsInfo_.size() && row < getNumberOfRows()))
109 __SS__ <<
"Invalid row col requested" << std::endl;
110 __COUT_ERR__ <<
"\n" << ss.str();
111 throw std::runtime_error(ss.str());
114 value = validateValueForColumn<T>(theDataView_[row][col],col,convertEnvironmentVariables);
118 void getValue(std::string& value,
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
127 T validateValueForColumn(
const std::string& value,
unsigned int col,
128 bool convertEnvironmentVariables=
true)
const
130 if(col >= columnsInfo_.size())
132 __SS__ <<
"Invalid col requested" << std::endl;
133 __COUT_ERR__ <<
"\n" << ss.str();
134 throw std::runtime_error(ss.str());
139 if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_NUMBER)
141 std::string data = convertEnvironmentVariables?convertEnvVariables(value):
146 __SS__ << (data +
" is not a number!") << std::endl;
147 __COUT__ <<
"\n" << ss.str();
148 throw std::runtime_error(ss.str());
151 if(
typeid(
double) ==
typeid(retValue))
152 retValue = strtod(data.c_str(),0);
153 else if(
typeid(
float) ==
typeid(retValue))
154 retValue = strtof(data.c_str(),0);
155 else if(data.size() > 2 && data[1] ==
'x')
156 retValue = strtol(data.c_str(),0,16);
157 else if(data.size() > 1 && data[0] ==
'b')
158 retValue = strtol(data.substr(1).c_str(),0,2);
160 retValue = strtol(data.c_str(),0,10);
162 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_FIXED_CHOICE_DATA &&
163 columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
164 (
typeid(int) ==
typeid(retValue) ||
165 typeid(
unsigned int) ==
typeid(retValue)))
172 if(value == ViewColumnInfo::DATATYPE_STRING_DEFAULT)
176 std::vector<std::string> choices = columnsInfo_[col].getDataChoices();
182 bool skipOne = (choices.size() &&
183 choices[0].find(
"arbitraryBool=") == 0);
185 for(retValue=1 + (skipOne?1:0);retValue-1<(T)choices.size();++retValue)
186 if(value == choices[retValue-1])
187 return retValue - (skipOne?1:0);
189 __SS__ <<
"\tInvalid value for column data type: " << columnsInfo_[col].getDataType()
190 <<
" in configuration " << tableName_
191 <<
" at column=" << columnsInfo_[col].getName()
192 <<
" for getValue with type '" << ots_demangle(
typeid(retValue).name())
194 <<
"Attempting to get index of '" << value
195 <<
" in fixed choice array, but was not found in array. "
196 <<
"Here are the valid choices:\n";
197 ss <<
"\t" << ViewColumnInfo::DATATYPE_STRING_DEFAULT <<
"\n";
198 for(
const auto &choice:choices)
199 ss <<
"\t" << choice <<
"\n";
200 __COUT__ <<
"\n" << ss.str();
201 throw std::runtime_error(ss.str());
204 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_STRING &&
205 typeid(bool) ==
typeid(retValue))
207 if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_ON_OFF)
208 retValue = (value == ViewColumnInfo::TYPE_VALUE_ON) ?
true:
false;
209 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_TRUE_FALSE)
210 retValue = (value == ViewColumnInfo::TYPE_VALUE_TRUE) ?
true:
false;
211 else if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_YES_NO)
212 retValue = (value == ViewColumnInfo::TYPE_VALUE_YES) ?
true:
false;
216 if(columnsInfo_[col].getType() == ViewColumnInfo::TYPE_FIXED_CHOICE_DATA)
217 __COUT_WARN__ <<
"For column type " << ViewColumnInfo::TYPE_FIXED_CHOICE_DATA
218 <<
" the only valid numeric types are 'int' and 'unsigned int.'";
220 __SS__ <<
"\tUnrecognized column data type: " << columnsInfo_[col].getDataType()
221 <<
" and column type: " << columnsInfo_[col].getType()
222 <<
", in configuration " << tableName_
223 <<
" at column=" << columnsInfo_[col].getName()
224 <<
" for getValue with type '" << ots_demangle(
typeid(retValue).name())
226 throw std::runtime_error(ss.str());
233 std::string validateValueForColumn(
const std::string& value,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
235 std::string getValueAsString(
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
236 std::string getEscapedValueAsString(
unsigned int row,
unsigned int col,
bool convertEnvironmentVariables=
true)
const;
237 bool isURIEncodedCommentTheSame(
const std::string &comment)
const;
239 const DataView& getDataView (
void)
const;
240 const std::vector<ViewColumnInfo>& getColumnsInfo (
void)
const;
241 std::vector<ViewColumnInfo>* getColumnsInfoP(
void);
246 void setUniqueStorageIdentifier (
const std::string &storageUID);
247 void setTableName (
const std::string &name );
248 void setComment (
const std::string &comment );
249 void setURIEncodedComment (
const std::string &uriComment );
250 void setAuthor (
const std::string &author );
251 void setCreationTime (time_t t );
252 void setLastAccessTime (time_t t = time(0) );
253 void setLooseColumnMatching (
bool setValue );
257 void setVersion (
const T &version)
266 void setValue(
const T &value,
unsigned int row,
unsigned int col)
268 if(!(col < columnsInfo_.size() && row < getNumberOfRows()))
270 __SS__ <<
"Invalid row (" << row <<
") col (" << col <<
") requested!" << std::endl;
271 throw std::runtime_error(ss.str());
274 if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_NUMBER)
276 std::stringstream ss;
278 theDataView_[row][col] = ss.str();
280 else if(columnsInfo_[col].getDataType() == ViewColumnInfo::DATATYPE_TIME &&
281 typeid(time_t) ==
typeid(value))
284 std::stringstream ss;
286 theDataView_[row][col] = ss.str();
290 __SS__ <<
"\tUnrecognized view data type: " << columnsInfo_[col].getDataType()
291 <<
" in configuration " << tableName_
292 <<
" at column=" << columnsInfo_[col].getName()
293 <<
" for setValue with type '" << ots_demangle(
typeid(value).name())
295 throw std::runtime_error(ss.str());
298 void setValue (
const std::string &value,
unsigned int row,
unsigned int col);
299 void setValue (
const char *value,
unsigned int row,
unsigned int col);
302 void setValueAsString (
const std::string &value,
unsigned int row,
unsigned int col);
305 void resizeDataView (
unsigned int nRows,
unsigned int nCols);
306 int addRow (
const std::string &author =
"", std::string baseNameAutoUID =
"");
307 void deleteRow (
int r);
312 iterator begin (
void) {
return theDataView_.begin();}
313 iterator end (
void) {
return theDataView_.end();}
314 const_iterator begin (
void)
const {
return theDataView_.begin();}
315 const_iterator end (
void)
const {
return theDataView_.end();}
317 void print (std::ostream &out = std::cout)
const;
318 void printJSON (std::ostream &out = std::cout)
const;
319 int fillFromJSON (
const std::string &json);
320 int fillFromCSV (
const std::string &data,
const int &dataOffset = 0,
const std::string &author =
"")
throw(std::runtime_error);
321 bool setURIEncodedValue (
const std::string &value,
const unsigned int &row,
const unsigned int &col,
const std::string &author =
"");
323 static std::string decodeURIComponent (
const std::string& data);
326 const unsigned int getOrInitColUID (
void);
327 const unsigned int getOrInitColStatus (
void);
333 std::string convertEnvVariables (
const std::string& data)
const;
334 bool isNumber (
const std::string& s)
const;
336 std::string uniqueStorageIdentifier_;
337 std::string tableName_ ;
339 std::string comment_ ;
340 std::string author_ ;
341 time_t creationTime_ ;
342 time_t lastAccessTime_ ;
343 unsigned int colUID_, colStatus_;
344 std::map<std::string, unsigned int> colLinkGroupIDs_;
346 bool fillWithLooseColumnMatching_;
347 unsigned int sourceColumnMismatchCount_, sourceColumnMissingCount_;
348 std::set<std::string> sourceColumnNames_;
350 std::vector<ViewColumnInfo> columnsInfo_ ;
351 DataView theDataView_ ;