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,
47  const std::string & moose_object_type,
48  bool deprecated)
49 {
50  if (_registered_tasks.find(task) == _registered_tasks.end())
51  mooseError("A ", task, " is not a registered task name.");
52 
53  if (!deprecated)
54  _moose_systems_to_tasks.insert(std::make_pair(moose_object_type, task));
55  else
56  _deprecated_list_moose_systems_to_tasks.insert(std::make_pair(moose_object_type, task));
57 }
58 
59 void
60 Syntax::addDependency(const std::string & task, const std::string & pre_req)
61 {
62  if (_registered_tasks.find(task) == _registered_tasks.end())
63  mooseError("A ", task, " is not a registered task name.");
64 
65  _tasks.insertDependency(task, pre_req);
66 }
67 
68 void
69 Syntax::addDependencySets(const std::string & action_sets)
70 {
71  std::vector<std::string> sets, prev_names, tasks;
72  MooseUtils::tokenize(action_sets, sets, 1, "()");
73 
74  for (unsigned int i = 0; i < sets.size(); ++i)
75  {
76  tasks.clear();
77  MooseUtils::tokenize(sets[i], tasks, 0, ", ");
78  for (unsigned int j = 0; j < tasks.size(); ++j)
79  {
80  // Each line should depend on each item in the previous line
81  for (unsigned int k = 0; k < prev_names.size(); ++k)
82  addDependency(tasks[j], prev_names[k]);
83  }
84  // Copy the the current items to the previous items for the next iteration
85  std::swap(tasks, prev_names);
86  }
87 }
88 
89 void
90 Syntax::deleteTaskDependencies(const std::string & task)
91 {
92  if (_registered_tasks.find(task) == _registered_tasks.end())
93  mooseError("A ", task, " is not a registered task name.");
94 
96 }
97 
98 void
100 {
101  _tasks.clear();
102 }
103 
104 const std::vector<std::string> &
106 {
107  try
108  {
109  return _tasks.getSortedValues();
110  }
112  {
113  const auto & cycle = e.getCyclicDependencies();
114  mooseError("Cyclic dependencies detected: ", MooseUtils::join(cycle, " <- "));
115  }
116 }
117 
118 const std::vector<std::vector<std::string>> &
120 {
121  return _tasks.getSortedValuesSets();
122 }
123 
124 bool
125 Syntax::hasTask(const std::string & task) const
126 {
127  return (_registered_tasks.find(task) != _registered_tasks.end());
128 }
129 
130 bool
131 Syntax::isActionRequired(const std::string & task) const
132 {
133  mooseDeprecated("Syntax::isActionRequired is deprecated, use shouldAutoBuild() instead");
134  return shouldAutoBuild(task);
135 }
136 
137 bool
138 Syntax::shouldAutoBuild(const std::string & task) const
139 {
140  auto map_pair = _registered_tasks.find(task);
141  mooseAssert(map_pair != _registered_tasks.end(), std::string("Unregistered task: ") + task);
142 
143  return map_pair->second;
144 }
145 
146 void
147 Syntax::registerActionSyntax(const std::string & action,
148  const std::string & syntax,
149  const std::string & task,
150  const std::string & file,
151  int line)
152 {
153  auto range = _syntax_to_actions.equal_range(syntax);
154  for (auto it = range.first; it != range.second; ++it)
155  if (it->second._action == action && it->second._task == task)
156  return;
157 
158  _syntax_to_actions.insert(std::make_pair(syntax, ActionInfo{action, task}));
159  _syntax_to_line.addInfo(syntax, action, task, file, line);
160  _actions_to_syntax_valid = false;
161 }
162 
163 void
164 Syntax::replaceActionSyntax(const std::string & action,
165  const std::string & syntax,
166  const std::string & task,
167  const std::string & file,
168  int line)
169 {
170  _syntax_to_actions.erase(syntax);
171  registerActionSyntax(action, syntax, task, file, line);
172 }
173 
174 void
175 Syntax::deprecateActionSyntax(const std::string & syntax)
176 {
177  const std::string message = "\"[" + syntax + "]\" is deprecated.";
178  deprecateActionSyntax(syntax, message);
179 }
180 
181 void
182 Syntax::deprecateActionSyntax(const std::string & syntax, const std::string & message)
183 {
184  _deprecated_syntax.insert(std::make_pair(syntax, message));
185 }
186 
187 std::string
188 Syntax::deprecatedActionSyntaxMessage(const std::string syntax)
189 {
190  auto it = _deprecated_syntax.find(syntax);
191 
192  if (it != _deprecated_syntax.end())
193  return it->second;
194  else
195  mooseError("The action syntax ", syntax, " is not deprecated");
196 }
197 
198 bool
199 Syntax::isDeprecatedSyntax(const std::string & syntax) const
200 {
201  return _deprecated_syntax.find(syntax) != _deprecated_syntax.end();
202 }
203 
204 std::vector<std::string>
205 Syntax::getSyntaxByAction(const std::string & action, const std::string & task)
206 {
207  // See if the reverse multimap has been built yet, if not build it now
209  {
210  std::transform(_syntax_to_actions.begin(),
211  _syntax_to_actions.end(),
212  std::inserter(_actions_to_syntax, _actions_to_syntax.begin()),
213  [](const std::pair<std::string, ActionInfo> pair) {
214  return std::make_pair(pair.second._action,
215  std::make_pair(pair.first, pair.second._task));
216  });
218  }
219 
220  std::vector<std::string> syntax;
221  auto it_pair = _actions_to_syntax.equal_range(action);
222  for (const auto & syntax_pair : as_range(it_pair))
223  // If task is blank, return all syntax, otherwise filter by task
224  if (task == "" || syntax_pair.second.second == task)
225  syntax.emplace_back(syntax_pair.second.first);
226 
227  return syntax;
228 }
229 
230 std::vector<std::string>
231 Syntax::getNonDeprecatedSyntaxByAction(const std::string & action, const std::string & task)
232 {
233  auto syntaxes = getSyntaxByAction(action, task);
234  for (auto syntax_it = begin(syntaxes); syntax_it != end(syntaxes);)
235  {
236  if (isDeprecatedSyntax(*syntax_it))
237  syntax_it = syntaxes.erase(syntax_it);
238  else
239  ++syntax_it;
240  }
241  return syntaxes;
242 }
243 
244 std::string
245 Syntax::isAssociated(const std::string & real_id,
246  bool * is_parent,
247  const std::map<std::string, std::set<std::string>> & alt_map) const
248 {
249  // if non-empty alt_map was provided then traverse its syntax instead of _syntax_to_actions
250  std::set<std::string> syntax_to_traverse;
251  if (!alt_map.empty())
252  std::transform(alt_map.begin(),
253  alt_map.end(),
254  std::inserter(syntax_to_traverse, syntax_to_traverse.end()),
255  [](auto pair) { return pair.first; });
256  else
257  std::transform(_syntax_to_actions.begin(),
258  _syntax_to_actions.end(),
259  std::inserter(syntax_to_traverse, syntax_to_traverse.end()),
260  [](auto pair) { return pair.first; });
261 
268  bool local_is_parent;
269  if (is_parent == nullptr)
270  is_parent = &local_is_parent; // Just so we don't have to keep checking below when we want to
271  // set the value
272  std::vector<std::string> real_elements, reg_elements;
273  std::string return_value;
274 
275  MooseUtils::tokenize(real_id, real_elements);
276 
277  *is_parent = false;
278  for (auto it = syntax_to_traverse.rbegin(); it != syntax_to_traverse.rend(); ++it)
279  {
280  std::string reg_id = *it;
281  if (reg_id == real_id)
282  {
283  *is_parent = false;
284  return reg_id;
285  }
286  reg_elements.clear();
287  MooseUtils::tokenize(reg_id, reg_elements);
288  if (real_elements.size() <= reg_elements.size())
289  {
290  bool keep_going = true;
291  for (unsigned int j = 0; keep_going && j < real_elements.size(); ++j)
292  {
293  if (real_elements[j] != reg_elements[j] && reg_elements[j] != std::string("*"))
294  keep_going = false;
295  }
296  if (keep_going)
297  {
298  if (real_elements.size() < reg_elements.size() && !*is_parent)
299  {
300  // We found a parent, the longest parent in fact but we need to keep
301  // looking to make sure that the real thing isn't registered
302  *is_parent = true;
303  return_value = reg_id;
304  }
305  else if (real_elements.size() == reg_elements.size())
306  {
307  *is_parent = false;
308  return reg_id;
309  }
310  }
311  }
312  }
313 
314  if (*is_parent)
315  return return_value;
316  else
317  return "";
318 }
319 
320 std::pair<std::multimap<std::string, Syntax::ActionInfo>::const_iterator,
321  std::multimap<std::string, Syntax::ActionInfo>::const_iterator>
322 Syntax::getActions(const std::string & syntax) const
323 {
324  return _syntax_to_actions.equal_range(syntax);
325 }
326 
327 bool
328 Syntax::verifyMooseObjectTask(const std::string & base, const std::string & task) const
329 {
330  auto iters = _moose_systems_to_tasks.equal_range(base);
331 
332  for (const auto & task_it : as_range(iters))
333  if (task == task_it.second)
334  return true;
335 
336  iters = _deprecated_list_moose_systems_to_tasks.equal_range(base);
337  for (const auto & task_it : as_range(iters))
338  if (task == task_it.second)
339  {
340  std::string object_tasks = "";
341  for (const auto & other_task : as_range(_moose_systems_to_tasks.equal_range(base)))
342  object_tasks += (object_tasks == "" ? "" : " ") + other_task.second;
343 
345  "Adding objects from system '" + base + "' through task '" + task +
346  "' is deprecated. This object should only be added from task(s): " + object_tasks +
347  ". This is likely caused by adding objects in a block they no longer belong to. For "
348  "example, FunctorMaterials should no longer be added in the [Materials] block.");
349  return true;
350  }
351 
352  return false;
353 }
354 
355 void
356 Syntax::registerSyntaxType(const std::string & syntax, const std::string & type)
357 {
358  _associated_types.insert(std::make_pair(syntax, type));
359 }
360 
361 const std::multimap<std::string, std::string> &
363 {
364  return _associated_types;
365 }
366 
367 const std::multimap<std::string, Syntax::ActionInfo> &
369 {
370  return _syntax_to_actions;
371 }
372 
374 Syntax::getLineInfo(const std::string & syntax,
375  const std::string & action,
376  const std::string & task) const
377 {
378  return _syntax_to_line.getInfo(syntax, action, task);
379 }
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:125
void appendTaskName(const std::string &task, const std::string &moose_object_type, bool deprecated)
Method to associate another "allowed" pluggable MOOSE system to an existing registered task...
Definition: Syntax.C:46
void clear()
Clear Items from the resolver.
const std::multimap< std::string, std::string > & getAssociatedTypes() const
Get a multimap of registered associations of syntax with type.
Definition: Syntax.C:362
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:322
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:779
const std::multimap< std::string, ActionInfo > & getAssociatedActions() const
Return all Syntax to Action associations.
Definition: Syntax.C:368
bool _actions_to_syntax_valid
Boolean indicating whether the _actions_to_syntax map is built and valid and synced.
Definition: Syntax.h:240
void deleteTaskDependencies(const std::string &task)
Deletes or removes the dependencies that this task depends on.
Definition: Syntax.C:90
void addDependency(const std::string &task, const std::string &pre_req)
Definition: Syntax.C:60
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
FileLineInfoMap _syntax_to_line
Definition: Syntax.h:245
DependencyResolver< std::string > _tasks
The dependency resolver.
Definition: Syntax.h:227
void swap(std::vector< T > &data, const std::size_t idx0, const std::size_t idx1, const libMesh::Parallel::Communicator &comm)
Swap function for serial or distributed vector of data.
Definition: Shuffle.h:494
void registerTaskName(const std::string &task, bool should_auto_build=false)
Method to register a new task.
Definition: Syntax.C:20
const std::vector< T > & getSortedValues()
This function also returns dependency resolved values but with a simpler single vector interface...
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:234
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:374
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:147
const std::vector< std::string > & getSortedTask()
Get a list of serialized tasks in a correct dependency order.
Definition: Syntax.C:105
const std::vector< std::vector< T > > & getSortedValuesSets()
Returns a vector of sets that represent dependency resolved values.
bool isActionRequired(const std::string &task) const
Returns a Boolean indicating whether the specified task is required.
Definition: Syntax.C:131
std::multimap< std::string, std::string > _deprecated_list_moose_systems_to_tasks
A list of Moose system objects to tasks that are deprecated for these systems.
Definition: Syntax.h:224
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:205
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:138
void addItem(const T &value)
Add an independent item to the set.
std::vector< std::string > getNonDeprecatedSyntaxByAction(const std::string &action, const std::string &task="")
Retrieve the non-deprecated syntax associated with the passed in action type string.
Definition: Syntax.C:231
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:199
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:216
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
const std::vector< std::vector< std::string > > & getSortedTaskSet()
Get a list of serialized tasks in a correct dependency order.
Definition: Syntax.C:119
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:350
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:164
void clearTaskDependencies()
Clears all tasks from the system object.
Definition: Syntax.C:99
void deleteDependenciesOfKey(const T &key)
Removes dependencies of the given key.
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:69
std::string isAssociated(const std::string &real_id, bool *is_parent, const std::map< std::string, std::set< std::string >> &alt_map={}) const
Method for determining whether a piece of syntax is associated with an Action an optional syntax map ...
Definition: Syntax.C:245
Syntax()
Definition: Syntax.C:17
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:219
const std::vector< T > & getCyclicDependencies() const
void registerSyntaxType(const std::string &syntax, const std::string &type)
Register a type with a block.
Definition: Syntax.C:356
std::map< std::string, std::string > _deprecated_syntax
The list of deprecated syntax items and the associated deprecated message.
Definition: Syntax.h:243
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:328
std::multimap< std::string, ActionInfo > _syntax_to_actions
The syntax object to ActionInfo (Action+task) associations.
Definition: Syntax.h:230
std::string deprecatedActionSyntaxMessage(const std::string syntax)
Returns the deprecation message for a given syntax that has been deprecated by deprecateActionSyntax...
Definition: Syntax.C:188
void deprecateActionSyntax(const std::string &syntax)
This method deprecates previously registered syntax.
Definition: Syntax.C:175
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"...
std::multimap< std::string, std::string > _associated_types
Syntax/Type association.
Definition: Syntax.h:237
std::string join(const T &strings, const std::string &delimiter)
Python like join function for strings.
Definition: MooseUtils.h:130