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 26 of file YAMLFormatter.h.

Constructor & Destructor Documentation

YAMLFormatter::YAMLFormatter ( bool  dump_mode)

Definition at line 26 of file YAMLFormatter.C.

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

Member Function Documentation

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

Referenced by printParams().

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 }
bool SyntaxTree::haveSeenIt ( const std::string &  prefix,
const std::string &  item 
) const
inherited

Definition at line 68 of file SyntaxTree.C.

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

69 {
70  return _params_printed.find(prefix + item) != _params_printed.end();
71 }
std::set< std::string > _params_printed
Definition: SyntaxTree.h:84
void SyntaxTree::insertNode ( std::string  syntax,
const std::string &  action,
bool  is_action_params = true,
InputParameters params = NULL 
)
inherited

Definition at line 32 of file SyntaxTree.C.

Referenced by ActionWarehouse::printInputFile().

36 {
37  if (_root == NULL)
38  _root = new TreeNode("", *this);
39 
40  _root->insertNode(syntax, action, is_action_params, params);
41 }
void insertNode(std::string &syntax, const std::string &action, bool is_action_params=true, InputParameters *params=NULL)
Definition: SyntaxTree.C:97
TreeNode * _root
Definition: SyntaxTree.h:80
bool SyntaxTree::isLongNames ( ) const
protectedinherited

Definition at line 241 of file SyntaxTree.C.

Referenced by SyntaxTree::TreeNode::print().

242 {
243  return _use_long_names;
244 }
bool _use_long_names
Definition: SyntaxTree.h:81
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 36 of file YAMLFormatter.C.

37 {
38  return "**END YAML DATA**\n";
39 }
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 29 of file YAMLFormatter.C.

30 {
31  // important: start and end yaml data delimiters used by python
32  return "**START YAML DATA**\n";
33 }
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 132 of file YAMLFormatter.C.

133 {
134  std::string indent(depth * 2, ' ');
135 
136  return indent + " subblocks:\n";
137 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:34
std::string SyntaxTree::print ( const std::string &  search_string)
inherited

Definition at line 44 of file SyntaxTree.C.

Referenced by ActionWarehouse::printInputFile().

45 {
46  bool found = false;
47  std::string output;
48 
49  // Clear the list of "seen" parameters before printing the tree
50  _params_printed.clear();
51 
52  if (_root)
53  output = _root->print(-1, search_string, found);
54 
55  if (found)
56  return preamble() + output + postscript();
57  else
58  return "";
59 }
TreeNode * _root
Definition: SyntaxTree.h:80
std::set< std::string > _params_printed
Definition: SyntaxTree.h:84
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 ...
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...
std::string print(short depth, const std::string &search_string, bool &found)
Definition: SyntaxTree.C:141
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 156 of file YAMLFormatter.C.

157 {
158  return std::string();
159 }
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 140 of file YAMLFormatter.C.

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 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:34
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
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 42 of file YAMLFormatter.C.

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 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.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) ...
bool haveSeenIt(const std::string &prefix, const std::string &item) const
Definition: SyntaxTree.C:68
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
void SyntaxTree::seenIt ( const std::string &  prefix,
const std::string &  item 
)
inherited

Definition at line 62 of file SyntaxTree.C.

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

63 {
64  _params_printed.insert(prefix + item);
65 }
std::set< std::string > _params_printed
Definition: SyntaxTree.h:84

Member Data Documentation

bool YAMLFormatter::_dump_mode
protected

Definition at line 46 of file YAMLFormatter.h.

TreeNode* SyntaxTree::_root
protectedinherited

Definition at line 80 of file SyntaxTree.h.

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

bool SyntaxTree::_use_long_names
protectedinherited

Definition at line 81 of file SyntaxTree.h.

Referenced by SyntaxTree::isLongNames().


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