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

This class produces produces a dump of the InputFileParameters that appears like the normal input file syntax. More...

#include <InputFileFormatter.h>

Inheritance diagram for InputFileFormatter:
[legend]

Public Member Functions

 InputFileFormatter (bool dump_mode)
 
virtual std::string printBlockOpen (const std::string &name, short depth, const std::string &) 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
 
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 information. More...
 
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 trailing information to the final formatted string. More...
 
virtual std::string preTraverse (short) const
 This method is called once at each node in the syntax tree before traversing child nodes. More...
 

Protected Member Functions

bool isLongNames () const
 

Protected Attributes

bool _dump_mode
 
TreeNode_root
 
bool _use_long_names
 

Detailed Description

This class produces produces a dump of the InputFileParameters that appears like the normal input file syntax.

Definition at line 19 of file InputFileFormatter.h.

Constructor & Destructor Documentation

◆ InputFileFormatter()

InputFileFormatter::InputFileFormatter ( bool  dump_mode)

Definition at line 17 of file InputFileFormatter.C.

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

Member Function Documentation

◆ haveSeenIt()

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

Definition at line 63 of file SyntaxTree.C.

Referenced by printParams(), and YAMLFormatter::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()

virtual std::string SyntaxFormatterInterface::postscript ( ) const
inlinevirtualinherited

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 in YAMLFormatter.

Definition at line 46 of file SyntaxFormatterInterface.h.

Referenced by SyntaxTree::print().

46 { return std::string(); }

◆ preamble()

virtual std::string SyntaxFormatterInterface::preamble ( ) const
inlinevirtualinherited

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 in YAMLFormatter.

Definition at line 38 of file SyntaxFormatterInterface.h.

Referenced by SyntaxTree::print().

38 { return std::string(); }

◆ preTraverse()

virtual std::string SyntaxFormatterInterface::preTraverse ( short  ) const
inlinevirtualinherited

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

Returns
- The formatted pre-node traversal string

Reimplemented in YAMLFormatter.

Definition at line 52 of file SyntaxFormatterInterface.h.

52 { return std::string(); }

◆ 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 InputFileFormatter::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 34 of file InputFileFormatter.C.

35 {
36  std::string indent(depth * 2, ' ');
37  std::string closing_string;
38 
39  if (depth)
40  closing_string = "../";
41 
42  return std::string("") + indent + "[" + closing_string + "]\n";
43 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:29

◆ printBlockOpen()

std::string InputFileFormatter::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 20 of file InputFileFormatter.C.

23 {
24  std::string indent(depth * 2, ' ');
25  std::string opening_string;
26 
27  if (depth)
28  opening_string = "./";
29 
30  return std::string("\n") + indent + "[" + opening_string + name + "]\n";
31 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:29

◆ printParams()

std::string InputFileFormatter::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 46 of file InputFileFormatter.C.

52 {
53  std::stringstream oss;
54 
55  std::string quotes = "";
56  std::string spacing = "";
57  std::string forward = "";
58  std::string backdots = "";
59  int offset = 30;
60  for (int i = 0; i < depth; ++i)
61  {
62  spacing += " ";
63  forward = ".";
64  offset -= 2;
65  }
66 
67  for (const auto & iter : params)
68  {
69  // We only want non-private params and params that we haven't already seen
70  if (params.isPrivate(iter.first) || haveSeenIt(fully_qualified_name, iter.first))
71  continue;
72 
73  std::string value = "INVALID";
74  if (params.isParamValid(iter.first))
75  {
76  // Print the parameter's value to a stringstream.
77  std::ostringstream toss;
78  iter.second->print(toss);
79  value = MooseUtils::trim(toss.str());
80  }
81  else if (params.hasDefaultCoupledValue(iter.first))
82  {
83  std::ostringstream toss;
84  toss << params.defaultCoupledValue(iter.first);
85  value = toss.str();
86  }
87 
88  // See if we match the search string
89  if (MooseUtils::wildCardMatch(iter.first, search_string) ||
90  MooseUtils::wildCardMatch(value, search_string))
91  {
92  // Don't print active if it is the default all, that means it's not in the input file - unless
93  // of course we are in dump mode
94  if (!_dump_mode && iter.first == "active")
95  {
96  libMesh::Parameters::Parameter<std::vector<std::string>> * val =
97  dynamic_cast<libMesh::Parameters::Parameter<std::vector<std::string>> *>(iter.second);
98  const std::vector<std::string> & active = val->get();
99  if (val != NULL && active.size() == 1 && active[0] == "__all__")
100  continue;
101  }
102 
103  // Mark it as "seen"
104  seenIt(fully_qualified_name, iter.first);
105 
106  // Don't print type if it is blank
107  if (iter.first == "type")
108  {
109  libMesh::Parameters::Parameter<std::string> * val =
110  dynamic_cast<libMesh::Parameters::Parameter<std::string> *>(iter.second);
111  const std::string & active = val->get();
112  if (val != NULL && active == "")
113  continue;
114  }
115 
116  found = true;
117  oss << spacing << " " << std::left << std::setw(offset) << iter.first << " = ";
118  // std::setw() takes an int
119  int l_offset = 30;
120 
121  if (!_dump_mode || value != "INVALID")
122  {
123  // If the value has spaces, surround it with quotes, otherwise no quotes
124  if (value.find(' ') != std::string::npos)
125  {
126  quotes = "'";
127  l_offset -= 2;
128  }
129  else
130  quotes = "";
131  oss << quotes << value << quotes;
132  l_offset -= value.size();
133  }
134  else if (_dump_mode && params.isParamRequired(iter.first))
135  {
136  oss << "(required)";
137  l_offset -= 10;
138  }
139 
140  // Documentation string
141  if (_dump_mode)
142  {
143  std::vector<std::string> elements;
144  std::string doc = params.getDocString(iter.first);
145  if (MooseUtils::trim(doc) != "")
146  {
147  MooseUtils::tokenize(doc, elements, 68, " \t");
148 
149  for (auto & element : elements)
150  MooseUtils::escape(element);
151 
152  oss << std::right << std::setw(l_offset) << "# " << elements[0];
153  for (unsigned int i = 1; i < elements.size(); ++i)
154  oss << " ...\n"
155  << " " << std::setw(63) << "# " << elements[i];
156  }
157  const std::string group = params.getGroupName(iter.first);
158  if (!group.empty())
159  {
160  if (MooseUtils::trim(doc) != "")
161  oss << " ...\n"
162  << " " << std::setw(70) << "# Group: " << group;
163  else
164  oss << std::right << std::setw(l_offset) << "# Group: " << group;
165  }
166  }
167  oss << "\n";
168  }
169  }
170 
171  return oss.str();
172 }
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:394
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:112
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:557
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 printParams(), and YAMLFormatter::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 InputFileFormatter::_dump_mode
protected

Definition at line 35 of file InputFileFormatter.h.

Referenced by printParams().

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