CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/FWCore/ParameterSet/src/ParameterWildcard.cc

Go to the documentation of this file.
00001 #include "FWCore/ParameterSet/interface/ParameterWildcard.h"
00002 
00003 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00005 #include "FWCore/ParameterSet/interface/VParameterSetEntry.h"
00006 #include "FWCore/Utilities/interface/Algorithms.h"
00007 
00008 #include "boost/bind.hpp"
00009 
00010 #include <cassert>
00011 #include <iomanip>
00012 #include <ostream>
00013 
00014 namespace edm {
00015 
00016   ParameterWildcard<ParameterSetDescription>::
00017   ParameterWildcard(std::string const& pattern, WildcardValidationCriteria criteria, bool isTracked) :
00018     ParameterWildcardBase(k_PSet, isTracked, criteria),
00019     psetDesc_() {
00020     throwIfInvalidPattern(pattern);
00021   }
00022 
00023   ParameterWildcard<ParameterSetDescription>::
00024   ParameterWildcard(char const* pattern, WildcardValidationCriteria criteria, bool isTracked) :
00025     ParameterWildcardBase(k_PSet, isTracked, criteria),
00026     psetDesc_() {
00027     throwIfInvalidPattern(pattern);
00028   }
00029 
00030   ParameterWildcard<ParameterSetDescription>::
00031   ParameterWildcard(std::string const& pattern, WildcardValidationCriteria criteria, bool isTracked, ParameterSetDescription const& desc) :
00032     ParameterWildcardBase(k_PSet, isTracked, criteria),
00033     psetDesc_(new ParameterSetDescription(desc)) {
00034     throwIfInvalidPattern(pattern);
00035   }
00036 
00037 
00038   ParameterWildcard<ParameterSetDescription>::
00039   ParameterWildcard(char const* pattern, WildcardValidationCriteria criteria, bool isTracked, ParameterSetDescription const& desc) :
00040     ParameterWildcardBase(k_PSet, isTracked, criteria),
00041     psetDesc_(new ParameterSetDescription(desc)) {
00042     throwIfInvalidPattern(pattern);
00043   }
00044 
00045 
00046   ParameterWildcard<ParameterSetDescription>::
00047   ~ParameterWildcard() { }
00048 
00049   ParameterDescriptionNode*
00050   ParameterWildcard<ParameterSetDescription>::
00051   clone() const {
00052     return new ParameterWildcard(*this);
00053   }
00054 
00055   void
00056   ParameterWildcard<ParameterSetDescription>::
00057   validate_(ParameterSet& pset,
00058             std::set<std::string>& validatedLabels,
00059             bool optional) const {
00060 
00061     std::vector<std::string> parameterNames  = pset.getParameterNamesForType<ParameterSet>(isTracked());
00062     validateMatchingNames(parameterNames, validatedLabels, optional);
00063 
00064     if(psetDesc_) {
00065       for_all(parameterNames,
00066               boost::bind(&ParameterWildcard<ParameterSetDescription>::validateDescription,
00067                           boost::cref(this),
00068                           _1,
00069                           boost::ref(pset)));
00070     }
00071   }
00072 
00073   void
00074   ParameterWildcard<ParameterSetDescription>::
00075   validateDescription(std::string const& parameterName,
00076                       ParameterSet& pset) const {
00077     ParameterSet* containedPSet = pset.getPSetForUpdate(parameterName);
00078     psetDesc_->validate(*containedPSet);
00079   }
00080 
00081   bool
00082   ParameterWildcard<ParameterSetDescription>::
00083   hasNestedContent_() {
00084     if(psetDesc_) return true;
00085     return false;
00086   }
00087 
00088   void
00089   ParameterWildcard<ParameterSetDescription>::
00090   printNestedContent_(std::ostream& os,
00091                       bool /*optional*/,
00092                       DocFormatHelper& dfh) {
00093 
00094     int indentation = dfh.indentation();
00095     if(dfh.parent() != DocFormatHelper::TOP) {
00096       indentation -= DocFormatHelper::offsetSectionContent();
00097     }
00098 
00099     printSpaces(os, indentation);
00100     os << "Section " << dfh.section() << "." << dfh.counter()
00101        << " description of PSet matching wildcard:";
00102     os << "\n";
00103     if(!dfh.brief()) os << "\n";
00104 
00105     std::stringstream ss;
00106     ss << dfh.section() << "." << dfh.counter();
00107     std::string newSection = ss.str();
00108 
00109     DocFormatHelper new_dfh(dfh);
00110     new_dfh.setSection(newSection);
00111     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
00112     new_dfh.setParent(DocFormatHelper::OTHER);
00113 
00114     psetDesc_->print(os, new_dfh);
00115   }
00116 
00117   bool
00118   ParameterWildcard<ParameterSetDescription>::
00119   exists_(ParameterSet const& pset) const {
00120 
00121     if(criteria() == RequireZeroOrMore) return true;
00122 
00123     std::vector<std::string> parameterNames  = pset.getParameterNamesForType<ParameterSet>(isTracked());
00124 
00125     if(criteria() == RequireAtLeastOne) return parameterNames.size() >= 1U;
00126     return parameterNames.size() == 1U;
00127   }
00128 
00129 // -------------------------------------------------------------------------
00130 
00131   ParameterWildcard<std::vector<ParameterSet> >::
00132   ParameterWildcard(std::string const& pattern, WildcardValidationCriteria criteria, bool isTracked) :
00133     ParameterWildcardBase(k_VPSet, isTracked, criteria),
00134     psetDesc_() {
00135     throwIfInvalidPattern(pattern);
00136   }
00137 
00138   ParameterWildcard<std::vector<ParameterSet> >::
00139   ParameterWildcard(char const* pattern, WildcardValidationCriteria criteria, bool isTracked) :
00140     ParameterWildcardBase(k_VPSet, isTracked, criteria),
00141     psetDesc_() {
00142     throwIfInvalidPattern(pattern);
00143   }
00144 
00145   ParameterWildcard<std::vector<ParameterSet> >::
00146   ParameterWildcard(std::string const& pattern, WildcardValidationCriteria criteria, bool isTracked,
00147                     ParameterSetDescription const& desc) :
00148     ParameterWildcardBase(k_VPSet, isTracked, criteria),
00149     psetDesc_(new ParameterSetDescription(desc)) {
00150     throwIfInvalidPattern(pattern);
00151   }
00152 
00153 
00154   ParameterWildcard<std::vector<ParameterSet> >::
00155   ParameterWildcard(char const* pattern, WildcardValidationCriteria criteria, bool isTracked,
00156                     ParameterSetDescription const& desc) :
00157     ParameterWildcardBase(k_VPSet, isTracked, criteria),
00158     psetDesc_(new ParameterSetDescription(desc)) {
00159     throwIfInvalidPattern(pattern);
00160   }
00161 
00162 
00163   ParameterWildcard<std::vector<ParameterSet> >::
00164   ~ParameterWildcard() { }
00165 
00166   ParameterDescriptionNode*
00167   ParameterWildcard<std::vector<ParameterSet> >::
00168   clone() const {
00169     return new ParameterWildcard(*this);
00170   }
00171 
00172   void
00173   ParameterWildcard<std::vector<ParameterSet> >::
00174   validate_(ParameterSet& pset,
00175             std::set<std::string>& validatedLabels,
00176             bool optional) const {
00177 
00178     std::vector<std::string> parameterNames  = pset.getParameterNamesForType<std::vector<ParameterSet> >(isTracked());
00179     validateMatchingNames(parameterNames, validatedLabels, optional);
00180 
00181     if(psetDesc_) {
00182       for_all(parameterNames,
00183               boost::bind(&ParameterWildcard<std::vector<ParameterSet> >::validatePSetVector,
00184                           boost::cref(this),
00185                           _1,
00186                           boost::ref(pset)));
00187     }
00188   }
00189 
00190   void
00191   ParameterWildcard<std::vector<ParameterSet> >::
00192   validatePSetVector(std::string const& parameterName, ParameterSet& pset) const {
00193     VParameterSetEntry* vpsetEntry = pset.getPSetVectorForUpdate(parameterName);
00194     assert(vpsetEntry);
00195     for(unsigned i = 0; i < vpsetEntry->size(); ++i) {
00196       psetDesc_->validate(vpsetEntry->psetInVector(i));
00197     }
00198   }
00199 
00200   bool
00201   ParameterWildcard<std::vector<ParameterSet> >::
00202   hasNestedContent_() {
00203     if(psetDesc_) return true;
00204     return false;
00205   }
00206 
00207   void
00208   ParameterWildcard<std::vector<ParameterSet> >::
00209   printNestedContent_(std::ostream& os,
00210                       bool /*optional*/,
00211                       DocFormatHelper& dfh) {
00212 
00213     int indentation = dfh.indentation();
00214     if(dfh.parent() != DocFormatHelper::TOP) {
00215       indentation -= DocFormatHelper::offsetSectionContent();
00216     }
00217 
00218     printSpaces(os, indentation);
00219     os << "Section " << dfh.section() << "." << dfh.counter()
00220        << " description used to validate all PSets which are in the VPSet matching the wildcard:";
00221     os << "\n";
00222     if(!dfh.brief()) os << "\n";
00223 
00224     std::stringstream ss;
00225     ss << dfh.section() << "." << dfh.counter();
00226     std::string newSection = ss.str();
00227 
00228     DocFormatHelper new_dfh(dfh);
00229     new_dfh.setSection(newSection);
00230     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
00231     new_dfh.setParent(DocFormatHelper::OTHER);
00232 
00233     psetDesc_->print(os, new_dfh);
00234   }
00235 
00236   bool
00237   ParameterWildcard<std::vector<ParameterSet> >::
00238   exists_(ParameterSet const& pset) const {
00239 
00240     if(criteria() == RequireZeroOrMore) return true;
00241 
00242     std::vector<std::string> parameterNames  = pset.getParameterNamesForType<std::vector<ParameterSet> >(isTracked());
00243 
00244     if(criteria() == RequireAtLeastOne) return parameterNames.size() >= 1U;
00245     return parameterNames.size() == 1U;
00246   }
00247 }