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