Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-23 03:13:14

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_(std::ostream& os,
0064                                       bool optional,
0065                                       bool& startWithComma,
0066                                       int indentation,
0067                                       CfiOptions& options,
0068                                       bool& wroteSomething) const {
0069     node_left_->writeCfi(os, optional, startWithComma, indentation, options, wroteSomething);
0070     node_right_->writeCfi(os, optional, startWithComma, indentation, options, wroteSomething);
0071   }
0072 
0073   void ANDGroupDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0074     if (dfh.parent() == DocFormatHelper::AND) {
0075       dfh.decrementCounter();
0076       node_left_->print(os, false, true, dfh);
0077       node_right_->print(os, false, true, dfh);
0078       return;
0079     }
0080 
0081     if (dfh.pass() == 1) {
0082       dfh.indent(os);
0083       os << "AND group:";
0084 
0085       if (dfh.brief()) {
0086         if (optional)
0087           os << " optional";
0088 
0089         if (!writeToCfi)
0090           os << " (do not write to cfi)";
0091 
0092         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0093       }
0094       // not brief
0095       else {
0096         os << "\n";
0097         dfh.indent2(os);
0098 
0099         if (optional)
0100           os << "optional";
0101         if (!writeToCfi)
0102           os << " (do not write to cfi)";
0103         if (optional || !writeToCfi) {
0104           os << "\n";
0105           dfh.indent2(os);
0106         }
0107 
0108         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0109 
0110         if (!comment().empty()) {
0111           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0112         }
0113         os << "\n";
0114       }
0115     }
0116   }
0117 
0118   void ANDGroupDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
0119     if (dfh.parent() == DocFormatHelper::AND) {
0120       dfh.decrementCounter();
0121       node_left_->printNestedContent(os, false, dfh);
0122       node_right_->printNestedContent(os, false, dfh);
0123       return;
0124     }
0125 
0126     int indentation = dfh.indentation();
0127     if (dfh.parent() != DocFormatHelper::TOP) {
0128       indentation -= DocFormatHelper::offsetSectionContent();
0129     }
0130 
0131     std::stringstream ss;
0132     ss << dfh.section() << "." << dfh.counter();
0133     std::string newSection = ss.str();
0134 
0135     printSpaces(os, indentation);
0136     os << "Section " << newSection << " AND group description:\n";
0137     printSpaces(os, indentation);
0138     if (optional) {
0139       os << "This optional AND group requires all or none of the following to be in the PSet\n";
0140     } else {
0141       os << "This AND group requires all of the following to be in the PSet\n";
0142     }
0143     if (!dfh.brief())
0144       os << "\n";
0145 
0146     DocFormatHelper new_dfh(dfh);
0147     new_dfh.init();
0148     new_dfh.setSection(newSection);
0149     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0150     new_dfh.setParent(DocFormatHelper::AND);
0151 
0152     node_left_->print(os, false, true, new_dfh);
0153     node_right_->print(os, false, true, new_dfh);
0154 
0155     new_dfh.setPass(1);
0156     new_dfh.setCounter(0);
0157 
0158     node_left_->print(os, false, true, new_dfh);
0159     node_right_->print(os, false, true, new_dfh);
0160 
0161     new_dfh.setPass(2);
0162     new_dfh.setCounter(0);
0163 
0164     node_left_->printNestedContent(os, false, new_dfh);
0165     node_right_->printNestedContent(os, false, new_dfh);
0166   }
0167 
0168   bool ANDGroupDescription::exists_(ParameterSet const& pset) const {
0169     return node_left_->exists(pset) && node_right_->exists(pset);
0170   }
0171 
0172   bool ANDGroupDescription::partiallyExists_(ParameterSet const& pset) const {
0173     return node_left_->partiallyExists(pset) || node_right_->partiallyExists(pset);
0174   }
0175 
0176   int ANDGroupDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }
0177 
0178   void ANDGroupDescription::throwIfDuplicateLabels(std::set<std::string> const& labelsLeft,
0179                                                    std::set<std::string> const& labelsRight) const {
0180     std::set<std::string> duplicateLabels;
0181     std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
0182     std::set_intersection(labelsLeft.begin(), labelsLeft.end(), labelsRight.begin(), labelsRight.end(), insertIter);
0183     if (!duplicateLabels.empty()) {
0184       std::stringstream ss;
0185       for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(), iEnd = duplicateLabels.end();
0186            iter != iEnd;
0187            ++iter) {
0188         ss << " \"" << *iter << "\"\n";
0189       }
0190       throw edm::Exception(errors::LogicError) << "Labels used in different nodes of a ParameterSetDescription\n"
0191                                                << "\"and\" expression must be unique.  The following duplicate\n"
0192                                                << "labels were detected:\n"
0193                                                << ss.str() << "\n";
0194     }
0195   }
0196 
0197   void ANDGroupDescription::throwIfDuplicateTypes(std::set<ParameterTypes> const& types1,
0198                                                   std::set<ParameterTypes> const& types2) const {
0199     if (!types1.empty()) {
0200       std::set<ParameterTypes> duplicateTypes;
0201       std::insert_iterator<std::set<ParameterTypes> > insertIter(duplicateTypes, duplicateTypes.begin());
0202       std::set_intersection(types1.begin(), types1.end(), types2.begin(), types2.end(), insertIter);
0203       if (!duplicateTypes.empty()) {
0204         std::stringstream ss;
0205         for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes.begin(), iEnd = duplicateTypes.end();
0206              iter != iEnd;
0207              ++iter) {
0208           ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
0209         }
0210         throw edm::Exception(errors::LogicError)
0211             << "Types used for wildcards in different nodes of a ParameterSetDescription\n"
0212             << "\"and\" expression must be different from types used for other parameters.\n"
0213             << "The following duplicate types were detected:\n"
0214             << ss.str() << "\n";
0215       }
0216     }
0217   }
0218 }  // namespace edm