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

28  : _search(search_string)
29 {
30  // Registry holds a map with labels (ie MooseApp) as keys and a vector of RegistryEntry
31  // as values. We need the reverse map: given an action or object name then get the label.
32  auto & actmap = Registry::allActions();
33  for (auto & entry : actmap)
34  for (auto & act : entry.second)
35  _action_label_map[act._classname] = std::make_pair(entry.first, act._file);
36 
37  auto & objmap = Registry::allObjects();
38  for (auto & entry : objmap)
39  for (auto & obj : entry.second)
40  {
41  std::string name = obj._name;
42  if (name.empty())
43  name = obj._alias;
44  if (name.empty())
45  name = obj._classname;
46  _object_label_map[name] = std::make_pair(entry.first, obj._file);
47  }
48 }
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 324 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree().

328 {
329  moosecontrib::Json::Value & json = getJson("", path, false);
330  if (lineinfo.isValid())
331  json[action]["tasks"][task_name]["file_info"][lineinfo.file()] = lineinfo.line();
332 }
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 152 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree().

153 {
154  // If they are doing a search they probably don't want to see this
155  if (_search.empty())
156  {
157  auto params = validParams<Action>();
158  moosecontrib::Json::Value jparams;
159  setParams(&params, true, jparams);
160  _root["global"]["parameters"] = jparams;
161 
162  // Just create a list of registered app names
163  moosecontrib::Json::Value apps;
164  auto factory = AppFactory::instance();
165  for (auto app = factory.registeredObjectsBegin(); app != factory.registeredObjectsEnd(); ++app)
166  apps.append(app->first);
167 
168  _root["global"]["registered_apps"] = apps;
169  }
170 }
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:22

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

Referenced by Parser::buildJsonSyntaxTree().

181 {
182  if (action == "EmptyAction")
183  return false;
184 
185  moosecontrib::Json::Value all_params;
186  bool search_match = !_search.empty() && (MooseUtils::wildCardMatch(path, _search) ||
189  auto count = setParams(params, search_match, all_params);
190  if (!_search.empty() && count == 0)
191  // no parameters that matched the search string
192  return false;
193 
194  moosecontrib::Json::Value & json = getJson(parent, path, is_type);
195 
196  if (is_action)
197  {
198  json[action]["parameters"] = all_params;
199  json[action]["description"] = params->getClassDescription();
200  json[action]["action_path"] = path;
201  auto label_pair = getActionLabel(action);
202  json[action]["label"] = label_pair.first;
203  json[action]["register_file"] = label_pair.second;
204  if (lineinfo.isValid())
205  json[action]["file_info"][lineinfo.file()] = lineinfo.line();
206  }
207  else if (params)
208  {
209  if (params->isParamValid("_moose_base"))
210  json["moose_base"] = params->get<std::string>("_moose_base");
211 
212  json["parameters"] = all_params;
213  json["syntax_path"] = path;
214  json["parent_syntax"] = parent;
215  json["description"] = params->getClassDescription();
216  auto label_pair = getObjectLabel(path);
217  json["label"] = label_pair.first;
218  json["register_file"] = label_pair.second;
219  if (lineinfo.isValid())
220  {
221  json["file_info"][lineinfo.file()] = lineinfo.line();
222  if (!classname.empty())
223  json["class"] = classname;
224  }
225  }
226  return true;
227 }
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:560
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 309 of file JsonSyntaxTree.C.

Referenced by Parser::buildJsonSyntaxTree().

310 {
312  {
313  auto & j = getJson(path);
314  j["associated_types"].append(type);
315  }
316  // If they are doing a search they probably don't want to see this
317  if (_search.empty())
318  {
319  _root["global"]["associated_types"][type].append(path);
320  }
321 }
moosecontrib::Json::Value _root
MatType type
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:560
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 335 of file JsonSyntaxTree.C.

Referenced by setParams().

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

Referenced by setParams().

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

◆ buildOutputString()

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

Definition at line 274 of file JsonSyntaxTree.C.

Referenced by setParams().

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

◆ getActionLabel()

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

Definition at line 396 of file JsonSyntaxTree.C.

Referenced by addParameters().

397 {
398  auto it = _action_label_map.find(action);
399  if (it != _action_label_map.end())
400  return it->second;
401  else
402  return std::make_pair("", "");
403 }
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 83 of file JsonSyntaxTree.C.

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

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

64 {
65  auto paths = splitPath(path);
66  mooseAssert(paths.size() > 0, "path is empty");
67  moosecontrib::Json::Value * next = &_root["blocks"][paths[0]];
68 
69  for (auto pit = paths.begin() + 1; pit != paths.end(); ++pit)
70  {
71  if (*pit == "*")
72  // It has an action syntax as a parent
73  next = &(*next)["star"];
74  else if (*pit == "<type>")
75  next = &(*next)["types"];
76  else
77  next = &(*next)["subblocks"][*pit];
78  }
79  return *next;
80 }
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 385 of file JsonSyntaxTree.C.

Referenced by addParameters().

386 {
387  auto paths = splitPath(obj);
388  auto it = _object_label_map.find(paths.back());
389  if (it != _object_label_map.end())
390  return it->second;
391  else
392  return std::make_pair("", "");
393 }
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 367 of file JsonSyntaxTree.C.

Referenced by setParams().

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

◆ setParams()

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

Definition at line 105 of file JsonSyntaxTree.C.

Referenced by addGlobal(), and addParameters().

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

◆ splitPath()

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

Definition at line 51 of file JsonSyntaxTree.C.

Referenced by getJson(), and getObjectLabel().

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

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: