https://mooseframework.inl.gov
Loading...
Searching...
No Matches
InputFileFormatter.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
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
18InputFileFormatter::InputFileFormatter(bool dump_mode) : SyntaxTree(), _dump_mode(dump_mode) {}
19
20std::string
21InputFileFormatter::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
34std::string
35InputFileFormatter::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
46std::string
47InputFileFormatter::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}
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.
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.
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.
InputFileFormatter(bool dump_mode)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
bool isPrivate(const std::string &name) const
Returns a Boolean indicating whether the specified parameter is private or not.
std::string getDocString(const std::string &name) const
Returns the documentation string for the specified parameter name.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
bool isParamRequired(const std::string &name) const
Returns a boolean indicating whether the specified parameter is required or not.
bool hasDefaultCoupledValue(const std::string &coupling_name) const
Return whether or not the requested parameter has a default coupled value.
std::string getGroupName(const std::string &param_name) const
This method retrieves the group name for the passed parameter name if one exists.
Real defaultCoupledValue(const std::string &coupling_name, unsigned int i=0) const
Get the default value for an optionally coupled variable.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another,...
void seenIt(const std::string &prefix, const std::string &item)
Definition SyntaxTree.C:57
bool haveSeenIt(const std::string &prefix, const std::string &item) const
Definition SyntaxTree.C:63
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 ...
void escape(std::string &str)
Definition MooseUtils.C:218
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
bool wildCardMatch(std::string name, std::string search_string)
Definition MooseUtils.C:899