www.mooseframework.org
ActionFactory.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 // MOOSE includes
16 #include "ActionFactory.h"
17 #include "MooseApp.h"
18 
19 unsigned int ActionFactory::_unique_id = 0;
20 
22 
24 
25 std::shared_ptr<Action>
26 ActionFactory::create(const std::string & action,
27  const std::string & action_name,
28  InputParameters parameters)
29 {
30  parameters.addPrivateParam("_moose_app", &_app);
31  parameters.addPrivateParam("action_type", action);
32  std::pair<ActionFactory::iterator, ActionFactory::iterator> iters;
33  BuildInfo * build_info = NULL;
34 
35  // Check to make sure that all required parameters are supplied
36  parameters.checkParams(action_name);
37 
38  iters = _name_to_build_info.equal_range(action);
39 
40  // Find the Action that matches the one we have registered based on unique_id
41  unsigned short count = 0;
42  for (ActionFactory::iterator it = iters.first; it != iters.second; ++it)
43  {
44  ++count;
45  if (parameters.have_parameter<unsigned int>("unique_id") &&
46  it->second._unique_id == parameters.get<unsigned int>("unique_id"))
47  {
48  build_info = &(it->second);
49  break;
50  }
51  }
52  // For backwards compatibility - If there is only one Action registered but it doesn't contain a
53  // unique_id that
54  // matches, then surely it must still be the correct one
55  if (count == 1 && !build_info)
56  build_info = &(iters.first->second);
57 
58  if (!build_info)
59  mooseError(
60  std::string("Unable to find buildable Action from supplied InputParameters Object for ") +
61  action_name);
62 
63  // Add the name to the parameters and create the object
64  parameters.set<std::string>("_action_name") = action_name;
65  std::shared_ptr<Action> action_obj = (*build_info->_build_pointer)(parameters);
66 
67  if (parameters.get<std::string>("task") == "")
68  action_obj->appendTask(build_info->_task);
69 
70  return action_obj;
71 }
72 
74 ActionFactory::getValidParams(const std::string & name)
75 {
82 
83  if (iter == _name_to_build_info.end())
84  mooseError(std::string("A '") + name + "' is not a registered Action\n\n");
85 
86  InputParameters params = (iter->second._params_pointer)();
87  params.addPrivateParam<unsigned int>("unique_id", iter->second._unique_id);
88  params.addPrivateParam("_moose_app", &_app);
90 
91  return params;
92 }
93 
94 std::string
95 ActionFactory::getTaskName(const std::string & action)
96 {
97  // We are returning only the first found instance here
98  std::multimap<std::string, BuildInfo>::iterator iter = _name_to_build_info.find(action);
99 
100  if (iter != _name_to_build_info.end())
101  return iter->second._task;
102  else
103  return "";
104 }
105 
108 {
109  return _name_to_build_info.begin();
110 }
111 
114 {
115  return _name_to_build_info.begin();
116 }
117 
120 {
121  return _name_to_build_info.end();
122 }
123 
126 {
127  return _name_to_build_info.end();
128 }
129 
130 std::pair<std::multimap<std::string, std::string>::const_iterator,
131  std::multimap<std::string, std::string>::const_iterator>
132 ActionFactory::getActionsByTask(const std::string & task) const
133 {
134  return _task_to_action_map.equal_range(task);
135 }
136 
137 std::set<std::string>
138 ActionFactory::getTasksByAction(const std::string & action) const
139 {
140  std::set<std::string> tasks;
141 
142  std::pair<std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator,
143  std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator>
144  iters = _name_to_build_info.equal_range(action);
145  for (std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator it = iters.first;
146  it != iters.second;
147  ++it)
148  tasks.insert(it->second._task);
149 
150  return tasks;
151 }
153 ActionFactory::getLineInfo(const std::string & name, const std::string & task) const
154 {
155  return _name_to_line.getInfo(name, task);
156 }
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
InputParameters getValidParams(const std::string &name)
Definition: ActionFactory.C:74
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.
iterator end()
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...
virtual ~ActionFactory()
Definition: ActionFactory.C:23
std::multimap< std::string, BuildInfo >::iterator iterator
Typedef for registered Action iterator.
buildActionPtr _build_pointer
MooseApp & _app
FileLineInfoMap _name_to_line
iterator begin()
ActionFactory(MooseApp &app)
Definition: ActionFactory.C:21
std::multimap< std::string, BuildInfo >::const_iterator const_iterator
ActionWarehouse & actionWarehouse()
Definition: MooseApp.h:130
void checkParams(const std::string &parsing_syntax)
This function checks parameters stored in the object to make sure they are in the correct state as th...
Holds file and line information.
Definition: FileLineInfo.h:24
std::multimap< std::string, BuildInfo > _name_to_build_info
std::string getTaskName(const std::string &action)
Definition: ActionFactory.C:95
std::multimap< std::string, std::string > _task_to_action_map
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > getActionsByTask(const std::string &task) const
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters parameters)
Definition: ActionFactory.C:26
static unsigned int _unique_id
Unique ID for identifying multiple registrations.
FileLineInfo getLineInfo(const std::string &name, const std::string &task) const
Gets file and line information where an action was registered.
std::set< std::string > getTasksByAction(const std::string &action) const
FileLineInfo getInfo(const std::string &key0) const
Get file/line info for a key.
Definition: FileLineInfo.C:81