Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-26 01:51:14

0001 
0002 #include "FWCore/ParameterSet/src/XORGroupDescription.h"
0003 #include "FWCore/Utilities/interface/EDMException.h"
0004 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0005 
0006 #include <ostream>
0007 #include <iomanip>
0008 
0009 namespace edm {
0010 
0011   XORGroupDescription::XORGroupDescription(ParameterDescriptionNode const& node_left,
0012                                            ParameterDescriptionNode const& node_right)
0013       : node_left_(node_left.clone()), node_right_(node_right.clone()) {}
0014 
0015   XORGroupDescription::XORGroupDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
0016                                            ParameterDescriptionNode const& node_right)
0017       : node_left_(std::move(node_left)), node_right_(node_right.clone()) {}
0018 
0019   XORGroupDescription::XORGroupDescription(ParameterDescriptionNode const& node_left,
0020                                            std::unique_ptr<ParameterDescriptionNode> node_right)
0021       : node_left_(node_left.clone()), node_right_(std::move(node_right)) {}
0022 
0023   XORGroupDescription::XORGroupDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
0024                                            std::unique_ptr<ParameterDescriptionNode> node_right)
0025       : node_left_(std::move(node_left)), node_right_(std::move(node_right)) {}
0026 
0027   void XORGroupDescription::checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
0028                                                        std::set<ParameterTypes>& parameterTypes,
0029                                                        std::set<ParameterTypes>& wildcardTypes) const {
0030     std::set<std::string> labelsLeft;
0031     std::set<ParameterTypes> parameterTypesLeft;
0032     std::set<ParameterTypes> wildcardTypesLeft;
0033     node_left_->checkAndGetLabelsAndTypes(labelsLeft, parameterTypesLeft, wildcardTypesLeft);
0034 
0035     std::set<std::string> labelsRight;
0036     std::set<ParameterTypes> parameterTypesRight;
0037     std::set<ParameterTypes> wildcardTypesRight;
0038     node_right_->checkAndGetLabelsAndTypes(labelsRight, parameterTypesRight, wildcardTypesRight);
0039 
0040     usedLabels.insert(labelsLeft.begin(), labelsLeft.end());
0041     usedLabels.insert(labelsRight.begin(), labelsRight.end());
0042 
0043     parameterTypes.insert(parameterTypesRight.begin(), parameterTypesRight.end());
0044     parameterTypes.insert(parameterTypesLeft.begin(), parameterTypesLeft.end());
0045 
0046     wildcardTypes.insert(wildcardTypesRight.begin(), wildcardTypesRight.end());
0047     wildcardTypes.insert(wildcardTypesLeft.begin(), wildcardTypesLeft.end());
0048   }
0049 
0050   void XORGroupDescription::validate_(ParameterSet& pset,
0051                                       std::set<std::string>& validatedLabels,
0052                                       Modifier modifier) const {
0053     int nExistLeft = node_left_->howManyXORSubNodesExist(pset);
0054     int nExistRight = node_right_->howManyXORSubNodesExist(pset);
0055     int nTotal = nExistLeft + nExistRight;
0056 
0057     if (nTotal == 0 && modifier != Modifier::kNone)
0058       return;
0059 
0060     if (nTotal > 1) {
0061       throwMoreThanOneParameter();
0062     }
0063 
0064     if (nExistLeft == 1) {
0065       node_left_->validate(pset, validatedLabels, Modifier::kNone);
0066     } else if (nExistRight == 1) {
0067       node_right_->validate(pset, validatedLabels, Modifier::kNone);
0068     } else if (nTotal == 0) {
0069       node_left_->validate(pset, validatedLabels, Modifier::kNone);
0070 
0071       // When missing parameters get inserted, both nodes could
0072       // be affected so we have to recheck both nodes.
0073       nExistLeft = node_left_->howManyXORSubNodesExist(pset);
0074       nExistRight = node_right_->howManyXORSubNodesExist(pset);
0075       nTotal = nExistLeft + nExistRight;
0076 
0077       if (nTotal != 1) {
0078         throwAfterValidation();
0079       }
0080     }
0081   }
0082 
0083   void XORGroupDescription::writeCfi_(std::ostream& os,
0084                                       Modifier modifier,
0085                                       bool& startWithComma,
0086                                       int indentation,
0087                                       CfiOptions& options,
0088                                       bool& wroteSomething) const {
0089     node_left_->writeCfi(os, modifier, startWithComma, indentation, options, wroteSomething);
0090   }
0091 
0092   void XORGroupDescription::print_(std::ostream& os, Modifier modifier, bool writeToCfi, DocFormatHelper& dfh) const {
0093     if (dfh.parent() == DocFormatHelper::XOR) {
0094       dfh.decrementCounter();
0095       node_left_->print(os, Modifier::kNone, true, dfh);
0096       node_right_->print(os, Modifier::kNone, true, dfh);
0097       return;
0098     }
0099 
0100     if (dfh.pass() == 1) {
0101       dfh.indent(os);
0102       os << "XOR group:";
0103 
0104       const bool optional = (modifier == Modifier::kOptional);
0105       const bool obsolete = (modifier == Modifier::kObsolete);
0106       if (dfh.brief()) {
0107         if (optional) {
0108           os << " optional";
0109         }
0110         if (obsolete) {
0111           os << " obsolete";
0112         }
0113         if (!writeToCfi)
0114           os << " (do not write to cfi)";
0115 
0116         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0117       }
0118       // not brief
0119       else {
0120         os << "\n";
0121         dfh.indent2(os);
0122 
0123         if (optional) {
0124           os << "optional";
0125         }
0126         if (obsolete) {
0127           os << " obsolete";
0128         }
0129         if (!writeToCfi)
0130           os << " (do not write to cfi)";
0131         if (optional || !writeToCfi) {
0132           os << "\n";
0133           dfh.indent2(os);
0134         }
0135 
0136         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0137 
0138         if (!comment().empty()) {
0139           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0140         }
0141         os << "\n";
0142       }
0143     }
0144   }
0145 
0146   void XORGroupDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
0147     if (dfh.parent() == DocFormatHelper::XOR) {
0148       dfh.decrementCounter();
0149       node_left_->printNestedContent(os, false, dfh);
0150       node_right_->printNestedContent(os, false, dfh);
0151       return;
0152     }
0153 
0154     int indentation = dfh.indentation();
0155     if (dfh.parent() != DocFormatHelper::TOP) {
0156       indentation -= DocFormatHelper::offsetSectionContent();
0157     }
0158 
0159     std::stringstream ss;
0160     ss << dfh.section() << "." << dfh.counter();
0161     std::string newSection = ss.str();
0162 
0163     printSpaces(os, indentation);
0164     os << "Section " << newSection << " XOR group description:\n";
0165     printSpaces(os, indentation);
0166     if (optional) {
0167       os << "This optional XOR group requires exactly one or none of the following to be in the PSet\n";
0168     } else {
0169       os << "This XOR group requires exactly one of the following to be in the PSet\n";
0170     }
0171     if (!dfh.brief())
0172       os << "\n";
0173 
0174     DocFormatHelper new_dfh(dfh);
0175     new_dfh.init();
0176     new_dfh.setSection(newSection);
0177     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0178     new_dfh.setParent(DocFormatHelper::XOR);
0179 
0180     node_left_->print(os, Modifier::kNone, true, new_dfh);
0181     node_right_->print(os, Modifier::kNone, true, new_dfh);
0182 
0183     new_dfh.setPass(1);
0184     new_dfh.setCounter(0);
0185 
0186     node_left_->print(os, Modifier::kNone, true, new_dfh);
0187     node_right_->print(os, Modifier::kNone, true, new_dfh);
0188 
0189     new_dfh.setPass(2);
0190     new_dfh.setCounter(0);
0191 
0192     node_left_->printNestedContent(os, false, new_dfh);
0193     node_right_->printNestedContent(os, false, new_dfh);
0194   }
0195 
0196   bool XORGroupDescription::exists_(ParameterSet const& pset) const {
0197     int nTotal = node_left_->howManyXORSubNodesExist(pset) + node_right_->howManyXORSubNodesExist(pset);
0198     return nTotal == 1;
0199   }
0200 
0201   bool XORGroupDescription::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0202 
0203   int XORGroupDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const {
0204     return node_left_->howManyXORSubNodesExist(pset) + node_right_->howManyXORSubNodesExist(pset);
0205   }
0206 
0207   void XORGroupDescription::throwMoreThanOneParameter() const {
0208     // Need to expand this error message to print more information
0209     // I guess I need to print out the entire node structure of
0210     // of the xor node and all the nodes it contains.
0211     throw edm::Exception(errors::LogicError)
0212         << "Exactly one parameter can exist in a ParameterSet from a list of\n"
0213         << "parameters described by an \"xor\" operator in a ParameterSetDescription.\n"
0214         << "This rule also applies in a more general sense to the other types\n"
0215         << "of nodes that can appear within a ParameterSetDescription.  Only one\n"
0216         << "can pass validation as \"existing\".\n";
0217   }
0218 
0219   void XORGroupDescription::throwAfterValidation() const {
0220     // Need to expand this error message to print more information
0221     // I guess I need to print out the entire node structure of
0222     // of the xor node and all the nodes it contains.
0223     throw edm::Exception(errors::LogicError)
0224         << "Exactly one parameter can exist in a ParameterSet from a list of\n"
0225         << "parameters described by an \"xor\" operator in a ParameterSetDescription.\n"
0226         << "This rule also applies in a more general sense to the other types\n"
0227         << "of nodes that can appear within a ParameterSetDescription.  Only one\n"
0228         << "can pass validation as \"existing\".  This error has occurred after an\n"
0229         << "attempt to insert missing parameters to fix the problem.\n";
0230   }
0231 }  // namespace edm