Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #include "YAMLFormatter.h" 11 : 12 : // MOOSE includes 13 : #include "MooseEnum.h" 14 : #include "MultiMooseEnum.h" 15 : #include "Parser.h" 16 : 17 : #include "libmesh/vector_value.h" 18 : #include "libmesh/point.h" 19 : 20 : // C++ includes 21 : #include <sstream> 22 : #include <vector> 23 : 24 20 : YAMLFormatter::YAMLFormatter(bool dump_mode) : SyntaxTree(true), _dump_mode(dump_mode) {} 25 : 26 : std::string 27 20 : YAMLFormatter::preamble() const 28 : { 29 : // important: start and end yaml data delimiters used by python 30 20 : return "**START YAML DATA**\n"; 31 : } 32 : 33 : std::string 34 20 : YAMLFormatter::postscript() const 35 : { 36 20 : return "**END YAML DATA**\n"; 37 : } 38 : 39 : std::string 40 46758 : YAMLFormatter::printParams(const std::string & prefix, 41 : const std::string & /*fully_qualified_name*/, 42 : InputParameters & params, 43 : short depth, 44 : const std::string & search_string, 45 : bool & found) 46 : { 47 46758 : std::ostringstream oss; 48 46758 : std::string indent(depth * 2, ' '); 49 : 50 1593786 : for (auto & iter : params) 51 : { 52 1547028 : std::string name = iter.first; 53 : // First make sure we want to see this parameter, also block active and type 54 3161376 : if (params.isPrivate(iter.first) || name == "active" || 55 3161376 : (search_string != "" && search_string != iter.first) || haveSeenIt(prefix, iter.first)) 56 1129912 : continue; 57 : 58 417116 : found = true; 59 : 60 : // Mark it as "seen" 61 417116 : seenIt(prefix, iter.first); 62 : 63 : // Block params may be required and will have a doc string 64 417116 : std::string required = params.isParamRequired(iter.first) ? "Yes" : "No"; 65 : 66 417116 : oss << indent << " - name: " << name << "\n"; 67 417116 : oss << indent << " required: " << required << "\n"; 68 417116 : oss << indent << " default: !!str "; 69 : 70 : // Only output default if it has one 71 417116 : if (params.isParamValid(iter.first)) 72 : { 73 : // prints the value, which is the default value when dumping the tree 74 : // because it hasn't been changed 75 : 76 : // Output stream, performing special operations for writing objects such as Points and 77 : // RealVectorValues 78 281727 : std::ostringstream toss; 79 281727 : buildOutputString(toss, iter); 80 : 81 : // remove additional '\n' possibly generated in output (breaks YAML parsing) 82 281727 : std::string tmp_str = toss.str(); 83 1294450 : for (auto & ch : tmp_str) 84 1012723 : if (ch == '\n') 85 0 : ch = ' '; 86 281727 : if (tmp_str == ",") 87 70 : oss << "\"" << tmp_str << "\""; 88 : else 89 281657 : oss << tmp_str; 90 281727 : } 91 135389 : else if (params.hasDefaultCoupledValue(iter.first)) 92 340 : oss << params.defaultCoupledValue(iter.first); 93 : 94 417116 : std::string doc = params.getDocString(iter.first); 95 417116 : MooseUtils::escape(doc); 96 : // Print the type 97 : oss << "\n" 98 417116 : << indent << " cpp_type: " << params.type(iter.first) << "\n" 99 417116 : << indent << " group_name: "; 100 417116 : std::string group_name = params.getGroupName(iter.first); 101 417116 : if (!group_name.empty()) 102 252279 : oss << "'" << group_name << "'"; 103 417116 : oss << "\n"; 104 : 105 417116 : oss << indent << " doc_unit: "; 106 417116 : std::string doc_unit = params.getDocUnit(iter.first); 107 417116 : if (!doc_unit.empty()) 108 0 : oss << "'" << doc_unit << "'"; 109 417116 : oss << "\n"; 110 : 111 417116 : if (params.have_parameter<MooseEnum>(name)) 112 12619 : addEnumOptionsAndDocs(oss, params.get<MooseEnum>(name), indent); 113 417116 : if (params.have_parameter<MultiMooseEnum>(name)) 114 12798 : addEnumOptionsAndDocs(oss, params.get<MultiMooseEnum>(name), indent); 115 417116 : if (params.have_parameter<ExecFlagEnum>(name)) 116 10852 : addEnumOptionsAndDocs(oss, params.get<ExecFlagEnum>(name), indent); 117 417116 : if (params.have_parameter<std::vector<MooseEnum>>(name)) 118 20 : addEnumOptionsAndDocs(oss, params.get<std::vector<MooseEnum>>(name)[0], indent); 119 : 120 417116 : oss << indent << " description: |\n " << indent << doc << std::endl; 121 1547028 : } 122 : 123 93516 : return oss.str(); 124 46758 : } 125 : 126 : template <typename T> 127 : void 128 36289 : YAMLFormatter::addEnumOptionsAndDocs(std::ostringstream & oss, 129 : T & param, 130 : const std::string & indent) 131 : { 132 36289 : oss << indent << " options: " << param.getRawNames() << '\n'; 133 36289 : const auto & docs = param.getItemDocumentation(); 134 36289 : if (!docs.empty()) 135 : { 136 30 : oss << indent << " option_docs:\n"; 137 130 : for (const auto & doc : docs) 138 : { 139 100 : oss << indent << " - name: " << doc.first.name() << "\n"; 140 100 : oss << indent << " description: |\n"; 141 100 : oss << indent << " " << doc.second << "\n"; 142 : } 143 : } 144 36289 : } 145 : 146 : std::string 147 47946 : YAMLFormatter::preTraverse(short depth) const 148 : { 149 47946 : std::string indent(depth * 2, ' '); 150 : 151 95892 : return indent + " subblocks:\n"; 152 47946 : } 153 : 154 : std::string 155 47946 : YAMLFormatter::printBlockOpen(const std::string & name, short depth, const std::string & doc) 156 : { 157 47946 : std::ostringstream oss; 158 47946 : std::string indent(depth * 2, ' '); 159 : 160 47946 : std::string docEscaped = doc; 161 47946 : MooseUtils::escape(docEscaped); 162 : 163 47946 : oss << indent << "- name: " << name << "\n"; 164 47946 : oss << indent << " description: |\n" << indent << " " << docEscaped << "\n"; 165 47946 : oss << indent << " parameters:\n"; 166 : 167 95892 : return oss.str(); 168 47946 : } 169 : 170 : std::string 171 47946 : YAMLFormatter::printBlockClose(const std::string & /*name*/, short /*depth*/) const 172 : { 173 47946 : return std::string(); 174 : } 175 : 176 : void 177 281727 : YAMLFormatter::buildOutputString( 178 : std::ostringstream & output, 179 : const std::iterator_traits<InputParameters::iterator>::value_type & p) 180 : { 181 281727 : libMesh::Parameters::Value * val = MooseUtils::get(p.second); 182 : 183 : // Account for Point 184 281727 : InputParameters::Parameter<Point> * ptr0 = dynamic_cast<InputParameters::Parameter<Point> *>(val); 185 : 186 : // Account for RealVectorValues 187 : InputParameters::Parameter<RealVectorValue> * ptr1 = 188 281727 : dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(val); 189 : 190 : // Output the Point components 191 281727 : if (ptr0) 192 270 : output << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " " 193 270 : << ptr0->get().operator()(2); 194 : 195 : // Output the RealVectorValue components 196 281457 : else if (ptr1) 197 120 : output << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " " 198 120 : << ptr1->get().operator()(2); 199 : 200 : // General case, call the print operator 201 : else 202 281337 : p.second->print(output); 203 281727 : }