https://mooseframework.inl.gov
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | 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...
 
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...
 
std::unique_ptr< MooseAppcreate (const std::string &app_type, const std::string &name, InputParameters parameters, MPI_Comm COMM_WORLD_IN)
 Create a MooseApp given a set of parameters. More...
 
std::shared_ptr< MooseAppcreateShared (const std::string &app_type, const std::string &name, InputParameters parameters, MPI_Comm COMM_WORLD_IN)
 
 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 std::unique_ptr< MooseAppcreate (const std::string &app_type, const std::vector< std::string > &cli_args={})
 Create an app with no input and command line arguments. More...
 
static std::unique_ptr< MooseAppcreate (std::unique_ptr< Parser > parser, std::unique_ptr< CommandLine > command_line)
 Create a MooseApp from a Parser and CommandLine, both of which should have parsed. More...
 
static std::shared_ptr< MooseAppcreateAppShared (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...
 

Static Public Attributes

static const std::string main_app_name = "main"
 The name for the "main" moose application. More...
 

Protected Attributes

AppFactoryBuildInfoMap _name_to_build_info
 

Private Member Functions

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...
 
void registerAppCapability (const std::string &app_name)
 Register an application name as a capability. More...
 
 AppFactory ()
 Private constructor for singleton pattern. More...
 
 FRIEND_TEST (::AppFactoryTest, manageAppParams)
 
 FRIEND_TEST (::AppFactoryTest, appCopyConstructParams)
 
 FRIEND_TEST (::AppFactoryTest, createNotRegistered)
 

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 54 of file AppFactory.h.

Constructor & Destructor Documentation

◆ ~AppFactory()

AppFactory::~AppFactory ( )
virtual

Definition at line 30 of file AppFactory.C.

30 {}

◆ 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

Private constructor for singleton pattern.

Definition at line 222 of file AppFactory.h.

Referenced by instance().

222 {}

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 53 of file AppFactory.C.

54 {
55  const auto id = getAppParamsID(params);
56  if (const auto it = _input_parameters.find(id); it != _input_parameters.end())
57  _input_parameters.erase(it);
58  else
59  mooseError(
60  "AppFactory::clearAppParams(): Parameters for application with ID ", id, " not found");
61 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
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:231
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:216

◆ create() [1/3]

std::unique_ptr< MooseApp > AppFactory::create ( const std::string &  app_type,
const std::vector< std::string > &  cli_args = {} 
)
static

Create an app with no input and command line arguments.

Useful for unit testing.

Definition at line 64 of file AppFactory.C.

Referenced by create(), MultiApp::createApp(), createAppShared(), Moose::createMooseApp(), createShared(), and MooseServer::parseDocumentForDiagnostics().

66 {
67  auto parser = std::make_unique<Parser>(std::vector<std::string>());
68  parser->parse();
69  parser->setAppType(app_type);
70 
71  auto command_line = std::make_unique<CommandLine>(std::vector<std::string>{"unused"});
72  command_line->addArguments(cli_args);
73  command_line->parse();
74 
75  return AppFactory::create(std::move(parser), std::move(command_line));
76 }
static std::unique_ptr< MooseApp > create(const std::string &app_type, const std::vector< std::string > &cli_args={})
Create an app with no input and command line arguments.
Definition: AppFactory.C:64

◆ create() [2/3]

std::unique_ptr< MooseApp > AppFactory::create ( std::unique_ptr< Parser parser,
std::unique_ptr< CommandLine command_line 
)
static

Create a MooseApp from a Parser and CommandLine, both of which should have parsed.

Definition at line 79 of file AppFactory.C.

80 {
81  mooseAssert(parser, "Not set");
82  mooseAssert(parser->getAppType().size(), "App type not set");
83  mooseAssert(parser->queryRoot(), "Has not parsed");
84  mooseAssert(command_line, "Not set");
85  mooseAssert(command_line->hasParsed(), "Has not parsed");
86 
87  const std::string app_type = parser->getAppType();
88 
89  auto app_params = AppFactory::instance().getValidParams(parser->getAppType());
90  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
91  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = std::move(command_line);
92 
93  return AppFactory::instance().create(app_type, main_app_name, app_params, MPI_COMM_WORLD);
94 }
static const std::string main_app_name
The name for the "main" moose application.
Definition: AppFactory.h:68
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
static std::unique_ptr< MooseApp > create(const std::string &app_type, const std::vector< std::string > &cli_args={})
Create an app with no input and command line arguments.
Definition: AppFactory.C:64
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:20
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:35

◆ create() [3/3]

std::unique_ptr< MooseApp > AppFactory::create ( const std::string &  app_type,
const std::string &  name,
InputParameters  parameters,
MPI_Comm  COMM_WORLD_IN 
)

Create a MooseApp given a set of parameters.

The Parser must be set in the _parser param and the CommandLine must be set in the _command_line param, both of which must have been parsed.

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

Definition at line 140 of file AppFactory.C.

144 {
145  // Error if the application type is not located
146  const auto it = _name_to_build_info.find(app_type);
147  if (it == _name_to_build_info.end())
148  mooseError("AppFactory::Create(): Application '" + app_type + "' was not registered");
149  auto & build_info = it->second;
150 
151  auto comm = std::make_shared<Parallel::Communicator>(comm_world_in);
152 
153  // Take the app_type and add it to the parameters so that it can be retrieved in the Application
154  parameters.set<std::string>(MooseBase::type_param) = app_type;
155  parameters.set<std::string>(MooseBase::name_param) = name;
156  parameters.set<std::string>(MooseBase::unique_name_param) = "Application/" + name;
157  parameters.set<std::shared_ptr<Parallel::Communicator>>("_comm") = comm;
158  parameters.set<std::string>("_app_name") = name;
159 
160  auto parser = parameters.get<std::shared_ptr<Parser>>("_parser");
161  mooseAssert(parser, "Parser not valid");
162  mooseAssert(parser->queryRoot(), "Parser has not parsed");
163 
164  auto command_line = parameters.get<std::shared_ptr<CommandLine>>("_command_line");
165  mooseAssert(command_line, "Command line not valid");
166  mooseAssert(command_line->hasParsed(), "Command line has not parsed");
167  command_line->populateCommandLineParams(parameters);
168 
169  // Historically we decided to non-const copy construct all application parameters. In
170  // order to get around that while apps are fixed (by taking a const reference instead),
171  // we store the app params here and the MooseApp constructor will query the InputParameters
172  // owned by ths factory instead of the ones that are passed to it (likely a const ref to a
173  // copy of the derived app's parmeters)
174  const auto & params = storeAppParams(parameters);
175 
176  build_info->_app_creation_count++;
177 
178  return build_info->build(params);
179 }
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
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
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:202
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
static const std::string unique_name_param
The name of the parameter that contains the unique object name.
Definition: MooseBase.h:57
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:193

◆ createAppShared()

std::shared_ptr< MooseApp > 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 97 of file AppFactory.C.

101 {
102  mooseDeprecated("Please update your main.C to adapt new main function in MOOSE framework, "
103  "see'test/src/main.C in MOOSE as an example of moose::main()'. ");
104 
105  // Populate the -i and --app parameters early
106  auto command_line_params = emptyInputParameters();
107  MooseApp::addInputParam(command_line_params);
108  MooseApp::addAppParam(command_line_params);
109  {
110  CommandLine pre_command_line(argc, argv);
111  pre_command_line.parse();
112  pre_command_line.populateCommandLineParams(command_line_params);
113  }
114  const auto & input_filenames = command_line_params.get<std::vector<std::string>>("input_file");
115  std::string app_type = command_line_params.get<std::string>("app_to_run");
116 
117  auto command_line = std::make_unique<CommandLine>(argc, argv);
118  command_line->parse();
119 
120  auto parser = std::make_unique<Parser>(input_filenames);
121  parser->setCommandLineParams(command_line->buildHitParams());
122  parser->parse();
123 
124  if (app_type.empty())
125  app_type = default_app_type;
126  else
127  mooseDeprecated("Please use [Application] block to specify application type, '--app <AppName>' "
128  "is deprecated and will be removed in a future release.");
129 
130  parser->setAppType(app_type);
131 
132  auto app_params = AppFactory::instance().getValidParams(app_type);
133  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
134  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = std::move(command_line);
135 
136  return AppFactory::instance().create(app_type, "main", app_params, comm_world_in);
137 }
static void addInputParam(InputParameters &params)
Definition: MooseApp.C:102
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
static std::unique_ptr< MooseApp > create(const std::string &app_type, const std::vector< std::string > &cli_args={})
Create an app with no input and command line arguments.
Definition: AppFactory.C:64
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:363
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:20
static void addAppParam(InputParameters &params)
Definition: MooseApp.C:95
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:35

◆ 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 191 of file AppFactory.C.

192 {
193  // Error if the application type is not located
194  const auto it = _name_to_build_info.find(app_type);
195  if (it == _name_to_build_info.end())
196  mooseError("AppFactory::createdAppCount(): '", app_type, "' is not a registered app");
197 
198  return it->second->_app_creation_count;
199 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:193

◆ createShared()

std::shared_ptr< MooseApp > AppFactory::createShared ( const std::string &  app_type,
const std::string &  name,
InputParameters  parameters,
MPI_Comm  COMM_WORLD_IN 
)

Definition at line 182 of file AppFactory.C.

186 {
187  return AppFactory::instance().create(app_type, name, parameters, comm_world_in);
188 }
static std::unique_ptr< MooseApp > create(const std::string &app_type, const std::vector< std::string > &cli_args={})
Create an app with no input and command line arguments.
Definition: AppFactory.C:64
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:20

◆ FRIEND_TEST() [1/3]

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

◆ FRIEND_TEST() [2/3]

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

◆ FRIEND_TEST() [3/3]

AppFactory::FRIEND_TEST ( ::AppFactoryTest  ,
createNotRegistered   
)
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 44 of file AppFactory.C.

45 {
46  const auto id = getAppParamsID(params);
47  if (const auto it = _input_parameters.find(id); it != _input_parameters.end())
48  return *it->second;
49  mooseError("AppFactory::getAppParams(): Parameters for application with ID ", id, " not found");
50 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
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:231
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:216

◆ 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 216 of file AppFactory.C.

Referenced by clearAppParams(), and getAppParams().

217 {
218  if (!params.have_parameter<std::size_t>("_app_params_id"))
219  mooseError("AppFactory::getAppParamsID(): Invalid application parameters (missing "
220  "'_app_params_id')");
221  return params.get<std::size_t>("_app_params_id");
222 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
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 35 of file AppFactory.C.

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

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

◆ instance()

AppFactory & AppFactory::instance ( )
static

Get the instance of the AppFactory.

Returns
Reference to the AppFactory instance

Definition at line 20 of file AppFactory.C.

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

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

◆ 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 168 of file AppFactory.h.

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

169  {
170  return _name_to_build_info.count(app_name);
171  }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:193

◆ 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 236 of file AppFactory.h.

237 {
238  if (isRegistered(name))
239  return;
240 
241  _name_to_build_info[name] = std::make_unique<AppFactoryBuildInfo<T>>();
242 
243  registerAppCapability(name);
244 }
bool isRegistered(const std::string &app_name) const
Returns a Boolean indicating whether an application type has been registered.
Definition: AppFactory.h:168
std::string name(const ElemQuality q)
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:193
void registerAppCapability(const std::string &app_name)
Register an application name as a capability.
Definition: AppFactory.C:225

◆ registerAppCapability()

void AppFactory::registerAppCapability ( const std::string &  app_name)
private

Register an application name as a capability.

Definition at line 225 of file AppFactory.C.

Referenced by reg().

226 {
227  auto & capabilities = Moose::internal::Capabilities::getCapabilities({});
228  capabilities.add(MooseUtils::toLower(app_name),
229  bool(true),
230  "MOOSE application " + app_name + " is available.");
231 }
static Capabilities & getCapabilities(const GetCapabilitiesPassKey)
Get the singleton Capabilities.
Definition: Capabilities.C:52
Capability & add(const std::string_view name, const Moose::Capability::Value &value, const std::string_view doc)
Add a capability.
std::string toLower(std::string name)
Convert supplied string to lower case.

◆ 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 182 of file AppFactory.h.

182 { return _name_to_build_info; }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:193

◆ registeredObjects()

const auto& AppFactory::registeredObjects ( ) const
inline

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

Definition at line 163 of file AppFactory.h.

Referenced by MultiApp::validParams().

163 { return _name_to_build_info; }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:193

◆ 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 202 of file AppFactory.C.

Referenced by create().

203 {
204  const std::size_t next_id =
205  _input_parameters.size() ? (std::prev(_input_parameters.end())->first + 1) : 0;
206  params.addPrivateParam<std::size_t>("_app_params_id", next_id);
207  const auto it_inserted_pair =
208  _input_parameters.emplace(next_id, std::make_unique<InputParameters>(params));
209  mooseAssert(it_inserted_pair.second, "Already exists");
210  auto & stored_params = *it_inserted_pair.first->second;
211  stored_params.finalize("");
212  return stored_params;
213 }
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:231

◆ 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 231 of file AppFactory.h.

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

◆ _name_to_build_info

AppFactoryBuildInfoMap AppFactory::_name_to_build_info
protected

◆ main_app_name

const std::string AppFactory::main_app_name = "main"
static

The name for the "main" moose application.

Definition at line 68 of file AppFactory.h.

Referenced by create(), MooseBase::messagePrefix(), and MooseServer::parseDocumentForDiagnostics().


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