www.mooseframework.org
SyntaxTree.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "SyntaxTree.h"
11 
12 #include "InputParameters.h"
13 #include "MooseUtils.h"
14 #include "Parser.h"
15 
16 #include <algorithm>
17 #include <cctype>
18 
19 SyntaxTree::SyntaxTree(bool use_long_names)
20  : SyntaxFormatterInterface(), _root(NULL), _use_long_names(use_long_names)
21 {
22 }
23 
25 
26 void
27 SyntaxTree::insertNode(std::string syntax,
28  const std::string & action,
29  bool is_action_params,
30  InputParameters * params)
31 {
32  if (_root == NULL)
33  _root = new TreeNode("", *this);
34 
35  _root->insertNode(syntax, action, is_action_params, params);
36 }
37 
38 std::string
39 SyntaxTree::print(const std::string & search_string)
40 {
41  bool found = false;
42  std::string output;
43 
44  // Clear the list of "seen" parameters before printing the tree
45  _params_printed.clear();
46 
47  if (_root)
48  output = _root->print(-1, search_string, found);
49 
50  if (found)
51  return preamble() + output + postscript();
52  else
53  return "";
54 }
55 
56 void
57 SyntaxTree::seenIt(const std::string & prefix, const std::string & item)
58 {
59  _params_printed.insert(prefix + item);
60 }
61 
62 bool
63 SyntaxTree::haveSeenIt(const std::string & prefix, const std::string & item) const
64 {
65  return _params_printed.find(prefix + item) != _params_printed.end();
66 }
67 
68 SyntaxTree::TreeNode::TreeNode(const std::string & name,
69  SyntaxTree & syntax_tree,
70  const std::string * action,
71  InputParameters * params,
72  TreeNode * parent)
73  : _name(name), _parent(parent), _syntax_tree(syntax_tree)
74 {
75  if (action)
76  _action_params.insert(std::make_pair(*action, new InputParameters(*params)));
77 }
78 
80 {
81  for (const auto & it : _action_params)
82  delete it.second;
83 
84  for (const auto & it : _moose_object_params)
85  delete it.second;
86 
87  for (const auto & it : _children)
88  delete it.second;
89 }
90 
91 void
92 SyntaxTree::TreeNode::insertNode(std::string & syntax,
93  const std::string & action,
94  bool is_action_params,
95  InputParameters * params)
96 {
97  std::string::size_type pos = syntax.find_first_of("/");
98  std::string item;
99  bool is_leaf = true;
100 
101  item = syntax.substr(0, pos);
102  if (pos != std::string::npos)
103  {
104  syntax = syntax.substr(pos + 1);
105  is_leaf = false;
106  }
107 
108  bool node_created = false;
109  if (_children.find(item) == _children.end())
110  {
111  _children[item] = new TreeNode(
112  item, _syntax_tree, is_leaf && is_action_params ? &action : NULL, params, this);
113  if (is_leaf && !is_action_params)
114  _children[item]->insertParams(action, is_action_params, params);
115  node_created = true;
116  }
117 
118  if (!is_leaf)
119  _children[item]->insertNode(syntax, action, is_action_params, params);
120  else if (!node_created)
121  _children[item]->insertParams(action, is_action_params, params);
122 }
123 
124 void
125 SyntaxTree::TreeNode::insertParams(const std::string & action,
126  bool is_action_params,
127  InputParameters * params)
128 {
129  if (is_action_params)
130  _action_params.insert(std::make_pair(action, new InputParameters(*params)));
131  else
132  _moose_object_params.insert(std::make_pair(action, new InputParameters(*params)));
133 }
134 
135 std::string
136 SyntaxTree::TreeNode::print(short depth, const std::string & search_string, bool & found)
137 {
138  std::string doc = "";
139  std::string long_name(getLongName());
140  std::string name(_syntax_tree.isLongNames() ? long_name : _name);
141  std::string out;
142 
143  if (depth < 0)
144  {
145  for (const auto & c_it : _children)
146  {
147  bool local_found = false;
148  std::string local_out(c_it.second->print(depth + 1, search_string, local_found));
149  found |= local_found; // Update the current frame's found variable
150  if (local_found)
151  out += local_out;
152  }
153  return out;
154  }
155 
156  // GlobalParamsAction is special - we need to just always print it out
157  // if (_name == "GlobalParamsAction")
158  // found = true;
159 
160  std::string indent((depth + 1) * 2, ' ');
161 
162  std::multimap<std::string, InputParameters *>::const_iterator it = _moose_object_params.begin();
163  do
164  {
165  bool local_found = false;
166  std::string local_out;
167 
168  // Compare the block name, if it's matched we are going to pass an empty search string
169  // which means match ALL parameters
170  std::string local_search_string;
171  if (MooseUtils::wildCardMatch(name, search_string))
172  found = true;
173  else
174  local_search_string = search_string;
175 
176  if (it != _moose_object_params.end())
177  doc = it->second->getClassDescription();
178  local_out += _syntax_tree.printBlockOpen(name, depth, doc);
179 
180  for (const auto & a_it : _action_params)
181  if (a_it.first != "EmptyAction")
182  {
183  local_out += _syntax_tree.printParams(
184  name, long_name, *(a_it.second), depth, local_search_string, local_found);
185  found |= local_found; // Update the current frame's found variable
186  // DEBUG
187  // Moose::out << "\n" << indent << "(" << ait->first << ")";
188  // DEBUG
189  }
190 
191  if (it != _moose_object_params.end())
192  {
193  local_out += _syntax_tree.printParams(
194  name, long_name, *it->second, depth, local_search_string, local_found);
195  found |= local_found;
196  // DEBUG
197  // Moose::out << "\n" << indent << "{" << it->first << "}";
198  // DEBUG
199  }
200 
201  local_out += _syntax_tree.preTraverse(depth);
202 
203  for (const auto & c_it : _children)
204  {
205  bool child_found = false;
206  std::string child_out(c_it.second->print(depth + 1, local_search_string, child_found));
207  found |= child_found; // Update the current frame's found variable
208 
209  if (child_found)
210  local_out += child_out;
211  }
212 
213  local_out += _syntax_tree.printBlockClose(name, depth);
214 
215  if (found)
216  out += local_out;
217 
218  // If there are no moose object params then we have to be careful about how we
219  // increment the iterator. We only want to increment if we aren't already
220  // at the end.
221  } while (it != _moose_object_params.end() && ++it != _moose_object_params.end());
222 
223  return out;
224 }
225 
226 std::string
227 SyntaxTree::TreeNode::getLongName(const std::string & delim) const
228 {
229  if (_parent)
230  return _parent->getLongName(delim) + delim + _name;
231  else
232  return _name;
233 }
234 
235 bool
237 {
238  return _use_long_names;
239 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:29
This interface is for classes that want to be called to format InputFileParameters.
void insertNode(std::string &syntax, const std::string &action, bool is_action_params=true, InputParameters *params=NULL)
Definition: SyntaxTree.C:92
std::string getLongName(const std::string &delim="/") const
Definition: SyntaxTree.C:227
void insertParams(const std::string &action, bool is_action_params, InputParameters *params=NULL)
Definition: SyntaxTree.C:125
TreeNode * _root
Definition: SyntaxTree.h:74
std::set< std::string > _params_printed
Definition: SyntaxTree.h:78
SyntaxTree(bool use_long_names=false)
Definition: SyntaxTree.C:19
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual std::string preamble() const
This method is called once at the beginning of the tree traversal and can be used to build up header ...
TreeNode(const std::string &name, SyntaxTree &syntax_tree, const std::string *action=NULL, InputParameters *params=NULL, TreeNode *parent=NULL)
Definition: SyntaxTree.C:68
virtual ~SyntaxTree()
Definition: SyntaxTree.C:24
std::multimap< std::string, InputParameters * > _action_params
Definition: SyntaxTree.h:65
bool _use_long_names
Definition: SyntaxTree.h:75
void insertNode(std::string syntax, const std::string &action, bool is_action_params=true, InputParameters *params=NULL)
Definition: SyntaxTree.C:27
std::string print(const std::string &search_string)
Definition: SyntaxTree.C:39
std::string print(short depth, const std::string &search_string, bool &found)
Definition: SyntaxTree.C:136
virtual std::string postscript() const
This method is called once at the end of the tree traversal and can be used to add any necessary trai...
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:560
bool isLongNames() const
Definition: SyntaxTree.C:236
bool haveSeenIt(const std::string &prefix, const std::string &item) const
Definition: SyntaxTree.C:63
This class represents a single node in our tree.
Definition: SyntaxTree.h:41
void seenIt(const std::string &prefix, const std::string &item)
Definition: SyntaxTree.C:57