00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00014
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include "FWCore/ParameterSet/interface/IfExistsDescription.h"
00017 #include "FWCore/ParameterSet/interface/IllegalParameters.h"
00018 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
00019 #include "FWCore/Utilities/interface/Algorithms.h"
00020 #include "FWCore/Utilities/interface/EDMException.h"
00021
00022 #include "boost/bind.hpp"
00023
00024 #include <sstream>
00025 #include <ostream>
00026 #include <iomanip>
00027 #include <algorithm>
00028
00029 namespace edm {
00030
00031 ParameterSetDescription::ParameterSetDescription():
00032 anythingAllowed_(false),
00033 unknown_(false) {
00034 }
00035
00036 ParameterSetDescription::~ParameterSetDescription() {
00037 }
00038
00039 void
00040 ParameterSetDescription::setComment(std::string const & value)
00041 { comment_ = value; }
00042
00043 void
00044 ParameterSetDescription::setComment(char const* value)
00045 { comment_ = value; }
00046
00047 void
00048 ParameterSetDescription::setAllowAnything()
00049 {
00050 anythingAllowed_ = true;
00051 }
00052
00053 void
00054 ParameterSetDescription::setUnknown()
00055 {
00056 unknown_ = true;
00057 }
00058
00059 ParameterDescriptionNode*
00060 ParameterSetDescription::
00061 addNode(ParameterDescriptionNode const& node) {
00062 std::auto_ptr<ParameterDescriptionNode> clonedNode(node.clone());
00063 return addNode(clonedNode, false, true);
00064 }
00065
00066 ParameterDescriptionNode*
00067 ParameterSetDescription::
00068 addNode(std::auto_ptr<ParameterDescriptionNode> node) {
00069 return addNode(node, false, true);
00070 }
00071
00072 ParameterDescriptionNode*
00073 ParameterSetDescription::
00074 addOptionalNode(ParameterDescriptionNode const& node, bool writeToCfi) {
00075 std::auto_ptr<ParameterDescriptionNode> clonedNode(node.clone());
00076 return addNode(clonedNode, true, writeToCfi);
00077 }
00078
00079 ParameterDescriptionNode*
00080 ParameterSetDescription::
00081 addOptionalNode(std::auto_ptr<ParameterDescriptionNode> node, bool writeToCfi) {
00082 return addNode(node, true, writeToCfi);
00083 }
00084
00085 ParameterDescriptionNode*
00086 ParameterSetDescription::
00087 addNode(std::auto_ptr<ParameterDescriptionNode> node,
00088 bool optional,
00089 bool writeToCfi) {
00090
00091 std::set<std::string> nodeLabels;
00092 std::set<ParameterTypes> nodeParameterTypes;
00093 std::set<ParameterTypes> nodeWildcardTypes;
00094 node->checkAndGetLabelsAndTypes(nodeLabels, nodeParameterTypes, nodeWildcardTypes);
00095 throwIfLabelsAlreadyUsed(nodeLabels);
00096 throwIfWildcardCollision(nodeParameterTypes, nodeWildcardTypes);
00097
00098 SetDescriptionEntry entry;
00099 entry.setOptional(optional);
00100 entry.setWriteToCfi(writeToCfi);
00101 entries_.push_back(entry);
00102 ParameterDescriptionNode* returnValue = node.get();
00103 entries_.back().setNode(node);
00104 return returnValue;
00105 }
00106
00107 void
00108 ParameterSetDescription::validate(ParameterSet & pset) const
00109 {
00110 if (unknown_ || anythingAllowed()) return;
00111
00112 std::set<std::string> validatedLabels;
00113 for_all(entries_,
00114 boost::bind(&ParameterSetDescription::validateNode, _1, boost::ref(pset), boost::ref(validatedLabels)));
00115
00116 std::vector<std::string> parameterNames = pset.getParameterNames();
00117 if (validatedLabels.size() != parameterNames.size()) {
00118
00119
00120
00121
00122
00123
00124 std::string module_label("@module_label");
00125 if (pset.exists(module_label)) {
00126 validatedLabels.insert(module_label);
00127 }
00128
00129 std::string module_type("@module_type");
00130 if (pset.exists(module_type)) {
00131 validatedLabels.insert(module_type);
00132 }
00133
00134 std::string module_edm_type("@module_edm_type");
00135 if (pset.exists(module_edm_type)) {
00136 validatedLabels.insert(module_edm_type);
00137 }
00138
00139 std::string service_type("@service_type");
00140 if (pset.exists(service_type)) {
00141 validatedLabels.insert(service_type);
00142 }
00143
00144
00145 if (validatedLabels.size() != parameterNames.size()) {
00146 if (IllegalParameters::throwAnException()) {
00147 throwIllegalParameters(parameterNames, validatedLabels);
00148 }
00149 }
00150 }
00151 }
00152
00153 void
00154 ParameterSetDescription::writeCfi(std::ostream & os,
00155 bool startWithComma,
00156 int indentation) const {
00157 bool wroteSomething = false;
00158
00159 for_all(entries_, boost::bind(&ParameterSetDescription::writeNode,
00160 _1,
00161 boost::ref(os),
00162 boost::ref(startWithComma),
00163 indentation,
00164 boost::ref(wroteSomething)));
00165
00166 if (wroteSomething) {
00167 char oldFill = os.fill();
00168 os << "\n" << std::setfill(' ') << std::setw(indentation - 2) << "";
00169 os.fill(oldFill);
00170 }
00171 }
00172
00173 void ParameterSetDescription::
00174 validateNode(SetDescriptionEntry const& entry,
00175 ParameterSet & pset,
00176 std::set<std::string> & validatedLabels) {
00177 entry.node()->validate(pset, validatedLabels, entry.optional());
00178 }
00179
00180 void ParameterSetDescription::
00181 print(std::ostream & os, DocFormatHelper & dfh) const {
00182
00183 if (isUnknown()) {
00184 dfh.indent(os);
00185 os << "Description is unknown. The configured PSet will not be validated\n";
00186 dfh.indent(os);
00187 os << "because the plugin has not defined this parameter set description.\n";
00188 if (!dfh.brief()) os << "\n";
00189 }
00190
00191 if (anythingAllowed()) {
00192 dfh.indent(os);
00193 os << "Description allows anything and requires nothing.\n";
00194 dfh.indent(os);
00195 os << "The configured PSet will not be validated.\n";
00196 if (!dfh.brief()) os << "\n";
00197 }
00198
00199 if (entries_.empty()) {
00200 dfh.indent(os);
00201 os << "Description is empty\n";
00202 if (!dfh.brief()) os << "\n";
00203 return;
00204 }
00205
00206
00207 dfh.setPass(0);
00208 dfh.setCounter(0);
00209 for_all(entries_, boost::bind(&ParameterSetDescription::printNode,
00210 _1,
00211 boost::ref(os),
00212 boost::ref(dfh)));
00213
00214
00215 dfh.setPass(1);
00216 dfh.setCounter(0);
00217 for_all(entries_, boost::bind(&ParameterSetDescription::printNode,
00218 _1,
00219 boost::ref(os),
00220 boost::ref(dfh)));
00221
00222
00223
00224 dfh.setPass(2);
00225 dfh.setCounter(0);
00226 for_all(entries_, boost::bind(&ParameterSetDescription::printNode,
00227 _1,
00228 boost::ref(os),
00229 boost::ref(dfh)));
00230 }
00231
00232 bool
00233 ParameterSetDescription::
00234 isLabelUnused(std::string const& label) const {
00235 return usedLabels_.find(label) == usedLabels_.end();
00236 }
00237
00238 void
00239 ParameterSetDescription::throwIllegalParameters(
00240 std::vector<std::string> const& parameterNames,
00241 std::set<std::string> const& validatedLabels) {
00242
00243 std::set<std::string> parNames(parameterNames.begin(), parameterNames.end());
00244
00245
00246 std::set<std::string> diffNames;
00247 std::insert_iterator<std::set<std::string> > insertIter(diffNames, diffNames.begin());
00248 std::set_difference(parNames.begin(), parNames.end(),
00249 validatedLabels.begin(), validatedLabels.end(),
00250 insertIter);
00251
00252 std::stringstream ss;
00253 for (std::set<std::string>::const_iterator iter = diffNames.begin(),
00254 iEnd = diffNames.end();
00255 iter != iEnd;
00256 ++iter) {
00257 ss << " \"" << *iter << "\"\n";
00258 }
00259 if (diffNames.size() == 1U) {
00260 throw edm::Exception(errors::Configuration)
00261 << "Illegal parameter found in configuration. The parameter is named:\n"
00262 << ss.str()
00263 << "You could be trying to use a parameter name that is not\n"
00264 << "allowed for this plugin or it could be misspelled.\n";
00265 }
00266 else {
00267 throw edm::Exception(errors::Configuration)
00268 << "Illegal parameters found in configuration. The parameters are named:\n"
00269 << ss.str()
00270 << "You could be trying to use parameter names that are not\n"
00271 << "allowed for this plugin or they could be misspelled.\n";
00272 }
00273 }
00274
00275 void
00276 ParameterSetDescription::writeNode(SetDescriptionEntry const& entry,
00277 std::ostream & os,
00278 bool & startWithComma,
00279 int indentation,
00280 bool & wroteSomething) {
00281 if (entry.writeToCfi()) {
00282 entry.node()->writeCfi(os, startWithComma, indentation, wroteSomething);
00283 }
00284 }
00285
00286 void
00287 ParameterSetDescription::printNode(SetDescriptionEntry const& entry,
00288 std::ostream & os,
00289 DocFormatHelper & dfh) {
00290 if (dfh.pass() < 2) {
00291 entry.node()->print(os, entry.optional(), entry.writeToCfi(), dfh);
00292 }
00293 else {
00294 entry.node()->printNestedContent(os, entry.optional(), dfh);
00295 }
00296 }
00297
00298 void
00299 ParameterSetDescription::
00300 throwIfLabelsAlreadyUsed(std::set<std::string> const& nodeLabels) {
00301
00302 std::set<std::string> duplicateLabels;
00303 std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
00304 std::set_intersection(nodeLabels.begin(), nodeLabels.end(),
00305 usedLabels_.begin(), usedLabels_.end(),
00306 insertIter);
00307 if (duplicateLabels.empty()) {
00308 usedLabels_.insert(nodeLabels.begin(), nodeLabels.end());
00309 }
00310 else {
00311
00312 std::stringstream ss;
00313 for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(),
00314 iEnd = duplicateLabels.end();
00315 iter != iEnd;
00316 ++iter) {
00317 ss << " \"" << *iter << "\"\n";
00318 }
00319 throw edm::Exception(errors::LogicError)
00320 << "Labels used in different nodes of a ParameterSetDescription\n"
00321 << "must be unique. The following duplicate labels were detected:\n"
00322 << ss.str()
00323 << "\n";
00324 }
00325 }
00326
00327 void
00328 ParameterSetDescription::
00329 throwIfWildcardCollision(std::set<ParameterTypes> const& nodeParameterTypes,
00330 std::set<ParameterTypes> const& nodeWildcardTypes) {
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 if (!nodeWildcardTypes.empty()) {
00344
00345 std::set<ParameterTypes> duplicateTypes1;
00346 std::insert_iterator<std::set<ParameterTypes> > insertIter1(duplicateTypes1, duplicateTypes1.begin());
00347 std::set_intersection(typesUsedForParameters_.begin(), typesUsedForParameters_.end(),
00348 nodeWildcardTypes.begin(), nodeWildcardTypes.end(),
00349 insertIter1);
00350
00351 if (!duplicateTypes1.empty()) {
00352
00353 std::stringstream ss;
00354 for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes1.begin(),
00355 iEnd = duplicateTypes1.end();
00356 iter != iEnd;
00357 ++iter) {
00358 ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
00359 }
00360 throw edm::Exception(errors::LogicError)
00361 << "Within a ParameterSetDescription, the type used for a wildcard must\n"
00362 << "not be the same as the type used for other parameters. This rule\n"
00363 << "is violated for the following types:\n"
00364 << ss.str()
00365 << "\n";
00366 }
00367 }
00368
00369 if (!typesUsedForWildcards_.empty()) {
00370
00371 std::set<ParameterTypes> duplicateTypes2;
00372 std::insert_iterator<std::set<ParameterTypes> > insertIter2(duplicateTypes2, duplicateTypes2.begin());
00373 std::set_intersection(typesUsedForWildcards_.begin(), typesUsedForWildcards_.end(),
00374 nodeParameterTypes.begin(), nodeParameterTypes.end(),
00375 insertIter2);
00376
00377 if (!duplicateTypes2.empty()) {
00378
00379 std::stringstream ss;
00380 for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes2.begin(),
00381 iEnd = duplicateTypes2.end();
00382 iter != iEnd;
00383 ++iter) {
00384 ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
00385 }
00386 throw edm::Exception(errors::LogicError)
00387 << "Within a ParameterSetDescription, the type used for a wildcard must\n"
00388 << "not be the same as the type used for other parameters. This rule is\n"
00389 << "violated for the following types :\n"
00390 << ss.str()
00391 << "\n";
00392 }
00393 }
00394
00395 typesUsedForParameters_.insert(nodeParameterTypes.begin(), nodeParameterTypes.end());
00396 typesUsedForWildcards_.insert(nodeWildcardTypes.begin(), nodeWildcardTypes.end());
00397 }
00398
00399 ParameterDescriptionNode*
00400 ParameterSetDescription::
00401 ifExists(ParameterDescriptionNode const& node1,
00402 ParameterDescriptionNode const& node2,
00403 bool optional, bool writeToCfi) {
00404 std::auto_ptr<ParameterDescriptionNode> pdIfExists(new IfExistsDescription(node1, node2));
00405 return addNode(pdIfExists, optional, writeToCfi);
00406 }
00407 }