Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:28:55

0001 
0002 #include "FWCore/ParameterSet/src/ANDGroupDescription.h"
0003 #include "FWCore/Utilities/interface/EDMException.h"
0004 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0005 
0006 #include <algorithm>
0007 #include <sstream>
0008 #include <ostream>
0009 #include <iomanip>
0010 
0011 namespace edm {
0012 
0013   ANDGroupDescription::ANDGroupDescription(ParameterDescriptionNode const& node_left,
0014                                            ParameterDescriptionNode const& node_right)
0015       : node_left_(node_left.clone()), node_right_(node_right.clone()) {}
0016 
0017   ANDGroupDescription::ANDGroupDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
0018                                            ParameterDescriptionNode const& node_right)
0019       : node_left_(std::move(node_left)), node_right_(node_right.clone()) {}
0020 
0021   ANDGroupDescription::ANDGroupDescription(ParameterDescriptionNode const& node_left,
0022                                            std::unique_ptr<ParameterDescriptionNode> node_right)
0023       : node_left_(node_left.clone()), node_right_(std::move(node_right)) {}
0024 
0025   ANDGroupDescription::ANDGroupDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
0026                                            std::unique_ptr<ParameterDescriptionNode> node_right)
0027       : node_left_(std::move(node_left)), node_right_(std::move(node_right)) {}
0028 
0029   void ANDGroupDescription::checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
0030                                                        std::set<ParameterTypes>& parameterTypes,
0031                                                        std::set<ParameterTypes>& wildcardTypes) const {
0032     std::set<std::string> labelsLeft;
0033     std::set<ParameterTypes> parameterTypesLeft;
0034     std::set<ParameterTypes> wildcardTypesLeft;
0035     node_left_->checkAndGetLabelsAndTypes(labelsLeft, parameterTypesLeft, wildcardTypesLeft);
0036 
0037     std::set<std::string> labelsRight;
0038     std::set<ParameterTypes> parameterTypesRight;
0039     std::set<ParameterTypes> wildcardTypesRight;
0040     node_right_->checkAndGetLabelsAndTypes(labelsRight, parameterTypesRight, wildcardTypesRight);
0041 
0042     throwIfDuplicateLabels(labelsLeft, labelsRight);
0043     throwIfDuplicateTypes(wildcardTypesLeft, parameterTypesRight);
0044     throwIfDuplicateTypes(wildcardTypesRight, parameterTypesLeft);
0045 
0046     usedLabels.insert(labelsLeft.begin(), labelsLeft.end());
0047     usedLabels.insert(labelsRight.begin(), labelsRight.end());
0048 
0049     parameterTypes.insert(parameterTypesRight.begin(), parameterTypesRight.end());
0050     parameterTypes.insert(parameterTypesLeft.begin(), parameterTypesLeft.end());
0051 
0052     wildcardTypes.insert(wildcardTypesRight.begin(), wildcardTypesRight.end());
0053     wildcardTypes.insert(wildcardTypesLeft.begin(), wildcardTypesLeft.end());
0054   }
0055 
0056   void ANDGroupDescription::validate_(ParameterSet& pset, std::set<std::string>& validatedLabels, bool optional) const {
0057     if (partiallyExists(pset) || !optional) {
0058       node_left_->validate(pset, validatedLabels, false);
0059       node_right_->validate(pset, validatedLabels, false);
0060     }
0061   }
0062 
0063   void ANDGroupDescription::writeCfi_(
0064       std::ostream& os, bool optional, bool& startWithComma, int indentation, bool& wroteSomething) const {
0065     node_left_->writeCfi(os, optional, startWithComma, indentation, wroteSomething);
0066     node_right_->writeCfi(os, optional, startWithComma, indentation, wroteSomething);
0067   }
0068 
0069   void ANDGroupDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0070     if (dfh.parent() == DocFormatHelper::AND) {
0071       dfh.decrementCounter();
0072       node_left_->print(os, false, true, dfh);
0073       node_right_->print(os, false, true, dfh);
0074       return;
0075     }
0076 
0077     if (dfh.pass() == 1) {
0078       dfh.indent(os);
0079       os << "AND group:";
0080 
0081       if (dfh.brief()) {
0082         if (optional)
0083           os << " optional";
0084 
0085         if (!writeToCfi)
0086           os << " (do not write to cfi)";
0087 
0088         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0089       }
0090       // not brief
0091       else {
0092         os << "\n";
0093         dfh.indent2(os);
0094 
0095         if (optional)
0096           os << "optional";
0097         if (!writeToCfi)
0098           os << " (do not write to cfi)";
0099         if (optional || !writeToCfi) {
0100           os << "\n";
0101           dfh.indent2(os);
0102         }
0103 
0104         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0105 
0106         if (!comment().empty()) {
0107           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0108         }
0109         os << "\n";
0110       }
0111     }
0112   }
0113 
0114   void ANDGroupDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
0115     if (dfh.parent() == DocFormatHelper::AND) {
0116       dfh.decrementCounter();
0117       node_left_->printNestedContent(os, false, dfh);
0118       node_right_->printNestedContent(os, false, dfh);
0119       return;
0120     }
0121 
0122     int indentation = dfh.indentation();
0123     if (dfh.parent() != DocFormatHelper::TOP) {
0124       indentation -= DocFormatHelper::offsetSectionContent();
0125     }
0126 
0127     std::stringstream ss;
0128     ss << dfh.section() << "." << dfh.counter();
0129     std::string newSection = ss.str();
0130 
0131     printSpaces(os, indentation);
0132     os << "Section " << newSection << " AND group description:\n";
0133     printSpaces(os, indentation);
0134     if (optional) {
0135       os << "This optional AND group requires all or none of the following to be in the PSet\n";
0136     } else {
0137       os << "This AND group requires all of the following to be in the PSet\n";
0138     }
0139     if (!dfh.brief())
0140       os << "\n";
0141 
0142     DocFormatHelper new_dfh(dfh);
0143     new_dfh.init();
0144     new_dfh.setSection(newSection);
0145     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0146     new_dfh.setParent(DocFormatHelper::AND);
0147 
0148     node_left_->print(os, false, true, new_dfh);
0149     node_right_->print(os, false, true, new_dfh);
0150 
0151     new_dfh.setPass(1);
0152     new_dfh.setCounter(0);
0153 
0154     node_left_->print(os, false, true, new_dfh);
0155     node_right_->print(os, false, true, new_dfh);
0156 
0157     new_dfh.setPass(2);
0158     new_dfh.setCounter(0);
0159 
0160     node_left_->printNestedContent(os, false, new_dfh);
0161     node_right_->printNestedContent(os, false, new_dfh);
0162   }
0163 
0164   bool ANDGroupDescription::exists_(ParameterSet const& pset) const {
0165     return node_left_->exists(pset) && node_right_->exists(pset);
0166   }
0167 
0168   bool ANDGroupDescription::partiallyExists_(ParameterSet const& pset) const {
0169     return node_left_->partiallyExists(pset) || node_right_->partiallyExists(pset);
0170   }
0171 
0172   int ANDGroupDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }
0173 
0174   void ANDGroupDescription::throwIfDuplicateLabels(std::set<std::string> const& labelsLeft,
0175                                                    std::set<std::string> const& labelsRight) const {
0176     std::set<std::string> duplicateLabels;
0177     std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
0178     std::set_intersection(labelsLeft.begin(), labelsLeft.end(), labelsRight.begin(), labelsRight.end(), insertIter);
0179     if (!duplicateLabels.empty()) {
0180       std::stringstream ss;
0181       for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(), iEnd = duplicateLabels.end();
0182            iter != iEnd;
0183            ++iter) {
0184         ss << " \"" << *iter << "\"\n";
0185       }
0186       throw edm::Exception(errors::LogicError) << "Labels used in different nodes of a ParameterSetDescription\n"
0187                                                << "\"and\" expression must be unique.  The following duplicate\n"
0188                                                << "labels were detected:\n"
0189                                                << ss.str() << "\n";
0190     }
0191   }
0192 
0193   void ANDGroupDescription::throwIfDuplicateTypes(std::set<ParameterTypes> const& types1,
0194                                                   std::set<ParameterTypes> const& types2) const {
0195     if (!types1.empty()) {
0196       std::set<ParameterTypes> duplicateTypes;
0197       std::insert_iterator<std::set<ParameterTypes> > insertIter(duplicateTypes, duplicateTypes.begin());
0198       std::set_intersection(types1.begin(), types1.end(), types2.begin(), types2.end(), insertIter);
0199       if (!duplicateTypes.empty()) {
0200         std::stringstream ss;
0201         for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes.begin(), iEnd = duplicateTypes.end();
0202              iter != iEnd;
0203              ++iter) {
0204           ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
0205         }
0206         throw edm::Exception(errors::LogicError)
0207             << "Types used for wildcards in different nodes of a ParameterSetDescription\n"
0208             << "\"and\" expression must be different from types used for other parameters.\n"
0209             << "The following duplicate types were detected:\n"
0210             << ss.str() << "\n";
0211       }
0212     }
0213   }
0214 }  // namespace edm