https://mooseframework.inl.gov
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends | 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 nlohmann::json & 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...
 

Static Public Member Functions

static std::string basicCppType (const std::string &cpp_type)
 Utilities for making sense of c++ types. More...
 

Protected Member Functions

std::string buildOptions (const std::iterator_traits< InputParameters::iterator >::value_type &p, bool &out_of_range_allowed, std::map< MooseEnumItem, std::string > &docs)
 
size_t setParams (InputParameters *params, bool search_match, nlohmann::json &all_params)
 
nlohmann::json & getJson (const std::string &parent, const std::string &path, bool is_type)
 
nlohmann::json & 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::string buildOutputString (const std::iterator_traits< InputParameters::iterator >::value_type &p)
 
static std::vector< std::string > splitPath (const std::string &path)
 

Protected Attributes

nlohmann::json _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
 

Friends

class MooseServer
 

Detailed Description

Holds the syntax in a Json::Value tree.

Definition at line 22 of file JsonSyntaxTree.h.

Constructor & Destructor Documentation

◆ JsonSyntaxTree()

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

Definition at line 29 of file JsonSyntaxTree.C.

29  : _search(search_string)
30 {
31  // Registry holds a map with labels (ie MooseApp) as keys and a vector of RegistryEntry
32  // as values. We need the reverse map: given an action or object name then get the label.
33  auto & actmap = Registry::allActions();
34  for (auto & entry : actmap)
35  for (auto & act : entry.second)
36  _action_label_map[act->_classname] = std::make_pair(entry.first, act->_file);
37 
38  auto & objmap = Registry::allObjects();
39  for (auto & entry : objmap)
40  for (auto & obj : entry.second)
41  _object_label_map[obj->name()] = std::make_pair(entry.first, obj->_file);
42 }
static const std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > & allActions()
Returns a per-label keyed map of all Actions in the registry.
Definition: Registry.h:227
static const std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > & allObjects()
Returns a per-label keyed map of all MooseObjects in the registry.
Definition: Registry.h:222
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

◆ ~JsonSyntaxTree()

virtual JsonSyntaxTree::~JsonSyntaxTree ( )
inlinevirtual

Definition at line 26 of file JsonSyntaxTree.h.

26 {}

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

359 {
360  nlohmann::json & json = getJson("", path, false);
361  if (lineinfo.isValid())
362  json[action]["tasks"][task_name]["file_info"][lineinfo.file()] = lineinfo.line();
363 }
nlohmann::json & getJson(const std::string &parent, const std::string &path, bool is_type)
int line() const
Definition: FileLineInfo.C:23
std::string file() const
Definition: FileLineInfo.C:29
bool isValid() const
Definition: FileLineInfo.C:17

◆ addGlobal()

void JsonSyntaxTree::addGlobal ( )

Add the global section to the output.

Definition at line 158 of file JsonSyntaxTree.C.

159 {
160  // If they are doing a search they probably don't want to see this
161  if (_search.empty())
162  {
163  auto params = Moose::Builder::validParams();
164  nlohmann::json jparams;
165  setParams(&params, true, jparams);
166  _root["global"]["parameters"] = jparams;
167 
168  // Just create a list of registered app names
169  nlohmann::json apps;
170  auto & factory = AppFactory::instance();
171  for (const auto & name_bi_pair : factory.registeredObjects())
172  apps.push_back(name_bi_pair.first);
173 
174  _root["global"]["registered_apps"] = apps;
175  }
176 }
size_t setParams(InputParameters *params, bool search_match, nlohmann::json &all_params)
static InputParameters validParams()
Parameters that are processed directly by the Parser and are valid anywhere in the input...
Definition: Builder.C:141
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:18
nlohmann::json _root
std::string _search

◆ 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_actionWhether we are adding the parameter for an action (except Components)
paramsThe InputParameters to add to the tree
lineinfoThe FileLineInfo where the action/path was registered
classnamethe name of the class being added
Returns
Whether the parameters were added to the tree (ie if it matched the search string).

Definition at line 179 of file JsonSyntaxTree.C.

187 {
188  if (action == "EmptyAction")
189  return false;
190 
191  nlohmann::json all_params;
192  bool search_match = !_search.empty() && (MooseUtils::wildCardMatch(path, _search) ||
195  auto count = setParams(params, search_match, all_params);
196  if (!_search.empty() && count == 0)
197  // no parameters that matched the search string
198  return false;
199 
200  nlohmann::json & json = getJson(parent, path, is_type);
201 
202  if (is_action)
203  {
204  json[action]["parameters"] = all_params;
205  json[action]["description"] = params->getClassDescription();
206  json[action]["action_path"] = path;
207  auto label_pair = getActionLabel(action);
208  json[action]["label"] = label_pair.first;
209  json[action]["register_file"] = label_pair.second;
210  if (lineinfo.isValid())
211  json[action]["file_info"][lineinfo.file()] = lineinfo.line();
212  }
213  else if (params)
214  {
215  if (params->isParamValid("_moose_base"))
216  json["moose_base"] = params->get<std::string>("_moose_base");
217 
218  json["parameters"] = all_params;
219  json["syntax_path"] = path;
220  json["parent_syntax"] = parent;
221  json["description"] = params->getClassDescription();
222  // We do this for ActionComponents which are registered as Actions but
223  // dumped to the syntax tree as Objects
224  if (params->isParamValid("_moose_base") && json["moose_base"] == "Action")
225  {
226  auto label_pair = getActionLabel(classname);
227  json["label"] = label_pair.first;
228  json["register_file"] = label_pair.second;
229  }
230  else
231  {
232  auto label_pair = getObjectLabel(path);
233  json["label"] = label_pair.first;
234  json["register_file"] = label_pair.second;
235  }
236  if (lineinfo.isValid())
237  {
238  json["file_info"][lineinfo.file()] = lineinfo.line();
239  if (!classname.empty())
240  json["class"] = classname;
241  }
242  }
243  return true;
244 }
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
nlohmann::json & getJson(const std::string &parent, const std::string &path, bool is_type)
size_t setParams(InputParameters *params, bool search_match, nlohmann::json &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:884
bool isValid() const
Definition: FileLineInfo.C:17
std::string _search
std::string getClassDescription() const
Returns the class description.
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 340 of file JsonSyntaxTree.C.

341 {
343  {
344  auto & j = getJson(path);
345  j["associated_types"].push_back(type);
346  }
347  // If they are doing a search they probably don't want to see this
348  if (_search.empty())
349  {
350  _root["global"]["associated_types"][type].push_back(path);
351  }
352 }
nlohmann::json & getJson(const std::string &parent, const std::string &path, bool is_type)
nlohmann::json _root
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:884
std::string _search

◆ basicCppType()

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

Utilities for making sense of c++ types.

Definition at line 366 of file JsonSyntaxTree.C.

Referenced by MooseServer::addParametersToList(), MooseServer::addValuesToList(), and setParams().

367 {
368  std::string s = "String";
369  if (cpp_type.find("std::vector") != std::string::npos ||
370  cpp_type.find("libMesh::VectorValue") != std::string::npos ||
371  cpp_type.find("libMesh::TensorValue") != std::string::npos ||
372  cpp_type.find("Eigen::Matrix") != std::string::npos)
373  {
374  // Get the template type and use its basic type for the array type
375  pcrecpp::RE r("^[^<]+<\\s*(.*)\\s*>$");
376  std::string t;
377  r.FullMatch(cpp_type, &t);
378 
379  // Capture type just to the first comma for Eigen::Matrix<type,V,W,X,Y,Z>
380  if (cpp_type.find("Eigen::Matrix") != std::string::npos)
381  t = t.substr(0, t.find(","));
382 
383  s = "Array:" + basicCppType(t);
384  }
385  else if (cpp_type.find("MultiMooseEnum") != std::string::npos ||
386  cpp_type.find("ExecFlagEnum") != std::string::npos ||
387  cpp_type.find("VectorPostprocessorName") != std::string::npos ||
388  cpp_type.find("std::map") != std::string::npos)
389  s = "Array:String";
390  else if (cpp_type.find("libMesh::Point") != std::string::npos)
391  s = "Array:Real";
392  else if (cpp_type == "int" || cpp_type == "unsigned int" || cpp_type == "short" ||
393  cpp_type == "unsigned short" || cpp_type == "char" || cpp_type == "unsigned char" ||
394  cpp_type == "long" || cpp_type == "unsigned long" || cpp_type == "long long" ||
395  cpp_type == "unsigned long long")
396  s = "Integer";
397  else if (cpp_type == "double" || cpp_type == "float")
398  s = "Real";
399  else if (cpp_type == "bool")
400  s = "Boolean";
401 
402  return s;
403 }
static std::string basicCppType(const std::string &cpp_type)
Utilities for making sense of c++ types.

◆ buildOptions()

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

Definition at line 247 of file JsonSyntaxTree.C.

Referenced by setParams().

250 {
252 
253  std::string options;
254  {
255  auto * enum_type = dynamic_cast<InputParameters::Parameter<MooseEnum> *>(val);
256  if (enum_type)
257  {
258  out_of_range_allowed = enum_type->get().isOutOfRangeAllowed();
259  options = enum_type->get().getRawNames();
260  docs = enum_type->get().getItemDocumentation();
261  }
262  }
263  {
264  auto * enum_type = dynamic_cast<InputParameters::Parameter<MultiMooseEnum> *>(val);
265  if (enum_type)
266  {
267  out_of_range_allowed = enum_type->get().isOutOfRangeAllowed();
268  options = enum_type->get().getRawNames();
269  docs = enum_type->get().getItemDocumentation();
270  }
271  }
272  {
273  auto * enum_type = dynamic_cast<InputParameters::Parameter<ExecFlagEnum> *>(val);
274  if (enum_type)
275  {
276  out_of_range_allowed = enum_type->get().isOutOfRangeAllowed();
277  options = enum_type->get().getRawNames();
278  docs = enum_type->get().getItemDocumentation();
279  }
280  }
281  {
282  auto * enum_type = dynamic_cast<InputParameters::Parameter<std::vector<MooseEnum>> *>(val);
283  if (enum_type)
284  {
285  out_of_range_allowed = (enum_type->get())[0].isOutOfRangeAllowed();
286  options = (enum_type->get())[0].getRawNames();
287  docs = enum_type->get()[0].getItemDocumentation();
288  }
289  }
290  {
291  auto * enum_type = dynamic_cast<InputParameters::Parameter<std::vector<MultiMooseEnum>> *>(val);
292  if (enum_type)
293  {
294  out_of_range_allowed = (enum_type->get())[0].isOutOfRangeAllowed();
295  options = (enum_type->get())[0].getRawNames();
296  docs = enum_type->get()[0].getItemDocumentation();
297  }
298  }
299  return options;
300 }
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155

◆ buildOutputString()

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

Definition at line 303 of file JsonSyntaxTree.C.

Referenced by MooseServer::addParametersToList(), and setParams().

305 {
307 
308  // Account for Point
309  std::stringstream str;
310  InputParameters::Parameter<Point> * ptr0 = dynamic_cast<InputParameters::Parameter<Point> *>(val);
311 
312  // Account for RealVectorValues
313  InputParameters::Parameter<RealVectorValue> * ptr1 =
314  dynamic_cast<InputParameters::Parameter<RealVectorValue> *>(val);
315 
316  // Output the Point components
317  if (ptr0)
318  str << ptr0->get().operator()(0) << " " << ptr0->get().operator()(1) << " "
319  << ptr0->get().operator()(2);
320 
321  // Output the RealVectorValue components
322  else if (ptr1)
323  str << ptr1->get().operator()(0) << " " << ptr1->get().operator()(1) << " "
324  << ptr1->get().operator()(2);
325 
326  // General case, call the print operator
327  else
328  val->print(str);
329 
330  // remove additional '\n' possibly generated in output (breaks JSON parsing)
331  std::string tmp_str = str.str();
332  for (auto & ch : tmp_str)
333  if (ch == '\n')
334  ch = ' ';
335 
336  return tmp_str.substr(0, tmp_str.find("<RESIDUAL>"));
337 }
virtual void print(std::ostream &) const=0
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155

◆ getActionLabel()

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

Definition at line 417 of file JsonSyntaxTree.C.

Referenced by addParameters().

418 {
419  auto it = _action_label_map.find(action);
420  if (it != _action_label_map.end())
421  return it->second;
422  else
423  return std::make_pair("", "");
424 }
std::map< std::string, std::pair< std::string, std::string > > _action_label_map

◆ getJson() [1/2]

nlohmann::json & JsonSyntaxTree::getJson ( const std::string &  parent,
const std::string &  path,
bool  is_type 
)
protected

Definition at line 77 of file JsonSyntaxTree.C.

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

78 {
79  if (parent.empty())
80  {
81  auto & j = getJson(path);
82  if (path.back() == '*' && !j.contains("subblock_types"))
83  j["subblock_types"] = nlohmann::json();
84  else if (path.back() != '*' && !j.contains("types"))
85  j["types"] = nlohmann::json();
86  return j["actions"];
87  }
88 
89  auto & parent_json = getJson(parent);
90  auto paths = splitPath(path);
91  std::string key = "subblock_types";
92  if (is_type)
93  key = "types";
94  auto & val = parent_json[key][paths.back()];
95  return val;
96 }
nlohmann::json & getJson(const std::string &parent, const std::string &path, bool is_type)
static std::vector< std::string > splitPath(const std::string &path)

◆ getJson() [2/2]

nlohmann::json & JsonSyntaxTree::getJson ( const std::string &  path)
protected

Definition at line 57 of file JsonSyntaxTree.C.

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

◆ getObjectLabel()

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

Definition at line 406 of file JsonSyntaxTree.C.

Referenced by addParameters().

407 {
408  auto paths = splitPath(obj);
409  auto it = _object_label_map.find(paths.back());
410  if (it != _object_label_map.end())
411  return it->second;
412  else
413  return std::make_pair("", "");
414 }
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 nlohmann::json& 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; }
nlohmann::json _root

◆ setParams()

size_t JsonSyntaxTree::setParams ( InputParameters params,
bool  search_match,
nlohmann::json &  all_params 
)
protected

Definition at line 99 of file JsonSyntaxTree.C.

Referenced by addGlobal(), and addParameters().

100 {
101  size_t count = 0;
102  for (auto & iter : *params)
103  {
104  // Make sure we want to see this parameter
105  bool param_match = !_search.empty() && MooseUtils::wildCardMatch(iter.first, _search);
106  if (params->isPrivate(iter.first) || (!_search.empty() && !search_match && !param_match))
107  continue;
108 
109  ++count;
110  nlohmann::json param_json;
111 
112  param_json["required"] = params->isParamRequired(iter.first);
113 
114  // Only output default if it has one
115  if (params->isParamValid(iter.first))
116  param_json["default"] = buildOutputString(iter);
117  else if (params->hasDefaultCoupledValue(iter.first))
118  param_json["default"] = params->defaultCoupledValue(iter.first);
119 
120  bool out_of_range_allowed = false;
121  std::map<MooseEnumItem, std::string> docs;
122  param_json["options"] = buildOptions(iter, out_of_range_allowed, docs);
123  if (!nlohmann::to_string(param_json["options"]).empty())
124  {
125  param_json["out_of_range_allowed"] = out_of_range_allowed;
126  if (!docs.empty())
127  {
128  nlohmann::json jdocs;
129  for (const auto & doc : docs)
130  jdocs[doc.first.name()] = doc.second;
131  param_json["option_docs"] = jdocs;
132  }
133  }
134  auto reserved_values = params->reservedValues(iter.first);
135  for (const auto & reserved : reserved_values)
136  param_json["reserved_values"].push_back(reserved);
137 
138  std::string t = MooseUtils::prettyCppType(params->type(iter.first));
139  param_json["cpp_type"] = t;
140  param_json["basic_type"] = basicCppType(t);
141  param_json["group_name"] = params->getGroupName(iter.first);
142  param_json["name"] = iter.first;
143 
144  std::string doc = params->getDocString(iter.first);
145  MooseUtils::escape(doc);
146  param_json["description"] = doc;
147 
148  param_json["doc_unit"] = params->getDocUnit(iter.first);
149 
150  param_json["controllable"] = params->isControllable(iter.first);
151  param_json["deprecated"] = params->isParamDeprecated(iter.first);
152  all_params[iter.first] = param_json;
153  }
154  return count;
155 }
std::string buildOptions(const std::iterator_traits< InputParameters::iterator >::value_type &p, bool &out_of_range_allowed, std::map< MooseEnumItem, std::string > &docs)
static std::string buildOutputString(const std::iterator_traits< InputParameters::iterator >::value_type &p)
static std::string basicCppType(const std::string &cpp_type)
Utilities for making sense of c++ types.
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:884
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:203
std::string prettyCppType(const std::string &cpp_type)
Definition: MooseUtils.C:1246

◆ splitPath()

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

Definition at line 45 of file JsonSyntaxTree.C.

Referenced by getJson(), and getObjectLabel().

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

Friends And Related Function Documentation

◆ MooseServer

friend class MooseServer
friend

Definition at line 108 of file JsonSyntaxTree.h.

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

Referenced by getObjectLabel(), and JsonSyntaxTree().

◆ _root

nlohmann::json JsonSyntaxTree::_root
protected

Definition at line 98 of file JsonSyntaxTree.h.

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

◆ _search

std::string JsonSyntaxTree::_search
protected

Definition at line 99 of file JsonSyntaxTree.h.

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


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