www.mooseframework.org
YAMLFormatter.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 YAMLFormatter::YAMLFormatter(bool dump_mode) : SyntaxTree(true), _dump_mode(dump_mode) {}
25 
26 std::string
28 {
29  // important: start and end yaml data delimiters used by python
30  return "**START YAML DATA**\n";
31 }
32 
33 std::string
35 {
36  return "**END YAML DATA**\n";
37 }
38 
39 std::string
40 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  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);
95  MooseUtils::escape(doc);
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 
104  {
105  InputParameters::Parameter<MooseEnum> * enum_type =
106  dynamic_cast<InputParameters::Parameter<MooseEnum> *>(iter.second);
107  if (enum_type)
108  oss << "\n" << indent << " options: " << enum_type->get().getRawNames();
109  }
110  {
111  InputParameters::Parameter<MultiMooseEnum> * enum_type =
112  dynamic_cast<InputParameters::Parameter<MultiMooseEnum> *>(iter.second);
113  if (enum_type)
114  oss << "\n" << indent << " options: " << enum_type->get().getRawNames();
115  }
116  {
117  InputParameters::Parameter<std::vector<MooseEnum>> * enum_type =
118  dynamic_cast<InputParameters::Parameter<std::vector<MooseEnum>> *>(iter.second);
119  if (enum_type)
120  oss << "\n" << indent << " options: " << (enum_type->get())[0].getRawNames();
121  }
122 
123  oss << "\n" << indent << " description: |\n " << indent << doc << "\n";
124  }
125 
126  return oss.str();
127 }
128 
129 std::string
130 YAMLFormatter::preTraverse(short depth) const
131 {
132  std::string indent(depth * 2, ' ');
133 
134  return indent + " subblocks:\n";
135 }
136 
137 std::string
138 YAMLFormatter::printBlockOpen(const std::string & name, short depth, const std::string & doc)
139 {
140  std::ostringstream oss;
141  std::string indent(depth * 2, ' ');
142 
143  std::string docEscaped = doc;
144  MooseUtils::escape(docEscaped);
145 
146  oss << indent << "- name: " << name << "\n";
147  oss << indent << " description: |\n" << indent << " " << docEscaped << "\n";
148  oss << indent << " parameters:\n";
149 
150  return oss.str();
151 }
152 
153 std::string
154 YAMLFormatter::printBlockClose(const std::string & /*name*/, short /*depth*/) const
155 {
156  return std::string();
157 }
158 
159 void
161  std::ostringstream & output,
162  const std::iterator_traits<InputParameters::iterator>::value_type & p)
163 {
164  // Account for Point
165  InputParameters::Parameter<Point> * ptr0 =
166  dynamic_cast<InputParameters::Parameter<Point> *>(p.second);
167 
168  // Account for RealVectorValues
169  InputParameters::Parameter<RealVectorValue> * ptr1 =
170  dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(p.second);
171 
172  // Output the Point components
173  if (ptr0)
174  output << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " "
175  << ptr0->get().operator()(2);
176 
177  // Output the RealVectorValue components
178  else if (ptr1)
179  output << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " "
180  << ptr1->get().operator()(2);
181 
182  // General case, call the print operator
183  else
184  p.second->print(output);
185 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:30
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...
Definition: YAMLFormatter.C:34
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) ...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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.
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.
YAMLFormatter(bool dump_mode)
Definition: YAMLFormatter.C:24
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 ...
Definition: YAMLFormatter.C:27
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.
Definition: YAMLFormatter.C:40
virtual std::string preTraverse(short depth) const override
This method is called once at each node in the syntax tree before traversing child nodes...
bool haveSeenIt(const std::string &prefix, const std::string &item) const
Definition: SyntaxTree.C:63
void seenIt(const std::string &prefix, const std::string &item)
Definition: SyntaxTree.C:57
void escape(std::string &str)
This function will escape all of the standard C++ escape characters so that they can be printed...
Definition: MooseUtils.C:95