www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
YAMLFormatter Class Reference

This class produces produces a yaml dump of the InputParameters that is machine parsable by any YAML formatter. More...

#include <YAMLFormatter.h>

Inheritance diagram for YAMLFormatter:
[legend]

Public Member Functions

 YAMLFormatter (bool dump_mode)
 
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 information. More...
 
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 trailing information to the final formatted string. More...
 
virtual std::string preTraverse (short depth) const override
 This method is called once at each node in the syntax tree before traversing child nodes. More...
 
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. More...
 
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. More...
 
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. More...
 
template<typename T >
void addEnumOptionsAndDocs (std::ostringstream &oss, T &param, const std::string &indent)
 
void insertNode (std::string syntax, const std::string &action, bool is_action_params=true, InputParameters *params=NULL)
 
std::string print (const std::string &search_string)
 
void seenIt (const std::string &prefix, const std::string &item)
 
bool haveSeenIt (const std::string &prefix, const std::string &item) const
 

Protected Member Functions

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) More...
 
bool isLongNames () const
 

Protected Attributes

bool _dump_mode
 
std::unique_ptr< TreeNode_root
 
bool _use_long_names
 

Detailed Description

This class produces produces a yaml dump of the InputParameters that is machine parsable by any YAML formatter.

Definition at line 22 of file YAMLFormatter.h.

Constructor & Destructor Documentation

◆ YAMLFormatter()

YAMLFormatter::YAMLFormatter ( bool  dump_mode)

Definition at line 24 of file YAMLFormatter.C.

24 : SyntaxTree(true), _dump_mode(dump_mode) {}
SyntaxTree(bool use_long_names=false)
Definition: SyntaxTree.C:19

Member Function Documentation

◆ addEnumOptionsAndDocs()

template<typename T >
void YAMLFormatter::addEnumOptionsAndDocs ( std::ostringstream &  oss,
T &  param,
const std::string &  indent 
)

Definition at line 122 of file YAMLFormatter.C.

Referenced by printParams().

125 {
126  oss << indent << " options: " << param.getRawNames() << '\n';
127  const auto & docs = param.getItemDocumentation();
128  if (!docs.empty())
129  {
130  oss << indent << " option_docs:\n";
131  for (const auto & doc : docs)
132  {
133  oss << indent << " - name: " << doc.first.name() << "\n";
134  oss << indent << " description: |\n";
135  oss << indent << " " << doc.second << "\n";
136  }
137  }
138 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:31

◆ buildOutputString()

void YAMLFormatter::buildOutputString ( std::ostringstream &  output,
const std::iterator_traits< InputParameters::iterator >::value_type &  p 
)
protected

Method for building an output string that accounts for specific types (e.g., Point)

Parameters
outputReference to the output string
iterInputParameters iterator that is being output

Definition at line 171 of file YAMLFormatter.C.

Referenced by printParams().

174 {
176 
177  // Account for Point
178  InputParameters::Parameter<Point> * ptr0 = dynamic_cast<InputParameters::Parameter<Point> *>(val);
179 
180  // Account for RealVectorValues
181  InputParameters::Parameter<RealVectorValue> * ptr1 =
182  dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(val);
183 
184  // Output the Point components
185  if (ptr0)
186  output << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " "
187  << ptr0->get().operator()(2);
188 
189  // Output the RealVectorValue components
190  else if (ptr1)
191  output << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " "
192  << ptr1->get().operator()(2);
193 
194  // General case, call the print operator
195  else
196  p.second->print(output);
197 }
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1147

◆ haveSeenIt()

bool SyntaxTree::haveSeenIt ( const std::string &  prefix,
const std::string &  item 
) const
inherited

Definition at line 63 of file SyntaxTree.C.

Referenced by InputFileFormatter::printParams(), and printParams().

64 {
65  return _params_printed.find(prefix + item) != _params_printed.end();
66 }
std::set< std::string > _params_printed
Definition: SyntaxTree.h:80

◆ insertNode()

void SyntaxTree::insertNode ( std::string  syntax,
const std::string &  action,
bool  is_action_params = true,
InputParameters params = NULL 
)
inherited

Definition at line 27 of file SyntaxTree.C.

Referenced by ActionWarehouse::printInputFile().

31 {
32  if (!_root)
33  _root = std::make_unique<TreeNode>("", *this);
34 
35  _root->insertNode(syntax, action, is_action_params, params);
36 }
std::unique_ptr< TreeNode > _root
Definition: SyntaxTree.h:76

◆ isLongNames()

bool SyntaxTree::isLongNames ( ) const
protectedinherited

Definition at line 227 of file SyntaxTree.C.

228 {
229  return _use_long_names;
230 }
bool _use_long_names
Definition: SyntaxTree.h:77

◆ postscript()

std::string YAMLFormatter::postscript ( ) const
overridevirtual

This method is called once at the end of the tree traversal and can be used to add any necessary trailing information to the final formatted string.

Returns
- The formatted postscript string

Reimplemented from SyntaxFormatterInterface.

Definition at line 34 of file YAMLFormatter.C.

35 {
36  return "**END YAML DATA**\n";
37 }

◆ preamble()

std::string YAMLFormatter::preamble ( ) const
overridevirtual

This method is called once at the beginning of the tree traversal and can be used to build up header information.

Returns
- The formatted preamble string

Reimplemented from SyntaxFormatterInterface.

Definition at line 27 of file YAMLFormatter.C.

28 {
29  // important: start and end yaml data delimiters used by python
30  return "**START YAML DATA**\n";
31 }

◆ preTraverse()

std::string YAMLFormatter::preTraverse ( short  ) const
overridevirtual

This method is called once at each node in the syntax tree before traversing child nodes.

Returns
- The formatted pre-node traversal string

Reimplemented from SyntaxFormatterInterface.

Definition at line 141 of file YAMLFormatter.C.

142 {
143  std::string indent(depth * 2, ' ');
144 
145  return indent + " subblocks:\n";
146 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:31

◆ print()

std::string SyntaxTree::print ( const std::string &  search_string)
inherited

Definition at line 39 of file SyntaxTree.C.

Referenced by ActionWarehouse::printInputFile().

40 {
41  bool found = false;
42  std::string output;
43 
44  // Clear the list of "seen" parameters before printing the tree
45  _params_printed.clear();
46 
47  if (_root)
48  output = _root->print(-1, search_string, found);
49 
50  if (found)
51  return preamble() + output + postscript();
52  else
53  return "";
54 }
std::set< std::string > _params_printed
Definition: SyntaxTree.h:80
virtual std::string preamble() const
This method is called once at the beginning of the tree traversal and can be used to build up header ...
std::unique_ptr< TreeNode > _root
Definition: SyntaxTree.h:76
virtual std::string postscript() const
This method is called once at the end of the tree traversal and can be used to add any necessary trai...

◆ printBlockClose()

std::string YAMLFormatter::printBlockClose ( const std::string &  name,
short  depth 
) const
overridevirtual

This method is called at the end of of each Node in the tree.

It is typically used to provide formatting necessary when closing blocks.

Returns
- The formatted block close string

Implements SyntaxFormatterInterface.

Definition at line 165 of file YAMLFormatter.C.

166 {
167  return std::string();
168 }

◆ printBlockOpen()

std::string YAMLFormatter::printBlockOpen ( const std::string &  name,
short  depth,
const std::string &  doc 
)
overridevirtual

This method is called at the beginning of each Node in the tree.

It is typically used to provide formatting necessary when opening new blocks.

Returns
- The formatted block open string

Implements SyntaxFormatterInterface.

Definition at line 149 of file YAMLFormatter.C.

150 {
151  std::ostringstream oss;
152  std::string indent(depth * 2, ' ');
153 
154  std::string docEscaped = doc;
155  MooseUtils::escape(docEscaped);
156 
157  oss << indent << "- name: " << name << "\n";
158  oss << indent << " description: |\n" << indent << " " << docEscaped << "\n";
159  oss << indent << " parameters:\n";
160 
161  return oss.str();
162 }
std::string name(const ElemQuality q)
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:31
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:196

◆ printParams()

std::string YAMLFormatter::printParams ( const std::string &  prefix,
const std::string &  fully_qualified_name,
InputParameters params,
short  depth,
const std::string &  search_string,
bool &  found 
)
overridevirtual

This function is called for each InputParameters object stored at a particular node.

It is responsible for formatting the parameters for the current node.

Returns
- The formatted parameters string for a Node.

Implements SyntaxFormatterInterface.

Definition at line 40 of file YAMLFormatter.C.

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  oss << "\n";
104 
105  if (params.have_parameter<MooseEnum>(name))
106  addEnumOptionsAndDocs(oss, params.get<MooseEnum>(name), indent);
107  if (params.have_parameter<MultiMooseEnum>(name))
108  addEnumOptionsAndDocs(oss, params.get<MultiMooseEnum>(name), indent);
109  if (params.have_parameter<ExecFlagEnum>(name))
110  addEnumOptionsAndDocs(oss, params.get<ExecFlagEnum>(name), indent);
111  if (params.have_parameter<std::vector<MooseEnum>>(name))
112  addEnumOptionsAndDocs(oss, params.get<std::vector<MooseEnum>>(name)[0], indent);
113 
114  oss << indent << " description: |\n " << indent << doc << std::endl;
115  }
116 
117  return oss.str();
118 }
std::string name(const ElemQuality q)
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:31
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
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) ...
void addEnumOptionsAndDocs(std::ostringstream &oss, T &param, const std::string &indent)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
bool haveSeenIt(const std::string &prefix, const std::string &item) const
Definition: SyntaxTree.C:63
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
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:196

◆ seenIt()

void SyntaxTree::seenIt ( const std::string &  prefix,
const std::string &  item 
)
inherited

Definition at line 57 of file SyntaxTree.C.

Referenced by InputFileFormatter::printParams(), and printParams().

58 {
59  _params_printed.insert(prefix + item);
60 }
std::set< std::string > _params_printed
Definition: SyntaxTree.h:80

Member Data Documentation

◆ _dump_mode

bool YAMLFormatter::_dump_mode
protected

Definition at line 44 of file YAMLFormatter.h.

◆ _root

std::unique_ptr<TreeNode> SyntaxTree::_root
protectedinherited

Definition at line 76 of file SyntaxTree.h.

Referenced by SyntaxTree::insertNode(), and SyntaxTree::print().

◆ _use_long_names

bool SyntaxTree::_use_long_names
protectedinherited

Definition at line 77 of file SyntaxTree.h.

Referenced by SyntaxTree::isLongNames().


The documentation for this class was generated from the following files: