www.mooseframework.org
Syntax.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 "Syntax.h"
11 #include "MooseUtils.h"
12 
13 #include "libmesh/simple_range.h"
14 
15 #include <algorithm>
16 
17 Syntax::Syntax() : _actions_to_syntax_valid(false) {}
18 
19 void
20 Syntax::registerTaskName(const std::string & task, bool should_auto_build)
21 {
22  if (_registered_tasks.count(task) > 0)
23  return;
24  _tasks.addItem(task);
25  _registered_tasks[task] = should_auto_build;
26 }
27 
28 void
29 Syntax::registerTaskName(const std::string & task,
30  const std::string & moose_object_type,
31  bool should_auto_build)
32 {
33  auto range = _moose_systems_to_tasks.equal_range(moose_object_type);
34  for (auto it = range.first; it != range.second; ++it)
35  if (it->second == task)
36  return;
37 
38  if (_registered_tasks.find(task) != _registered_tasks.end())
39  mooseError("A ", task, " is already registered. Do you need to use appendTaskName instead?");
40 
41  registerTaskName(task, should_auto_build);
42  _moose_systems_to_tasks.insert(std::make_pair(moose_object_type, task));
43 }
44 
45 void
46 Syntax::appendTaskName(const std::string & task, const std::string & moose_object_type)
47 {
48  if (_registered_tasks.find(task) == _registered_tasks.end())
49  mooseError("A ", task, " is not a registered task name.");
50 
51  _moose_systems_to_tasks.insert(std::make_pair(moose_object_type, task));
52 }
53 
54 void
55 Syntax::addDependency(const std::string & task, const std::string & pre_req)
56 {
57  if (_registered_tasks.find(task) == _registered_tasks.end())
58  mooseError("A ", task, " is not a registered task name.");
59 
60  _tasks.insertDependency(task, pre_req);
61 }
62 
63 void
64 Syntax::addDependencySets(const std::string & action_sets)
65 {
66  std::vector<std::string> sets, prev_names, tasks;
67  MooseUtils::tokenize(action_sets, sets, 1, "()");
68 
69  for (unsigned int i = 0; i < sets.size(); ++i)
70  {
71  tasks.clear();
72  MooseUtils::tokenize(sets[i], tasks, 0, ", ");
73  for (unsigned int j = 0; j < tasks.size(); ++j)
74  {
75  // Each line should depend on each item in the previous line
76  for (unsigned int k = 0; k < prev_names.size(); ++k)
77  addDependency(tasks[j], prev_names[k]);
78  }
79  // Copy the the current items to the previous items for the next iteration
80  std::swap(tasks, prev_names);
81  }
82 }
83 
84 void
85 Syntax::deleteTaskDependencies(const std::string & task)
86 {
87  if (_registered_tasks.find(task) == _registered_tasks.end())
88  mooseError("A ", task, " is not a registered task name.");
89 
91 }
92 
93 void
95 {
96  _tasks.clear();
97 }
98 
99 const std::vector<std::string> &
101 {
102  return _tasks.getSortedValues();
103 }
104 
105 const std::vector<std::vector<std::string>> &
107 {
108  return _tasks.getSortedValuesSets();
109 }
110 
111 bool
112 Syntax::hasTask(const std::string & task) const
113 {
114  return (_registered_tasks.find(task) != _registered_tasks.end());
115 }
116 
117 bool
118 Syntax::isActionRequired(const std::string & task) const
119 {
120  mooseDeprecated("Syntax::isActionRequired is deprecated, use shouldAutoBuild() instead");
121  return shouldAutoBuild(task);
122 }
123 
124 bool
125 Syntax::shouldAutoBuild(const std::string & task) const
126 {
127  auto map_pair = _registered_tasks.find(task);
128  mooseAssert(map_pair != _registered_tasks.end(), std::string("Unregistered task: ") + task);
129 
130  return map_pair->second;
131 }
132 
133 void
134 Syntax::registerActionSyntax(const std::string & action,
135  const std::string & syntax,
136  const std::string & task,
137  const std::string & file,
138  int line)
139 {
140  auto range = _syntax_to_actions.equal_range(syntax);
141  for (auto it = range.first; it != range.second; ++it)
142  if (it->second._action == action && it->second._task == task)
143  return;
144 
145  _syntax_to_actions.insert(std::make_pair(syntax, ActionInfo{action, task}));
146  _syntax_to_line.addInfo(syntax, action, task, file, line);
147  _actions_to_syntax_valid = false;
148 }
149 
150 void
151 Syntax::replaceActionSyntax(const std::string & action,
152  const std::string & syntax,
153  const std::string & task,
154  const std::string & file,
155  int line)
156 {
157  _syntax_to_actions.erase(syntax);
158  registerActionSyntax(action, syntax, task, file, line);
159 }
160 
161 void
162 Syntax::deprecateActionSyntax(const std::string & syntax)
163 {
164  const std::string message = "\"[" + syntax + "]\" is deprecated.";
165  deprecateActionSyntax(syntax, message);
166 }
167 
168 void
169 Syntax::deprecateActionSyntax(const std::string & syntax, const std::string & message)
170 {
171  _deprecated_syntax.insert(std::make_pair(syntax, message));
172 }
173 
174 std::string
175 Syntax::deprecatedActionSyntaxMessage(const std::string syntax)
176 {
177  auto it = _deprecated_syntax.find(syntax);
178 
179  if (it != _deprecated_syntax.end())
180  return it->second;
181  else
182  mooseError("The action syntax ", syntax, " is not deprecated");
183 }
184 
185 bool
186 Syntax::isDeprecatedSyntax(const std::string & syntax) const
187 {
188  return _deprecated_syntax.find(syntax) != _deprecated_syntax.end();
189 }
190 
191 std::vector<std::string>
192 Syntax::getSyntaxByAction(const std::string & action, const std::string & task)
193 {
194  // See if the reverse multimap has been built yet, if not build it now
196  {
197  std::transform(_syntax_to_actions.begin(),
198  _syntax_to_actions.end(),
199  std::inserter(_actions_to_syntax, _actions_to_syntax.begin()),
200  [](const std::pair<std::string, ActionInfo> pair) {
201  return std::make_pair(pair.second._action,
202  std::make_pair(pair.first, pair.second._task));
203  });
205  }
206 
207  std::vector<std::string> syntax;
208  auto it_pair = _actions_to_syntax.equal_range(action);
209  for (const auto & syntax_pair : as_range(it_pair))
210  // If task is blank, return all syntax, otherwise filter by task
211  if (task == "" || syntax_pair.second.second == task)
212  syntax.emplace_back(syntax_pair.second.first);
213 
214  return syntax;
215 }
216 
217 std::string
218 Syntax::isAssociated(const std::string & real_id, bool * is_parent) const
219 {
226  bool local_is_parent;
227  if (is_parent == nullptr)
228  is_parent = &local_is_parent; // Just so we don't have to keep checking below when we want to
229  // set the value
230  std::vector<std::string> real_elements, reg_elements;
231  std::string return_value;
232 
233  MooseUtils::tokenize(real_id, real_elements);
234 
235  *is_parent = false;
236  for (auto it = _syntax_to_actions.rbegin(); it != _syntax_to_actions.rend(); ++it)
237  {
238  std::string reg_id = it->first;
239  if (reg_id == real_id)
240  {
241  *is_parent = false;
242  return reg_id;
243  }
244  reg_elements.clear();
245  MooseUtils::tokenize(reg_id, reg_elements);
246  if (real_elements.size() <= reg_elements.size())
247  {
248  bool keep_going = true;
249  for (unsigned int j = 0; keep_going && j < real_elements.size(); ++j)
250  {
251  if (real_elements[j] != reg_elements[j] && reg_elements[j] != std::string("*"))
252  keep_going = false;
253  }
254  if (keep_going)
255  {
256  if (real_elements.size() < reg_elements.size() && !*is_parent)
257  {
258  // We found a parent, the longest parent in fact but we need to keep
259  // looking to make sure that the real thing isn't registered
260  *is_parent = true;
261  return_value = reg_id;
262  }
263  else if (real_elements.size() == reg_elements.size())
264  {
265  *is_parent = false;
266  return reg_id;
267  }
268  }
269  }
270  }
271 
272  if (*is_parent)
273  return return_value;
274  else
275  return "";
276 }
277 
278 std::pair<std::multimap<std::string, Syntax::ActionInfo>::const_iterator,
279  std::multimap<std::string, Syntax::ActionInfo>::const_iterator>
280 Syntax::getActions(const std::string & syntax) const
281 {
282  return _syntax_to_actions.equal_range(syntax);
283 }
284 
285 bool
286 Syntax::verifyMooseObjectTask(const std::string & base, const std::string & task) const
287 {
288  auto iters = _moose_systems_to_tasks.equal_range(base);
289 
290  for (const auto & task_it : as_range(iters))
291  if (task == task_it.second)
292  return true;
293 
294  return false;
295 }
296 
297 void
298 Syntax::registerSyntaxType(const std::string & syntax, const std::string & type)
299 {
300  _associated_types.insert(std::make_pair(syntax, type));
301 }
302 
303 const std::multimap<std::string, std::string> &
305 {
306  return _associated_types;
307 }
308 
309 const std::multimap<std::string, Syntax::ActionInfo> &
311 {
312  return _syntax_to_actions;
313 }
314 
316 Syntax::getLineInfo(const std::string & syntax,
317  const std::string & action,
318  const std::string & task) const
319 {
320  return _syntax_to_line.getInfo(syntax, action, task);
321 }
bool hasTask(const std::string &task) const
Returns a Boolean indicating whether or not a task is registered with the syntax object.
Definition: Syntax.C:112
const std::multimap< std::string, std::string > & getAssociatedTypes() const
Get a multimap of registered associations of syntax with type.
Definition: Syntax.C:304
std::pair< std::multimap< std::string, ActionInfo >::const_iterator, std::multimap< std::string, ActionInfo >::const_iterator > getActions(const std::string &syntax) const
Returns a pair of multimap iterators to all the ActionInfo objects associated with a given piece of s...
Definition: Syntax.C:280
void deleteDependenciesOfKey(const T &key)
Removes dependencies of the given key.
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:532
const std::vector< std::vector< T > > & getSortedValuesSets()
Returns a vector of sets that represent dependency resolved values.
const std::multimap< std::string, ActionInfo > & getAssociatedActions() const
Return all Syntax to Action associations.
Definition: Syntax.C:310
bool _actions_to_syntax_valid
Boolean indicating whether the _actions_to_syntax map is built and valid and synced.
Definition: Syntax.h:223
void deleteTaskDependencies(const std::string &task)
Deletes or removes the dependencies that this task depends on.
Definition: Syntax.C:85
void addDependency(const std::string &task, const std::string &pre_req)
Method to insert a new task in the list of existing tasks at the specified location.
Definition: Syntax.C:55
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
FileLineInfoMap _syntax_to_line
Definition: Syntax.h:228
DependencyResolver< std::string > _tasks
The dependency resolver.
Definition: Syntax.h:210
void registerTaskName(const std::string &task, bool should_auto_build=false)
Method to register a new task.
Definition: Syntax.C:20
std::multimap< std::string, std::pair< std::string, std::string > > _actions_to_syntax
The ActionInfo (Action+task) to syntax associations (built only when needed) Action -> (Syntax...
Definition: Syntax.h:217
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:316
void registerActionSyntax(const std::string &action, const std::string &syntax, const std::string &task="", const std::string &file="", int line=-1)
Registration function for associating Moose Actions with syntax.
Definition: Syntax.C:134
void clear()
Clear Items from the resolver.
const std::vector< std::string > & getSortedTask()
Get a list of serialized tasks in a correct dependency order.
Definition: Syntax.C:100
bool isActionRequired(const std::string &task) const
Returns a Boolean indicating whether the specified task is required.
Definition: Syntax.C:118
FileLineInfo getInfo(const std::string &key0) const
Get file/line info for a key.
Definition: FileLineInfo.C:76
void addInfo(const std::string &key0, const std::string &file, int line)
Associate a key with file/line info.
Definition: FileLineInfo.C:35
std::vector< std::string > getSyntaxByAction(const std::string &action, const std::string &task="")
Retrieve the syntax associated with the passed in action type string.
Definition: Syntax.C:192
const std::vector< T > & getSortedValues()
This function also returns dependency resolved values but with a simpler single vector interface...
bool shouldAutoBuild(const std::string &task) const
Returns a Boolean indicating whether MOOSE should attempt to automatically create an Action to satisf...
Definition: Syntax.C:125
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:186
std::string isAssociated(const std::string &real_id, bool *is_parent) const
Method for determining whether a piece of syntax is associated with an Action TODO: I need a better n...
Definition: Syntax.C:218
std::map< std::string, bool > _registered_tasks
The list of registered tasks and a flag indicating whether or not they should be auto-built.
Definition: Syntax.h:204
const std::vector< std::vector< std::string > > & getSortedTaskSet()
Get a list of serialized tasks in a correct dependency order.
Definition: Syntax.C:106
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:236
X_global swap(X_sys)
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:151
void clearTaskDependencies()
Clears all tasks from the system object.
Definition: Syntax.C:94
Holds file and line information.
Definition: FileLineInfo.h:18
void addDependencySets(const std::string &action_sets)
Adds all dependencies in a single call.
Definition: Syntax.C:64
Syntax()
Definition: Syntax.C:17
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:207
void registerSyntaxType(const std::string &syntax, const std::string &type)
Register a type with a block.
Definition: Syntax.C:298
std::map< std::string, std::string > _deprecated_syntax
The list of deprecated syntax items and the associated deprecated message.
Definition: Syntax.h:226
bool verifyMooseObjectTask(const std::string &base, const std::string &task) const
Returns a Boolean indicating whether a task is associated with on of the MOOSE pluggable systems (BAS...
Definition: Syntax.C:286
std::multimap< std::string, ActionInfo > _syntax_to_actions
The syntax object to ActionInfo (Action+task) associations.
Definition: Syntax.h:213
void appendTaskName(const std::string &task, const std::string &moose_object_type)
Method to associate another "allowed" pluggable MOOSE system to an existing registered task...
Definition: Syntax.C:46
void addItem(const T &value)
Add an independent item to the set.
std::string deprecatedActionSyntaxMessage(const std::string syntax)
Returns the deprecation message for a given syntax that has been deprecated by deprecateActionSyntax...
Definition: Syntax.C:175
void deprecateActionSyntax(const std::string &syntax)
This method deprecates previously registered syntax.
Definition: Syntax.C:162
std::multimap< std::string, std::string > _associated_types
Syntax/Type association.
Definition: Syntax.h:220
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"...