Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-23 03:13:15

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, std::set<std::string>& validatedLabels, bool optional) const {
0051     int nExistLeft = node_left_->howManyXORSubNodesExist(pset);
0052     int nExistRight = node_right_->howManyXORSubNodesExist(pset);
0053     int nTotal = nExistLeft + nExistRight;
0054 
0055     if (nTotal == 0 && optional)
0056       return;
0057 
0058     if (nTotal > 1) {
0059       throwMoreThanOneParameter();
0060     }
0061 
0062     if (nExistLeft == 1) {
0063       node_left_->validate(pset, validatedLabels, false);
0064     } else if (nExistRight == 1) {
0065       node_right_->validate(pset, validatedLabels, false);
0066     } else if (nTotal == 0) {
0067       node_left_->validate(pset, validatedLabels, false);
0068 
0069       // When missing parameters get inserted, both nodes could
0070       // be affected so we have to recheck both nodes.
0071       nExistLeft = node_left_->howManyXORSubNodesExist(pset);
0072       nExistRight = node_right_->howManyXORSubNodesExist(pset);
0073       nTotal = nExistLeft + nExistRight;
0074 
0075       if (nTotal != 1) {
0076         throwAfterValidation();
0077       }
0078     }
0079   }
0080 
0081   void XORGroupDescription::writeCfi_(std::ostream& os,
0082                                       bool optional,
0083                                       bool& startWithComma,
0084                                       int indentation,
0085                                       CfiOptions& options,
0086                                       bool& wroteSomething) const {
0087     node_left_->writeCfi(os, optional, startWithComma, indentation, options, wroteSomething);
0088   }
0089 
0090   void XORGroupDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0091     if (dfh.parent() == DocFormatHelper::XOR) {
0092       dfh.decrementCounter();
0093       node_left_->print(os, false, true, dfh);
0094       node_right_->print(os, false, true, dfh);
0095       return;
0096     }
0097 
0098     if (dfh.pass() == 1) {
0099       dfh.indent(os);
0100       os << "XOR group:";
0101 
0102       if (dfh.brief()) {
0103         if (optional)
0104           os << " optional";
0105 
0106         if (!writeToCfi)
0107           os << " (do not write to cfi)";
0108 
0109         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0110       }
0111       // not brief
0112       else {
0113         os << "\n";
0114         dfh.indent2(os);
0115 
0116         if (optional)
0117           os << "optional";
0118         if (!writeToCfi)
0119           os << " (do not write to cfi)";
0120         if (optional || !writeToCfi) {
0121           os << "\n";
0122           dfh.indent2(os);
0123         }
0124 
0125         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0126 
0127         if (!comment().empty()) {
0128           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0129         }
0130         os << "\n";
0131       }
0132     }
0133   }
0134 
0135   void XORGroupDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
0136     if (dfh.parent() == DocFormatHelper::XOR) {
0137       dfh.decrementCounter();
0138       node_left_->printNestedContent(os, false, dfh);
0139       node_right_->printNestedContent(os, false, dfh);
0140       return;
0141     }
0142 
0143     int indentation = dfh.indentation();
0144     if (dfh.parent() != DocFormatHelper::TOP) {
0145       indentation -= DocFormatHelper::offsetSectionContent();
0146     }
0147 
0148     std::stringstream ss;
0149     ss << dfh.section() << "." << dfh.counter();
0150     std::string newSection = ss.str();
0151 
0152     printSpaces(os, indentation);
0153     os << "Section " << newSection << " XOR group description:\n";
0154     printSpaces(os, indentation);
0155     if (optional) {
0156       os << "This optional XOR group requires exactly one or none of the following to be in the PSet\n";
0157     } else {
0158       os << "This XOR group requires exactly one of the following to be in the PSet\n";
0159     }
0160     if (!dfh.brief())
0161       os << "\n";
0162 
0163     DocFormatHelper new_dfh(dfh);
0164     new_dfh.init();
0165     new_dfh.setSection(newSection);
0166     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0167     new_dfh.setParent(DocFormatHelper::XOR);
0168 
0169     node_left_->print(os, false, true, new_dfh);
0170     node_right_->print(os, false, true, new_dfh);
0171 
0172     new_dfh.setPass(1);
0173     new_dfh.setCounter(0);
0174 
0175     node_left_->print(os, false, true, new_dfh);
0176     node_right_->print(os, false, true, new_dfh);
0177 
0178     new_dfh.setPass(2);
0179     new_dfh.setCounter(0);
0180 
0181     node_left_->printNestedContent(os, false, new_dfh);
0182     node_right_->printNestedContent(os, false, new_dfh);
0183   }
0184 
0185   bool XORGroupDescription::exists_(ParameterSet const& pset) const {
0186     int nTotal = node_left_->howManyXORSubNodesExist(pset) + node_right_->howManyXORSubNodesExist(pset);
0187     return nTotal == 1;
0188   }
0189 
0190   bool XORGroupDescription::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0191 
0192   int XORGroupDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const {
0193     return node_left_->howManyXORSubNodesExist(pset) + node_right_->howManyXORSubNodesExist(pset);
0194   }
0195 
0196   void XORGroupDescription::throwMoreThanOneParameter() const {
0197     // Need to expand this error message to print more information
0198     // I guess I need to print out the entire node structure of
0199     // of the xor node and all the nodes it contains.
0200     throw edm::Exception(errors::LogicError)
0201         << "Exactly one parameter can exist in a ParameterSet from a list of\n"
0202         << "parameters described by an \"xor\" operator in a ParameterSetDescription.\n"
0203         << "This rule also applies in a more general sense to the other types\n"
0204         << "of nodes that can appear within a ParameterSetDescription.  Only one\n"
0205         << "can pass validation as \"existing\".\n";
0206   }
0207 
0208   void XORGroupDescription::throwAfterValidation() const {
0209     // Need to expand this error message to print more information
0210     // I guess I need to print out the entire node structure of
0211     // of the xor node and all the nodes it contains.
0212     throw edm::Exception(errors::LogicError)
0213         << "Exactly one parameter can exist in a ParameterSet from a list of\n"
0214         << "parameters described by an \"xor\" operator in a ParameterSetDescription.\n"
0215         << "This rule also applies in a more general sense to the other types\n"
0216         << "of nodes that can appear within a ParameterSetDescription.  Only one\n"
0217         << "can pass validation as \"existing\".  This error has occurred after an\n"
0218         << "attempt to insert missing parameters to fix the problem.\n";
0219   }
0220 }  // namespace edm