00001 #include "otsdaq-core/ConfigurationInterface/ConfigurationTree.h"
00002 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationBase.h"
00003
00004 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00005
00006 #include <typeinfo>
00007
00008
00009 using namespace ots;
00010
00011
00012 #undef __MF_SUBJECT__
00013 #define __MF_SUBJECT__ "ConfigurationTree"
00014
00015 const std::string ConfigurationTree::DISCONNECTED_VALUE = "X";
00016 const std::string ConfigurationTree::VALUE_TYPE_DISCONNECTED = "Disconnected";
00017 const std::string ConfigurationTree::VALUE_TYPE_NODE = "Node";
00018
00019
00020 ConfigurationTree::ConfigurationTree()
00021 : configMgr_ (0),
00022 configuration_ (0),
00023 groupId_ (""),
00024 linkParentConfig_ (0),
00025 linkColName_ (""),
00026 linkColValue_ (""),
00027 disconnectedTargetName_ (""),
00028 disconnectedLinkID_ (""),
00029 childLinkIndex_ (""),
00030 row_ (0),
00031 col_ (0),
00032 configView_ (0)
00033 {
00034
00035
00036 }
00037
00038 ConfigurationTree::ConfigurationTree(const ConfigurationManager* const &configMgr, const ConfigurationBase* const &config)
00039 : ConfigurationTree(configMgr, config, "" , 0 ,
00040 "" , "" , "" ,
00041 "" , "" ,
00042 ConfigurationView::INVALID , ConfigurationView::INVALID )
00043 {
00044
00045
00046 }
00047
00048
00049 ConfigurationTree::ConfigurationTree(
00050 const ConfigurationManager* const& configMgr,
00051 const ConfigurationBase* const& config,
00052 const std::string& groupId,
00053 const ConfigurationBase* const& linkParentConfig,
00054 const std::string& linkColName,
00055 const std::string& linkColValue,
00056 const std::string& disconnectedTargetName,
00057 const std::string& disconnectedLinkID,
00058 const std::string& childLinkIndex,
00059 const unsigned int row,
00060 const unsigned int col)
00061 : configMgr_ (configMgr),
00062 configuration_ (config),
00063 groupId_ (groupId),
00064 linkParentConfig_ (linkParentConfig),
00065 linkColName_ (linkColName),
00066 linkColValue_ (linkColValue),
00067 disconnectedTargetName_ (disconnectedTargetName),
00068 disconnectedLinkID_ (disconnectedLinkID),
00069 childLinkIndex_ (childLinkIndex),
00070 row_ (row),
00071 col_ (col),
00072 configView_ (0)
00073 {
00074
00075
00076 if(!configMgr_)
00077 {
00078 std::stringstream ss;
00079 ss << __COUT_HDR_FL__ << "Invalid empty pointer given to tree!\n" <<
00080 "\n\tconfigMgr_=" << configMgr_ <<
00081 "\n\tconfiguration_=" << configuration_ <<
00082 "\n\tconfigView_=" << configView_ <<
00083 std::endl;
00084 __MOUT__ << "\n" << ss.str();
00085 throw std::runtime_error(ss.str());
00086 }
00087
00088 if(configuration_)
00089 configView_ = &(configuration_->getView());
00090
00091
00092 if(configView_ &&
00093 configView_->getColumnInfo(configView_->getColUID()).getType() != ViewColumnInfo::TYPE_UID)
00094 {
00095 __SS__ << "Missing UID column (must column of type " << ViewColumnInfo::TYPE_UID <<
00096 ") in config view : " << configView_->getTableName() << std::endl;
00097 __MOUT__ << "\n" << ss.str();
00098 throw std::runtime_error(ss.str());
00099 }
00100 }
00101
00102
00103
00104 ConfigurationTree::~ConfigurationTree(void)
00105 {
00106
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 void ConfigurationTree::print(const unsigned int &depth, std::ostream &out) const
00116 {
00117 recursivePrint(*this,depth,out,"\t");
00118 }
00119
00120
00121 void ConfigurationTree::recursivePrint(const ConfigurationTree &t, unsigned int depth, std::ostream &out, std::string space)
00122 {
00123 if(t.isValueNode())
00124 out << space << t.getValueName() << " :\t" << t.getValueAsString() << std::endl;
00125 else
00126 {
00127 if(t.isLinkNode())
00128 {
00129 out << space << t.getValueName();
00130 if(t.isDisconnected())
00131 {
00132 out << " :\t" << t.getValueAsString() << std::endl;
00133 return;
00134 }
00135 out << " (" <<
00136 (t.isGroupLinkNode()?"Group":"U") <<
00137 "ID=" << t.getValueAsString() <<
00138 ") : " << std::endl;
00139 }
00140 else
00141 out << space << t.getValueAsString() << " : " << std::endl;
00142
00143
00144
00145 if(depth >= 1)
00146 {
00147 auto C = t.getChildren();
00148 if(!C.empty())
00149 out << space << "{" << std::endl;
00150 for(auto &c:C)
00151 recursivePrint(c.second,depth-1,out,space + " ");
00152 if(!C.empty())
00153 out << space << "}" << std::endl;
00154 }
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163 void ConfigurationTree::getValue(std::string& value) const
00164 {
00165
00166
00167 if(row_ != ConfigurationView::INVALID && col_ != ConfigurationView::INVALID)
00168 {
00169
00170 try
00171 {
00172 ConfigurationTree valueAsTreeNode = getValueAsTreeNode();
00173
00174 __MOUT__ << "Success following path to tree node!" << std::endl;
00175
00176
00177
00178
00179
00180
00181 value = configView_->validateValueForColumn(
00182 valueAsTreeNode.getValueAsString(),col_);
00183
00184
00185 __MOUT__ << "Successful value!" << std::endl;
00186
00187
00188
00189
00190
00191 return;
00192 }
00193 catch(...)
00194 {
00195
00196 }
00197
00198
00199 configView_->getValue(value,row_,col_);
00200 }
00201 else if(row_ == ConfigurationView::INVALID && col_ == ConfigurationView::INVALID)
00202 {
00203 if(isLinkNode() && isDisconnected())
00204 value = (groupId_ == "") ? getValueName():groupId_;
00205 else
00206 value = (groupId_ == "") ? configuration_->getConfigurationName():groupId_;
00207 }
00208 else if(row_ == ConfigurationView::INVALID)
00209 {
00210 __MOUT__ << std::endl;
00211 throw std::runtime_error("Malformed ConfigurationTree");
00212 }
00213 else if(col_ == ConfigurationView::INVALID)
00214 configView_->getValue(value,row_,configView_->getColUID());
00215 else
00216 {
00217 __MOUT__ << std::endl;
00218 throw std::runtime_error("Impossible.");
00219 }
00220
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00234
00235 std::string ConfigurationTree::getValue() const
00236 {
00237 std::string value;
00238 ConfigurationTree::getValue(value);
00239 return value;
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 }
00268
00269
00270
00271
00272 std::string ConfigurationTree::getEscapedValue() const
00273 {
00274 if(row_ != ConfigurationView::INVALID && col_ != ConfigurationView::INVALID)
00275 return configView_->getEscapedValueAsString(row_,col_);
00276
00277 __SS__ << "Can't get escaped value except from a value node!" <<
00278 " This node is type '" << getNodeType() << "." << std::endl;
00279 __MOUT_ERR__ << "\n" << ss.str();
00280 throw std::runtime_error(ss.str());
00281 }
00282
00283
00284
00285 const std::string& ConfigurationTree::getConfigurationName(void) const
00286 {
00287 if(!configuration_)
00288 {
00289 __SS__ << "Can't get configuration name of node with no configuration pointer!" << std::endl;
00290 throw std::runtime_error(ss.str());
00291 }
00292 return configuration_->getConfigurationName();
00293 }
00294
00295
00296
00297 const std::string& ConfigurationTree::getDisconnectedTableName(void) const
00298 {
00299 if(isLinkNode() && isDisconnected()) return disconnectedTargetName_;
00300
00301 __SS__ << "Can't get disconnected target name of node unless it is a disconnected link node!" << std::endl;
00302 throw std::runtime_error(ss.str());
00303 }
00304
00305
00306
00307 const std::string& ConfigurationTree::getDisconnectedLinkID(void) const
00308 {
00309 if(isLinkNode() && isDisconnected()) return disconnectedLinkID_;
00310
00311 __SS__ << "Can't get disconnected target name of node unless it is a disconnected link node!" << std::endl;
00312 throw std::runtime_error(ss.str());
00313 }
00314
00315
00316
00317 const ConfigurationVersion& ConfigurationTree::getConfigurationVersion(void) const
00318 {
00319 if(!configView_)
00320 {
00321 __SS__ << "Can't get configuration version of node with no config view pointer!" << std::endl;
00322 throw std::runtime_error(ss.str());
00323 }
00324 return configView_->getVersion();
00325 }
00326
00327
00328
00329 const time_t& ConfigurationTree::getConfigurationCreationTime(void) const
00330 {
00331 if(!configView_)
00332 {
00333 __SS__ << "Can't get configuration creation time of node with no config view pointer!" << std::endl;
00334 throw std::runtime_error(ss.str());
00335 }
00336 return configView_->getCreationTime();
00337 }
00338
00339
00340
00341
00342
00343
00344 std::vector<std::string> ConfigurationTree::getFixedChoices(void) const
00345 {
00346 if(!configView_)
00347 {
00348 __SS__ << "Can't get fixed choices of node with no config view pointer!" << std::endl;
00349 throw std::runtime_error(ss.str());
00350 }
00351
00352 if(getValueType() != ViewColumnInfo::TYPE_FIXED_CHOICE_DATA &&
00353 getValueType() != ViewColumnInfo::TYPE_BITMAP_DATA &&
00354 !isLinkNode())
00355 {
00356 __SS__ << "Can't get fixed choices of node with value type of '" <<
00357 getValueType() << ".' Node must be a link or a value node with type '" <<
00358 ViewColumnInfo::TYPE_BITMAP_DATA << "' or '" <<
00359 ViewColumnInfo::TYPE_FIXED_CHOICE_DATA << ".'" << std::endl;
00360 throw std::runtime_error(ss.str());
00361 }
00362
00363 std::vector<std::string> retVec;
00364
00365 if(isLinkNode())
00366 {
00367 if(!linkParentConfig_)
00368 {
00369 __SS__ << "Can't get fixed choices of node with no parent config view pointer!" << std::endl;
00370 throw std::runtime_error(ss.str());
00371 }
00372
00373 __MOUT__ << getChildLinkIndex() << std::endl;
00374 __MOUT__ << linkColName_ << std::endl;
00375
00376
00377
00378
00379 const ConfigurationView* parentView = &(linkParentConfig_->getView());
00380 int c = parentView->findCol(linkColName_);
00381
00382 __MOUT__ << "Link " << c << std::endl;
00383
00384 std::pair<unsigned int , unsigned int > linkPair;
00385 bool isGroupLink;
00386 parentView->getChildLink(c, isGroupLink, linkPair);
00387 c = linkPair.first;
00388
00389 __MOUT__ << "Link " << c << std::endl;
00390
00391 std::vector<std::string> choices = parentView->getColumnInfo(c).getDataChoices();
00392 for(const auto &choice:choices)
00393 retVec.push_back(choice);
00394
00395 return retVec;
00396 }
00397
00398
00399 retVec.push_back(configView_->getColumnInfo(col_).getDefaultValue());
00400 std::vector<std::string> choices = configView_->getColumnInfo(col_).getDataChoices();
00401 for(const auto &choice:choices)
00402 retVec.push_back(choice);
00403
00404 return retVec;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 const std::string& ConfigurationTree::getValueAsString(bool returnLinkTableValue) const
00416 {
00417 if(isLinkNode())
00418 {
00419 if(returnLinkTableValue)
00420 return linkColValue_;
00421 else if(isDisconnected())
00422 return ConfigurationTree::DISCONNECTED_VALUE;
00423 else if(row_ == ConfigurationView::INVALID && col_ == ConfigurationView::INVALID)
00424 return (groupId_ == "")?configuration_->getConfigurationName():groupId_;
00425 else if(col_ == ConfigurationView::INVALID)
00426 return configView_->getDataView()[row_][configView_->getColUID()];
00427 else
00428 {
00429 __MOUT__ << std::endl;
00430 throw std::runtime_error("Impossible Link.");
00431 }
00432 }
00433 else if(row_ != ConfigurationView::INVALID && col_ != ConfigurationView::INVALID)
00434 return configView_->getDataView()[row_][col_];
00435 else if(row_ == ConfigurationView::INVALID && col_ == ConfigurationView::INVALID)
00436 return (groupId_ == "")?configuration_->getConfigurationName():groupId_;
00437 else if(row_ == ConfigurationView::INVALID)
00438 {
00439 __MOUT__ << std::endl;
00440 throw std::runtime_error("Malformed ConfigurationTree");
00441 }
00442 else if(col_ == ConfigurationView::INVALID)
00443 return configView_->getDataView()[row_][configView_->getColUID()];
00444 else
00445 {
00446 __MOUT__ << std::endl;
00447 throw std::runtime_error("Impossible.");
00448 }
00449 }
00450
00451
00452
00453
00454
00455 const std::string& ConfigurationTree::getUIDAsString(void) const
00456 {
00457 if(isValueNode() || isUIDLinkNode())
00458 return configView_->getDataView()[row_][configView_->getColUID()];
00459
00460 {
00461 __SS__ << "Can't get UID of node with type '" <<
00462 getNodeType() << ".' Node type must be '" <<
00463 ConfigurationTree::NODE_TYPE_VALUE << "' or '" <<
00464 ConfigurationTree::NODE_TYPE_UID_LINK << ".'" << std::endl;
00465 throw std::runtime_error(ss.str());
00466 }
00467 }
00468
00469
00470
00471
00472 const std::string& ConfigurationTree::getValueDataType(void) const
00473 {
00474 if(isValueNode())
00475 return configView_->getColumnInfo(col_).getDataType();
00476 else
00477 return ViewColumnInfo::DATATYPE_STRING;
00478 }
00479
00480
00481
00482
00483 bool ConfigurationTree::isDefaultValue(void) const
00484 {
00485 if(!isValueNode()) return false;
00486
00487 if(getValueDataType() == ViewColumnInfo::DATATYPE_STRING)
00488 {
00489 if(getValueType() == ViewColumnInfo::TYPE_ON_OFF ||
00490 getValueType() == ViewColumnInfo::TYPE_TRUE_FALSE ||
00491 getValueType() == ViewColumnInfo::TYPE_YES_NO)
00492 return getValueAsString() == ViewColumnInfo::DATATYPE_BOOL_DEFAULT;
00493 else if(getValueType() == ViewColumnInfo::TYPE_COMMENT)
00494 return getValueAsString() == ViewColumnInfo::DATATYPE_COMMENT_DEFAULT ||
00495 getValueAsString() == "";
00496 else
00497 return getValueAsString() == ViewColumnInfo::DATATYPE_STRING_DEFAULT;
00498 }
00499 else if(getValueDataType() == ViewColumnInfo::DATATYPE_NUMBER)
00500 return getValueAsString() == ViewColumnInfo::DATATYPE_NUMBER_DEFAULT;
00501 else if(getValueDataType() == ViewColumnInfo::DATATYPE_TIME)
00502 return getValueAsString() == ViewColumnInfo::DATATYPE_TIME_DEFAULT;
00503 else
00504 return false;
00505 }
00506
00507
00508
00509
00510 const std::string& ConfigurationTree::getValueType(void) const
00511 {
00512 if(isValueNode())
00513 return configView_->getColumnInfo(col_).getType();
00514 else if(isLinkNode() && isDisconnected())
00515 return ConfigurationTree::VALUE_TYPE_DISCONNECTED;
00516 else
00517 return ConfigurationTree::VALUE_TYPE_NODE;
00518 }
00519
00520
00521
00522
00523 const ViewColumnInfo& ConfigurationTree::getColumnInfo(void) const
00524 {
00525 if(isValueNode())
00526 return configView_->getColumnInfo(col_);
00527 else
00528 {
00529 __SS__ << "Can only get column info from a value node! " <<
00530 "The node type is " << getNodeType() << std::endl;
00531 __MOUT__ << "\n" << ss.str() << std::endl;
00532 throw std::runtime_error(ss.str());
00533 }
00534
00535 }
00536
00537
00538
00539 const unsigned int& ConfigurationTree::getRow(void) const
00540 { return row_; }
00541
00542
00543
00544 const unsigned int& ConfigurationTree::getColumn(void) const
00545 { return col_; }
00546
00547
00548
00549 const std::string& ConfigurationTree::getChildLinkIndex(void) const
00550 {
00551 if(!isLinkNode())
00552 {
00553 __SS__ << "Can only get link ID from a link! " <<
00554 "The node type is " << getNodeType() << std::endl;
00555 __MOUT__ << "\n" << ss.str() << std::endl;
00556 throw std::runtime_error(ss.str());
00557 }
00558 return childLinkIndex_;
00559 }
00560
00561
00562
00563
00564 const std::string& ConfigurationTree::getValueName(void) const
00565 {
00566 if(isValueNode())
00567 return configView_->getColumnInfo(col_).getName();
00568 else if(isLinkNode())
00569 return linkColName_;
00570 else
00571 {
00572 __SS__ << "Can only get value name of a value node!" << std::endl;
00573 __MOUT__ << "\n" << ss.str() << std::endl;
00574 throw std::runtime_error(ss.str());
00575 }
00576
00577 }
00578
00579
00580
00581
00582 ConfigurationTree ConfigurationTree::recurse(const ConfigurationTree& tree,
00583 const std::string& childPath, bool doNotThrowOnBrokenUIDLinks)
00584 {
00585
00586
00587 if(childPath.length() <= 1)
00588 return tree;
00589 return tree.getNode(childPath,doNotThrowOnBrokenUIDLinks);
00590 }
00591
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625 ConfigurationTree ConfigurationTree::getNode(const std::string &nodeString,
00626 bool doNotThrowOnBrokenUIDLinks) const
00627 {
00628
00629
00630
00631
00632 if(nodeString.length() < 1) throw std::runtime_error("Invalid node name!");
00633
00634 bool startingSlash = nodeString[0] == '/';
00635
00636 std::string nodeName = nodeString.substr(startingSlash?1:0, nodeString.find('/',1)-(startingSlash?1:0));
00637
00638
00639 std::string childPath = nodeString.substr(nodeName.length() + (startingSlash?1:0));
00640
00641
00642
00643
00644 try
00645 {
00646
00647
00648 if(row_ == ConfigurationView::INVALID && col_ == ConfigurationView::INVALID)
00649 {
00650 if(!configView_)
00651 {
00652 __SS__ << "Missing configView pointer! Likely attempting to access a child node through a disconnected link node." << std::endl;
00653 __MOUT_ERR__ << "\n" << ss.str();
00654 throw std::runtime_error(ss.str());
00655 }
00656
00657
00658 return recurse(ConfigurationTree(
00659 configMgr_,
00660 configuration_,
00661 "",
00662 0 ,
00663 "",
00664 "",
00665 "",
00666 "",
00667 "",
00668
00669 (groupId_ == "")?
00670 configView_->findRow(configView_->getColUID(),nodeName)
00671 : configView_->findRowInGroup(configView_->getColUID(),
00672 nodeName,groupId_,childLinkIndex_) ),
00673 childPath, doNotThrowOnBrokenUIDLinks);
00674 }
00675 else if(row_ == ConfigurationView::INVALID)
00676 {
00677 __SS__ << "Malformed ConfigurationTree" << std::endl;
00678 __MOUT_ERR__ << "\n" << ss.str();
00679 throw std::runtime_error(ss.str());
00680 }
00681 else if(col_ == ConfigurationView::INVALID)
00682 {
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693 if(!configView_)
00694 {
00695 __SS__ << "Missing configView pointer! Likely attempting to access a child node through a disconnected link node." << std::endl;
00696 __MOUT_ERR__ << "\n" << ss.str();
00697 throw std::runtime_error(ss.str());
00698 }
00699
00700 unsigned int c = configView_->findCol(nodeName);
00701 std::pair<unsigned int , unsigned int > linkPair;
00702 bool isGroupLink, isLink;
00703 if((isLink = configView_->getChildLink(c, isGroupLink, linkPair)) &&
00704 !isGroupLink)
00705 {
00706
00707
00708
00709
00710
00711 const ConfigurationBase* childConfig;
00712 try
00713 {
00714 childConfig = configMgr_->getConfigurationByName(configView_->getDataView()[row_][linkPair.first]);
00715 childConfig->getView();
00716
00717 if(doNotThrowOnBrokenUIDLinks)
00718 {
00719 childConfig->getView().findRow(childConfig->getView().getColUID(),
00720 configView_->getDataView()[row_][linkPair.second]);
00721 }
00722 }
00723 catch(...)
00724 {
00725
00726
00727
00728
00729
00730
00731 return ConfigurationTree(
00732 configMgr_,
00733 0,
00734 "",
00735 configuration_,
00736 nodeName,
00737 configView_->getDataView()[row_][c],
00738 configView_->getDataView()[row_][linkPair.first],
00739 configView_->getDataView()[row_][linkPair.second],
00740 configView_->getColumnInfo(c).getChildLinkIndex());
00741 }
00742
00743 return recurse(
00744 ConfigurationTree(
00745 configMgr_,
00746 childConfig,
00747 "",
00748 configuration_,
00749 nodeName,
00750 configView_->getDataView()[row_][c],
00751 "",
00752 "",
00753 configView_->getColumnInfo(c).getChildLinkIndex(),
00754 childConfig->getView().findRow(childConfig->getView().getColUID(),
00755 configView_->getDataView()[row_][linkPair.second])
00756 ),
00757 childPath, doNotThrowOnBrokenUIDLinks);
00758 }
00759 else if(isLink)
00760 {
00761
00762
00763
00764
00765
00766 const ConfigurationBase* childConfig;
00767 try
00768 {
00769 childConfig = configMgr_->getConfigurationByName(
00770 configView_->getDataView()[row_][linkPair.first]);
00771 childConfig->getView();
00772 }
00773 catch(...)
00774 {
00775 if(configView_->getDataView()[row_][linkPair.first] !=
00776 ViewColumnInfo::DATATYPE_LINK_DEFAULT)
00777 __MOUT_WARN__ << "Found disconnected node! Failed link target from nodeName=" <<
00778 nodeName << " to table:id=" <<
00779 configView_->getDataView()[row_][linkPair.first] << ":" <<
00780 configView_->getDataView()[row_][linkPair.second] <<
00781 std::endl;
00782
00783
00784 return ConfigurationTree(configMgr_,0,
00785 configView_->getDataView()[row_][linkPair.second],
00786 configuration_,
00787 nodeName,
00788 configView_->getDataView()[row_][c],
00789 configView_->getDataView()[row_][linkPair.first],
00790 configView_->getDataView()[row_][linkPair.second],
00791 configView_->getColumnInfo(c).getChildLinkIndex()
00792 );
00793 }
00794
00795 return recurse(
00796 ConfigurationTree(
00797 configMgr_,
00798 childConfig,
00799 configView_->getDataView()[row_][linkPair.second],
00800 configuration_,
00801 nodeName,
00802 configView_->getDataView()[row_][c],
00803 "",
00804 "",
00805 configView_->getColumnInfo(c).getChildLinkIndex()
00806 ),
00807 childPath, doNotThrowOnBrokenUIDLinks);
00808 }
00809 else
00810 {
00811
00812
00813 return ConfigurationTree(
00814 configMgr_,
00815 configuration_,"",
00816 0 ,
00817 "","","","","",
00818 row_,c);
00819 }
00820 }
00821
00822 }
00823 catch(std::runtime_error &e)
00824 {
00825 __SS__ << "\n\nError occurred descending from node '" << getValue() <<
00826 "' in table '" << getConfigurationName() <<
00827 "' looking for child '" << nodeName << "'\n\n" << std::endl;
00828 ss << "--- Additional error detail: \n\n" << e.what() << std::endl;
00829 throw std::runtime_error(ss.str());
00830 }
00831 catch(...)
00832 {
00833 __SS__ << "\n\nError occurred descending from node '" << getValue() <<
00834 "' in table '" << getConfigurationName() <<
00835 "' looking for child '" << nodeName << "'\n\n" << std::endl;
00836 throw std::runtime_error(ss.str());
00837 }
00838
00839
00840 __SS__ << "\n\nError occurred descending from node '" << getValue() <<
00841 "' in table '" << getConfigurationName() <<
00842 "' looking for child '" << nodeName << "'\n\n" <<
00843 "Invalid depth! getNode() called from a value point in the Configuration Tree." << std::endl;
00844 throw std::runtime_error(ss.str());
00845 }
00846
00847
00848
00849 ConfigurationTree ConfigurationTree::getBackNode(std::string nodeName, unsigned int backSteps) const
00850 {
00851 for(unsigned int i=0; i<backSteps; i++)
00852 nodeName = nodeName.substr(0, nodeName.find_last_of('/'));
00853
00854 return getNode(nodeName);
00855 }
00856
00857
00858
00859
00860 bool ConfigurationTree::isValueNode(void) const
00861 {
00862 return (row_ != ConfigurationView::INVALID && col_ != ConfigurationView::INVALID);
00863 }
00864
00865
00866
00867
00868
00869
00870 bool ConfigurationTree::isDisconnected(void) const
00871 {
00872 if(!isLinkNode())
00873 {
00874 __SS__ << "\n\nError occurred testing link connection at node with value '" <<
00875 getValue() <<
00876 "' in table '" << getConfigurationName() <<
00877 "'\n\n" << std::endl;
00878 ss << "This is not a Link node! It is node type '" <<
00879 getNodeType() << ".' Only a Link node can be disconnected." << std::endl;
00880 __MOUT__ << "\n" << ss.str();
00881 throw std::runtime_error(ss.str());
00882 }
00883
00884 return !configuration_ || !configView_;
00885 }
00886
00887
00888
00889
00890 bool ConfigurationTree::isLinkNode(void) const
00891 {
00892 return linkColName_ != "";
00893 }
00894
00895
00896
00897
00898 const std::string ConfigurationTree::NODE_TYPE_GROUP_TABLE = "GroupConfigurationNode";
00899 const std::string ConfigurationTree::NODE_TYPE_TABLE = "ConfigurationNode";
00900 const std::string ConfigurationTree::NODE_TYPE_GROUP_LINK = "GroupLinkNode";
00901 const std::string ConfigurationTree::NODE_TYPE_UID_LINK = "UIDLinkNode";
00902 const std::string ConfigurationTree::NODE_TYPE_VALUE = "ValueNode";
00903 const std::string ConfigurationTree::NODE_TYPE_UID = "UIDNode";
00904 std::string ConfigurationTree::getNodeType(void) const
00905 {
00906 if(isConfigurationNode() && groupId_ != "") return ConfigurationTree::NODE_TYPE_GROUP_TABLE;
00907 if(isConfigurationNode()) return ConfigurationTree::NODE_TYPE_TABLE;
00908 if(isGroupLinkNode()) return ConfigurationTree::NODE_TYPE_GROUP_LINK;
00909 if(isLinkNode()) return ConfigurationTree::NODE_TYPE_UID_LINK;
00910 if(isValueNode()) return ConfigurationTree::NODE_TYPE_VALUE;
00911 return ConfigurationTree::NODE_TYPE_UID;
00912 }
00913
00914
00915
00916
00917 bool ConfigurationTree::isGroupLinkNode(void) const
00918 {
00919 return (isLinkNode() && groupId_ != "");
00920 }
00921
00922
00923
00924
00925 bool ConfigurationTree::isUIDLinkNode(void) const
00926 {
00927 return (isLinkNode() && groupId_ == "");
00928 }
00929
00930
00931
00932
00933 bool ConfigurationTree::isUIDNode(void) const
00934 {
00935 return (row_ != ConfigurationView::INVALID && col_ == ConfigurationView::INVALID);
00936 }
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955 std::vector<ConfigurationTree::RecordField> ConfigurationTree::getCommonFields(
00956 const std::vector<std::string /*uid*/> &recordList,
00957 const std::vector<std::string /*relative-path*/> &fieldAcceptList,
00958 const std::vector<std::string /*relative-path*/> &fieldRejectList,
00959 unsigned int depth) const
00960 {
00961
00962 if(!isConfigurationNode())
00963 {
00964 __SS__ << "Can only get getCommonFields from a table node! " <<
00965 "The node type is " << getNodeType() << std::endl;
00966 __MOUT__ << "\n" << ss.str() << std::endl;
00967 throw std::runtime_error(ss.str());
00968 }
00969
00970 std::vector<ConfigurationTree::RecordField> fieldCandidateList;
00971 std::vector<int> fieldCount;
00972
00973 --depth;
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008 bool found;
01009 auto tableName = getConfigurationName();
01010
01011 for(unsigned int i=0;i<recordList.size();++i)
01012 {
01013
01014
01015 auto recordChildren = getNode(recordList[i]).getChildren();
01016 for(const auto &fieldNode : recordChildren)
01017 {
01018
01019
01020 if(fieldNode.second.isValueNode())
01021 {
01022
01023 if(fieldNode.second.getColumnInfo().getType() ==
01024 ViewColumnInfo::TYPE_AUTHOR ||
01025 fieldNode.second.getColumnInfo().getType() ==
01026 ViewColumnInfo::TYPE_TIMESTAMP)
01027 continue;
01028
01029
01030 if(!i)
01031 {
01032
01033 found = fieldAcceptList.size()?false:true;
01034 for(const auto &fieldFilter : fieldAcceptList)
01035 if(ConfigurationTree::wildCardMatch(
01036 fieldFilter,fieldNode.first))
01037 {
01038 found = true;
01039 break;
01040 }
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070 if(found)
01071 {
01072
01073
01074 found = true;
01075 for(const auto &fieldFilter : fieldRejectList)
01076 if(ConfigurationTree::wildCardMatch(
01077 fieldFilter,fieldNode.first))
01078 {
01079 found = false;
01080 break;
01081 }
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111 }
01112
01113
01114 if(found)
01115 {
01116 fieldCandidateList.push_back(
01117 ConfigurationTree::RecordField(
01118 tableName,
01119 recordList[i],
01120 fieldNode.first,
01121 "",
01122 &fieldNode.second.getColumnInfo()
01123 ));
01124 fieldCount.push_back(-1);
01125 }
01126 }
01127
01128
01129 }
01130 else if(depth > 0 &&
01131 fieldNode.second.isUIDLinkNode() &&
01132 !fieldNode.second.isDisconnected())
01133 {
01134
01135 fieldNode.second.recursiveGetCommonFields(
01136 fieldCandidateList,
01137 fieldCount,
01138 fieldAcceptList,
01139 fieldRejectList,
01140 depth,
01141 fieldNode.first + "/",
01142 !i
01143 );
01144 }
01145 }
01146
01147 }
01148
01149
01150
01151
01152
01153
01154 for(unsigned int i=0;i<fieldCandidateList.size();++i)
01155 {
01156
01157
01158
01159 if(fieldCount[i] != -1 &&
01160 fieldCount[i] != (int)recordList.size())
01161 {
01162
01163
01164
01165 fieldCount.erase(fieldCount.begin() + i);
01166 fieldCandidateList.erase(fieldCandidateList.begin() + i);
01167 --i;
01168 }
01169 }
01170
01171
01172
01173
01174
01175 return fieldCandidateList;
01176 }
01177
01178
01179
01180
01181
01182
01183 std::set<std::string > ConfigurationTree::getUniqueValuesForField(
01184 const std::vector<std::string /*relative-path*/> &recordList,
01185 const std::string &fieldName) const
01186 {
01187
01188 if(!isConfigurationNode())
01189 {
01190 __SS__ << "Can only get getCommonFields from a table node! " <<
01191 "The node type is " << getNodeType() << std::endl;
01192 __MOUT__ << "\n" << ss.str() << std::endl;
01193 throw std::runtime_error(ss.str());
01194 }
01195
01196 std::set<std::string > uniqueValues;
01197
01198
01199
01200
01201
01202
01203 for(unsigned int i=0;i<recordList.size();++i)
01204 {
01205 __MOUT__ << "Checking " << recordList[i] << std::endl;
01206
01207
01208
01209
01210
01211
01212
01213 uniqueValues.emplace(getNode(recordList[i]).getNode(fieldName).getValueAsString(true));
01214 }
01215
01216 return uniqueValues;
01217 }
01218
01219
01220
01221
01222 void ConfigurationTree::recursiveGetCommonFields(
01223 std::vector<ConfigurationTree::RecordField> &fieldCandidateList,
01224 std::vector<int> &fieldCount,
01225 const std::vector<std::string /*relative-path*/> &fieldAcceptList,
01226 const std::vector<std::string /*relative-path*/> &fieldRejectList,
01227 unsigned int depth,
01228 const std::string &relativePathBase,
01229 bool inFirstRecord
01230 ) const
01231 {
01232 --depth;
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254 bool found;
01255 auto tableName = getConfigurationName();
01256 auto uid = getUIDAsString();
01257 unsigned int j;
01258
01259 auto recordChildren = getChildren();
01260 for(const auto &fieldNode : recordChildren)
01261 {
01262 if(fieldNode.second.isValueNode())
01263 {
01264
01265 if(fieldNode.second.getColumnInfo().getType() ==
01266 ViewColumnInfo::TYPE_AUTHOR ||
01267 fieldNode.second.getColumnInfo().getType() ==
01268 ViewColumnInfo::TYPE_TIMESTAMP)
01269 continue;
01270
01271
01272 if(inFirstRecord)
01273 {
01274
01275 found = fieldAcceptList.size()?false:true;
01276 for(const auto &fieldFilter : fieldAcceptList)
01277 if(ConfigurationTree::wildCardMatch(
01278 fieldFilter,fieldNode.first))
01279 {
01280 found = true;
01281 break;
01282 }
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313 if(found)
01314 {
01315
01316
01317 found = true;
01318 for(const auto &fieldFilter : fieldRejectList)
01319 if(ConfigurationTree::wildCardMatch(
01320 fieldFilter,fieldNode.first))
01321 {
01322 found = false;
01323 break;
01324 }
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354 }
01355
01356
01357 if(found)
01358 {
01359
01360
01361 fieldCandidateList.push_back(
01362 ConfigurationTree::RecordField(
01363 tableName,
01364 uid,
01365 fieldNode.first,
01366 relativePathBase,
01367 &fieldNode.second.getColumnInfo()
01368 ));
01369 fieldCount.push_back(1);
01370 }
01371 }
01372 else
01373 {
01374
01375
01376 for(j=0;j<fieldCandidateList.size();++j)
01377 {
01378 if((relativePathBase + fieldNode.first) ==
01379 (fieldCandidateList[j].relativePath_ +
01380 fieldCandidateList[j].columnName_))
01381 {
01382
01383
01384
01385 ++fieldCount[j];
01386 break;
01387 }
01388 }
01389 }
01390 }
01391 else if(depth > 0 &&
01392 fieldNode.second.isUIDLinkNode() &&
01393 !fieldNode.second.isDisconnected())
01394 {
01395
01396 fieldNode.second.recursiveGetCommonFields(
01397 fieldCandidateList,
01398 fieldCount,
01399 fieldAcceptList,
01400 fieldRejectList,
01401 depth,
01402 (relativePathBase + fieldNode.first) + "/",
01403 inFirstRecord
01404 );
01405 }
01406 }
01407 }
01408
01409
01410
01411
01412
01413 std::vector<std::pair<std::string,ConfigurationTree> > ConfigurationTree::getChildren(
01414 std::map<std::string /*relative-path*/, std::string /*value*/> filterMap) const
01415 {
01416 std::vector<std::pair<std::string,ConfigurationTree> > retMap;
01417
01418
01419
01420 bool filtering = filterMap.size();
01421 bool skip;
01422 std::string fieldValue;
01423
01424 std::vector<std::string> childrenNames = getChildrenNames();
01425 for(auto &childName : childrenNames)
01426 {
01427
01428
01429 if(filtering)
01430 {
01431
01432 skip = false;
01433
01434
01435 for(const auto &filterPair:filterMap)
01436 {
01437 std::string filterPath = childName + "/" + filterPair.first;
01438 try
01439 {
01440
01441
01442 std::istringstream f(filterPair.second);
01443
01444 skip = true;
01445
01446 while (getline(f, fieldValue, ','))
01447 {
01448
01449
01450
01451
01452
01453
01454
01455 __MOUT__ << "\t\tCheck: " << filterPair.first <<
01456 " == " << fieldValue << " ??? " <<
01457 this->getNode(filterPath).getValueAsString(true) <<
01458 std::endl;
01459
01460 if(ConfigurationTree::wildCardMatch(
01461 ConfigurationView::decodeURIComponent(fieldValue),
01462 this->getNode(filterPath).getValueAsString(true) ))
01463 {
01464
01465 skip = false;
01466 break;
01467 }
01468
01469
01470
01471
01472
01473
01474
01475
01476 }
01477 }
01478 catch(...)
01479 {
01480 __SS__ << "Failed to access filter path '" <<
01481 filterPath << "' - aborting." << std::endl;
01482 __MOUT_ERR__ << "\n" << ss.str();
01483 throw std::runtime_error(ss.str());
01484 }
01485
01486 if(skip) break;
01487 }
01488
01489 if(skip) continue;
01490
01491 __MOUT__ << "\tChild accepted: " << childName << std::endl;
01492 }
01493
01494 retMap.push_back(std::pair<std::string,ConfigurationTree>(childName,
01495 this->getNode(childName, true)));
01496 }
01497
01498
01499 return retMap;
01500 }
01501
01502
01503
01504
01505
01506 bool ConfigurationTree::wildCardMatch(const std::string& needle, const std::string& haystack)
01507 try
01508 {
01509
01510
01511
01512
01513 if(needle.size() == 0)
01514 return true;
01515
01516 if(needle[0] == '*' &&
01517 needle[needle.size()-1] == '*' )
01518 return std::string::npos != haystack.find(needle.substr(1,needle.size()-2));
01519
01520 if(needle[0] == '*')
01521 return needle.substr(1) ==
01522 haystack.substr(haystack.size() - (needle.size()-1));
01523
01524 if(needle[needle.size()-1] == '*')
01525 return needle.substr(0,needle.size()-1) ==
01526 haystack.substr(0,needle.size()-1);
01527
01528
01529 return needle == haystack;
01530 }
01531 catch(...)
01532 {
01533 return false;
01534 }
01535
01536
01537
01538
01539
01540 std::map<std::string,ConfigurationTree> ConfigurationTree::getChildrenMap(void) const
01541 {
01542 std::map<std::string,ConfigurationTree> retMap;
01543
01544
01545
01546 std::vector<std::string> childrenNames = getChildrenNames();
01547 for(auto& childName : childrenNames)
01548 {
01549
01550 retMap.insert(std::pair<std::string,ConfigurationTree>(childName, this->getNode(childName)));
01551 }
01552
01553
01554 return retMap;
01555 }
01556
01557
01558 bool ConfigurationTree::isConfigurationNode(void) const
01559 {
01560 return (row_ == ConfigurationView::INVALID && col_ == ConfigurationView::INVALID);
01561 }
01562
01563
01564
01565
01566 std::vector<std::string> ConfigurationTree::getChildrenNames(void) const
01567 {
01568 std::vector<std::string> retSet;
01569
01570 if(!configView_)
01571 {
01572 __SS__ << "Can not get children names of '" <<
01573 getValueAsString() <<
01574 "' with null configuration view pointer!" << std::endl;
01575 if(isLinkNode() && isDisconnected())
01576 ss << " This node is a disconnected link to " <<
01577 getDisconnectedTableName() << std::endl;
01578 __MOUT_ERR__ << "\n" << ss.str();
01579 throw std::runtime_error(ss.str());
01580 }
01581
01582 if(row_ == ConfigurationView::INVALID && col_ == ConfigurationView::INVALID)
01583 {
01584
01585
01586
01587 for(unsigned int r = 0; r<configView_->getNumberOfRows(); ++r)
01588 if(groupId_ == "" ||
01589 configView_->isEntryInGroup(r,childLinkIndex_,groupId_))
01590
01591
01592 retSet.push_back(configView_->getDataView()[r][configView_->getColUID()]);
01593 }
01594 else if(row_ == ConfigurationView::INVALID)
01595 throw std::runtime_error("Malformed ConfigurationTree");
01596 else if(col_ == ConfigurationView::INVALID)
01597 {
01598
01599
01600
01601 for(unsigned int c = 0; c<configView_->getNumberOfColumns(); ++c)
01602 if(c == configView_->getColUID() ||
01603 configView_->getColumnInfo(c).isChildLinkGroupID() ||
01604 configView_->getColumnInfo(c).isChildLinkUID())
01605 continue;
01606 else
01607 retSet.push_back(configView_->getColumnInfo(c).getName());
01608 }
01609 else
01610 {
01611 __MOUT__ << "\n\nError occurred looking for children of nodeName=" << getValueName() << "\n\n" << std::endl;
01612 throw std::runtime_error("Invalid depth! getChildrenValues() called from a value point in the Configuration Tree.");
01613 }
01614
01615 return retSet;
01616 }
01617
01618
01619
01620
01621
01622
01623 ConfigurationTree ConfigurationTree::getValueAsTreeNode(void) const
01624 {
01625
01626
01627
01628 if(!configView_)
01629 {
01630 __SS__ << "Invalid node for get value." << std::endl;
01631 __MOUT__ << ss.str();
01632 throw std::runtime_error(ss.str());
01633 }
01634
01635 std::string valueString = configView_->getValueAsString(row_,col_,true );
01636
01637 if(valueString.size() && valueString[0] == '/')
01638 {
01639
01640 try
01641 {
01642 ConfigurationTree retNode = configMgr_->getNode(valueString);
01643 __MOUT__ << "Found a valid tree path in value!" << std::endl;
01644 return retNode;
01645 }
01646 catch(...)
01647 {
01648 __SS__ << "Invalid tree path." << std::endl;
01649
01650 throw std::runtime_error(ss.str());
01651 }
01652 }
01653
01654
01655 {
01656 __SS__ << "Invalid value string '" << valueString <<
01657 "' - must start with a '/' character." << std::endl;
01658 throw std::runtime_error(ss.str());
01659 }
01660 }
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671