www.mooseframework.org
JsonSyntaxTree.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 "JsonSyntaxTree.h"
16 
17 // MOOSE includes
18 #include "MooseEnum.h"
19 #include "MultiMooseEnum.h"
20 #include "Parser.h"
21 #include "pcrecpp.h"
22 
23 // C++ includes
24 #include <algorithm>
25 #include <cctype>
26 
27 JsonSyntaxTree::JsonSyntaxTree(const std::string & search_string) : _search(search_string) {}
28 
29 std::vector<std::string>
30 JsonSyntaxTree::splitPath(const std::string & path)
31 {
32  std::string s;
33  std::istringstream f(path);
34  std::vector<std::string> paths;
35  while (std::getline(f, s, '/'))
36  if (s.size() > 0)
37  paths.push_back(s);
38  return paths;
39 }
40 
41 moosecontrib::Json::Value &
42 JsonSyntaxTree::getJson(const std::string & path)
43 {
44  auto paths = splitPath(path);
45  mooseAssert(paths.size() > 0, "path is empty");
46  moosecontrib::Json::Value * next = &_root["blocks"][paths[0]];
47 
48  for (auto pit = paths.begin() + 1; pit != paths.end(); ++pit)
49  {
50  if (*pit == "*")
51  // It has an action syntax as a parent
52  next = &(*next)["star"];
53  else if (*pit == "<type>")
54  next = &(*next)["types"];
55  else
56  next = &(*next)["subblocks"][*pit];
57  }
58  return *next;
59 }
60 
61 moosecontrib::Json::Value &
62 JsonSyntaxTree::getJson(const std::string & parent, const std::string & path, bool is_type)
63 {
64  if (parent.empty())
65  {
66  auto & j = getJson(path);
67  if (path.back() == '*' && !j.isMember("subblock_types"))
68  j["subblock_types"] = moosecontrib::Json::Value();
69  else if (path.back() != '*' && !j.isMember("types"))
70  j["types"] = moosecontrib::Json::Value();
71  return j["actions"];
72  }
73 
74  auto & parent_json = getJson(parent);
75  auto paths = splitPath(path);
76  std::string key = "subblock_types";
77  if (is_type)
78  key = "types";
79  auto & val = parent_json[key][paths.back()];
80  return val;
81 }
82 
83 size_t
85  bool search_match,
86  moosecontrib::Json::Value & all_params)
87 {
88  size_t count = 0;
89  for (auto & iter : *params)
90  {
91  // Make sure we want to see this parameter
92  bool param_match = !_search.empty() && MooseUtils::wildCardMatch(iter.first, _search);
93  if (params->isPrivate(iter.first) || (!_search.empty() && !search_match && !param_match))
94  continue;
95 
96  ++count;
97  moosecontrib::Json::Value param_json;
98 
99  param_json["required"] = params->isParamRequired(iter.first);
100 
101  // Only output default if it has one
102  if (params->isParamValid(iter.first))
103  param_json["default"] = buildOutputString(iter);
104  else if (params->hasDefaultCoupledValue(iter.first))
105  param_json["default"] = params->defaultCoupledValue(iter.first);
106 
107  bool out_of_range_allowed = false;
108  param_json["options"] = buildOptions(iter, out_of_range_allowed);
109  if (!param_json["options"].asString().empty())
110  param_json["out_of_range_allowed"] = out_of_range_allowed;
111  auto reserved_values = params->reservedValues(iter.first);
112  for (const auto & reserved : reserved_values)
113  param_json["reserved_values"].append(reserved);
114 
115  std::string t = prettyCppType(params->type(iter.first));
116  param_json["cpp_type"] = t;
117  param_json["basic_type"] = basicCppType(t);
118  param_json["group_name"] = params->getGroupName(iter.first);
119  param_json["name"] = iter.first;
120 
121  std::string doc = params->getDocString(iter.first);
122  MooseUtils::escape(doc);
123  param_json["description"] = doc;
124  all_params[iter.first] = param_json;
125  }
126  return count;
127 }
128 
129 void
131 {
132  // If they are doing a search they probably don't want to see this
133  if (_search.empty())
134  {
135  auto params = validParams<Action>();
136  moosecontrib::Json::Value jparams;
137  setParams(&params, true, jparams);
138  _root["global"]["parameters"] = jparams;
139  }
140 }
141 
142 bool
143 JsonSyntaxTree::addParameters(const std::string & parent,
144  const std::string & path,
145  bool is_type,
146  const std::string & action,
147  bool is_action,
148  InputParameters * params,
149  const FileLineInfo & lineinfo,
150  const std::string & classname)
151 {
152  if (action == "EmptyAction")
153  return false;
154 
155  moosecontrib::Json::Value all_params;
156  bool search_match = !_search.empty() && (MooseUtils::wildCardMatch(path, _search) ||
159  auto count = setParams(params, search_match, all_params);
160  if (!_search.empty() && count == 0)
161  // no parameters that matched the search string
162  return false;
163 
164  moosecontrib::Json::Value & json = getJson(parent, path, is_type);
165 
166  if (is_action)
167  {
168  json[action]["parameters"] = all_params;
169  json[action]["description"] = params->getClassDescription();
170  json[action]["action_path"] = path;
171  if (lineinfo.isValid())
172  json[action]["file_info"][lineinfo.file()] = lineinfo.line();
173  }
174  else if (params)
175  {
176  if (params->isParamValid("_moose_base"))
177  json["moose_base"] = params->get<std::string>("_moose_base");
178 
179  json["parameters"] = all_params;
180  json["syntax_path"] = path;
181  json["parent_syntax"] = parent;
182  json["description"] = params->getClassDescription();
183  if (lineinfo.isValid())
184  {
185  json["file_info"][lineinfo.file()] = lineinfo.line();
186  if (!classname.empty())
187  json["class"] = classname;
188  }
189  }
190  return true;
191 }
192 
193 std::string
194 JsonSyntaxTree::buildOptions(const std::iterator_traits<InputParameters::iterator>::value_type & p,
195  bool & out_of_range_allowed)
196 {
197  std::string options;
198  {
199  InputParameters::Parameter<MooseEnum> * enum_type =
200  dynamic_cast<InputParameters::Parameter<MooseEnum> *>(p.second);
201  if (enum_type)
202  {
203  out_of_range_allowed = enum_type->get().isOutOfRangeAllowed();
204  options = enum_type->get().getRawNames();
205  }
206  }
207  {
208  InputParameters::Parameter<MultiMooseEnum> * enum_type =
209  dynamic_cast<InputParameters::Parameter<MultiMooseEnum> *>(p.second);
210  if (enum_type)
211  {
212  out_of_range_allowed = enum_type->get().isOutOfRangeAllowed();
213  options = enum_type->get().getRawNames();
214  }
215  }
216  {
217  InputParameters::Parameter<std::vector<MooseEnum>> * enum_type =
218  dynamic_cast<InputParameters::Parameter<std::vector<MooseEnum>> *>(p.second);
219  if (enum_type)
220  {
221  out_of_range_allowed = (enum_type->get())[0].isOutOfRangeAllowed();
222  options = (enum_type->get())[0].getRawNames();
223  }
224  }
225  return options;
226 }
227 
228 std::string
230  const std::iterator_traits<InputParameters::iterator>::value_type & p)
231 {
232  // Account for Point
233  std::stringstream str;
234  InputParameters::Parameter<Point> * ptr0 =
235  dynamic_cast<InputParameters::Parameter<Point> *>(p.second);
236 
237  // Account for RealVectorValues
238  InputParameters::Parameter<RealVectorValue> * ptr1 =
239  dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(p.second);
240 
241  // Output the Point components
242  if (ptr0)
243  str << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " "
244  << ptr0->get().operator()(2);
245 
246  // Output the RealVectorValue components
247  else if (ptr1)
248  str << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " "
249  << ptr1->get().operator()(2);
250 
251  // General case, call the print operator
252  else
253  p.second->print(str);
254 
255  // remove additional '\n' possibly generated in output (breaks JSON parsing)
256  std::string tmp_str = str.str();
257  for (auto & ch : tmp_str)
258  if (ch == '\n')
259  ch = ' ';
260  return tmp_str;
261 }
262 
263 void
264 JsonSyntaxTree::addSyntaxType(const std::string & path, const std::string type)
265 {
267  {
268  auto & j = getJson(path);
269  j["associated_types"].append(type);
270  }
271  // If they are doing a search they probably don't want to see this
272  if (_search.empty())
273  {
274  _root["global"]["associated_types"][type].append(path);
275  }
276 }
277 
278 void
279 JsonSyntaxTree::addActionTask(const std::string & path,
280  const std::string & action,
281  const std::string & task_name,
282  const FileLineInfo & lineinfo)
283 {
284  moosecontrib::Json::Value & json = getJson("", path, false);
285  if (lineinfo.isValid())
286  json[action]["tasks"][task_name]["file_info"][lineinfo.file()] = lineinfo.line();
287 }
288 
289 std::string
290 JsonSyntaxTree::basicCppType(const std::string & cpp_type)
291 {
292  std::string s = "String";
293  if (cpp_type.find("std::vector") != std::string::npos ||
294  cpp_type.find("libMesh::VectorValue") != std::string::npos ||
295  cpp_type.find("libMesh::TensorValue") != std::string::npos)
296  {
297  // Get the template type and use its basic type for the array type
298  pcrecpp::RE r("^[^<]+<\\s*(.*)\\s*>$");
299  std::string t;
300  r.FullMatch(cpp_type, &t);
301  s = "Array:" + basicCppType(t);
302  }
303  else if (cpp_type.find("MultiMooseEnum") != std::string::npos ||
304  cpp_type.find("VectorPostprocessorName") != std::string::npos)
305  s = "Array:String";
306  else if (cpp_type.find("libMesh::Point") != std::string::npos)
307  s = "Array:Real";
308  else if (cpp_type == "int" || cpp_type == "unsigned int" || cpp_type == "short" ||
309  cpp_type == "unsigned short" || cpp_type == "char" || cpp_type == "unsigned char" ||
310  cpp_type == "long" || cpp_type == "unsigned long")
311  s = "Integer";
312  else if (cpp_type == "double" || cpp_type == "float")
313  s = "Real";
314  else if (cpp_type == "bool")
315  s = "Boolean";
316 
317  return s;
318 }
319 
320 std::string
321 JsonSyntaxTree::prettyCppType(const std::string & cpp_type)
322 {
323  // On mac many of the std:: classes are inline namespaced with __1
324  // On linux std::string can be inline namespaced with __cxx11
325  std::string s = cpp_type;
326  pcrecpp::RE("std::__\\w+::").GlobalReplace("std::", &s);
327  // It would be nice if std::string actually looked normal
328  pcrecpp::RE("\\s*std::basic_string<char, std::char_traits<char>, std::allocator<char> >\\s*")
329  .GlobalReplace("std::string", &s);
330  // It would be nice if std::vector looked normal
331  pcrecpp::RE r("std::vector<([[:print:]]+),\\s?std::allocator<\\s?\\1\\s?>\\s?>");
332  r.GlobalReplace("std::vector<\\1>", &s);
333  // Do it again for nested vectors
334  r.GlobalReplace("std::vector<\\1>", &s);
335  return s;
336 }
void addSyntaxType(const std::string &path, const std::string type)
Add an associated type to a block.
void addActionTask(const std::string &path, const std::string &action, const std::string &task, const FileLineInfo &lineinfo)
Add a task to the tree.
size_t setParams(InputParameters *params, bool search_match, moosecontrib::Json::Value &all_params)
static std::vector< std::string > splitPath(const std::string &path)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
int line() const
Definition: FileLineInfo.C:28
std::string buildOptions(const std::iterator_traits< InputParameters::iterator >::value_type &p, bool &out_of_range_allowed)
std::string prettyCppType(const std::string &cpp_type)
bool isValid() const
Definition: FileLineInfo.C:22
std::string buildOutputString(const std::iterator_traits< InputParameters::iterator >::value_type &p)
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.
JsonSyntaxTree(const std::string &search_string)
moosecontrib::Json::Value _root
bool addParameters(const std::string &parent_path, const std::string &path, bool is_type, const std::string &action, bool is_action, InputParameters *params, const FileLineInfo &lineinfo, const std::string &classname)
Add parameters to the tree.
std::string basicCppType(const std::string &cpp_type)
Holds file and line information.
Definition: FileLineInfo.h:24
MatType type
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:511
std::string _search
std::string getClassDescription() const
Returns the class description.
void addGlobal()
Add the global section to the output.
moosecontrib::Json::Value & getJson(const std::string &parent, const std::string &path, bool is_type)
std::string file() const
Definition: FileLineInfo.C:34
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:77
InputParameters validParams< Action >()
Definition: Action.C:23