https://mooseframework.inl.gov
Loading...
Searching...
No Matches
YAMLFormatter.C
Go to the documentation of this file.
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
24YAMLFormatter::YAMLFormatter(bool dump_mode) : SyntaxTree(true), _dump_mode(dump_mode) {}
25
26std::string
28{
29 // important: start and end yaml data delimiters used by python
30 return "**START YAML DATA**\n";
31}
32
33std::string
35{
36 return "**END YAML DATA**\n";
37}
38
39std::string
40YAMLFormatter::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 std::ostringstream oss;
48 std::string indent(depth * 2, ' ');
49
50 for (auto & iter : params)
51 {
52 std::string name = iter.first;
53 // First make sure we want to see this parameter, also block active and type
54 if (params.isPrivate(iter.first) || name == "active" ||
55 (search_string != "" && search_string != iter.first) || haveSeenIt(prefix, iter.first))
56 continue;
57
58 found = true;
59
60 // Mark it as "seen"
61 seenIt(prefix, iter.first);
62
63 // Block params may be required and will have a doc string
64 std::string required = params.isParamRequired(iter.first) ? "Yes" : "No";
65
66 oss << indent << " - name: " << name << "\n";
67 oss << indent << " required: " << required << "\n";
68 oss << indent << " default: !!str ";
69
70 // Only output default if it has one
71 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 std::ostringstream toss;
79 buildOutputString(toss, iter);
80
81 // remove additional '\n' possibly generated in output (breaks YAML parsing)
82 std::string tmp_str = toss.str();
83 for (auto & ch : tmp_str)
84 if (ch == '\n')
85 ch = ' ';
86 if (tmp_str == ",")
87 oss << "\"" << tmp_str << "\"";
88 else
89 oss << tmp_str;
90 }
91 else if (params.hasDefaultCoupledValue(iter.first))
92 oss << params.defaultCoupledValue(iter.first);
93
94 std::string doc = params.getDocString(iter.first);
96 // Print the type
97 oss << "\n"
98 << indent << " cpp_type: " << params.type(iter.first) << "\n"
99 << indent << " group_name: ";
100 std::string group_name = params.getGroupName(iter.first);
101 if (!group_name.empty())
102 oss << "'" << group_name << "'";
103 oss << "\n";
104
105 oss << indent << " doc_unit: ";
106 std::string doc_unit = params.getDocUnit(iter.first);
107 if (!doc_unit.empty())
108 oss << "'" << doc_unit << "'";
109 oss << "\n";
110
111 oss << indent << " doc_range: ";
112 std::string doc_range;
113 if (params.isRangeChecked(iter.first))
114 doc_range = params.rangeCheckedFunction(iter.first);
115 if (!doc_range.empty())
116 oss << "'" << doc_range << "'";
117 oss << "\n";
118
119 if (params.have_parameter<MooseEnum>(name))
120 addEnumOptionsAndDocs(oss, params.get<MooseEnum>(name), indent);
121 if (params.have_parameter<MultiMooseEnum>(name))
122 addEnumOptionsAndDocs(oss, params.get<MultiMooseEnum>(name), indent);
123 if (params.have_parameter<ExecFlagEnum>(name))
124 addEnumOptionsAndDocs(oss, params.get<ExecFlagEnum>(name), indent);
125 if (params.have_parameter<std::vector<MooseEnum>>(name))
126 addEnumOptionsAndDocs(oss, params.get<std::vector<MooseEnum>>(name)[0], indent);
127
128 oss << indent << " description: |\n " << indent << doc << std::endl;
129 }
130
131 return oss.str();
132}
133
134template <typename T>
135void
137 T & param,
138 const std::string & indent)
139{
140 oss << indent << " options: " << param.getRawNames() << '\n';
141 const auto & docs = param.getItemDocumentation();
142 if (!docs.empty())
143 {
144 oss << indent << " option_docs:\n";
145 for (const auto & doc : docs)
146 {
147 oss << indent << " - name: " << doc.first.name() << "\n";
148 oss << indent << " description: |\n";
149 oss << indent << " " << doc.second << "\n";
150 }
151 }
152}
153
154std::string
156{
157 std::string indent(depth * 2, ' ');
158
159 return indent + " subblocks:\n";
160}
161
162std::string
163YAMLFormatter::printBlockOpen(const std::string & name, short depth, const std::string & doc)
164{
165 std::ostringstream oss;
166 std::string indent(depth * 2, ' ');
167
168 std::string docEscaped = doc;
169 MooseUtils::escape(docEscaped);
170
171 oss << indent << "- name: " << name << "\n";
172 oss << indent << " description: |\n" << indent << " " << docEscaped << "\n";
173 oss << indent << " parameters:\n";
174
175 return oss.str();
176}
177
178std::string
179YAMLFormatter::printBlockClose(const std::string & /*name*/, short /*depth*/) const
180{
181 return std::string();
182}
183
184void
186 std::ostringstream & output,
187 const std::iterator_traits<InputParameters::iterator>::value_type & p)
188{
189 libMesh::Parameters::Value * val = MooseUtils::get(p.second);
190
191 // Account for Point
192 InputParameters::Parameter<Point> * ptr0 = dynamic_cast<InputParameters::Parameter<Point> *>(val);
193
194 // Account for RealVectorValues
195 InputParameters::Parameter<RealVectorValue> * ptr1 =
196 dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(val);
197
198 // Output the Point components
199 if (ptr0)
200 output << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " "
201 << ptr0->get().operator()(2);
202
203 // Output the RealVectorValue components
204 else if (ptr1)
205 output << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " "
206 << ptr1->get().operator()(2);
207
208 // General case, call the print operator
209 else
210 p.second->print(output);
211}
A MultiMooseEnum object to hold "execute_on" flags.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
bool isPrivate(const std::string &name) const
Returns a Boolean indicating whether the specified parameter is private or not.
std::string rangeCheckedFunction(const std::string &name) const
Return the range check function for any parameter (empty string if it is not range checked)
std::string getDocString(const std::string &name) const
Returns the documentation string for the specified parameter name.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
std::string type(const std::string &name) const
Prints the type of the requested parameter by name.
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
std::string getDocUnit(const std::string &name) const
Returns the documentation unit string for the specified parameter name.
bool isParamRequired(const std::string &name) const
Returns a boolean indicating whether the specified parameter is required or not.
bool hasDefaultCoupledValue(const std::string &coupling_name) const
Return whether or not the requested parameter has a default coupled value.
std::string getGroupName(const std::string &param_name) const
This method retrieves the group name for the passed parameter name if one exists.
Real defaultCoupledValue(const std::string &coupling_name, unsigned int i=0) const
Get the default value for an optionally coupled variable.
bool isRangeChecked(const std::string &param_name) const
Return whether a parameter has a range check.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another,...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type.
void seenIt(const std::string &prefix, const std::string &item)
Definition SyntaxTree.C:57
bool haveSeenIt(const std::string &prefix, const std::string &item) const
Definition SyntaxTree.C:63
YAMLFormatter(bool dump_mode)
virtual std::string printBlockOpen(const std::string &name, short depth, const std::string &doc) override
This method is called at the beginning of each Node in the tree.
void buildOutputString(std::ostringstream &output, const std::iterator_traits< InputParameters::iterator >::value_type &p)
Method for building an output string that accounts for specific types (e.g., Point)
virtual std::string preamble() const override
This method is called once at the beginning of the tree traversal and can be used to build up header ...
virtual std::string printParams(const std::string &prefix, const std::string &fully_qualified_name, InputParameters &params, short depth, const std::string &search_string, bool &found) override
This function is called for each InputParameters object stored at a particular node.
virtual std::string postscript() const override
This method is called once at the end of the tree traversal and can be used to add any necessary trai...
virtual std::string preTraverse(short depth) const override
This method is called once at each node in the syntax tree before traversing child nodes.
void addEnumOptionsAndDocs(std::ostringstream &oss, T &param, const std::string &indent)
virtual std::string printBlockClose(const std::string &name, short depth) const override
This method is called at the end of of each Node in the tree.
void escape(std::string &str)
Definition MooseUtils.C:218