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)
 
std::pair< std::string, std::string > getObjectLabel (const std::string &obj) const
 
std::pair< std::string, std::string > getActionLabel (const std::string &action) const
 

Static Protected Member Functions

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

Protected Attributes

moosecontrib::Json::Value _root
 
std::string _search
 
std::map< std::string, std::pair< std::string, std::string > > _action_label_map
 
std::map< std::string, std::pair< std::string, std::string > > _object_label_map
 

Detailed Description

Holds the syntax in a Json::Value tree.

Definition at line 23 of file JsonSyntaxTree.h.

Constructor & Destructor Documentation

◆ JsonSyntaxTree()

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

Definition at line 26 of file JsonSyntaxTree.C.

26  : _search(search_string)
27 {
28  // Registry holds a map with labels (ie MooseApp) as keys and a vector of RegistryEntry
29  // as values. We need the reverse map: given an action or object name then get the label.
30  auto & actmap = Registry::allActions();
31  for (auto & entry : actmap)
32  for (auto & act : entry.second)
33  _action_label_map[act._classname] = std::make_pair(entry.first, act._file);
34 
35  auto & objmap = Registry::allObjects();
36  for (auto & entry : objmap)
37  for (auto & obj : entry.second)
38  {
39  std::string name = obj._name;
40  if (name.empty())
41  name = obj._alias;
42  if (name.empty())
43  name = obj._classname;
44  _object_label_map[name] = std::make_pair(entry.first, obj._file);
45  }
46 }
static const std::map< std::string, std::vector< RegistryEntry > > & allActions()
Returns a per-label keyed map of all Actions in the registry.
Definition: Registry.C:32
if(nl->nonlinearSolver() ->matvec &&nl->nonlinearSolver() ->residual_and_jacobian_object)
std::map< std::string, std::pair< std::string, std::string > > _action_label_map
std::string _search
std::map< std::string, std::pair< std::string, std::string > > _object_label_map
static const std::map< std::string, std::vector< RegistryEntry > > & allObjects()
Returns a per-label keyed map of all MooseObjects in the registry.
Definition: Registry.C:27

◆ ~JsonSyntaxTree()

virtual JsonSyntaxTree::~JsonSyntaxTree ( )
inlinevirtual

Definition at line 27 of file JsonSyntaxTree.h.

27 {}

Member Function Documentation

◆ addActionTask()

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 322 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree().

326 {
327  moosecontrib::Json::Value & json = getJson("", path, false);
328  if (lineinfo.isValid())
329  json[action]["tasks"][task_name]["file_info"][lineinfo.file()] = lineinfo.line();
330 }
int line() const
Definition: FileLineInfo.C:23
std::string file() const
Definition: FileLineInfo.C:29
bool isValid() const
Definition: FileLineInfo.C:17
moosecontrib::Json::Value & getJson(const std::string &parent, const std::string &path, bool is_type)

◆ addGlobal()

void JsonSyntaxTree::addGlobal ( )

Add the global section to the output.

Definition at line 150 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree().

151 {
152  // If they are doing a search they probably don't want to see this
153  if (_search.empty())
154  {
155  auto params = validParams<Action>();
156  moosecontrib::Json::Value jparams;
157  setParams(&params, true, jparams);
158  _root["global"]["parameters"] = jparams;
159 
160  // Just create a list of registered app names
161  moosecontrib::Json::Value apps;
162  auto factory = AppFactory::instance();
163  for (auto app = factory.registeredObjectsBegin(); app != factory.registeredObjectsEnd(); ++app)
164  apps.append(app->first);
165 
166  _root["global"]["registered_apps"] = apps;
167  }
168 }
size_t setParams(InputParameters *params, bool search_match, moosecontrib::Json::Value &all_params)
moosecontrib::Json::Value _root
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:17
std::string _search
InputParameters validParams< Action >()
Definition: Action.C:19

◆ addParameters()

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 171 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree().

179 {
180  if (action == "EmptyAction")
181  return false;
182 
183  moosecontrib::Json::Value all_params;
184  bool search_match = !_search.empty() && (MooseUtils::wildCardMatch(path, _search) ||
187  auto count = setParams(params, search_match, all_params);
188  if (!_search.empty() && count == 0)
189  // no parameters that matched the search string
190  return false;
191 
192  moosecontrib::Json::Value & json = getJson(parent, path, is_type);
193 
194  if (is_action)
195  {
196  json[action]["parameters"] = all_params;
197  json[action]["description"] = params->getClassDescription();
198  json[action]["action_path"] = path;
199  auto label_pair = getActionLabel(action);
200  json[action]["label"] = label_pair.first;
201  json[action]["register_file"] = label_pair.second;
202  if (lineinfo.isValid())
203  json[action]["file_info"][lineinfo.file()] = lineinfo.line();
204  }
205  else if (params)
206  {
207  if (params->isParamValid("_moose_base"))
208  json["moose_base"] = params->get<std::string>("_moose_base");
209 
210  json["parameters"] = all_params;
211  json["syntax_path"] = path;
212  json["parent_syntax"] = parent;
213  json["description"] = params->getClassDescription();
214  auto label_pair = getObjectLabel(path);
215  json["label"] = label_pair.first;
216  json["register_file"] = label_pair.second;
217  if (lineinfo.isValid())
218  {
219  json["file_info"][lineinfo.file()] = lineinfo.line();
220  if (!classname.empty())
221  json["class"] = classname;
222  }
223  }
224  return true;
225 }
size_t setParams(InputParameters *params, bool search_match, moosecontrib::Json::Value &all_params)
int line() const
Definition: FileLineInfo.C:23
std::pair< std::string, std::string > getActionLabel(const std::string &action) const
std::pair< std::string, std::string > getObjectLabel(const std::string &obj) const
std::string file() const
Definition: FileLineInfo.C:29
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:569
bool isValid() const
Definition: FileLineInfo.C:17
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)
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.

◆ addSyntaxType()

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 307 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree().

308 {
310  {
311  auto & j = getJson(path);
312  j["associated_types"].append(type);
313  }
314  // If they are doing a search they probably don't want to see this
315  if (_search.empty())
316  {
317  _root["global"]["associated_types"][type].append(path);
318  }
319 }
moosecontrib::Json::Value _root
MatType type
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:569
std::string _search
moosecontrib::Json::Value & getJson(const std::string &parent, const std::string &path, bool is_type)

◆ basicCppType()

std::string JsonSyntaxTree::basicCppType ( const std::string &  cpp_type)
protected

Definition at line 333 of file JsonSyntaxTree.C.

Referenced by setParams().

334 {
335  std::string s = "String";
336  if (cpp_type.find("std::vector") != std::string::npos ||
337  cpp_type.find("libMesh::VectorValue") != std::string::npos ||
338  cpp_type.find("libMesh::TensorValue") != std::string::npos)
339  {
340  // Get the template type and use its basic type for the array type
341  pcrecpp::RE r("^[^<]+<\\s*(.*)\\s*>$");
342  std::string t;
343  r.FullMatch(cpp_type, &t);
344  s = "Array:" + basicCppType(t);
345  }
346  else if (cpp_type.find("MultiMooseEnum") != std::string::npos ||
347  cpp_type.find("ExecFlagEnum") != std::string::npos ||
348  cpp_type.find("VectorPostprocessorName") != std::string::npos)
349  s = "Array:String";
350  else if (cpp_type.find("libMesh::Point") != std::string::npos)
351  s = "Array:Real";
352  else if (cpp_type == "int" || cpp_type == "unsigned int" || cpp_type == "short" ||
353  cpp_type == "unsigned short" || cpp_type == "char" || cpp_type == "unsigned char" ||
354  cpp_type == "long" || cpp_type == "unsigned long")
355  s = "Integer";
356  else if (cpp_type == "double" || cpp_type == "float")
357  s = "Real";
358  else if (cpp_type == "bool")
359  s = "Boolean";
360 
361  return s;
362 }
std::string basicCppType(const std::string &cpp_type)

◆ buildOptions()

std::string JsonSyntaxTree::buildOptions ( const std::iterator_traits< InputParameters::iterator >::value_type &  p,
bool &  out_of_range_allowed 
)
protected

Definition at line 228 of file JsonSyntaxTree.C.

Referenced by setParams().

230 {
231  std::string options;
232  {
233  InputParameters::Parameter<MooseEnum> * enum_type =
234  dynamic_cast<InputParameters::Parameter<MooseEnum> *>(p.second);
235  if (enum_type)
236  {
237  out_of_range_allowed = enum_type->get().isOutOfRangeAllowed();
238  options = enum_type->get().getRawNames();
239  }
240  }
241  {
242  InputParameters::Parameter<MultiMooseEnum> * enum_type =
243  dynamic_cast<InputParameters::Parameter<MultiMooseEnum> *>(p.second);
244  if (enum_type)
245  {
246  out_of_range_allowed = enum_type->get().isOutOfRangeAllowed();
247  options = enum_type->get().getRawNames();
248  }
249  }
250  {
251  InputParameters::Parameter<ExecFlagEnum> * enum_type =
252  dynamic_cast<InputParameters::Parameter<ExecFlagEnum> *>(p.second);
253  if (enum_type)
254  {
255  out_of_range_allowed = enum_type->get().isOutOfRangeAllowed();
256  options = enum_type->get().getRawNames();
257  }
258  }
259  {
260  InputParameters::Parameter<std::vector<MooseEnum>> * enum_type =
261  dynamic_cast<InputParameters::Parameter<std::vector<MooseEnum>> *>(p.second);
262  if (enum_type)
263  {
264  out_of_range_allowed = (enum_type->get())[0].isOutOfRangeAllowed();
265  options = (enum_type->get())[0].getRawNames();
266  }
267  }
268  return options;
269 }

◆ buildOutputString()

std::string JsonSyntaxTree::buildOutputString ( const std::iterator_traits< InputParameters::iterator >::value_type &  p)
protected

Definition at line 272 of file JsonSyntaxTree.C.

Referenced by setParams().

274 {
275  // Account for Point
276  std::stringstream str;
277  InputParameters::Parameter<Point> * ptr0 =
278  dynamic_cast<InputParameters::Parameter<Point> *>(p.second);
279 
280  // Account for RealVectorValues
281  InputParameters::Parameter<RealVectorValue> * ptr1 =
282  dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(p.second);
283 
284  // Output the Point components
285  if (ptr0)
286  str << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " "
287  << ptr0->get().operator()(2);
288 
289  // Output the RealVectorValue components
290  else if (ptr1)
291  str << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " "
292  << ptr1->get().operator()(2);
293 
294  // General case, call the print operator
295  else
296  p.second->print(str);
297 
298  // remove additional '\n' possibly generated in output (breaks JSON parsing)
299  std::string tmp_str = str.str();
300  for (auto & ch : tmp_str)
301  if (ch == '\n')
302  ch = ' ';
303  return tmp_str;
304 }

◆ getActionLabel()

std::pair< std::string, std::string > JsonSyntaxTree::getActionLabel ( const std::string &  action) const
protected

Definition at line 394 of file JsonSyntaxTree.C.

Referenced by addParameters().

395 {
396  auto it = _action_label_map.find(action);
397  if (it != _action_label_map.end())
398  return it->second;
399  else
400  return std::make_pair("", "");
401 }
std::map< std::string, std::pair< std::string, std::string > > _action_label_map

◆ getJson() [1/2]

moosecontrib::Json::Value & JsonSyntaxTree::getJson ( const std::string &  parent,
const std::string &  path,
bool  is_type 
)
protected

Definition at line 81 of file JsonSyntaxTree.C.

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

82 {
83  if (parent.empty())
84  {
85  auto & j = getJson(path);
86  if (path.back() == '*' && !j.isMember("subblock_types"))
87  j["subblock_types"] = moosecontrib::Json::Value();
88  else if (path.back() != '*' && !j.isMember("types"))
89  j["types"] = moosecontrib::Json::Value();
90  return j["actions"];
91  }
92 
93  auto & parent_json = getJson(parent);
94  auto paths = splitPath(path);
95  std::string key = "subblock_types";
96  if (is_type)
97  key = "types";
98  auto & val = parent_json[key][paths.back()];
99  return val;
100 }
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)

◆ getJson() [2/2]

moosecontrib::Json::Value & JsonSyntaxTree::getJson ( const std::string &  path)
protected

Definition at line 61 of file JsonSyntaxTree.C.

62 {
63  auto paths = splitPath(path);
64  mooseAssert(paths.size() > 0, "path is empty");
65  moosecontrib::Json::Value * next = &_root["blocks"][paths[0]];
66 
67  for (auto pit = paths.begin() + 1; pit != paths.end(); ++pit)
68  {
69  if (*pit == "*")
70  // It has an action syntax as a parent
71  next = &(*next)["star"];
72  else if (*pit == "<type>")
73  next = &(*next)["types"];
74  else
75  next = &(*next)["subblocks"][*pit];
76  }
77  return *next;
78 }
static std::vector< std::string > splitPath(const std::string &path)
moosecontrib::Json::Value _root

◆ getObjectLabel()

std::pair< std::string, std::string > JsonSyntaxTree::getObjectLabel ( const std::string &  obj) const
protected

Definition at line 383 of file JsonSyntaxTree.C.

Referenced by addParameters().

384 {
385  auto paths = splitPath(obj);
386  auto it = _object_label_map.find(paths.back());
387  if (it != _object_label_map.end())
388  return it->second;
389  else
390  return std::make_pair("", "");
391 }
static std::vector< std::string > splitPath(const std::string &path)
std::map< std::string, std::pair< std::string, std::string > > _object_label_map

◆ getRoot()

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 64 of file JsonSyntaxTree.h.

Referenced by MooseApp::setupOptions().

64 { return _root; }
moosecontrib::Json::Value _root

◆ prettyCppType()

std::string JsonSyntaxTree::prettyCppType ( const std::string &  cpp_type)
protected

Definition at line 365 of file JsonSyntaxTree.C.

Referenced by setParams().

366 {
367  // On mac many of the std:: classes are inline namespaced with __1
368  // On linux std::string can be inline namespaced with __cxx11
369  std::string s = cpp_type;
370  pcrecpp::RE("std::__\\w+::").GlobalReplace("std::", &s);
371  // It would be nice if std::string actually looked normal
372  pcrecpp::RE("\\s*std::basic_string<char, std::char_traits<char>, std::allocator<char> >\\s*")
373  .GlobalReplace("std::string", &s);
374  // It would be nice if std::vector looked normal
375  pcrecpp::RE r("std::vector<([[:print:]]+),\\s?std::allocator<\\s?\\1\\s?>\\s?>");
376  r.GlobalReplace("std::vector<\\1>", &s);
377  // Do it again for nested vectors
378  r.GlobalReplace("std::vector<\\1>", &s);
379  return s;
380 }

◆ setParams()

size_t JsonSyntaxTree::setParams ( InputParameters params,
bool  search_match,
moosecontrib::Json::Value &  all_params 
)
protected

Definition at line 103 of file JsonSyntaxTree.C.

Referenced by addGlobal(), and addParameters().

106 {
107  size_t count = 0;
108  for (auto & iter : *params)
109  {
110  // Make sure we want to see this parameter
111  bool param_match = !_search.empty() && MooseUtils::wildCardMatch(iter.first, _search);
112  if (params->isPrivate(iter.first) || (!_search.empty() && !search_match && !param_match))
113  continue;
114 
115  ++count;
116  moosecontrib::Json::Value param_json;
117 
118  param_json["required"] = params->isParamRequired(iter.first);
119 
120  // Only output default if it has one
121  if (params->isParamValid(iter.first))
122  param_json["default"] = buildOutputString(iter);
123  else if (params->hasDefaultCoupledValue(iter.first))
124  param_json["default"] = params->defaultCoupledValue(iter.first);
125 
126  bool out_of_range_allowed = false;
127  param_json["options"] = buildOptions(iter, out_of_range_allowed);
128  if (!param_json["options"].asString().empty())
129  param_json["out_of_range_allowed"] = out_of_range_allowed;
130  auto reserved_values = params->reservedValues(iter.first);
131  for (const auto & reserved : reserved_values)
132  param_json["reserved_values"].append(reserved);
133 
134  std::string t = prettyCppType(params->type(iter.first));
135  param_json["cpp_type"] = t;
136  param_json["basic_type"] = basicCppType(t);
137  param_json["group_name"] = params->getGroupName(iter.first);
138  param_json["name"] = iter.first;
139 
140  std::string doc = params->getDocString(iter.first);
141  MooseUtils::escape(doc);
142  param_json["description"] = doc;
143  param_json["deprecated"] = params->isParamDeprecated(iter.first);
144  all_params[iter.first] = param_json;
145  }
146  return count;
147 }
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:569
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:94

◆ splitPath()

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

Definition at line 49 of file JsonSyntaxTree.C.

Referenced by getJson(), and getObjectLabel().

50 {
51  std::string s;
52  std::istringstream f(path);
53  std::vector<std::string> paths;
54  while (std::getline(f, s, '/'))
55  if (s.size() > 0)
56  paths.push_back(s);
57  return paths;
58 }

Member Data Documentation

◆ _action_label_map

std::map<std::string, std::pair<std::string, std::string> > JsonSyntaxTree::_action_label_map
protected

Maps storing action/object name to the label and file location

Definition at line 101 of file JsonSyntaxTree.h.

Referenced by getActionLabel(), and JsonSyntaxTree().

◆ _object_label_map

std::map<std::string, std::pair<std::string, std::string> > JsonSyntaxTree::_object_label_map
protected

Definition at line 102 of file JsonSyntaxTree.h.

Referenced by getObjectLabel(), and JsonSyntaxTree().

◆ _root

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

Definition at line 96 of file JsonSyntaxTree.h.

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

◆ _search

std::string JsonSyntaxTree::_search
protected

Definition at line 97 of file JsonSyntaxTree.h.

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


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