www.mooseframework.org
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
JsonSyntaxTree Class Reference

Holds the syntax in a Json::Value tree. More...

#include <JsonSyntaxTree.h>

Public Member Functions

 JsonSyntaxTree (const std::string &search_string)
 
virtual ~JsonSyntaxTree ()
 
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. More...
 
void addActionTask (const std::string &path, const std::string &action, const std::string &task, const FileLineInfo &lineinfo)
 Add a task to the tree. More...
 
const moosecontrib::Json::Value & getRoot () const
 Get the root of the tree. More...
 
void addSyntaxType (const std::string &path, const std::string type)
 Add an associated type to a block. More...
 
void addGlobal ()
 Add the global section to the output. More...
 

Protected Member Functions

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)
 
std::string basicCppType (const std::string &cpp_type)
 
size_t setParams (InputParameters *params, bool search_match, moosecontrib::Json::Value &all_params)
 
std::string buildOutputString (const std::iterator_traits< InputParameters::iterator >::value_type &p)
 
moosecontrib::Json::Value & getJson (const std::string &parent, const std::string &path, bool is_type)
 
moosecontrib::Json::Value & getJson (const std::string &path)
 

Static Protected Member Functions

static std::vector< std::string > splitPath (const std::string &path)
 

Protected Attributes

moosecontrib::Json::Value _root
 
std::string _search
 

Detailed Description

Holds the syntax in a Json::Value tree.

Definition at line 27 of file JsonSyntaxTree.h.

Constructor & Destructor Documentation

JsonSyntaxTree::JsonSyntaxTree ( const std::string &  search_string)

Definition at line 27 of file JsonSyntaxTree.C.

27 : _search(search_string) {}
std::string _search
virtual JsonSyntaxTree::~JsonSyntaxTree ( )
inlinevirtual

Definition at line 31 of file JsonSyntaxTree.h.

31 {}

Member Function Documentation

void JsonSyntaxTree::addActionTask ( const std::string &  path,
const std::string &  action,
const std::string &  task,
const FileLineInfo lineinfo 
)

Add a task to the tree.

Parameters
pathThe path of the action
actionName of the action
taskName of the task
lineinfoThe FileLineInfo where the action/task was registered

Definition at line 279 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree(), and ~JsonSyntaxTree().

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 }
int line() const
Definition: FileLineInfo.C:28
bool isValid() const
Definition: FileLineInfo.C:22
moosecontrib::Json::Value & getJson(const std::string &parent, const std::string &path, bool is_type)
std::string file() const
Definition: FileLineInfo.C:34
void JsonSyntaxTree::addGlobal ( )

Add the global section to the output.

Definition at line 130 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree(), and getRoot().

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 }
size_t setParams(InputParameters *params, bool search_match, moosecontrib::Json::Value &all_params)
moosecontrib::Json::Value _root
std::string _search
InputParameters validParams< Action >()
Definition: Action.C:23
bool JsonSyntaxTree::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.

Parameters
parent_pathThe parent syntax path that the action belongs to
pathThe path of the action
is_typeWhether this belongs to a "<type>" or not
actionName of the action
is_actionWheter it is an action
paramsThe InputParameters to add to the tree
lineinfoThe FileLineInfo where the action/path was registered
Returns
Whether the parameters were added to the tree (ie if it matched the search string).

Definition at line 143 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree(), and ~JsonSyntaxTree().

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 }
size_t setParams(InputParameters *params, bool search_match, moosecontrib::Json::Value &all_params)
int line() const
Definition: FileLineInfo.C:28
bool isValid() const
Definition: FileLineInfo.C:22
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:511
std::string _search
std::string getClassDescription() const
Returns the class description.
moosecontrib::Json::Value & getJson(const std::string &parent, const std::string &path, bool is_type)
std::string file() const
Definition: FileLineInfo.C:34
void JsonSyntaxTree::addSyntaxType ( const std::string &  path,
const std::string  type 
)

Add an associated type to a block.

Parameters
pathPath of the block
typeType name to associate the block with

Definition at line 264 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree(), and getRoot().

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 }
moosecontrib::Json::Value _root
MatType type
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:511
std::string _search
moosecontrib::Json::Value & getJson(const std::string &parent, const std::string &path, bool is_type)
std::string JsonSyntaxTree::basicCppType ( const std::string &  cpp_type)
protected

Definition at line 290 of file JsonSyntaxTree.C.

Referenced by getRoot(), and setParams().

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 }
std::string basicCppType(const std::string &cpp_type)
std::string JsonSyntaxTree::buildOptions ( const std::iterator_traits< InputParameters::iterator >::value_type &  p,
bool &  out_of_range_allowed 
)
protected

Definition at line 194 of file JsonSyntaxTree.C.

Referenced by getRoot(), and setParams().

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 }
std::string JsonSyntaxTree::buildOutputString ( const std::iterator_traits< InputParameters::iterator >::value_type &  p)
protected

Definition at line 229 of file JsonSyntaxTree.C.

Referenced by getRoot(), and setParams().

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 }
moosecontrib::Json::Value & JsonSyntaxTree::getJson ( const std::string &  parent,
const std::string &  path,
bool  is_type 
)
protected

Definition at line 62 of file JsonSyntaxTree.C.

Referenced by addActionTask(), addParameters(), addSyntaxType(), and getRoot().

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 }
static std::vector< std::string > splitPath(const std::string &path)
moosecontrib::Json::Value & getJson(const std::string &parent, const std::string &path, bool is_type)
moosecontrib::Json::Value & JsonSyntaxTree::getJson ( const std::string &  path)
protected

Definition at line 42 of file JsonSyntaxTree.C.

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 }
static std::vector< std::string > splitPath(const std::string &path)
moosecontrib::Json::Value _root
const moosecontrib::Json::Value& JsonSyntaxTree::getRoot ( ) const
inline

Get the root of the tree.

Returns
The top level Json::Value holding the tree.

Definition at line 68 of file JsonSyntaxTree.h.

Referenced by MooseApp::setupOptions().

68 { return _root; }
moosecontrib::Json::Value _root
std::string JsonSyntaxTree::prettyCppType ( const std::string &  cpp_type)
protected

Definition at line 321 of file JsonSyntaxTree.C.

Referenced by getRoot(), and setParams().

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 }
size_t JsonSyntaxTree::setParams ( InputParameters params,
bool  search_match,
moosecontrib::Json::Value &  all_params 
)
protected

Definition at line 84 of file JsonSyntaxTree.C.

Referenced by addGlobal(), addParameters(), and getRoot().

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 }
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)
std::string buildOutputString(const std::iterator_traits< InputParameters::iterator >::value_type &p)
std::string basicCppType(const std::string &cpp_type)
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:511
std::string _search
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
std::vector< std::string > JsonSyntaxTree::splitPath ( const std::string &  path)
staticprotected

Definition at line 30 of file JsonSyntaxTree.C.

Referenced by getJson(), and getRoot().

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 }

Member Data Documentation

moosecontrib::Json::Value JsonSyntaxTree::_root
protected

Definition at line 97 of file JsonSyntaxTree.h.

Referenced by addGlobal(), addSyntaxType(), getJson(), and getRoot().

std::string JsonSyntaxTree::_search
protected

Definition at line 98 of file JsonSyntaxTree.h.

Referenced by addGlobal(), addParameters(), addSyntaxType(), and setParams().


The documentation for this class was generated from the following files: