https://mooseframework.inl.gov
ActionFactory.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 #include "Parser.h"
16 
18 
20 
21 void
22 ActionFactory::reg(std::shared_ptr<RegistryEntryBase> obj)
23 {
24  const std::string & name = obj->_classname;
25  const std::string & task = obj->_name;
26 
27  auto key = std::make_pair(name, task);
28  if (_current_objs.count(key) > 0)
29  return;
30  _current_objs.insert(key);
31 
32  BuildInfo build_info{obj, task};
33  _name_to_build_info.insert(std::make_pair(name, build_info));
34  _task_to_action_map.insert(std::make_pair(task, name));
35  _tasks.insert(task);
36  _name_to_line.addInfo(name, task, obj->_file, obj->_line);
37 }
38 
39 std::shared_ptr<Action>
40 ActionFactory::create(const std::string & action,
41  const std::string & full_action_name,
42  InputParameters & incoming_parser_params)
43 {
44  std::string action_name = MooseUtils::shortName(full_action_name);
45  incoming_parser_params.addPrivateParam(MooseBase::app_param, &_app);
46  incoming_parser_params.set<std::string>(MooseBase::type_param) = action;
47 
48  std::pair<ActionFactory::iterator, ActionFactory::iterator> iters;
49 
50  const std::string unique_action_name =
51  action + incoming_parser_params.get<std::string>("task") + full_action_name;
52 
53  // Create the actual parameters object that the object will reference
55  unique_action_name, incoming_parser_params, 0, {});
56 
57  if (!action_params.getHitNode())
58  {
59  // If we currently are in an action, that means that we're creating an
60  // action from within an action. Associate the action creating this one
61  // with the new action's parameters so that errors can be associated with it
62  if (const auto hit_node = _app.getCurrentActionHitNode())
63  action_params.setHitNode(*hit_node, {});
64  // Don't have one, so just use the root
65  else
66  action_params.setHitNode(_app.parser().getRoot(), {});
67  }
68 
69  action_params.set<std::string>(Action::unique_action_name_param) = unique_action_name;
70  action_params.set<std::string>(MooseBase::name_param) = action_name;
71 
72  // Check and finalize the parameters
73  action_params.finalize(action_name);
74 
75  iters = _name_to_build_info.equal_range(action);
76  BuildInfo * build_info = &(iters.first->second);
77  if (!build_info)
78  mooseError(
79  std::string("Unable to find buildable Action from supplied InputParameters Object for ") +
80  action_name);
81 
82  // Create the object
83  _currently_constructing.push_back(&action_params);
84  std::shared_ptr<Action> action_obj = build_info->_obj_pointer->buildAction(action_params);
85  _currently_constructing.pop_back();
86 
87  if (action_params.get<std::string>("task") == "")
88  action_obj->appendTask(build_info->_task);
89 
90  return action_obj;
91 }
92 
94 ActionFactory::getValidParams(const std::string & name)
95 {
102 
103  if (iter == _name_to_build_info.end())
104  mooseError(std::string("A '") + name + "' is not a registered Action\n\n");
105 
106  InputParameters params = iter->second._obj_pointer->buildParameters();
109 
110  return params;
111 }
112 
113 std::string
114 ActionFactory::getTaskName(const std::string & action)
115 {
116  // We are returning only the first found instance here
117  std::multimap<std::string, BuildInfo>::iterator iter = _name_to_build_info.find(action);
118 
119  if (iter != _name_to_build_info.end())
120  return iter->second._task;
121  else
122  return "";
123 }
124 
127 {
128  return _name_to_build_info.begin();
129 }
130 
133 {
134  return _name_to_build_info.begin();
135 }
136 
139 {
140  return _name_to_build_info.end();
141 }
142 
145 {
146  return _name_to_build_info.end();
147 }
148 
149 std::pair<std::multimap<std::string, std::string>::const_iterator,
150  std::multimap<std::string, std::string>::const_iterator>
151 ActionFactory::getActionsByTask(const std::string & task) const
152 {
153  return _task_to_action_map.equal_range(task);
154 }
155 
156 std::set<std::string>
157 ActionFactory::getTasksByAction(const std::string & action) const
158 {
159  std::set<std::string> tasks;
160 
161  std::pair<std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator,
162  std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator>
163  iters = _name_to_build_info.equal_range(action);
164  for (std::multimap<std::string, ActionFactory::BuildInfo>::const_iterator it = iters.first;
165  it != iters.second;
166  ++it)
167  tasks.insert(it->second._task);
168 
169  return tasks;
170 }
171 
172 const InputParameters *
174 {
175  return _currently_constructing.size() ? _currently_constructing.back() : nullptr;
176 }
177 
179 ActionFactory::getLineInfo(const std::string & name, const std::string & task) const
180 {
181  return _name_to_line.getInfo(name, task);
182 }
std::string name(const ElemQuality q)
static const std::string name_param
The name of the parameter that contains the object name.
Definition: MooseBase.h:55
static const std::string app_param
The name of the parameter that contains the MooseApp.
Definition: MooseBase.h:59
static const std::string type_param
The name of the parameter that contains the object type.
Definition: MooseBase.h:53
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:94
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
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:2900
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > getActionsByTask(const std::string &task) const
Returns begin and end iterators in a multimap from tasks to actions names.
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.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
void reg(std::shared_ptr< RegistryEntryBase > obj)
Definition: ActionFactory.C:22
iterator end()
hit::Node & getRoot()
Definition: Parser.C:449
Base class for MOOSE-based applications.
Definition: MooseApp.h:96
Storage for action instances.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static const std::string unique_action_name_param
The name of the parameter that contains the unique action name.
Definition: Action.h:37
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:19
std::set< std::string > _tasks
The registered tasks.
const hit::Node * getCurrentActionHitNode() const
Definition: MooseApp.C:3095
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:40
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:608
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:17
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
const Parser & parser() const
Definition: MooseApp.C:1987
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:204
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
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.