www.mooseframework.org
ActionFactory.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 // MOOSE includes
11 #include "ActionFactory.h"
12 #include "MooseApp.h"
13 
15 
17 
18 void
19 ActionFactory::reg(const std::string & name,
20  const std::string & task,
21  buildActionPtr obj_builder,
22  paramsActionPtr ref_params,
23  const std::string & file,
24  int line)
25 {
26  auto key = std::make_pair(name, task);
27  if (_current_objs.count(key) > 0)
28  return;
29  _current_objs.insert(key);
30 
31  BuildInfo build_info;
32  build_info._build_pointer = obj_builder;
33  build_info._params_pointer = ref_params;
34  build_info._task = task;
35  _name_to_build_info.insert(std::make_pair(name, build_info));
36  _task_to_action_map.insert(std::make_pair(task, name));
37  _name_to_line.addInfo(name, task, file, line);
38 }
39 
40 std::shared_ptr<Action>
41 ActionFactory::create(const std::string & action,
42  const std::string & action_name,
43  InputParameters parameters)
44 {
45  parameters.addPrivateParam("_moose_app", &_app);
46  parameters.addPrivateParam("action_type", action);
47  std::pair<ActionFactory::iterator, ActionFactory::iterator> iters;
48 
49  // Check to make sure that all required parameters are supplied
50  parameters.checkParams(action_name);
51 
52  iters = _name_to_build_info.equal_range(action);
53  BuildInfo * build_info = &(iters.first->second);
54  if (!build_info)
55  mooseError(
56  std::string("Unable to find buildable Action from supplied InputParameters Object for ") +
57  action_name);
58 
59  // Add the name to the parameters and create the object
60  parameters.set<std::string>("_action_name") = action_name;
61  std::shared_ptr<Action> action_obj = (*build_info->_build_pointer)(parameters);
62 
63  if (parameters.get<std::string>("task") == "")
64  action_obj->appendTask(build_info->_task);
65 
66  return action_obj;
67 }
68 
70 ActionFactory::getValidParams(const std::string & name)
71 {
78 
79  if (iter == _name_to_build_info.end())
80  mooseError(std::string("A '") + name + "' is not a registered Action\n\n");
81 
82  InputParameters params = (iter->second._params_pointer)();
83  params.addPrivateParam("_moose_app", &_app);
85 
86  return params;
87 }
88 
89 std::string
90 ActionFactory::getTaskName(const std::string & action)
91 {
92  // We are returning only the first found instance here
93  std::multimap<std::string, BuildInfo>::iterator iter = _name_to_build_info.find(action);
94 
95  if (iter != _name_to_build_info.end())
96  return iter->second._task;
97  else
98  return "";
99 }
100 
103 {
104  return _name_to_build_info.begin();
105 }
106 
109 {
110  return _name_to_build_info.begin();
111 }
112 
115 {
116  return _name_to_build_info.end();
117 }
118 
121 {
122  return _name_to_build_info.end();
123 }
124 
125 std::pair<std::multimap<std::string, std::string>::const_iterator,
126  std::multimap<std::string, std::string>::const_iterator>
127 ActionFactory::getActionsByTask(const std::string & task) const
128 {
129  return _task_to_action_map.equal_range(task);
130 }
131 
132 std::set<std::string>
133 ActionFactory::getTasksByAction(const std::string & action) const
134 {
135  std::set<std::string> tasks;
136 
137  std::pair<std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator,
138  std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator>
139  iters = _name_to_build_info.equal_range(action);
140  for (std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator it = iters.first;
141  it != iters.second;
142  ++it)
143  tasks.insert(it->second._task);
144 
145  return tasks;
146 }
148 ActionFactory::getLineInfo(const std::string & name, const std::string & task) const
149 {
150  return _name_to_line.getInfo(name, task);
151 }
void reg(const std::string &name, const std::string &task, const std::string &file="", int line=-1)
Definition: ActionFactory.h:74
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:70
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
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::set< std::string > getTasksByAction(const std::string &action) const
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:59
Storage for action instances.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
FileLineInfo getLineInfo(const std::string &name, const std::string &task) const
Gets file and line information where an action was registered.
virtual ~ActionFactory()
Definition: ActionFactory.C:16
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::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:14
InputParameters(* paramsActionPtr)()
Typedef for validParams.
Definition: ActionFactory.h:49
std::set< std::pair< std::string, std::string > > _current_objs
set<objectname, task> used to track if an object previously added is being added again ...
std::multimap< std::string, BuildInfo >::const_iterator const_iterator
ActionWarehouse & actionWarehouse()
Definition: MooseApp.h:146
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:18
std::multimap< std::string, BuildInfo > _name_to_build_info
std::string getTaskName(const std::string &action)
Definition: ActionFactory.C:90
std::multimap< std::string, std::string > _task_to_action_map
paramsActionPtr _params_pointer
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters parameters)
Definition: ActionFactory.C:41
std::shared_ptr< Action >(*)(const InputParameters &parameters) buildActionPtr
Definition: Registry.h:171