www.mooseframework.org
InputFileFormatter.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "InputFileFormatter.h"
11 #include "MooseUtils.h"
12 #include "InputParameters.h"
13 
14 #include <sstream>
15 #include <vector>
16 #include <iomanip>
17 
18 InputFileFormatter::InputFileFormatter(bool dump_mode) : SyntaxTree(), _dump_mode(dump_mode) {}
19 
20 std::string
21 InputFileFormatter::printBlockOpen(const std::string & name,
22  short depth,
23  const std::string & /*doc*/)
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 }
33 
34 std::string
35 InputFileFormatter::printBlockClose(const std::string & /*name*/, short depth) const
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 }
45 
46 std::string
47 InputFileFormatter::printParams(const std::string & /*prefix*/,
48  const std::string & fully_qualified_name,
49  InputParameters & params,
50  short depth,
51  const std::string & search_string,
52  bool & found)
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 }
std::string name(const ElemQuality q)
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:31
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
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.
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.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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
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.
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