https://mooseframework.inl.gov
Classes | Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
AppFactory Class Reference

Generic AppFactory class for building Application objects. More...

#include <AppFactory.h>

Classes

class  ClearAppParamsKey
 Class that is used as a parameter to clearAppParams() that allows only MooseApp to call clearAppParams(). More...
 

Public Member Functions

virtual ~AppFactory ()
 
template<typename T >
void reg (const std::string &name)
 Register a new object. More...
 
InputParameters getValidParams (const std::string &name)
 Get valid parameters for the object. More...
 
const InputParametersgetAppParams (const InputParameters &params) const
 
void clearAppParams (const InputParameters &params, const ClearAppParamsKey)
 Clears the stored parameters for the given application parameteres. More...
 
MooseAppPtr createShared (const std::string &app_type, const std::string &name, InputParameters parameters, MPI_Comm COMM_WORLD_IN)
 Build an application object (must be registered) More...
 
const auto & registeredObjects () const
 Returns a reference to the map from names to AppFactoryBuildInfo pointers. More...
 
bool isRegistered (const std::string &app_name) const
 Returns a Boolean indicating whether an application type has been registered. More...
 
std::size_t createdAppCount (const std::string &app_type) const
 
const AppFactoryBuildInfoMapregisteredObjectBuildInfos () const
 Returns the map of object name to a function pointer for building said object's input parameters. More...
 
 AppFactory (AppFactory const &)=delete
 Don't allow creation through copy/move construction or assignment. More...
 
Registryoperator= (AppFactory const &)=delete
 
 AppFactory (AppFactory &&)=delete
 
Registryoperator= (AppFactory &&)=delete
 

Static Public Member Functions

static AppFactoryinstance ()
 Get the instance of the AppFactory. More...
 
static InputParameters validParams ()
 
static MooseAppPtr createAppShared (int argc, char **argv, std::unique_ptr< Parser > parser)
 Helper function for creating a MooseApp from command-line arguments and a Parser. More...
 
static MooseAppPtr createAppShared (const std::string &default_app_type, int argc, char **argv, MPI_Comm comm_word=MPI_COMM_WORLD)
 Deprecated helper function for creating a MooseApp for Apps haven't adapted to the new Parser and Builder changes. More...
 

Protected Attributes

AppFactoryBuildInfoMap _name_to_build_info
 

Private Member Functions

 AppFactory ()
 
const InputParametersstoreAppParams (InputParameters &params)
 Stores the given parameters within _input_parameters for app construction. More...
 
std::size_t getAppParamsID (const InputParameters &params) const
 Get the ID for the InputParameters associated with an application, used in storing them in _input_parameters. More...
 
 FRIEND_TEST (::AppFactoryTest, manageAppParams)
 
 FRIEND_TEST (::AppFactoryTest, appCopyConstructParams)
 

Private Attributes

std::map< std::size_t, std::unique_ptr< InputParameters > > _input_parameters
 Storage of input parameters used in applications (ID (from getAppParamsID()) -> params) More...
 

Detailed Description

Generic AppFactory class for building Application objects.

Definition at line 62 of file AppFactory.h.

Constructor & Destructor Documentation

◆ ~AppFactory()

AppFactory::~AppFactory ( )
virtual

Definition at line 28 of file AppFactory.C.

28 {}

◆ AppFactory() [1/3]

AppFactory::AppFactory ( AppFactory const &  )
delete

Don't allow creation through copy/move construction or assignment.

◆ AppFactory() [2/3]

AppFactory::AppFactory ( AppFactory &&  )
delete

◆ AppFactory() [3/3]

AppFactory::AppFactory ( )
inlineprivate

Definition at line 184 of file AppFactory.h.

Referenced by instance().

184 {}

Member Function Documentation

◆ clearAppParams()

void AppFactory::clearAppParams ( const InputParameters params,
const ClearAppParamsKey   
)

Clears the stored parameters for the given application parameteres.

See getAppParams() for why this is needed.

Definition at line 49 of file AppFactory.C.

Referenced by MooseApp::~MooseApp().

50 {
51  const auto id = getAppParamsID(params);
52  if (const auto it = _input_parameters.find(id); it != _input_parameters.end())
53  _input_parameters.erase(it);
54  else
55  mooseError(
56  "AppFactory::clearAppParams(): Parameters for application with ID ", id, " not found");
57 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
std::map< std::size_t, std::unique_ptr< InputParameters > > _input_parameters
Storage of input parameters used in applications (ID (from getAppParamsID()) -> params) ...
Definition: AppFactory.h:212
std::size_t getAppParamsID(const InputParameters &params) const
Get the ID for the InputParameters associated with an application, used in storing them in _input_par...
Definition: AppFactory.C:193

◆ createAppShared() [1/2]

MooseAppPtr AppFactory::createAppShared ( int  argc,
char **  argv,
std::unique_ptr< Parser parser 
)
static

Helper function for creating a MooseApp from command-line arguments and a Parser.

The parser must be set and have an app type set.

Definition at line 60 of file AppFactory.C.

Referenced by Moose::createMooseApp().

61 {
62  mooseAssert(parser, "Not set");
63  mooseAssert(parser->getAppType().size(), "App type not set");
64  const std::string app_type = parser->getAppType();
65 
66  auto command_line = std::make_unique<CommandLine>(argc, argv);
67  command_line->parse();
68 
69  auto app_params = AppFactory::instance().getValidParams(parser->getAppType());
70  app_params.set<int>("_argc") = argc;
71  app_params.set<char **>("_argv") = argv;
72  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = std::move(command_line);
73  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
74 
75  return AppFactory::instance().createShared(app_type, "main", app_params, MPI_COMM_WORLD);
76 }
MooseAppPtr createShared(const std::string &app_type, const std::string &name, InputParameters parameters, MPI_Comm COMM_WORLD_IN)
Build an application object (must be registered)
Definition: AppFactory.C:127
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:18
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:31

◆ createAppShared() [2/2]

MooseAppPtr AppFactory::createAppShared ( const std::string &  default_app_type,
int  argc,
char **  argv,
MPI_Comm  comm_word = MPI_COMM_WORLD 
)
static

Deprecated helper function for creating a MooseApp for Apps haven't adapted to the new Parser and Builder changes.

This function needed to be removed after the new Parser and Builder merged

Definition at line 79 of file AppFactory.C.

83 {
84  mooseDeprecated("Please update your main.C to adapt new main function in MOOSE framework, "
85  "see'test/src/main.C in MOOSE as an example of moose::main()'. ");
86 
87  auto command_line_params = emptyInputParameters();
88  MooseApp::addInputParam(command_line_params);
89  MooseApp::addAppParam(command_line_params);
90 
91  {
92  CommandLine pre_command_line(argc, argv);
93  pre_command_line.parse();
94  pre_command_line.populateCommandLineParams(command_line_params);
95  }
96 
97  const auto & input_filenames = command_line_params.get<std::vector<std::string>>("input_file");
98  auto parser = std::make_unique<Parser>(input_filenames);
99  if (input_filenames.size())
100  parser->parse();
101 
102  std::string app_type = command_line_params.get<std::string>("app_to_run");
103  if (app_type.empty())
104  app_type = default_app_type;
105  else
106  mooseDeprecated("Please use [Application] block to specify application type, '--app <AppName>' "
107  "is deprecated and will be removed in a future release.");
108 
109  parser->setAppType(app_type);
110  auto app_params = AppFactory::instance().getValidParams(app_type);
111 
112  app_params.set<int>("_argc") = argc;
113  app_params.set<char **>("_argv") = argv;
114 
115  auto command_line = std::make_unique<CommandLine>(argc, argv);
116  command_line->parse();
117  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = std::move(command_line);
118 
119  // Take the front parser and add it to the parameters so that it can be retrieved in the
120  // Application
121  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
122 
123  return AppFactory::instance().createShared(app_type, "main", app_params, comm_world_in);
124 }
MooseAppPtr createShared(const std::string &app_type, const std::string &name, InputParameters parameters, MPI_Comm COMM_WORLD_IN)
Build an application object (must be registered)
Definition: AppFactory.C:127
static void addInputParam(InputParameters &params)
Definition: MooseApp.C:101
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
InputParameters emptyInputParameters()
This class wraps provides and tracks access to command line parameters.
Definition: CommandLine.h:29
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:353
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:18
static void addAppParam(InputParameters &params)
Definition: MooseApp.C:94
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:31

◆ createdAppCount()

std::size_t AppFactory::createdAppCount ( const std::string &  app_type) const
Returns
the amount of times the AppFactory created the named App-type

Definition at line 168 of file AppFactory.C.

169 {
170  // Error if the application type is not located
171  const auto it = _name_to_build_info.find(app_type);
172  if (it == _name_to_build_info.end())
173  mooseError("AppFactory::createdAppCount(): '", app_type, "' is not a registered app");
174 
175  return it->second->_app_creation_count;
176 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:180

◆ createShared()

MooseAppPtr AppFactory::createShared ( const std::string &  app_type,
const std::string &  name,
InputParameters  parameters,
MPI_Comm  COMM_WORLD_IN 
)

Build an application object (must be registered)

Parameters
app_typeType of the application being constructed
nameName for the object
parametersParameters this object should have
Returns
The created object

Definition at line 127 of file AppFactory.C.

Referenced by MultiApp::createApp(), createAppShared(), and MooseServer::parseDocumentForDiagnostics().

131 {
132  // Error if the application type is not located
133  const auto it = _name_to_build_info.find(app_type);
134  if (it == _name_to_build_info.end())
135  mooseError("Object '" + app_type + "' was not registered.");
136  auto & build_info = it->second;
137 
138  // Take the app_type and add it to the parameters so that it can be retrieved in the Application
139  parameters.set<std::string>("_type") = app_type;
140 
141  auto comm = std::make_shared<Parallel::Communicator>(comm_world_in);
142 
143  parameters.set<std::shared_ptr<Parallel::Communicator>>("_comm") = comm;
144  parameters.set<std::string>("_app_name") = name;
145 
146  if (!parameters.isParamValid("_command_line"))
147  mooseError("Valid CommandLine object required");
148 
149  std::shared_ptr<CommandLine> command_line =
150  parameters.get<std::shared_ptr<CommandLine>>("_command_line");
151  mooseAssert(command_line->hasParsed(), "Should have been parsed");
152 
153  command_line->populateCommandLineParams(parameters);
154 
155  // Historically we decided to non-const copy construct all application parameters. In
156  // order to get around that while apps are fixed (by taking a const reference instead),
157  // we store the app params here and the MooseApp constructor will query the InputParameters
158  // owned by ths factory instead of the ones that are passed to it (likely a const ref to a
159  // copy of the derived app's parmeters)
160  const auto & params = storeAppParams(parameters);
161 
162  build_info->_app_creation_count++;
163 
164  return build_info->build(params);
165 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
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.
const InputParameters & storeAppParams(InputParameters &params)
Stores the given parameters within _input_parameters for app construction.
Definition: AppFactory.C:179
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:180
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.

◆ FRIEND_TEST() [1/2]

AppFactory::FRIEND_TEST ( ::AppFactoryTest  ,
manageAppParams   
)
private

◆ FRIEND_TEST() [2/2]

AppFactory::FRIEND_TEST ( ::AppFactoryTest  ,
appCopyConstructParams   
)
private

◆ getAppParams()

const InputParameters & AppFactory::getAppParams ( const InputParameters params) const
Returns
The parameters for the application named name

This is needed because we poorly decided to not pass references of the InputParameters in all derived MooseApp objects. This enables the MooseApp to get the copy of the parameters that it was actually built with using this factory.

Definition at line 40 of file AppFactory.C.

41 {
42  const auto id = getAppParamsID(params);
43  if (const auto it = _input_parameters.find(id); it != _input_parameters.end())
44  return *it->second;
45  mooseError("AppFactory::getAppParams(): Parameters for application with ID ", id, " not found");
46 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
std::map< std::size_t, std::unique_ptr< InputParameters > > _input_parameters
Storage of input parameters used in applications (ID (from getAppParamsID()) -> params) ...
Definition: AppFactory.h:212
std::size_t getAppParamsID(const InputParameters &params) const
Get the ID for the InputParameters associated with an application, used in storing them in _input_par...
Definition: AppFactory.C:193

◆ getAppParamsID()

std::size_t AppFactory::getAppParamsID ( const InputParameters params) const
private

Get the ID for the InputParameters associated with an application, used in storing them in _input_parameters.

This is needed until app constructors do not copy construct parameters. See getAppParams() for more information.

The parameters passed in here (from the app) could be copy-constructed parameters, but will contain a "_app_params_id" parameter that allows us to get the actual parameters (owned by this factory).

Definition at line 193 of file AppFactory.C.

Referenced by clearAppParams(), and getAppParams().

194 {
195  if (!params.have_parameter<std::size_t>("_app_params_id"))
196  mooseError("AppFactory::getAppParamsID(): Invalid application parameters (missing "
197  "'_app_params_id')");
198  return params.get<std::size_t>("_app_params_id");
199 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
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.
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.

◆ getValidParams()

InputParameters AppFactory::getValidParams ( const std::string &  name)

Get valid parameters for the object.

Parameters
nameName of the object whose parameter we are requesting
Returns
Parameters of the object

Definition at line 31 of file AppFactory.C.

Referenced by MultiApp::createApp(), and createAppShared().

32 {
33  if (const auto it = _name_to_build_info.find(name); it != _name_to_build_info.end())
34  return it->second->buildParameters();
35 
36  mooseError(std::string("A '") + name + "' is not a registered object\n\n");
37 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:180

◆ instance()

AppFactory & AppFactory::instance ( )
static

Get the instance of the AppFactory.

Returns
Reference to the AppFactory instance

Definition at line 18 of file AppFactory.C.

Referenced by JsonSyntaxTree::addGlobal(), MultiApp::createApp(), MultiApp::createApps(), createAppShared(), Moose::createMooseApp(), MooseApp::dynamicAppRegistration(), MooseServer::parseDocumentForDiagnostics(), MultiApp::validParams(), and MooseApp::~MooseApp().

19 {
20  // We need a naked new here (_not_ a smart pointer or object instance) due to what seems like a
21  // bug in clang's static object destruction when using dynamic library loading.
22  static AppFactory * instance = nullptr;
23  if (!instance)
24  instance = new AppFactory;
25  return *instance;
26 }
Generic AppFactory class for building Application objects.
Definition: AppFactory.h:62
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:18

◆ isRegistered()

bool AppFactory::isRegistered ( const std::string &  app_name) const
inline

Returns a Boolean indicating whether an application type has been registered.

Definition at line 155 of file AppFactory.h.

Referenced by MultiApp::createApp(), and reg().

156  {
157  return _name_to_build_info.count(app_name);
158  }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:180

◆ operator=() [1/2]

Registry& AppFactory::operator= ( AppFactory const &  )
delete

◆ operator=() [2/2]

Registry& AppFactory::operator= ( AppFactory &&  )
delete

◆ reg()

template<typename T >
void AppFactory::reg ( const std::string &  name)

Register a new object.

Parameters
nameName of the object to register

Definition at line 217 of file AppFactory.h.

218 {
219  if (isRegistered(name))
220  return;
221 
222  _name_to_build_info[name] = std::make_unique<AppFactoryBuildInfo<T>>();
224  name, true, "MOOSE application " + name + " is available.");
225 }
bool isRegistered(const std::string &app_name) const
Returns a Boolean indicating whether an application type has been registered.
Definition: AppFactory.h:155
std::string name(const ElemQuality q)
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:180
static Capabilities & getCapabilityRegistry()
Definition: Capabilities.C:22
void add(const std::string &capability, CapabilityUtils::Type value, const std::string &doc)
register a new capability
Definition: Capabilities.C:33

◆ registeredObjectBuildInfos()

const AppFactoryBuildInfoMap& AppFactory::registeredObjectBuildInfos ( ) const
inline

Returns the map of object name to a function pointer for building said object's input parameters.

Definition at line 169 of file AppFactory.h.

169 { return _name_to_build_info; }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:180

◆ registeredObjects()

const auto& AppFactory::registeredObjects ( ) const
inline

Returns a reference to the map from names to AppFactoryBuildInfo pointers.

Definition at line 150 of file AppFactory.h.

Referenced by MultiApp::validParams().

150 { return _name_to_build_info; }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:180

◆ storeAppParams()

const InputParameters & AppFactory::storeAppParams ( InputParameters params)
private

Stores the given parameters within _input_parameters for app construction.

Also calls finalize() on the parameters.

Definition at line 179 of file AppFactory.C.

Referenced by createShared().

180 {
181  const std::size_t next_id =
182  _input_parameters.size() ? (std::prev(_input_parameters.end())->first + 1) : 0;
183  params.addPrivateParam<std::size_t>("_app_params_id", next_id);
184  const auto it_inserted_pair =
185  _input_parameters.emplace(next_id, std::make_unique<InputParameters>(params));
186  mooseAssert(it_inserted_pair.second, "Already exists");
187  auto & stored_params = *it_inserted_pair.first->second;
188  stored_params.finalize("");
189  return stored_params;
190 }
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...
std::map< std::size_t, std::unique_ptr< InputParameters > > _input_parameters
Storage of input parameters used in applications (ID (from getAppParamsID()) -> params) ...
Definition: AppFactory.h:212

◆ validParams()

static InputParameters AppFactory::validParams ( )
static

Member Data Documentation

◆ _input_parameters

std::map<std::size_t, std::unique_ptr<InputParameters> > AppFactory::_input_parameters
private

Storage of input parameters used in applications (ID (from getAppParamsID()) -> params)

Definition at line 212 of file AppFactory.h.

Referenced by clearAppParams(), getAppParams(), and storeAppParams().

◆ _name_to_build_info

AppFactoryBuildInfoMap AppFactory::_name_to_build_info
protected

The documentation for this class was generated from the following files: