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 InputParameters 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
 
std::unique_ptr< TreeNode_root
 
bool _use_long_names
 

Detailed Description

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

Definition at line 18 of file InputFileFormatter.h.

Constructor & Destructor Documentation

◆ InputFileFormatter()

InputFileFormatter::InputFileFormatter ( bool  dump_mode)

Definition at line 18 of file InputFileFormatter.C.

18 : 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: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()

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 41 of file SyntaxFormatterInterface.h.

Referenced by SyntaxTree::print().

41 { 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 33 of file SyntaxFormatterInterface.h.

Referenced by SyntaxTree::print().

33 { 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 47 of file SyntaxFormatterInterface.h.

47 { 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 }
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 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 35 of file InputFileFormatter.C.

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

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

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

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

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

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

Member Data Documentation

◆ _dump_mode

bool InputFileFormatter::_dump_mode
protected

Definition at line 34 of file InputFileFormatter.h.

Referenced by printParams().

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