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