https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
21void
22ActionFactory::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
39std::shared_ptr<Action>
40ActionFactory::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)
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
94ActionFactory::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
113std::string
114ActionFactory::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
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
149std::pair<std::multimap<std::string, std::string>::const_iterator,
150 std::multimap<std::string, std::string>::const_iterator>
151ActionFactory::getActionsByTask(const std::string & task) const
152{
153 return _task_to_action_map.equal_range(task);
154}
155
156std::set<std::string>
157ActionFactory::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
172const InputParameters *
174{
175 return _currently_constructing.size() ? _currently_constructing.back() : nullptr;
176}
177
179ActionFactory::getLineInfo(const std::string & name, const std::string & task) const
180{
181 return _name_to_line.getInfo(name, task);
182}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
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
iterator end()
std::string getTaskName(const std::string &action)
void reg(std::shared_ptr< RegistryEntryBase > obj)
std::set< std::string > getTasksByAction(const std::string &action) const
virtual ~ActionFactory()
FileLineInfoMap _name_to_line
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters &parameters)
std::multimap< std::string, BuildInfo >::const_iterator const_iterator
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.
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 > _tasks
The registered tasks.
std::vector< const InputParameters * > _currently_constructing
The object's parameters that are currently being constructed (if any).
std::multimap< std::string, BuildInfo > _name_to_build_info
InputParameters getValidParams(const std::string &name)
std::multimap< std::string, BuildInfo >::iterator iterator
Typedef for registered Action iterator.
iterator begin()
ActionFactory(MooseApp &app)
std::multimap< std::string, std::string > _task_to_action_map
const InputParameters * currentlyConstructing() const
MooseApp & _app
Storage for action instances.
static const std::string unique_action_name_param
The name of the parameter that contains the unique action name.
Definition Action.h:41
void addInfo(const std::string &key0, const std::string &file, int line)
Associate a key with file/line info.
FileLineInfo getInfo(const std::string &key0) const
Get file/line info for a key.
Holds file and line information.
InputParameters & addInputParameters(const std::string &name, const InputParameters &parameters, THREAD_ID tid, const AddRemoveParamsKey)
Method for adding a new InputParameters object.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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...
void setHitNode(const std::string &param, const hit::Node &node, const SetParamHitNodeKey)
Sets the hit node associated with the parameter param to node.
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.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
Base class for MOOSE-based applications.
Definition MooseApp.h:110
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition MooseApp.h:217
const Parser & parser() const
Definition MooseApp.C:1879
const hit::Node * getCurrentActionHitNode() const
Definition MooseApp.C:3062
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition MooseApp.C:2867
static const std::string name_param
The name of the parameter that contains the object name.
Definition MooseBase.h:55
static const std::string type_param
The name of the parameter that contains the object type.
Definition MooseBase.h:53
static const std::string app_param
The name of the parameter that contains the MooseApp.
Definition MooseBase.h:59
hit::Node & getRoot()
Definition Parser.C:525
std::string shortName(const std::string &name)
Definition MooseUtils.C:623