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 InputFileParameters 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...
 
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
 
TreeNode_root
 
bool _use_long_names
 

Detailed Description

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

Definition at line 21 of file YAMLFormatter.h.

Constructor & Destructor Documentation

◆ YAMLFormatter()

YAMLFormatter::YAMLFormatter ( bool  dump_mode)

Definition at line 21 of file YAMLFormatter.C.

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

Member Function Documentation

◆ 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 157 of file YAMLFormatter.C.

Referenced by printParams().

160 {
161  // Account for Point
162  InputParameters::Parameter<Point> * ptr0 =
163  dynamic_cast<InputParameters::Parameter<Point> *>(p.second);
164 
165  // Account for RealVectorValues
166  InputParameters::Parameter<RealVectorValue> * ptr1 =
167  dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(p.second);
168 
169  // Output the Point components
170  if (ptr0)
171  output << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " "
172  << ptr0->get().operator()(2);
173 
174  // Output the RealVectorValue components
175  else if (ptr1)
176  output << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " "
177  << ptr1->get().operator()(2);
178 
179  // General case, call the print operator
180  else
181  p.second->print(output);
182 }

◆ 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:79

◆ 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 == NULL)
33  _root = new TreeNode("", *this);
34 
35  _root->insertNode(syntax, action, is_action_params, params);
36 }
void insertNode(std::string &syntax, const std::string &action, bool is_action_params=true, InputParameters *params=NULL)
Definition: SyntaxTree.C:92
TreeNode * _root
Definition: SyntaxTree.h:75

◆ isLongNames()

bool SyntaxTree::isLongNames ( ) const
protectedinherited

Definition at line 236 of file SyntaxTree.C.

237 {
238  return _use_long_names;
239 }
bool _use_long_names
Definition: SyntaxTree.h:76

◆ 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 31 of file YAMLFormatter.C.

32 {
33  return "**END YAML DATA**\n";
34 }

◆ 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 24 of file YAMLFormatter.C.

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

◆ 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 127 of file YAMLFormatter.C.

128 {
129  std::string indent(depth * 2, ' ');
130 
131  return indent + " subblocks:\n";
132 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:29

◆ 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 }
TreeNode * _root
Definition: SyntaxTree.h:75
std::set< std::string > _params_printed
Definition: SyntaxTree.h:79
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::string print(short depth, const std::string &search_string, bool &found)
Definition: SyntaxTree.C:136
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 151 of file YAMLFormatter.C.

152 {
153  return std::string();
154 }

◆ 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 135 of file YAMLFormatter.C.

136 {
137  std::ostringstream oss;
138  std::string indent(depth * 2, ' ');
139 
140  std::string docEscaped = doc;
141  MooseUtils::escape(docEscaped);
142 
143  oss << indent << "- name: " << name << "\n";
144  oss << indent << " description: |\n" << indent << " " << docEscaped << "\n";
145  oss << indent << " parameters:\n";
146 
147  return oss.str();
148 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:29
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:94

◆ 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 37 of file YAMLFormatter.C.

43 {
44  std::ostringstream oss;
45  std::string indent(depth * 2, ' ');
46 
47  for (auto & iter : params)
48  {
49  std::string name = iter.first;
50  // First make sure we want to see this parameter, also block active and type
51  if (params.isPrivate(iter.first) || name == "active" ||
52  (search_string != "" && search_string != iter.first) || haveSeenIt(prefix, iter.first))
53  continue;
54 
55  found = true;
56 
57  // Mark it as "seen"
58  seenIt(prefix, iter.first);
59 
60  // Block params may be required and will have a doc string
61  std::string required = params.isParamRequired(iter.first) ? "Yes" : "No";
62 
63  oss << indent << " - name: " << name << "\n";
64  oss << indent << " required: " << required << "\n";
65  oss << indent << " default: !!str ";
66 
67  // Only output default if it has one
68  if (params.isParamValid(iter.first))
69  {
70  // prints the value, which is the default value when dumping the tree
71  // because it hasn't been changed
72 
73  // Output stream, performing special operations for writing objects such as Points and
74  // RealVectorValues
75  std::ostringstream toss;
76  buildOutputString(toss, iter);
77 
78  // remove additional '\n' possibly generated in output (breaks YAML parsing)
79  std::string tmp_str = toss.str();
80  for (auto & ch : tmp_str)
81  if (ch == '\n')
82  ch = ' ';
83  if (tmp_str == ",")
84  oss << "\"" << tmp_str << "\"";
85  else
86  oss << tmp_str;
87  }
88  else if (params.hasDefaultCoupledValue(iter.first))
89  oss << params.defaultCoupledValue(iter.first);
90 
91  std::string doc = params.getDocString(iter.first);
92  MooseUtils::escape(doc);
93  // Print the type
94  oss << "\n"
95  << indent << " cpp_type: " << params.type(iter.first) << "\n"
96  << indent << " group_name: ";
97  std::string group_name = params.getGroupName(iter.first);
98  if (!group_name.empty())
99  oss << "'" << group_name << "'";
100 
101  {
102  InputParameters::Parameter<MooseEnum> * enum_type =
103  dynamic_cast<InputParameters::Parameter<MooseEnum> *>(iter.second);
104  if (enum_type)
105  oss << "\n" << indent << " options: " << enum_type->get().getRawNames();
106  }
107  {
108  InputParameters::Parameter<MultiMooseEnum> * enum_type =
109  dynamic_cast<InputParameters::Parameter<MultiMooseEnum> *>(iter.second);
110  if (enum_type)
111  oss << "\n" << indent << " options: " << enum_type->get().getRawNames();
112  }
113  {
114  InputParameters::Parameter<std::vector<MooseEnum>> * enum_type =
115  dynamic_cast<InputParameters::Parameter<std::vector<MooseEnum>> *>(iter.second);
116  if (enum_type)
117  oss << "\n" << indent << " options: " << (enum_type->get())[0].getRawNames();
118  }
119 
120  oss << "\n" << indent << " description: |\n " << indent << doc << "\n";
121  }
122 
123  return oss.str();
124 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:29
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) ...
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:94

◆ 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:79

Member Data Documentation

◆ _dump_mode

bool YAMLFormatter::_dump_mode
protected

Definition at line 41 of file YAMLFormatter.h.

◆ _root

TreeNode* SyntaxTree::_root
protectedinherited

Definition at line 75 of file SyntaxTree.h.

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

◆ _use_long_names

bool SyntaxTree::_use_long_names
protectedinherited

Definition at line 76 of file SyntaxTree.h.

Referenced by SyntaxTree::isLongNames().


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