www.mooseframework.org
Syntax.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 "Syntax.h"
16 #include "MooseUtils.h"
17 
19 
20 void
21 Syntax::registerTaskName(std::string task, bool is_required)
22 {
23  if (_registered_tasks.find(task) != _registered_tasks.end())
24  mooseError("A ", task, " is already registered. Do you need to use appendTaskName instead?");
25 
26  _tasks.addItem(task);
27  _registered_tasks[task] = is_required;
28 }
29 
30 void
31 Syntax::registerTaskName(std::string task, std::string moose_object_type, bool is_required)
32 {
33  if (_registered_tasks.find(task) != _registered_tasks.end())
34  mooseError("A ", task, " is already registered. Do you need to use appendTaskName instead?");
35 
36  _tasks.addItem(task);
37  _registered_tasks[task] = is_required;
38  _moose_systems_to_tasks.insert(std::make_pair(moose_object_type, task));
39 }
40 
41 void
42 Syntax::appendTaskName(std::string task, std::string moose_object_type)
43 {
44  if (_registered_tasks.find(task) == _registered_tasks.end())
45  mooseError("A ", task, " is not a registered task name.");
46 
47  _moose_systems_to_tasks.insert(std::make_pair(moose_object_type, task));
48 }
49 
50 void
51 Syntax::addDependency(std::string task, std::string pre_req)
52 {
53  if (_registered_tasks.find(task) == _registered_tasks.end())
54  mooseError("A ", task, " is not a registered task name.");
55 
56  _tasks.insertDependency(task, pre_req);
57 }
58 
59 void
60 Syntax::addDependencySets(const std::string & action_sets)
61 {
62  std::vector<std::string> sets, prev_names, tasks;
63  MooseUtils::tokenize(action_sets, sets, 1, "()");
64 
65  for (unsigned int i = 0; i < sets.size(); ++i)
66  {
67  tasks.clear();
68  MooseUtils::tokenize(sets[i], tasks, 0, ", ");
69  for (unsigned int j = 0; j < tasks.size(); ++j)
70  {
71  // Each line should depend on each item in the previous line
72  for (unsigned int k = 0; k < prev_names.size(); ++k)
73  addDependency(tasks[j], prev_names[k]);
74  }
75  // Copy the the current items to the previous items for the next iteration
76  std::swap(tasks, prev_names);
77  }
78 }
79 
80 const std::vector<std::string> &
82 {
83  return _tasks.getSortedValues();
84 }
85 
86 const std::vector<std::vector<std::string>> &
88 {
89  return _tasks.getSortedValuesSets();
90 }
91 
92 bool
93 Syntax::hasTask(const std::string & task)
94 {
95  return (_registered_tasks.find(task) != _registered_tasks.end());
96 }
97 
98 bool
99 Syntax::isActionRequired(const std::string & task)
100 {
101  return _registered_tasks[task];
102 }
103 
104 void
105 Syntax::registerActionSyntax(const std::string & action,
106  const std::string & syntax,
107  const std::string & task,
108  const std::string & file,
109  int line)
110 {
111  ActionInfo action_info;
112  action_info._action = action;
113  action_info._task = task;
114 
115  _associated_actions.insert(std::make_pair(syntax, action_info));
116  _syntax_to_line.addInfo(syntax, action, task, file, line);
117 }
118 
119 void
120 Syntax::replaceActionSyntax(const std::string & action,
121  const std::string & syntax,
122  const std::string & task,
123  const std::string & file,
124  int line)
125 {
126  _associated_actions.erase(syntax);
127  registerActionSyntax(action, syntax, task, file, line);
128 }
129 
130 void
131 Syntax::deprecateActionSyntax(const std::string & syntax)
132 {
133  _deprecated_syntax.insert(syntax);
134 }
135 
136 bool
137 Syntax::isDeprecatedSyntax(const std::string & syntax) const
138 {
139  return _deprecated_syntax.find(syntax) != _deprecated_syntax.end();
140 }
141 
142 std::string
143 Syntax::getSyntaxByAction(const std::string & action, const std::string & task)
144 {
145  std::string syntax;
151  for (const auto & iter : _associated_actions)
152  if (iter.second._action == action && (iter.second._task == task || iter.second._task == ""))
153  syntax = iter.first;
154 
155  return syntax;
156 }
157 
158 std::string
159 Syntax::isAssociated(const std::string & real_id, bool * is_parent)
160 {
170  bool local_is_parent;
171  if (is_parent == NULL)
172  is_parent = &local_is_parent; // Just so we don't have to keep checking below when we want to
173  // set the value
174  std::multimap<std::string, ActionInfo>::reverse_iterator it;
175  std::vector<std::string> real_elements, reg_elements;
176  std::string return_value;
177 
178  MooseUtils::tokenize(real_id, real_elements);
179 
180  *is_parent = false;
181  for (it = _associated_actions.rbegin(); it != _associated_actions.rend(); ++it)
182  {
183  std::string reg_id = it->first;
184  if (reg_id == real_id)
185  {
186  *is_parent = false;
187  return reg_id;
188  }
189  reg_elements.clear();
190  MooseUtils::tokenize(reg_id, reg_elements);
191  if (real_elements.size() <= reg_elements.size())
192  {
193  bool keep_going = true;
194  for (unsigned int j = 0; keep_going && j < real_elements.size(); ++j)
195  {
196  if (real_elements[j] != reg_elements[j] && reg_elements[j] != std::string("*"))
197  keep_going = false;
198  }
199  if (keep_going)
200  {
201  if (real_elements.size() < reg_elements.size() && !*is_parent)
202  {
203  // We found a parent, the longest parent in fact but we need to keep
204  // looking to make sure that the real thing isn't registered
205  *is_parent = true;
206  return_value = reg_id;
207  }
208  else if (real_elements.size() == reg_elements.size())
209  {
210  *is_parent = false;
211  return reg_id;
212  }
213  }
214  }
215  }
216 
217  if (*is_parent)
218  return return_value;
219  else
220  return std::string("");
221 }
222 
223 std::pair<std::multimap<std::string, Syntax::ActionInfo>::iterator,
224  std::multimap<std::string, Syntax::ActionInfo>::iterator>
225 Syntax::getActions(const std::string & name)
226 {
227  return _associated_actions.equal_range(name);
228 }
229 
230 bool
231 Syntax::verifyMooseObjectTask(const std::string & base, const std::string & task) const
232 {
233  std::pair<std::multimap<std::string, std::string>::const_iterator,
234  std::multimap<std::string, std::string>::const_iterator>
235  iters = _moose_systems_to_tasks.equal_range(base);
236 
237  for (std::multimap<std::string, std::string>::const_iterator it = iters.first; it != iters.second;
238  ++it)
239  if (task == it->second)
240  return true;
241 
242  return false;
243 }
244 
245 void
246 Syntax::registerSyntaxType(const std::string & syntax, const std::string & type)
247 {
248  _associated_types.insert(std::make_pair(syntax, type));
249 }
250 
252 Syntax::getLineInfo(const std::string & syntax,
253  const std::string & action,
254  const std::string & task) const
255 {
256  return _syntax_to_line.getInfo(syntax, action, task);
257 }
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:350
const std::vector< std::vector< T > > & getSortedValuesSets()
Returns a vector of sets that represent dependency resolved values.
std::set< std::string > _deprecated_syntax
Definition: Syntax.h:142
FileLineInfoMap _syntax_to_line
Definition: Syntax.h:143
DependencyResolver< std::string > _tasks
The dependency resolver.
Definition: Syntax.h:134
FileLineInfo getLineInfo(const std::string &syntax, const std::string &action, const std::string &task) const
Gets the file and line where the syntax/action/task combo was registered.
Definition: Syntax.C:252
void addDependency(std::string task, std::string pre_req)
Definition: Syntax.C:51
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
void registerActionSyntax(const std::string &action, const std::string &syntax, const std::string &task="", const std::string &file="", int line=-1)
Definition: Syntax.C:105
const std::vector< std::string > & getSortedTask()
Definition: Syntax.C:81
void addInfo(const std::string &key0, const std::string &file, int line)
Associate a key with file/line info.
Definition: FileLineInfo.C:40
std::string _action
Definition: Syntax.h:31
const std::vector< T > & getSortedValues()
This function also returns dependency resolved values but with a simpler single vector interface...
void registerTaskName(std::string task, bool is_required)
Definition: Syntax.C:21
bool verifyMooseObjectTask(const std::string &base, const std::string &task) const
Definition: Syntax.C:231
std::string isAssociated(const std::string &real_id, bool *is_parent)
Method for determining whether a piece of syntax is associated with an Action TODO: I need a better n...
Definition: Syntax.C:159
std::map< std::string, bool > _registered_tasks
The list of registered tasks and a flag indicating whether or not they are required.
Definition: Syntax.h:128
bool isDeprecatedSyntax(const std::string &syntax) const
Returns a Boolean indicating whether the syntax has been deprecated through a call to deprecateAction...
Definition: Syntax.C:137
const std::vector< std::vector< std::string > > & getSortedTaskSet()
Definition: Syntax.C:87
std::pair< std::multimap< std::string, ActionInfo >::iterator, std::multimap< std::string, ActionInfo >::iterator > getActions(const std::string &name)
Definition: Syntax.C:225
X_global swap(X_sys)
bool isActionRequired(const std::string &task)
Definition: Syntax.C:99
std::string getSyntaxByAction(const std::string &action, const std::string &task)
Definition: Syntax.C:143
std::multimap< std::string, ActionInfo > _associated_actions
Actions/Syntax association.
Definition: Syntax.h:137
void replaceActionSyntax(const std::string &action, const std::string &syntax, const std::string &task, const std::string &file="", int line=-1)
Registration function that replaces existing Moose Actions with a completely new action Note: This fu...
Definition: Syntax.C:120
Holds file and line information.
Definition: FileLineInfo.h:24
void addDependencySets(const std::string &action_sets)
Definition: Syntax.C:60
std::string _task
Definition: Syntax.h:32
Syntax()
Definition: Syntax.C:18
MatType type
std::multimap< std::string, std::string > _moose_systems_to_tasks
The list of Moose system objects to tasks. This map indicates which tasks are allowed to build certai...
Definition: Syntax.h:131
void registerSyntaxType(const std::string &syntax, const std::string &type)
Registration a type with a block.
Definition: Syntax.C:246
void addItem(const T &value)
Add an independent item to the set.
void deprecateActionSyntax(const std::string &syntax)
This method deprecates previously registered syntax.
Definition: Syntax.C:131
bool hasTask(const std::string &task)
Definition: Syntax.C:93
std::multimap< std::string, std::string > _associated_types
Syntax/Type association.
Definition: Syntax.h:140
void appendTaskName(std::string task, std::string moose_object_type)
Definition: Syntax.C:42
FileLineInfo getInfo(const std::string &key0) const
Get file/line info for a key.
Definition: FileLineInfo.C:81
void insertDependency(const T &key, const T &value)
Insert a dependency pair - the first value or the "key" depends on the second value or the "value"...