Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/ParameterSet/interface/ParameterWildcard.h"
0002 
0003 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0005 #include "FWCore/ParameterSet/interface/VParameterSetEntry.h"
0006 #include "FWCore/Utilities/interface/Algorithms.h"
0007 
0008 #include <cassert>
0009 #include <iomanip>
0010 #include <ostream>
0011 
0012 namespace edm {
0013 
0014   ParameterWildcard<ParameterSetDescription>::ParameterWildcard(std::string const& pattern,
0015                                                                 WildcardValidationCriteria criteria,
0016                                                                 bool isTracked)
0017       : ParameterWildcardBase(k_PSet, isTracked, criteria), psetDesc_() {
0018     throwIfInvalidPattern(pattern);
0019   }
0020 
0021   ParameterWildcard<ParameterSetDescription>::ParameterWildcard(char const* pattern,
0022                                                                 WildcardValidationCriteria criteria,
0023                                                                 bool isTracked)
0024       : ParameterWildcardBase(k_PSet, isTracked, criteria), psetDesc_() {
0025     throwIfInvalidPattern(pattern);
0026   }
0027 
0028   ParameterWildcard<ParameterSetDescription>::ParameterWildcard(std::string const& pattern,
0029                                                                 WildcardValidationCriteria criteria,
0030                                                                 bool isTracked,
0031                                                                 ParameterSetDescription const& desc)
0032       : ParameterWildcardBase(k_PSet, isTracked, criteria), psetDesc_(new ParameterSetDescription(desc)) {
0033     throwIfInvalidPattern(pattern);
0034   }
0035 
0036   ParameterWildcard<ParameterSetDescription>::ParameterWildcard(char const* pattern,
0037                                                                 WildcardValidationCriteria criteria,
0038                                                                 bool isTracked,
0039                                                                 ParameterSetDescription const& desc)
0040       : ParameterWildcardBase(k_PSet, isTracked, criteria), psetDesc_(new ParameterSetDescription(desc)) {
0041     throwIfInvalidPattern(pattern);
0042   }
0043 
0044   ParameterWildcard<ParameterSetDescription>::~ParameterWildcard() {}
0045 
0046   ParameterDescriptionNode* ParameterWildcard<ParameterSetDescription>::clone() const {
0047     return new ParameterWildcard(*this);
0048   }
0049 
0050   void ParameterWildcard<ParameterSetDescription>::validate_(ParameterSet& pset,
0051                                                              std::set<std::string>& validatedLabels,
0052                                                              bool optional) const {
0053     std::vector<std::string> parameterNames = pset.getParameterNamesForType<ParameterSet>(isTracked());
0054     validateMatchingNames(parameterNames, validatedLabels, optional);
0055 
0056     if (psetDesc_) {
0057       for_all(parameterNames,
0058               std::bind(&ParameterWildcard<ParameterSetDescription>::validateDescription,
0059                         this,
0060                         std::placeholders::_1,
0061                         std::ref(pset)));
0062     }
0063   }
0064 
0065   void ParameterWildcard<ParameterSetDescription>::validateDescription(std::string const& parameterName,
0066                                                                        ParameterSet& pset) const {
0067     ParameterSet* containedPSet = pset.getPSetForUpdate(parameterName);
0068     psetDesc_->validate(*containedPSet);
0069   }
0070 
0071   bool ParameterWildcard<ParameterSetDescription>::hasNestedContent_() const {
0072     if (psetDesc_)
0073       return true;
0074     return false;
0075   }
0076 
0077   void ParameterWildcard<ParameterSetDescription>::printNestedContent_(std::ostream& os,
0078                                                                        bool /*optional*/,
0079                                                                        DocFormatHelper& dfh) const {
0080     int indentation = dfh.indentation();
0081     if (dfh.parent() != DocFormatHelper::TOP) {
0082       indentation -= DocFormatHelper::offsetSectionContent();
0083     }
0084 
0085     printSpaces(os, indentation);
0086     os << "Section " << dfh.section() << "." << dfh.counter() << " description of PSet matching wildcard:";
0087     os << "\n";
0088     if (!dfh.brief())
0089       os << "\n";
0090 
0091     std::stringstream ss;
0092     ss << dfh.section() << "." << dfh.counter();
0093     std::string newSection = ss.str();
0094 
0095     DocFormatHelper new_dfh(dfh);
0096     new_dfh.setSection(newSection);
0097     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0098     new_dfh.setParent(DocFormatHelper::OTHER);
0099 
0100     psetDesc_->print(os, new_dfh);
0101   }
0102 
0103   bool ParameterWildcard<ParameterSetDescription>::exists_(ParameterSet const& pset) const {
0104     if (criteria() == RequireZeroOrMore)
0105       return true;
0106 
0107     std::vector<std::string> parameterNames = pset.getParameterNamesForType<ParameterSet>(isTracked());
0108 
0109     if (criteria() == RequireAtLeastOne)
0110       return !parameterNames.empty();
0111     return parameterNames.size() == 1U;
0112   }
0113 
0114   // -------------------------------------------------------------------------
0115 
0116   ParameterWildcard<std::vector<ParameterSet> >::ParameterWildcard(std::string const& pattern,
0117                                                                    WildcardValidationCriteria criteria,
0118                                                                    bool isTracked)
0119       : ParameterWildcardBase(k_VPSet, isTracked, criteria), psetDesc_() {
0120     throwIfInvalidPattern(pattern);
0121   }
0122 
0123   ParameterWildcard<std::vector<ParameterSet> >::ParameterWildcard(char const* pattern,
0124                                                                    WildcardValidationCriteria criteria,
0125                                                                    bool isTracked)
0126       : ParameterWildcardBase(k_VPSet, isTracked, criteria), psetDesc_() {
0127     throwIfInvalidPattern(pattern);
0128   }
0129 
0130   ParameterWildcard<std::vector<ParameterSet> >::ParameterWildcard(std::string const& pattern,
0131                                                                    WildcardValidationCriteria criteria,
0132                                                                    bool isTracked,
0133                                                                    ParameterSetDescription const& desc)
0134       : ParameterWildcardBase(k_VPSet, isTracked, criteria), psetDesc_(new ParameterSetDescription(desc)) {
0135     throwIfInvalidPattern(pattern);
0136   }
0137 
0138   ParameterWildcard<std::vector<ParameterSet> >::ParameterWildcard(char const* pattern,
0139                                                                    WildcardValidationCriteria criteria,
0140                                                                    bool isTracked,
0141                                                                    ParameterSetDescription const& desc)
0142       : ParameterWildcardBase(k_VPSet, isTracked, criteria), psetDesc_(new ParameterSetDescription(desc)) {
0143     throwIfInvalidPattern(pattern);
0144   }
0145 
0146   ParameterWildcard<std::vector<ParameterSet> >::~ParameterWildcard() {}
0147 
0148   ParameterDescriptionNode* ParameterWildcard<std::vector<ParameterSet> >::clone() const {
0149     return new ParameterWildcard(*this);
0150   }
0151 
0152   void ParameterWildcard<std::vector<ParameterSet> >::validate_(ParameterSet& pset,
0153                                                                 std::set<std::string>& validatedLabels,
0154                                                                 bool optional) const {
0155     std::vector<std::string> parameterNames = pset.getParameterNamesForType<std::vector<ParameterSet> >(isTracked());
0156     validateMatchingNames(parameterNames, validatedLabels, optional);
0157 
0158     if (psetDesc_) {
0159       for_all(parameterNames,
0160               std::bind(&ParameterWildcard<std::vector<ParameterSet> >::validatePSetVector,
0161                         this,
0162                         std::placeholders::_1,
0163                         std::ref(pset)));
0164     }
0165   }
0166 
0167   void ParameterWildcard<std::vector<ParameterSet> >::validatePSetVector(std::string const& parameterName,
0168                                                                          ParameterSet& pset) const {
0169     VParameterSetEntry* vpsetEntry = pset.getPSetVectorForUpdate(parameterName);
0170     assert(vpsetEntry);
0171     for (unsigned i = 0; i < vpsetEntry->size(); ++i) {
0172       psetDesc_->validate(vpsetEntry->psetInVector(i));
0173     }
0174   }
0175 
0176   bool ParameterWildcard<std::vector<ParameterSet> >::hasNestedContent_() const {
0177     if (psetDesc_)
0178       return true;
0179     return false;
0180   }
0181 
0182   void ParameterWildcard<std::vector<ParameterSet> >::printNestedContent_(std::ostream& os,
0183                                                                           bool /*optional*/,
0184                                                                           DocFormatHelper& dfh) const {
0185     int indentation = dfh.indentation();
0186     if (dfh.parent() != DocFormatHelper::TOP) {
0187       indentation -= DocFormatHelper::offsetSectionContent();
0188     }
0189 
0190     printSpaces(os, indentation);
0191     os << "Section " << dfh.section() << "." << dfh.counter()
0192        << " description used to validate all PSets which are in the VPSet matching the wildcard:";
0193     os << "\n";
0194     if (!dfh.brief())
0195       os << "\n";
0196 
0197     std::stringstream ss;
0198     ss << dfh.section() << "." << dfh.counter();
0199     std::string newSection = ss.str();
0200 
0201     DocFormatHelper new_dfh(dfh);
0202     new_dfh.setSection(newSection);
0203     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0204     new_dfh.setParent(DocFormatHelper::OTHER);
0205 
0206     psetDesc_->print(os, new_dfh);
0207   }
0208 
0209   bool ParameterWildcard<std::vector<ParameterSet> >::exists_(ParameterSet const& pset) const {
0210     if (criteria() == RequireZeroOrMore)
0211       return true;
0212 
0213     std::vector<std::string> parameterNames = pset.getParameterNamesForType<std::vector<ParameterSet> >(isTracked());
0214 
0215     if (criteria() == RequireAtLeastOne)
0216       return !parameterNames.empty();
0217     return parameterNames.size() == 1U;
0218   }
0219 }  // namespace edm