File indexing completed on 2023-03-17 11:03:26
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_(
0075 std::ostream& os, bool optional, bool& startWithComma, int indentation, bool& wroteSomething) const {
0076 node_left_->writeCfi(os, optional, startWithComma, indentation, wroteSomething);
0077 }
0078
0079 void ORGroupDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0080 if (dfh.parent() == DocFormatHelper::OR) {
0081 dfh.decrementCounter();
0082 node_left_->print(os, false, true, dfh);
0083 node_right_->print(os, false, true, dfh);
0084 return;
0085 }
0086
0087 if (dfh.pass() == 1) {
0088 dfh.indent(os);
0089 os << "OR group:";
0090
0091 if (dfh.brief()) {
0092 if (optional)
0093 os << " optional";
0094
0095 if (!writeToCfi)
0096 os << " (do not write to cfi)";
0097
0098 os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0099 }
0100
0101 else {
0102 os << "\n";
0103 dfh.indent2(os);
0104
0105 if (optional)
0106 os << "optional";
0107 if (!writeToCfi)
0108 os << " (do not write to cfi)";
0109 if (optional || !writeToCfi) {
0110 os << "\n";
0111 dfh.indent2(os);
0112 }
0113
0114 os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0115
0116 if (!comment().empty()) {
0117 DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0118 }
0119 os << "\n";
0120 }
0121 }
0122 }
0123
0124 void ORGroupDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
0125 if (dfh.parent() == DocFormatHelper::OR) {
0126 dfh.decrementCounter();
0127 node_left_->printNestedContent(os, false, dfh);
0128 node_right_->printNestedContent(os, false, dfh);
0129 return;
0130 }
0131
0132 int indentation = dfh.indentation();
0133 if (dfh.parent() != DocFormatHelper::TOP) {
0134 indentation -= DocFormatHelper::offsetSectionContent();
0135 }
0136
0137 std::stringstream ss;
0138 ss << dfh.section() << "." << dfh.counter();
0139 std::string newSection = ss.str();
0140
0141 printSpaces(os, indentation);
0142 os << "Section " << newSection << " OR group description:\n";
0143 printSpaces(os, indentation);
0144 if (optional) {
0145
0146
0147
0148 os << "This optional OR group requires at least one or none of the following to be in the PSet\n";
0149 } else {
0150 os << "This OR group requires at least one of the following to be in the PSet\n";
0151 }
0152 if (!dfh.brief())
0153 os << "\n";
0154
0155 DocFormatHelper new_dfh(dfh);
0156 new_dfh.init();
0157 new_dfh.setSection(newSection);
0158 new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0159 new_dfh.setParent(DocFormatHelper::OR);
0160
0161 node_left_->print(os, false, true, new_dfh);
0162 node_right_->print(os, false, true, new_dfh);
0163
0164 new_dfh.setPass(1);
0165 new_dfh.setCounter(0);
0166
0167 node_left_->print(os, false, true, new_dfh);
0168 node_right_->print(os, false, true, new_dfh);
0169
0170 new_dfh.setPass(2);
0171 new_dfh.setCounter(0);
0172
0173 node_left_->printNestedContent(os, false, new_dfh);
0174 node_right_->printNestedContent(os, false, new_dfh);
0175 }
0176
0177 bool ORGroupDescription::exists_(ParameterSet const& pset) const {
0178 return node_left_->exists(pset) || node_right_->exists(pset);
0179 }
0180
0181 bool ORGroupDescription::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0182
0183 int ORGroupDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }
0184
0185 void ORGroupDescription::throwIfDuplicateLabels(std::set<std::string> const& labelsLeft,
0186 std::set<std::string> const& labelsRight) const {
0187 std::set<std::string> duplicateLabels;
0188 std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
0189 std::set_intersection(labelsLeft.begin(), labelsLeft.end(), labelsRight.begin(), labelsRight.end(), insertIter);
0190 if (!duplicateLabels.empty()) {
0191 std::stringstream ss;
0192 for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(), iEnd = duplicateLabels.end();
0193 iter != iEnd;
0194 ++iter) {
0195 ss << " \"" << *iter << "\"\n";
0196 }
0197 throw edm::Exception(errors::LogicError) << "Labels used in a node of a ParameterSetDescription\n"
0198 << "\"or\" expression must be not be the same as labels used\n"
0199 << "in other nodes of the expression. The following duplicate\n"
0200 << "labels were detected:\n"
0201 << ss.str() << "\n";
0202 }
0203 }
0204
0205 void ORGroupDescription::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 a node of a ParameterSetDescription\n"
0220 << "\"or\" expression must be different from types used for other parameters\n"
0221 << "in other nodes. The following duplicate types were detected:\n"
0222 << ss.str() << "\n";
0223 }
0224 }
0225 }
0226 }