Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:30

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_(
0082       std::ostream& os, bool optional, bool& startWithComma, int indentation, bool& wroteSomething) const {
0083     node_left_->writeCfi(os, optional, startWithComma, indentation, wroteSomething);
0084   }
0085 
0086   void XORGroupDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0087     if (dfh.parent() == DocFormatHelper::XOR) {
0088       dfh.decrementCounter();
0089       node_left_->print(os, false, true, dfh);
0090       node_right_->print(os, false, true, dfh);
0091       return;
0092     }
0093 
0094     if (dfh.pass() == 1) {
0095       dfh.indent(os);
0096       os << "XOR group:";
0097 
0098       if (dfh.brief()) {
0099         if (optional)
0100           os << " optional";
0101 
0102         if (!writeToCfi)
0103           os << " (do not write to cfi)";
0104 
0105         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0106       }
0107       // not brief
0108       else {
0109         os << "\n";
0110         dfh.indent2(os);
0111 
0112         if (optional)
0113           os << "optional";
0114         if (!writeToCfi)
0115           os << " (do not write to cfi)";
0116         if (optional || !writeToCfi) {
0117           os << "\n";
0118           dfh.indent2(os);
0119         }
0120 
0121         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0122 
0123         if (!comment().empty()) {
0124           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0125         }
0126         os << "\n";
0127       }
0128     }
0129   }
0130 
0131   void XORGroupDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
0132     if (dfh.parent() == DocFormatHelper::XOR) {
0133       dfh.decrementCounter();
0134       node_left_->printNestedContent(os, false, dfh);
0135       node_right_->printNestedContent(os, false, dfh);
0136       return;
0137     }
0138 
0139     int indentation = dfh.indentation();
0140     if (dfh.parent() != DocFormatHelper::TOP) {
0141       indentation -= DocFormatHelper::offsetSectionContent();
0142     }
0143 
0144     std::stringstream ss;
0145     ss << dfh.section() << "." << dfh.counter();
0146     std::string newSection = ss.str();
0147 
0148     printSpaces(os, indentation);
0149     os << "Section " << newSection << " XOR group description:\n";
0150     printSpaces(os, indentation);
0151     if (optional) {
0152       os << "This optional XOR group requires exactly one or none of the following to be in the PSet\n";
0153     } else {
0154       os << "This XOR group requires exactly 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::XOR);
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 XORGroupDescription::exists_(ParameterSet const& pset) const {
0182     int nTotal = node_left_->howManyXORSubNodesExist(pset) + node_right_->howManyXORSubNodesExist(pset);
0183     return nTotal == 1;
0184   }
0185 
0186   bool XORGroupDescription::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0187 
0188   int XORGroupDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const {
0189     return node_left_->howManyXORSubNodesExist(pset) + node_right_->howManyXORSubNodesExist(pset);
0190   }
0191 
0192   void XORGroupDescription::throwMoreThanOneParameter() const {
0193     // Need to expand this error message to print more information
0194     // I guess I need to print out the entire node structure of
0195     // of the xor node and all the nodes it contains.
0196     throw edm::Exception(errors::LogicError)
0197         << "Exactly one parameter can exist in a ParameterSet from a list of\n"
0198         << "parameters described by an \"xor\" operator in a ParameterSetDescription.\n"
0199         << "This rule also applies in a more general sense to the other types\n"
0200         << "of nodes that can appear within a ParameterSetDescription.  Only one\n"
0201         << "can pass validation as \"existing\".\n";
0202   }
0203 
0204   void XORGroupDescription::throwAfterValidation() const {
0205     // Need to expand this error message to print more information
0206     // I guess I need to print out the entire node structure of
0207     // of the xor node and all the nodes it contains.
0208     throw edm::Exception(errors::LogicError)
0209         << "Exactly one parameter can exist in a ParameterSet from a list of\n"
0210         << "parameters described by an \"xor\" operator in a ParameterSetDescription.\n"
0211         << "This rule also applies in a more general sense to the other types\n"
0212         << "of nodes that can appear within a ParameterSetDescription.  Only one\n"
0213         << "can pass validation as \"existing\".  This error has occurred after an\n"
0214         << "attempt to insert missing parameters to fix the problem.\n";
0215   }
0216 }  // namespace edm