www.mooseframework.org
JsonInputFileFormatter.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "JsonInputFileFormatter.h"
16 #include "MooseUtils.h"
17 
18 #include <vector>
19 
21 
22 std::string
23 JsonInputFileFormatter::toString(const moosecontrib::Json::Value & root)
24 {
25  _stream.clear();
26  _stream.str("");
27  for (auto && name : root["blocks"].getMemberNames())
28  addBlock(name, root["blocks"][name], true);
29  return _stream.str();
30 }
31 
32 void
33 JsonInputFileFormatter::addLine(const std::string & line,
34  size_t max_line_len,
35  const std::string & comment)
36 {
37  if (line.empty() && comment.empty())
38  {
39  _stream << "\n";
40  return;
41  }
42 
43  std::string indent(_level * _spaces, ' ');
44  auto doc = MooseUtils::trim(comment);
45  if (doc.empty()) // Not comment so just print out the line normally
46  {
47  _stream << indent << line << "\n";
48  return;
49  }
50 
51  // We have a comment so we need to break it up over multiple lines if necessary
52  // and make sure that they all start at the same spot.
53  _stream << indent << line;
54  std::vector<std::string> elements;
55 
56  // if the line is empty we can just start the comment right away
57  int extra = 1;
58  if (line.empty())
59  extra = 0;
60 
61  // be careful of really long lines.
62  int len = 100 - max_line_len - indent.size();
63  if (len < 0)
64  len = 20;
65 
66  MooseUtils::tokenize(doc, elements, len, " \t");
67  std::string first(max_line_len - line.size() + extra, ' ');
68  _stream << first << "# " << elements[0] << "\n";
69  std::string cindent(max_line_len + indent.size() + extra, ' ');
70  for (size_t i = 1; i < elements.size(); ++i)
71  _stream << cindent << "# " << elements[i] << "\n";
72 }
73 
74 void
75 JsonInputFileFormatter::addBlock(const std::string & name,
76  const moosecontrib::Json::Value & block,
77  bool toplevel)
78 {
79  addLine("");
80  if (toplevel)
81  addLine("[" + name + "]");
82  else
83  addLine("[./" + name + "]");
84 
85  _level++;
86  if (block.isMember("description") && !block["description"].asString().empty())
87  addLine("", 0, block["description"].asString());
88 
89  if (block.isMember("parameters"))
90  addParameters(block["parameters"]);
91 
92  if (block.isMember("actions"))
93  {
94  // there could be duplicate parameters across actions, last one wins
95  moosecontrib::Json::Value all_params;
96  auto & actions = block["actions"];
97  for (auto && name : actions.getMemberNames())
98  {
99  auto & params = actions[name]["parameters"];
100  for (auto && param_name : params.getMemberNames())
101  all_params[param_name] = params[param_name];
102  }
103  addParameters(all_params);
104  }
105 
106  if (block.isMember("star"))
107  addBlock("*", block["star"]);
108 
109  addTypes("subblock_types", block);
110  addTypes("types", block);
111 
112  if (block.isMember("subblocks"))
113  {
114  auto & subblocks = block["subblocks"];
115  if (!subblocks.isNull())
116  for (auto && name : subblocks.getMemberNames())
117  addBlock(name, subblocks[name]);
118  }
119 
120  _level--;
121  if (toplevel)
122  addLine("[]");
123  else
124  addLine("[../]");
125 }
126 
127 void
128 JsonInputFileFormatter::addTypes(const std::string & key, const moosecontrib::Json::Value & block)
129 {
130  if (!block.isMember(key))
131  return;
132  auto & types = block[key];
133  if (types.isNull())
134  return;
135 
136  addLine("");
137  addLine("[./<types>]");
138  _level++;
139  for (auto && name : types.getMemberNames())
140  addBlock("<" + name + ">", types[name]);
141  _level--;
142  addLine("[../]");
143 }
144 
145 void
146 JsonInputFileFormatter::addParameters(const moosecontrib::Json::Value & params)
147 {
148  size_t max_name = 0;
149  for (auto && name : params.getMemberNames())
150  if (name.size() > max_name)
151  max_name = name.size();
152 
153  size_t max_len = 0;
154  std::map<std::string, std::string> lines;
155  for (auto && name : params.getMemberNames())
156  {
157  auto & param = params[name];
158  auto def = MooseUtils::trim(param["default"].asString());
159  if (def.find(' ') != std::string::npos)
160  def = "'" + def + "'";
161  std::string indent(max_name - name.size(), ' ');
162  std::string required;
163  if (param["required"].asBool())
164  required = "(required)";
165  std::string l = name + indent + " = " + def + required;
166  if (l.size() > max_len)
167  max_len = l.size();
168  lines[name] = l;
169  }
170  for (auto && name : params.getMemberNames())
171  {
172  auto & param = params[name];
173  auto & l = lines[name];
174  auto desc = param["description"].asString();
175  addLine(l, max_len, desc);
176 
177  auto group = param["group_name"].asString();
178  if (!group.empty())
179  addLine("", max_len + 1, "Group: " + group); // a +1 to account for an empty line
180  }
181 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:34
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:350
std::string toString(const moosecontrib::Json::Value &root)
Returns a string representation of the tree in input file format.
void addLine(const std::string &line, size_t max_line_len=0, const std::string &comment="")
Adds a line to the output.
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
Definition: MooseUtils.C:95
void addBlock(const std::string &name, const moosecontrib::Json::Value &block, bool top=false)
Adds a new block to the output.
void addParameters(const moosecontrib::Json::Value &params)
Add a comment to the block.
void addTypes(const std::string &key, const moosecontrib::Json::Value &block)
Add a dictionary of type blocks to the output.