www.mooseframework.org
YAMLFormatter.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "YAMLFormatter.h"
16 
17 // MOOSE includes
18 #include "MooseEnum.h"
19 #include "MultiMooseEnum.h"
20 #include "Parser.h"
21 
22 // C++ includes
23 #include <sstream>
24 #include <vector>
25 
26 YAMLFormatter::YAMLFormatter(bool dump_mode) : SyntaxTree(true), _dump_mode(dump_mode) {}
27 
28 std::string
30 {
31  // important: start and end yaml data delimiters used by python
32  return "**START YAML DATA**\n";
33 }
34 
35 std::string
37 {
38  return "**END YAML DATA**\n";
39 }
40 
41 std::string
42 YAMLFormatter::printParams(const std::string & prefix,
43  const std::string & /*fully_qualified_name*/,
44  InputParameters & params,
45  short depth,
46  const std::string & search_string,
47  bool & found)
48 {
49  std::ostringstream oss;
50  std::string indent(depth * 2, ' ');
51 
52  for (auto & iter : params)
53  {
54  std::string name = iter.first;
55  // First make sure we want to see this parameter, also block active and type
56  if (params.isPrivate(iter.first) || name == "active" ||
57  (search_string != "" && search_string != iter.first) || haveSeenIt(prefix, iter.first))
58  continue;
59 
60  found = true;
61 
62  // Mark it as "seen"
63  seenIt(prefix, iter.first);
64 
65  // Block params may be required and will have a doc string
66  std::string required = params.isParamRequired(iter.first) ? "Yes" : "No";
67 
68  oss << indent << " - name: " << name << "\n";
69  oss << indent << " required: " << required << "\n";
70  oss << indent << " default: !!str ";
71 
72  // Only output default if it has one
73  if (params.isParamValid(iter.first))
74  {
75  // prints the value, which is the default value when dumping the tree
76  // because it hasn't been changed
77 
78  // Output stream, performing special operations for writing objects such as Points and
79  // RealVectorValues
80  std::ostringstream toss;
81  buildOutputString(toss, iter);
82 
83  // remove additional '\n' possibly generated in output (breaks YAML parsing)
84  std::string tmp_str = toss.str();
85  for (auto & ch : tmp_str)
86  if (ch == '\n')
87  ch = ' ';
88  if (tmp_str == ",")
89  oss << "\"" << tmp_str << "\"";
90  else
91  oss << tmp_str;
92  }
93  else if (params.hasDefaultCoupledValue(iter.first))
94  oss << params.defaultCoupledValue(iter.first);
95 
96  std::string doc = params.getDocString(iter.first);
97  MooseUtils::escape(doc);
98  // Print the type
99  oss << "\n"
100  << indent << " cpp_type: " << params.type(iter.first) << "\n"
101  << indent << " group_name: ";
102  std::string group_name = params.getGroupName(iter.first);
103  if (!group_name.empty())
104  oss << "'" << group_name << "'";
105 
106  {
107  InputParameters::Parameter<MooseEnum> * enum_type =
108  dynamic_cast<InputParameters::Parameter<MooseEnum> *>(iter.second);
109  if (enum_type)
110  oss << "\n" << indent << " options: " << enum_type->get().getRawNames();
111  }
112  {
113  InputParameters::Parameter<MultiMooseEnum> * enum_type =
114  dynamic_cast<InputParameters::Parameter<MultiMooseEnum> *>(iter.second);
115  if (enum_type)
116  oss << "\n" << indent << " options: " << enum_type->get().getRawNames();
117  }
118  {
119  InputParameters::Parameter<std::vector<MooseEnum>> * enum_type =
120  dynamic_cast<InputParameters::Parameter<std::vector<MooseEnum>> *>(iter.second);
121  if (enum_type)
122  oss << "\n" << indent << " options: " << (enum_type->get())[0].getRawNames();
123  }
124 
125  oss << "\n" << indent << " description: |\n " << indent << doc << "\n";
126  }
127 
128  return oss.str();
129 }
130 
131 std::string
132 YAMLFormatter::preTraverse(short depth) const
133 {
134  std::string indent(depth * 2, ' ');
135 
136  return indent + " subblocks:\n";
137 }
138 
139 std::string
140 YAMLFormatter::printBlockOpen(const std::string & name, short depth, const std::string & doc)
141 {
142  std::ostringstream oss;
143  std::string indent(depth * 2, ' ');
144 
145  std::string docEscaped = doc;
146  MooseUtils::escape(docEscaped);
147 
148  oss << indent << "- name: " << name << "\n";
149  oss << indent << " description: |\n" << indent << " " << docEscaped << "\n";
150  oss << indent << " parameters:\n";
151 
152  return oss.str();
153 }
154 
155 std::string
156 YAMLFormatter::printBlockClose(const std::string & /*name*/, short /*depth*/) const
157 {
158  return std::string();
159 }
160 
161 void
163  std::ostringstream & output,
164  const std::iterator_traits<InputParameters::iterator>::value_type & p)
165 {
166  // Account for Point
167  InputParameters::Parameter<Point> * ptr0 =
168  dynamic_cast<InputParameters::Parameter<Point> *>(p.second);
169 
170  // Account for RealVectorValues
171  InputParameters::Parameter<RealVectorValue> * ptr1 =
172  dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(p.second);
173 
174  // Output the Point components
175  if (ptr0)
176  output << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " "
177  << ptr0->get().operator()(2);
178 
179  // Output the RealVectorValue components
180  else if (ptr1)
181  output << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " "
182  << ptr1->get().operator()(2);
183 
184  // General case, call the print operator
185  else
186  p.second->print(output);
187 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:34
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 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 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.
virtual std::string preTraverse(short depth) const override
This method is called once at each node in the syntax tree before traversing child nodes...
YAMLFormatter(bool dump_mode)
Definition: YAMLFormatter.C:26
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:29
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:42
bool haveSeenIt(const std::string &prefix, const std::string &item) const
Definition: SyntaxTree.C:68
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:36
void seenIt(const std::string &prefix, const std::string &item)
Definition: SyntaxTree.C:62
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:77