www.mooseframework.org
ActionWarehouse.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 "ActionWarehouse.h"
16 #include "ActionFactory.h"
17 #include "Parser.h"
18 #include "MooseObjectAction.h"
19 #include "InputFileFormatter.h"
20 #include "InputParameters.h"
21 #include "MooseMesh.h"
22 #include "AddVariableAction.h"
23 #include "AddAuxVariableAction.h"
24 #include "XTermConstants.h"
25 #include "InfixIterator.h"
26 #include "FEProblem.h"
27 
30  _app(app),
31  _syntax(syntax),
32  _action_factory(factory),
33  _generator_valid(false),
34  _show_actions(false),
35  _show_parser(false)
36 {
37 }
38 
40 
41 void
43 {
45  for (const auto & name : _ordered_names)
47 }
48 
49 void
51 {
52  for (auto & ptr : _all_ptrs)
53  ptr.reset();
54 
55  _action_blocks.clear();
56  _generator_valid = false;
57 
58  // Due to the way ActionWarehouse is cleaned up (see MooseApp's
59  // destructor) we must guarantee that ActionWarehouse::clear()
60  // releases all the resources which have to be released _before_ the
61  // _comm object owned by the MooseApp is destroyed.
62  _problem.reset();
63  _displaced_mesh.reset();
64  _mesh.reset();
65 }
66 
67 void
68 ActionWarehouse::addActionBlock(std::shared_ptr<Action> action)
69 {
80  std::string registered_identifier =
81  action->parameters().get<std::string>("registered_identifier");
82  std::set<std::string> tasks;
83 
84  if (_show_parser)
85  Moose::err << COLOR_DEFAULT << "Parsing Syntax: " << COLOR_GREEN << action->name()
86  << '\n'
87  << COLOR_DEFAULT << "Building Action: " << COLOR_DEFAULT << action->type()
88  << '\n'
89  << COLOR_DEFAULT << "Registered Identifier: " << COLOR_GREEN << registered_identifier
90  << '\n'
91  << COLOR_DEFAULT << "Specific Task: " << COLOR_CYAN
92  << action->specificTaskName() << '\n';
93 
117  if (action->specificTaskName() != "") // Case 1
118  tasks.insert(action->specificTaskName());
119  else if (registered_identifier == "") // Case 2
120  {
121  std::set<std::string> local_tasks = action->getAllTasks();
122  mooseAssert(local_tasks.size() == 1, "More than one task inside of the " << action->name());
123  tasks.insert(*local_tasks.begin());
124  }
125  else // Case 3
126  tasks = _action_factory.getTasksByAction(action->type());
127 
128  // TODO: Now we need to weed out the double registrations!
129  for (const auto & task : tasks)
130  {
131  // Some error checking
132  if (!_syntax.hasTask(task))
133  mooseError("A(n) ", task, " is not a registered task");
134 
135  // Make sure that the ObjectAction task and Action task are consistent
136  // otherwise that means that is action was built by the wrong type
137  std::shared_ptr<MooseObjectAction> moa = std::dynamic_pointer_cast<MooseObjectAction>(action);
138  if (moa.get())
139  {
140  const InputParameters & mparams = moa->getObjectParams();
141 
142  if (mparams.have_parameter<std::string>("_moose_base"))
143  {
144  const std::string & base = mparams.get<std::string>("_moose_base");
145 
146  if (!_syntax.verifyMooseObjectTask(base, task))
147  mooseError("Task ", task, " is not registered to build ", base, " derived objects");
148  }
149  else
150  mooseError("Unable to locate registered base parameter for ", moa->getMooseObjectType());
151  }
152 
153  // Add the current task to current action
154  action->appendTask(task);
155 
156  if (_show_parser)
157  Moose::err << COLOR_YELLOW << "Adding Action: " << COLOR_DEFAULT << action->type()
158  << " (" << COLOR_YELLOW << task << COLOR_DEFAULT << ")\n";
159 
160  // Add it to the warehouse
161  _all_ptrs.push_back(action);
162  _action_blocks[task].push_back(action.get());
163  }
164 
165  if (_show_parser)
166  Moose::err << std::endl;
167 }
168 
171 {
172  return _action_blocks[task].begin();
173 }
174 
177 {
178  return _action_blocks[task].end();
179 }
180 
181 const std::list<Action *> &
182 ActionWarehouse::getActionListByName(const std::string & task) const
183 {
184  const auto it = _action_blocks.find(task);
185  if (it == _action_blocks.end())
186  mooseError("The task ", task, " does not exist.");
187 
188  return it->second;
189 }
190 
191 bool
192 ActionWarehouse::hasActions(const std::string & task) const
193 {
194  return _action_blocks.find(task) != _action_blocks.end();
195 }
196 
197 void
198 ActionWarehouse::buildBuildableActions(const std::string & task)
199 {
200  if (_syntax.isActionRequired(task) && _action_blocks[task].empty())
201  {
202  bool ret_value = false;
203  std::pair<std::multimap<std::string, std::string>::const_iterator,
204  std::multimap<std::string, std::string>::const_iterator>
205  range = _action_factory.getActionsByTask(task);
206  for (std::multimap<std::string, std::string>::const_iterator it = range.first;
207  it != range.second;
208  ++it)
209  {
210  InputParameters params = _action_factory.getValidParams(it->second);
211  params.set<ActionWarehouse *>("awh") = this;
212 
213  if (params.areAllRequiredParamsValid())
214  {
215  params.set<std::string>("registered_identifier") = "(AutoBuilt)";
216  params.set<std::string>("task") = task;
217  addActionBlock(_action_factory.create(it->second, "", params));
218  ret_value = true;
219  }
220  }
221 
222  if (!ret_value)
223  _unsatisfied_dependencies.insert(task);
224  }
225 }
226 
227 void
229 {
230  std::stringstream oss;
231  bool empty = true;
232 
233  for (const auto & udep : _unsatisfied_dependencies)
234  {
235  if (_action_blocks.find(udep) == _action_blocks.end())
236  {
237  if (empty)
238  empty = false;
239  else
240  oss << " ";
241  oss << udep;
242  }
243  }
244 
245  if (!empty)
246  mooseError(
247  std::string(
248  "The following unsatisfied actions where found while setting up the MOOSE problem:\n") +
249  oss.str() + "\n");
250 }
251 
252 void
254 {
264  std::ostringstream oss;
265 
266  const auto & ordered_names = _syntax.getSortedTaskSet();
267  for (const auto & task_vector : ordered_names)
268  {
269  oss << "[DBG][ACT] (" << COLOR_YELLOW;
270  std::copy(
271  task_vector.begin(), task_vector.end(), infix_ostream_iterator<std::string>(oss, ", "));
272  oss << COLOR_DEFAULT << ")\n";
273 
274  std::set<std::string> task_set(task_vector.begin(), task_vector.end());
275  for (const auto & task : task_set)
276  {
277  if (_action_blocks.find(task) == _action_blocks.end())
278  continue;
279 
280  for (const auto & act : _action_blocks.at(task))
281  {
282  // The Syntax of the Action if it exists
283  if (act->name() != "")
284  oss << "[DBG][ACT]\t" << COLOR_GREEN << act->name() << COLOR_DEFAULT << '\n';
285 
286  // The task sets
287  oss << "[DBG][ACT]\t" << act->type();
288  const std::set<std::string> tasks = act->getAllTasks();
289  if (tasks.size() > 1)
290  {
291  oss << " (";
292  // Break the current Action's tasks into 2 sets, those intersecting with current set and
293  // then the difference.
294  std::set<std::string> intersection, difference;
295  std::set_intersection(tasks.begin(),
296  tasks.end(),
297  task_set.begin(),
298  task_set.end(),
299  std::inserter(intersection, intersection.end()));
300  std::set_difference(tasks.begin(),
301  tasks.end(),
302  intersection.begin(),
303  intersection.end(),
304  std::inserter(difference, difference.end()));
305 
306  oss << COLOR_CYAN;
307  std::copy(intersection.begin(),
308  intersection.end(),
310  oss << COLOR_MAGENTA << (difference.empty() ? "" : ", ");
311  std::copy(
312  difference.begin(), difference.end(), infix_ostream_iterator<std::string>(oss, ", "));
313  oss << COLOR_DEFAULT << ")";
314  }
315  oss << '\n';
316  }
317  }
318  }
319 
320  if (_show_actions)
321  _console << oss.str() << std::endl;
322 }
323 
324 void
326 {
327  if (_show_actions)
328  {
329  _console << "[DBG][ACT] Action Dependency Sets:\n";
331 
332  _console << "\n[DBG][ACT] Executing actions:" << std::endl;
333  }
334 
335  for (const auto & task : _ordered_names)
337 }
338 
339 void
341 {
342  // Set the current task name
343  _current_task = task;
344 
345  for (ActionIterator act_iter = actionBlocksWithActionBegin(task);
346  act_iter != actionBlocksWithActionEnd(task);
347  ++act_iter)
348  {
349  if (_show_actions)
350  {
351  _console << "[DBG][ACT] "
352  << "TASK (" << COLOR_YELLOW << std::setw(24) << task << COLOR_DEFAULT << ") "
353  << "TYPE (" << COLOR_YELLOW << std::setw(32) << (*act_iter)->type() << COLOR_DEFAULT
354  << ") "
355  << "NAME (" << COLOR_YELLOW << std::setw(16) << (*act_iter)->name() << COLOR_DEFAULT
356  << ")" << std::endl;
357 
358  (*act_iter)->act();
359  }
360  else
361  (*act_iter)->act();
362  }
363 }
364 
365 void
367 {
368  InputFileFormatter tree(false);
369 
370  std::map<std::string, std::vector<Action *>>::iterator iter;
371 
372  std::vector<Action *> ordered_actions;
373  for (const auto & block : _action_blocks)
374  for (const auto & act : block.second)
375  ordered_actions.push_back(act);
376 
377  for (const auto & act : ordered_actions)
378  {
379  std::string name;
380  if (act->isParamValid("parser_syntax"))
381  name = act->getParam<std::string>("parser_syntax");
382  else
383  name = act->name();
384  const std::set<std::string> & tasks = act->getAllTasks();
385  mooseAssert(!tasks.empty(), "Task list is empty");
386 
387  bool is_parent;
388  if (_syntax.isAssociated(name, &is_parent) != "")
389  {
390  InputParameters params = act->parameters();
391 
392  // TODO: Do we need to insert more nodes for each task?
393  tree.insertNode(name, *tasks.begin(), true, &params);
394 
395  MooseObjectAction * moose_object_action = dynamic_cast<MooseObjectAction *>(act);
396  if (moose_object_action)
397  {
398  InputParameters obj_params = moose_object_action->getObjectParams();
399  tree.insertNode(name, *tasks.begin(), false, &obj_params);
400  }
401  }
402  }
403 
404  out << tree.print("");
405 }
406 
407 std::shared_ptr<FEProblem>
409 {
411  "ActionWarehouse::problem() is deprecated, please use ActionWarehouse::problemBase() \n");
412  return std::dynamic_pointer_cast<FEProblem>(_problem);
413 }
void checkUnsatisfiedActions() const
This method checks the actions stored in the warehouse against the list of required registered action...
std::list< Action * >::iterator ActionIterator
alias to hide implementation details
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:30
InputParameters getValidParams(const std::string &name)
Definition: ActionFactory.C:74
Syntax & _syntax
Reference to a "syntax" of actions.
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
const std::list< Action * > & getActionListByName(const std::string &task) const
Retrieve a constant list of Action pointers associated with the passed in task.
Base class for MOOSE-based applications.
Definition: MooseApp.h:58
Storage for action instances.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const std::vector< std::string > & getSortedTask()
Definition: Syntax.C:81
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
void printInputFile(std::ostream &out)
This method uses the Actions in the warehouse to reproduce the input file.
bool hasActions(const std::string &task) const
Check if Actions associated with passed in task exist.
bool areAllRequiredParamsValid() const
This method returns true if all of the parameters in this object are valid (i.e.
void executeActionsWithAction(const std::string &name)
This method executes only the actions in the warehouse that satisfy the task passed in...
std::vector< std::string > _ordered_names
The container that holds the sorted action names from the DependencyResolver.
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
InputParameters & getObjectParams()
Retreive the parameters of the object to be created by this action.
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:70
An inteface for the _console for outputting to the Console object.
const std::vector< std::vector< std::string > > & getSortedTaskSet()
Definition: Syntax.C:87
std::map< std::string, std::list< Action * > > _action_blocks
Pointers to the actual parsed input file blocks.
bool isActionRequired(const std::string &task)
Definition: Syntax.C:99
std::shared_ptr< MooseMesh > _displaced_mesh
Possible mesh for displaced problem.
bool _generator_valid
Flag to indicate whether or not there is an active iterator on this class.
void insertNode(std::string syntax, const std::string &action, bool is_action_params=true, InputParameters *params=NULL)
Definition: SyntaxTree.C:32
std::vector< std::shared_ptr< Action > > _all_ptrs
std::shared_ptr< FEProblemBase > _problem
Problem class.
std::string print(const std::string &search_string)
Definition: SyntaxTree.C:44
ActionWarehouse(MooseApp &app, Syntax &syntax, ActionFactory &factory)
std::shared_ptr< MooseMesh > _mesh
Mesh class.
bool empty() const
returns a Boolean indicating whether the warehouse is empty or not.
void build()
Builds all auto-buildable tasks.
Holding syntax for parsing input files.
Definition: Syntax.h:26
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters parameters)
Definition: ActionFactory.C:26
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > getActionsByTask(const std::string &task) const
void executeAllActions()
This method loops over all actions in the warehouse and executes them.
ActionIterator actionBlocksWithActionEnd(const std::string &task)
std::set< std::string > _unsatisfied_dependencies
Use to store the current list of unsatisfied dependencies.
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
std::shared_ptr< FEProblem > problem()
bool hasTask(const std::string &task)
Definition: Syntax.C:93
ActionIterator actionBlocksWithActionBegin(const std::string &task)
Iterators to the Actions in the warehouse.
void clear()
This method deletes all of the Actions in the warehouse.
void buildBuildableActions(const std::string &task)
This method auto-builds all Actions that needs to be built and adds them to ActionWarehouse.
void printActionDependencySets() const
This method is used only during debugging when show_actions is set to true.
std::set< std::string > getTasksByAction(const std::string &action) const
ActionFactory & _action_factory
The Factory that builds Actions.
This class produces produces a dump of the InputFileParameters that appears like the normal input fil...
std::string _current_task