www.mooseframework.org
Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | List of all members
AppFactory Class Reference

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

#include <AppFactory.h>

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...
 
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 (const std::string &default_app_type, int argc, char **argv, std::unique_ptr< Parser > parser, MPI_Comm comm_word=MPI_COMM_WORLD)
 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 ()
 

Detailed Description

Generic AppFactory class for building Application objects.

Definition at line 55 of file AppFactory.h.

Constructor & Destructor Documentation

◆ ~AppFactory()

AppFactory::~AppFactory ( )
virtual

Definition at line 27 of file AppFactory.C.

27 {}

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

Referenced by instance().

148 {}

Member Function Documentation

◆ createAppShared() [1/2]

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

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

Definition at line 39 of file AppFactory.C.

Referenced by Moose::createMooseApp().

44 {
45  auto command_line = std::make_shared<CommandLine>(argc, argv);
46  auto which_app_param = emptyInputParameters();
47 
48  MooseApp::addAppParam(which_app_param);
49  command_line->addCommandLineOptionsFromParams(which_app_param);
50 
51  std::string app_type;
52  if (!command_line->search("app_to_run", app_type))
53  app_type = default_app_type;
54  else
55  mooseDeprecated("Please use [Application] block to specify application type, '--app <AppName>' "
56  "is deprecated and will be removed in a future release.");
57 
58  auto app_params = AppFactory::instance().getValidParams(app_type);
59  parser->setAppType(app_type);
60 
61  app_params.set<int>("_argc") = argc;
62  app_params.set<char **>("_argv") = argv;
63  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = command_line;
64 
65  // Take the front parser and add it to the parameters so that it can be retrieved in the
66  // Application
67  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
68 
69  return AppFactory::instance().createShared(app_type, "main", app_params, comm_world_in);
70 }
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:124
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
InputParameters emptyInputParameters()
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:17
static void addAppParam(InputParameters &params)
Definition: MooseApp.C:85
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:30

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

77 {
78  mooseDeprecated("Please update your main.C to adapt new main function in MOOSE framework, "
79  "see'test/src/main.C in MOOSE as an example of moose::main()'. ");
80 
81  auto command_line = std::make_shared<CommandLine>(argc, argv);
82  auto which_app_param = emptyInputParameters();
83 
84  which_app_param.addCommandLineParam<std::vector<std::string>>(
85  "input_file",
86  "-i <input_files>",
87  "Specify one or multiple input files. Multiple files get merged into a single simulation "
88  "input.");
89 
90  command_line->addCommandLineOptionsFromParams(which_app_param);
91 
92  std::vector<std::string> input_filenames;
93  command_line->search("input_file", input_filenames);
94 
95  auto parser = std::make_unique<Parser>(input_filenames);
96  if (input_filenames.size())
97  parser->parse();
98 
99  MooseApp::addAppParam(which_app_param);
100  command_line->addCommandLineOptionsFromParams(which_app_param);
101 
102  std::string app_type;
103  if (!command_line->search("app_to_run", app_type))
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  app_params.set<std::shared_ptr<CommandLine>>("_command_line") = command_line;
115 
116  // Take the front parser and add it to the parameters so that it can be retrieved in the
117  // Application
118  app_params.set<std::shared_ptr<Parser>>("_parser") = std::move(parser);
119 
120  return AppFactory::instance().createShared(app_type, "main", app_params, comm_world_in);
121 }
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:124
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
InputParameters emptyInputParameters()
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:17
static void addAppParam(InputParameters &params)
Definition: MooseApp.C:85
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:30

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

161 {
162  // Error if the application type is not located
163  const auto it = _name_to_build_info.find(app_type);
164  if (it == _name_to_build_info.end())
165  mooseError("AppFactory::createdAppCount(): '", app_type, "' is not a registered app");
166 
167  return it->second->_app_creation_count;
168 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:144

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

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

128 {
129  // Error if the application type is not located
130  const auto it = _name_to_build_info.find(app_type);
131  if (it == _name_to_build_info.end())
132  mooseError("Object '" + app_type + "' was not registered.");
133  auto & build_info = it->second;
134 
135  // Take the app_type and add it to the parameters so that it can be retrieved in the Application
136  parameters.set<std::string>("_type") = app_type;
137 
138  // Check to make sure that all required parameters are supplied
139  parameters.finalize("");
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  command_line->addCommandLineOptionsFromParams(parameters);
152  command_line->populateInputParams(parameters);
153 
154  build_info->_app_creation_count++;
155 
156  return build_info->build(parameters);
157 }
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.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
void finalize(const std::string &parsing_syntax)
Finalizes the parameters, which must be done before constructing any objects with these parameters (t...
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:144
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.

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

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

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

◆ instance()

AppFactory & AppFactory::instance ( )
static

Get the instance of the AppFactory.

Returns
Pointer to the AppFactory instance

Definition at line 17 of file AppFactory.C.

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

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

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

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

120  {
121  return _name_to_build_info.count(app_name);
122  }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:144

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

154 {
155  if (isRegistered(name))
156  return;
157 
158  _name_to_build_info[name] = std::make_unique<AppFactoryBuildInfo<T>>();
159 }
bool isRegistered(const std::string &app_name) const
Returns a Boolean indicating whether an application type has been registered.
Definition: AppFactory.h:119
std::string name(const ElemQuality q)
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:144

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

133 { return _name_to_build_info; }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:144

◆ registeredObjects()

const auto& AppFactory::registeredObjects ( ) const
inline

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

Definition at line 114 of file AppFactory.h.

Referenced by MultiApp::validParams().

114 { return _name_to_build_info; }
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:144

◆ validParams()

static InputParameters AppFactory::validParams ( )
static

Member Data Documentation

◆ _name_to_build_info

AppFactoryBuildInfoMap AppFactory::_name_to_build_info
protected

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