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"
14 #include "MooseObjectAction.h"
15 
17 
19 
20 void
21 ActionFactory::reg(std::shared_ptr<RegistryEntryBase> obj)
22 {
23  const std::string & name = obj->_classname;
24  const std::string & task = obj->_name;
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{obj, task};
32  _name_to_build_info.insert(std::make_pair(name, build_info));
33  _task_to_action_map.insert(std::make_pair(task, name));
34  _tasks.insert(task);
35  _name_to_line.addInfo(name, task, obj->_file, obj->_line);
36 }
37 
38 std::shared_ptr<Action>
39 ActionFactory::create(const std::string & action,
40  const std::string & full_action_name,
41  InputParameters & incoming_parser_params)
42 {
43  std::string action_name = MooseUtils::shortName(full_action_name);
44  incoming_parser_params.addPrivateParam("_moose_app", &_app);
45  incoming_parser_params.addPrivateParam("action_type", action);
46  std::pair<ActionFactory::iterator, ActionFactory::iterator> iters;
47 
48  std::string unique_action_name =
49  action + incoming_parser_params.get<std::string>("task") + full_action_name;
50  // Create the actual parameters object that the object will reference
52  unique_action_name, incoming_parser_params, 0, {});
53 
54  if (!action_params.getHitNode())
55  {
56  // If we currently are in an action, that means that we're creating an
57  // action from within an action. Associate the action creating this one
58  // with the new action's parameters so that errors can be associated with it
59  if (const auto hit_node = _app.getCurrentActionHitNode())
60  action_params.setHitNode(*hit_node, {});
61  // Don't have one, so just use the root
62  else
63  action_params.setHitNode(*_app.parser().root(), {});
64  }
65 
66  // Check and finalize the parameters
67  action_params.finalize(action_name);
68 
69  iters = _name_to_build_info.equal_range(action);
70  BuildInfo * build_info = &(iters.first->second);
71  if (!build_info)
72  mooseError(
73  std::string("Unable to find buildable Action from supplied InputParameters Object for ") +
74  action_name);
75 
76  // Add the name to the parameters
77  action_params.set<std::string>("_action_name") = action_name;
78  action_params.set<std::string>("_unique_action_name") = unique_action_name;
79 
80  // Create the object
81  _currently_constructing.push_back(&action_params);
82  std::shared_ptr<Action> action_obj = build_info->_obj_pointer->buildAction(action_params);
83  _currently_constructing.pop_back();
84 
85  if (action_params.get<std::string>("task") == "")
86  action_obj->appendTask(build_info->_task);
87 
88  return action_obj;
89 }
90 
92 ActionFactory::getValidParams(const std::string & name)
93 {
100 
101  if (iter == _name_to_build_info.end())
102  mooseError(std::string("A '") + name + "' is not a registered Action\n\n");
103 
104  InputParameters params = iter->second._obj_pointer->buildParameters();
105  params.addPrivateParam("_moose_app", &_app);
107 
108  return params;
109 }
110 
111 std::string
112 ActionFactory::getTaskName(const std::string & action)
113 {
114  // We are returning only the first found instance here
115  std::multimap<std::string, BuildInfo>::iterator iter = _name_to_build_info.find(action);
116 
117  if (iter != _name_to_build_info.end())
118  return iter->second._task;
119  else
120  return "";
121 }
122 
125 {
126  return _name_to_build_info.begin();
127 }
128 
131 {
132  return _name_to_build_info.begin();
133 }
134 
137 {
138  return _name_to_build_info.end();
139 }
140 
143 {
144  return _name_to_build_info.end();
145 }
146 
147 std::pair<std::multimap<std::string, std::string>::const_iterator,
148  std::multimap<std::string, std::string>::const_iterator>
149 ActionFactory::getActionsByTask(const std::string & task) const
150 {
151  return _task_to_action_map.equal_range(task);
152 }
153 
154 std::set<std::string>
155 ActionFactory::getTasksByAction(const std::string & action) const
156 {
157  std::set<std::string> tasks;
158 
159  std::pair<std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator,
160  std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator>
161  iters = _name_to_build_info.equal_range(action);
162  for (std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator it = iters.first;
163  it != iters.second;
164  ++it)
165  tasks.insert(it->second._task);
166 
167  return tasks;
168 }
169 
170 const InputParameters *
172 {
173  return _currently_constructing.size() ? _currently_constructing.back() : nullptr;
174 }
175 
177 ActionFactory::getLineInfo(const std::string & name, const std::string & task) const
178 {
179  return _name_to_line.getInfo(name, task);
180 }
std::string name(const ElemQuality q)
Parser & parser()
Definition: MooseApp.C:1346
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:92
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition: MooseApp.C:2218
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
InputParameters & addInputParameters(const std::string &name, const InputParameters &parameters, THREAD_ID tid, const AddRemoveParamsKey)
Method for adding a new InputParameters object.
void reg(std::shared_ptr< RegistryEntryBase > obj)
Definition: ActionFactory.C:21
iterator end()
Base class for MOOSE-based applications.
Definition: MooseApp.h:73
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:18
std::set< std::string > _tasks
The registered tasks.
const hit::Node * getCurrentActionHitNode() const
Definition: MooseApp.C:2413
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::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters &parameters)
Definition: ActionFactory.C:39
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:598
std::multimap< std::string, BuildInfo >::iterator iterator
Typedef for registered Action iterator.
Definition: ActionFactory.h:83
MooseApp & _app
FileLineInfoMap _name_to_line
std::shared_ptr< RegistryEntryBase > _obj_pointer
Definition: ActionFactory.h:78
iterator begin()
ActionFactory(MooseApp &app)
Definition: ActionFactory.C:16
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
Definition: ActionFactory.h:84
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:195
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
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)
std::multimap< std::string, std::string > _task_to_action_map
hit::Node * root()
Definition: Parser.h:146
const InputParameters * currentlyConstructing() const
void setHitNode(const std::string &param, const hit::Node &node, const SetParamHitNodeKey)
Sets the hit node associated with the parameter param to node.