Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-26 01:51:13

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