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

Generic factory class for build all sorts of objects. More...

#include <Factory.h>

Public Member Functions

 Factory (MooseApp &app)
 
virtual ~Factory ()
 
void reg (std::shared_ptr< RegistryEntryBase > obj)
 
FileLineInfo getLineInfo (const std::string &name) const
 Gets file and line information where an object was initially registered. More...
 
void associateNameToClass (const std::string &name, const std::string &class_name)
 Associates an object name with a class name. More...
 
std::string associatedClassName (const std::string &name) const
 Get the associated class name for an object name. More...
 
InputParameters getValidParams (const std::string &name) const
 Get valid parameters for the object. More...
 
template<typename T >
std::unique_ptr< T > clone (const T &object)
 Clones the object object. More...
 
template<typename T >
std::unique_ptr< T > copyConstruct (const T &object)
 Copy constructs the object object. More...
 
void releaseSharedObjects (const MooseObject &moose_object, THREAD_ID tid=0)
 Releases any shared resources created as a side effect of creating an object through the Factory::create method(s). More...
 
void restrictRegisterableObjects (const std::vector< std::string > &names)
 Calling this object with a non-empty vector will cause this factory to ignore registrations from any object not contained within the list. More...
 
const auto & registeredObjects () const
 Returns a reference to the map from names to RegistryEntryBase pointers. More...
 
bool isRegistered (const std::string &obj_name) const
 Returns a Boolean indicating whether an object type has been registered. More...
 
std::vector< std::string > getConstructedObjects () const
 Get a list of all constructed Moose Object types. More...
 
MooseAppapp ()
 
const InputParameterscurrentlyConstructing () const
 
std::unique_ptr< MooseObjectcreateUnique (const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
 Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) More...
 
std::shared_ptr< MooseObjectcreate (const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
 
template<typename T >
std::unique_ptr< T > createUnique (const std::string &obj_name, const std::string &name, const InputParameters &parameters, const THREAD_ID tid=0)
 Build an object (must be registered) More...
 
template<typename T >
std::shared_ptr< T > create (const std::string &obj_name, const std::string &name, const InputParameters &parameters, const THREAD_ID tid=0)
 

Private Member Functions

std::time_t parseTime (std::string)
 Parse time string (mm/dd/yyyy HH:MM) More...
 
void deprecatedMessage (const std::string obj_name) const
 Show the appropriate message for deprecated objects. More...
 
void reportUnregisteredError (const std::string &obj_name) const
 Prints error information when an object is not registered. More...
 
InputParametersinitialize (const std::string &type, const std::string &name, const InputParameters &from_params, const THREAD_ID tid)
 Initializes the data structures and the parameters (in the InputParameterWarehouse) for the object with the given state. More...
 
void finalize (const std::string &type, const MooseObject &object)
 Finalizes the creaction of object of type type. More...
 

Private Attributes

MooseApp_app
 Reference to the application. More...
 
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
 Storage for pointers to the object registry entry. More...
 
FileLineInfoMap _name_to_line
 
std::map< std::string, std::string > _name_to_class
 Object name to class name association. More...
 
std::map< std::string, std::time_t > _deprecated_time
 Storage for deprecated object experiation dates. More...
 
std::map< std::string, std::string > _deprecated_name
 Storage for the deprecated objects that have replacements. More...
 
std::set< std::string > _registerable_objects
 The list of objects that may be registered. More...
 
std::set< std::string > _constructed_types
 Constructed Moose Object types. More...
 
std::set< std::string > _deprecated_types
 Set of deprecated object types that have been printed. More...
 
std::set< std::pair< std::string, std::string > > _objects_by_label
 set<label/appname, objectname> used to track if an object previously added is being added again - which is okay/allowed, while still allowing us to detect/reject cases of duplicate object name registration where the label/appname is not identical. More...
 
std::vector< const InputParameters * > _currently_constructing
 The object's parameters that are currently being constructed (if any). More...
 
std::map< const MooseObject *, unsigned int_clone_counter
 Counter for keeping track of the number of times an object with a given name has been cloned so that we can continue to create objects with unique names. More...
 

Detailed Description

Generic factory class for build all sorts of objects.

Definition at line 28 of file Factory.h.

Constructor & Destructor Documentation

◆ Factory()

Factory::Factory ( MooseApp app)

Definition at line 17 of file Factory.C.

17 : _app(app) {}
MooseApp & _app
Reference to the application.
Definition: Factory.h:207
MooseApp & app()
Definition: Factory.h:159

◆ ~Factory()

Factory::~Factory ( )
virtual

Definition at line 19 of file Factory.C.

19 {}

Member Function Documentation

◆ app()

MooseApp& Factory::app ( )
inline

Definition at line 159 of file Factory.h.

159 { return _app; }
MooseApp & _app
Reference to the application.
Definition: Factory.h:207

◆ associatedClassName()

std::string Factory::associatedClassName ( const std::string &  name) const

Get the associated class name for an object name.

This will return an empty string if the name was not previously associated with a class name via associateNameToClass()

Definition at line 271 of file Factory.C.

Referenced by Moose::Builder::buildJsonSyntaxTree().

272 {
273  auto it = _name_to_class.find(name);
274  if (it == _name_to_class.end())
275  return "";
276  else
277  return it->second;
278 }
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:215

◆ associateNameToClass()

void Factory::associateNameToClass ( const std::string &  name,
const std::string &  class_name 
)

Associates an object name with a class name.

Primarily used with the registerNamed* macros to store the mapping between the object name and the class that implements the object.

Definition at line 265 of file Factory.C.

Referenced by Registry::registerObjectsTo().

266 {
267  _name_to_class[name] = class_name;
268 }
std::string name(const ElemQuality q)
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:215

◆ clone()

template<typename T >
std::unique_ptr< T > Factory::clone ( const T &  object)

Clones the object object.

Under the hood, this creates a copy of the InputParameters from object and constructs a new object with the copied parameters. The suffix _clone will be added to the object's name, where is incremented each time the object is cloned.

Definition at line 275 of file Factory.h.

Referenced by SingleRankPartitioner::clone(), RandomPartitioner::clone(), BlockWeightedPartitioner::clone(), PetscExternalPartitioner::clone(), LibmeshPartitioner::clone(), HierarchicalGridPartitioner::clone(), and GridPartitioner::clone().

276 {
277  static_assert(std::is_base_of_v<MooseObject, T>, "Not a MooseObject");
278 
279  const auto tid = object.template getParam<THREAD_ID>("_tid");
280  if (tid != 0)
281  mooseError("Factory::clone(): The object ",
282  object.typeAndName(),
283  " is threaded but cloning does not work with threaded objects");
284 
285  // Clone the parameters; we can't copy construct InputParameters
286  InputParameters cloned_params = emptyInputParameters();
287  cloned_params += object.parameters();
288  if (const auto hit_node = object.parameters().getHitNode())
289  cloned_params.setHitNode(*hit_node, {});
290 
291  // Fill the new parameters in the warehouse
292  const auto type = static_cast<const MooseBase &>(object).type();
293  const auto clone_count = _clone_counter[&object]++;
294  const auto name = object.name() + "_clone" + std::to_string(clone_count);
295  const auto & params = initialize(type, name, cloned_params, 0);
296 
297  // Construct the object
298  _currently_constructing.push_back(&params);
299  auto cloned_object = std::make_unique<T>(params);
300  _currently_constructing.pop_back();
301 
302  // Do some sanity checking
303  finalize(type, *cloned_object);
304 
305  return cloned_object;
306 }
std::string name(const ElemQuality q)
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:32
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
InputParameters emptyInputParameters()
InputParameters & initialize(const std::string &type, const std::string &name, const InputParameters &from_params, const THREAD_ID tid)
Initializes the data structures and the parameters (in the InputParameterWarehouse) for the object wi...
Definition: Factory.C:281
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
Definition: Factory.h:240
void finalize(const std::string &type, const MooseObject &object)
Finalizes the creaction of object of type type.
Definition: Factory.C:325
std::map< const MooseObject *, unsigned int > _clone_counter
Counter for keeping track of the number of times an object with a given name has been cloned so that ...
Definition: Factory.h:244
void setHitNode(const std::string &param, const hit::Node &node, const SetParamHitNodeKey)
Sets the hit node associated with the parameter param to node.

◆ copyConstruct()

template<typename T >
std::unique_ptr< T > Factory::copyConstruct ( const T &  object)

Copy constructs the object object.

Under the hood, the new object's parameters will point to the same address as the parameters in object. This can be dangerous and thus this is only allowed for a subset of objects.

Definition at line 310 of file Factory.h.

Referenced by ElementSideNeighborLayers::clone(), ElementPointNeighborLayers::clone(), RedistributeProperties::clone(), ProxyRelationshipManager::clone(), TiledMesh::safeClone(), FileMesh::safeClone(), ImageMesh::safeClone(), SpiralAnnularMesh::safeClone(), GeneratedMesh::safeClone(), RinglebMesh::safeClone(), ConcentricCircleMesh::safeClone(), AnnularMesh::safeClone(), MeshGeneratorMesh::safeClone(), StitchedMesh::safeClone(), and PatternedMesh::safeClone().

311 {
312  static_assert(std::is_base_of_v<MooseObject, T>, "Not a MooseObject");
313 
314  const auto type = static_cast<const MooseBase &>(object).type();
315  const auto base = object.parameters().getBase();
316  if (!base || (*base != "MooseMesh" && *base != "RelationshipManager"))
317  mooseError("Copy construction of ", type, " objects is not supported.");
318 
319  _currently_constructing.push_back(&object.parameters());
320  auto cloned_object = std::make_unique<T>(object);
321  _currently_constructing.pop_back();
322 
323  finalize(type, *cloned_object);
324 
325  return cloned_object;
326 }
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:32
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
Definition: Factory.h:240
void finalize(const std::string &type, const MooseObject &object)
Finalizes the creaction of object of type type.
Definition: Factory.C:325

◆ create() [1/2]

std::shared_ptr< MooseObject > Factory::create ( const std::string &  obj_name,
const std::string &  name,
const InputParameters parameters,
THREAD_ID  tid = 0,
bool  print_deprecated = true 
)

Definition at line 110 of file Factory.C.

Referenced by CreateExecutionerAction::act(), PartitionerAction::act(), CreateProblemAction::act(), CreateProblemDefaultAction::act(), AdaptivityAction::act(), SetupMeshAction::act(), SetupPreconditionerAction::act(), SetupPredictorAction::act(), CreateDisplacedProblemAction::act(), MaterialDerivativeTestAction::act(), SetAdaptivityOptionsAction::act(), AddControlAction::act(), AddPeriodicBCAction::act(), FEProblemBase::addAnyRedistributers(), NonlinearSystemBase::addBoundaryCondition(), NonlinearSystemBase::addConstraint(), NonlinearSystemBase::addDamper(), NonlinearSystemBase::addDGKernel(), NonlinearSystemBase::addDiracKernel(), MooseApp::addExecutor(), FEProblemBase::addFunction(), FEProblemBase::addFunctorMaterial(), FEProblemBase::addFVInitialCondition(), FEProblemBase::addIndicator(), FEProblemBase::addInitialCondition(), NonlinearSystemBase::addInterfaceKernel(), MooseEigenSystem::addKernel(), AuxiliarySystem::addKernel(), NonlinearSystemBase::addKernel(), FEProblem::addLineSearch(), FEProblemBase::addMarker(), FEProblemBase::addMaterialHelper(), FEProblemBase::addMeshDivision(), FEProblemBase::addMultiApp(), NonlinearSystemBase::addNodalKernel(), FEProblemBase::addObject(), FEProblemBase::addOutput(), FEProblemBase::addPredictor(), CreateDisplacedProblemAction::addProxyRelationshipManagers(), Action::addRelationshipManager(), AuxiliarySystem::addScalarKernel(), NonlinearSystemBase::addScalarKernel(), NonlinearSystemBase::addSplit(), AuxiliarySystem::addTimeIntegrator(), NonlinearSystemBase::addTimeIntegrator(), FEProblemBase::addTransfer(), FEProblemBase::addUserObject(), SystemBase::addVariable(), AStableDirk4::AStableDirk4(), MooseObjectUnitTest::buildObjects(), MooseApp::createExecutors(), and MeshGeneratorSystem::createMeshGenerator().

115 {
116  std::shared_ptr<MooseObject> object =
117  createUnique(obj_name, name, parameters, tid, print_deprecated);
118 
119  if (auto fep = std::dynamic_pointer_cast<FEProblemBase>(object))
120  _app.actionWarehouse().problemBase() = fep;
121 
122  return object;
123 }
MooseApp & _app
Reference to the application.
Definition: Factory.h:207
std::shared_ptr< FEProblemBase > & problemBase()
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:195
std::unique_ptr< MooseObject > createUnique(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:86

◆ create() [2/2]

template<typename T >
std::shared_ptr< T > Factory::create ( const std::string &  obj_name,
const std::string &  name,
const InputParameters parameters,
const THREAD_ID  tid = 0 
)

Definition at line 265 of file Factory.h.

269 {
270  return std::move(createUnique<T>(obj_name, name, parameters, tid));
271 }

◆ createUnique() [1/2]

std::unique_ptr< MooseObject > Factory::createUnique ( const std::string &  obj_name,
const std::string &  name,
const InputParameters parameters,
THREAD_ID  tid = 0,
bool  print_deprecated = true 
)

Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>())

Parameters
obj_nameType of the object being constructed
nameName for the object
parametersParameters this object should have
tidThe thread id that this copy will be created for
print_deprecatedcontrols the deprecated message
Returns
The created object

Definition at line 86 of file Factory.C.

Referenced by MooseObjectUnitTest::buildObjects(), OversampleOutput::cloneMesh(), create(), and createUnique().

91 {
92  if (print_deprecated)
93  mooseDeprecated("Factory::create() is deprecated, please use Factory::create<T>() instead");
94 
95  // Build the parameters that are stored in the InputParameterWarehouse for this
96  // object, set a few other things and do a little error checking
97  auto & warehouse_params = initialize(obj_name, name, parameters, tid);
98 
99  // call the function pointer to build the object
100  _currently_constructing.push_back(&warehouse_params);
101  auto obj = _name_to_object.at(obj_name)->build(warehouse_params);
102  _currently_constructing.pop_back();
103 
104  finalize(obj_name, *obj);
105 
106  return obj;
107 }
InputParameters & initialize(const std::string &type, const std::string &name, const InputParameters &from_params, const THREAD_ID tid)
Initializes the data structures and the parameters (in the InputParameterWarehouse) for the object wi...
Definition: Factory.C:281
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
Definition: Factory.h:240
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:210
void finalize(const std::string &type, const MooseObject &object)
Finalizes the creaction of object of type type.
Definition: Factory.C:325

◆ createUnique() [2/2]

template<typename T >
std::unique_ptr< T > Factory::createUnique ( const std::string &  obj_name,
const std::string &  name,
const InputParameters parameters,
const THREAD_ID  tid = 0 
)

Build an object (must be registered)

Parameters
obj_nameType of the object being constructed
nameName for the object
parametersParameters this object should have
tidThe thread id that this copy will be created for
Returns
The created object

Definition at line 249 of file Factory.h.

253 {
254  auto object = createUnique(obj_name, name, parameters, tid, false);
255  if (!dynamic_cast<T *>(object.get()))
256  mooseError("We expected to create an object of type '" + demangle(typeid(T).name()) +
257  "'.\nInstead we received a parameters object for type '" + obj_name +
258  "'.\nDid you call the wrong \"add\" method in your Action?");
259 
260  return std::unique_ptr<T>(static_cast<T *>(object.release()));
261 }
std::string name(const ElemQuality q)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::string demangle(const char *name)
std::unique_ptr< MooseObject > createUnique(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:86

◆ currentlyConstructing()

const InputParameters * Factory::currentlyConstructing ( ) const
Returns
The InputParameters for the object that is currently being constructed, if any.

Can be used to ensure that all MooseObjects are created using the Factory

Definition at line 253 of file Factory.C.

Referenced by MooseObject::MooseObject().

254 {
255  return _currently_constructing.size() ? _currently_constructing.back() : nullptr;
256 }
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
Definition: Factory.h:240

◆ deprecatedMessage()

void Factory::deprecatedMessage ( const std::string  obj_name) const
private

Show the appropriate message for deprecated objects.

Parameters
obj_nameName of the deprecated object

Definition at line 160 of file Factory.C.

Referenced by getValidParams(), and initialize().

161 {
162  const auto time_it = _deprecated_time.find(obj_name);
163 
164  // If the object is not deprecated return
165  if (time_it == _deprecated_time.end())
166  return;
167 
168  // If the message has already been printed, return
169  if (_deprecated_types.count(obj_name))
170  return;
171  _deprecated_types.emplace(obj_name);
172 
173  // We dont need a backtrace on this, this is user-facing
174  const auto current_show_trace = Moose::show_trace;
175  Moose::show_trace = false;
176 
177  // Get the current time
178  std::time_t now;
179  time(&now);
180 
181  // Get the stop time
182  std::time_t t_end = time_it->second;
183 
184  // Message storage
185  std::ostringstream msg;
186 
187  const auto name_it = _deprecated_name.find(obj_name);
188 
189  // Expired object
190  if (now > t_end)
191  {
192  msg << "***** Invalid Object: " << obj_name << " *****\n";
193  msg << "Expired on " << ctime(&t_end);
194 
195  // Append replacement object, if it exists
196  if (name_it != _deprecated_name.end())
197  msg << "Update your application using the '" << name_it->second << "' object";
198 
199  // Produce the error message
200  mooseDeprecationExpired(msg.str());
201  }
202 
203  // Expiring object
204  else
205  {
206  // Build the basic message
207  msg << "Deprecated Object: " << obj_name << "\n";
208  msg << "This object will be removed on " << ctime(&t_end);
209 
210  // Append replacement object, if it exists
211  if (name_it != _deprecated_name.end())
212  msg << "Replace " << obj_name << " with " << name_it->second;
213 
214  // Produce the error message
215  mooseDeprecated(msg.str());
216  }
217  Moose::show_trace = current_show_trace;
218 }
bool show_trace
Set to true (the default) to print the stack trace with error and warning messages - false to omit it...
Definition: Moose.C:642
std::set< std::string > _deprecated_types
Set of deprecated object types that have been printed.
Definition: Factory.h:230
void mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:321
std::map< std::string, std::time_t > _deprecated_time
Storage for deprecated object experiation dates.
Definition: Factory.h:218
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:313
std::map< std::string, std::string > _deprecated_name
Storage for the deprecated objects that have replacements.
Definition: Factory.h:221

◆ finalize()

void Factory::finalize ( const std::string &  type,
const MooseObject object 
)
private

Finalizes the creaction of object of type type.

This will do some sanity checking on whether or not the parameters in the created object match the valid paramters of the associated type.

Definition at line 325 of file Factory.C.

Referenced by clone(), copyConstruct(), and createUnique().

326 {
327  // Make sure no unexpected parameters were added by the object's constructor or by the action
328  // initiating this create call. All parameters modified by the constructor must have already
329  // been specified in the object's validParams function.
330  InputParameters orig_params = getValidParams(type);
331  const auto & object_params = object.parameters();
332  if (orig_params.n_parameters() != object_params.n_parameters())
333  {
334  std::set<std::string> orig, populated;
335  for (const auto & it : orig_params)
336  orig.emplace(it.first);
337  for (const auto & it : object_params)
338  populated.emplace(it.first);
339 
340  std::set<std::string> diff;
341  std::set_difference(populated.begin(),
342  populated.end(),
343  orig.begin(),
344  orig.end(),
345  std::inserter(diff, diff.begin()));
346 
347  if (!diff.empty())
348  {
349  std::stringstream ss;
350  for (const auto & name : diff)
351  ss << ", " << name;
352  object.mooseError("Attempted to set unregistered parameter(s):\n ", ss.str().substr(2));
353  }
354  }
355 }
std::string name(const ElemQuality q)
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:67
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...

◆ getConstructedObjects()

std::vector< std::string > Factory::getConstructedObjects ( ) const

Get a list of all constructed Moose Object types.

Definition at line 244 of file Factory.C.

Referenced by MooseApp::runInputFile().

245 {
246  std::vector<std::string> list;
247  for (const auto & name : _constructed_types)
248  list.push_back(name);
249  return list;
250 }
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:227

◆ getLineInfo()

FileLineInfo Factory::getLineInfo ( const std::string &  name) const

Gets file and line information where an object was initially registered.

Parameters
nameObject name
Returns
The FileLineInfo associated with name

Definition at line 259 of file Factory.C.

Referenced by Moose::Builder::buildJsonSyntaxTree(), and MooseServer::gatherDocumentDefinitionLocations().

260 {
261  return _name_to_line.getInfo(name);
262 }
FileLineInfoMap _name_to_line
Definition: Factory.h:212
FileLineInfo getInfo(const std::string &key0) const
Get file/line info for a key.
Definition: FileLineInfo.C:76

◆ getValidParams()

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

Get valid parameters for the object.

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

Definition at line 67 of file Factory.C.

Referenced by AB2PredictorCorrector::AB2PredictorCorrector(), ElementIDOutputAction::act(), AutoCheckpointAction::act(), CreateProblemDefaultAction::act(), AdaptivityAction::act(), SetupMeshAction::act(), ComposeTimeStepperAction::act(), SetupDebugAction::act(), SetupTimeStepperAction::act(), SetupResidualDebugAction::act(), SetAdaptivityOptionsAction::act(), CreateDisplacedProblemAction::act(), MaterialDerivativeTestAction::act(), DisplayGhostingAction::act(), AddPeriodicBCAction::act(), MaterialOutputAction::act(), AddNodalNormalsAction::act(), FEProblemBase::addAuxArrayVariable(), FEProblemBase::addAuxScalarVariable(), FEProblemBase::addAuxVariable(), DiffusionCG::addFEBCs(), DiffusionCG::addFEKernels(), DiffusionFV::addFVBCs(), DiffusionFV::addFVKernels(), FEProblem::addLineSearch(), MeshGenerator::addMeshSubgenerator(), DiffusionFV::addNonlinearVariables(), DiffusionCG::addNonlinearVariables(), CreateDisplacedProblemAction::addProxyRelationshipManagers(), Action::addRelationshipManager(), FVFluxKernel::adjustRMGhostLayers(), AStableDirk4::AStableDirk4(), MooseObjectUnitTest::buildObjects(), OversampleOutput::cloneMesh(), MooseBaseParameterInterface::connectControllableParams(), MooseApp::createExecutors(), DumpObjectsProblem::deduceNecessaryParameters(), ExtraIDIntegralReporter::ExtraIDIntegralReporter(), finalize(), FunctorSmootherTempl< T >::FunctorSmootherTempl(), DiffusionFV::getAdditionalRMParams(), FEProblemBase::getFunction(), MooseServer::getHoverDisplayText(), MooseServer::getObjectParameters(), MaterialOutputAction::getParams(), OverlayMeshGenerator::OverlayMeshGenerator(), ProjectedStatefulMaterialStorageAction::processProperty(), Transient::setupTimeIntegrator(), and SideSetExtruderGenerator::SideSetExtruderGenerator().

68 {
69  const auto it = _name_to_object.find(obj_name);
70 
71  // Check if the object is registered
72  if (it == _name_to_object.end())
73  reportUnregisteredError(obj_name);
74 
75  // Print out deprecated message, if it exists
76  deprecatedMessage(obj_name);
77 
78  // Return the parameters
79  auto params = it->second->buildParameters();
80  params.addPrivateParam("_moose_app", &_app);
81 
82  return params;
83 }
void reportUnregisteredError(const std::string &obj_name) const
Prints error information when an object is not registered.
Definition: Factory.C:221
MooseApp & _app
Reference to the application.
Definition: Factory.h:207
void deprecatedMessage(const std::string obj_name) const
Show the appropriate message for deprecated objects.
Definition: Factory.C:160
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:210

◆ initialize()

InputParameters & Factory::initialize ( const std::string &  type,
const std::string &  name,
const InputParameters from_params,
const THREAD_ID  tid 
)
private

Initializes the data structures and the parameters (in the InputParameterWarehouse) for the object with the given state.

Definition at line 281 of file Factory.C.

Referenced by clone(), and createUnique().

285 {
286  // Pointer to the object constructor
287  const auto it = _name_to_object.find(type);
288 
289  // Check if the object is registered
290  if (it == _name_to_object.end())
292 
293  // Print out deprecated message, if it exists
294  deprecatedMessage(type);
295 
296  // Create the actual parameters object that the object will reference
297  InputParameters & params =
298  _app.getInputParameterWarehouse().addInputParameters(name, from_params, tid, {});
299 
300  // Add the hit node from the action if it isn't set already (it might be set
301  // already because someone had a better option than just the action)
302  // If it isn't set, it typically means that this object was created by a
303  // non-MooseObjectAction Action
304  if (!params.getHitNode() || params.getHitNode()->isRoot())
305  if (const auto hit_node = _app.getCurrentActionHitNode())
306  params.setHitNode(*hit_node, {});
307 
308  // Set the _type parameter
309  params.set<std::string>("_type") = type;
310 
311  // Check to make sure that all required parameters are supplied
312  params.finalize(name);
313 
314  // register type name as constructed
315  _constructed_types.insert(type);
316 
317  // add FEProblem pointers to object's params object
319  _app.actionWarehouse().problemBase()->setInputParametersFEProblem(params);
320 
321  return params;
322 }
void reportUnregisteredError(const std::string &obj_name) const
Prints error information when an object is not registered.
Definition: Factory.C:221
const hit::Node * getHitNode(const std::string &param) const
MooseApp & _app
Reference to the application.
Definition: Factory.h:207
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition: MooseApp.C:2218
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.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const hit::Node * getCurrentActionHitNode() const
Definition: MooseApp.C:2413
void finalize(const std::string &parsing_syntax)
Finalizes the parameters, which must be done before constructing any objects with these parameters (t...
std::shared_ptr< FEProblemBase > & problemBase()
void deprecatedMessage(const std::string obj_name) const
Show the appropriate message for deprecated objects.
Definition: Factory.C:160
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:195
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:210
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:227
void setHitNode(const std::string &param, const hit::Node &node, const SetParamHitNodeKey)
Sets the hit node associated with the parameter param to node.

◆ isRegistered()

bool Factory::isRegistered ( const std::string &  obj_name) const
inline

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

Definition at line 152 of file Factory.h.

Referenced by MooseServer::gatherDocumentDefinitionLocations(), MooseServer::getHoverDisplayText(), and MooseServer::getObjectParameters().

152 { return _name_to_object.count(obj_name); }
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:210

◆ parseTime()

std::time_t Factory::parseTime ( std::string  t_str)
private

Parse time string (mm/dd/yyyy HH:MM)

Parameters
t_strString with the object expiration date, this must be in the form mm/dd/yyyy HH:MM
Returns
A time_t object with the expiration date

Definition at line 138 of file Factory.C.

Referenced by reg().

139 {
140  // The string must be a certain length to be valid
141  if (t_str.size() != 16)
142  mooseError("The deprecated time not formatted correctly; it must be given as mm/dd/yyyy HH:MM");
143 
144  // Store the time, the time must be specified as: mm/dd/yyyy HH:MM
145  std::time_t t_end;
146  struct tm * t_end_info;
147  time(&t_end);
148  t_end_info = localtime(&t_end);
149  t_end_info->tm_mon = std::atoi(t_str.substr(0, 2).c_str()) - 1;
150  t_end_info->tm_mday = std::atoi(t_str.substr(3, 2).c_str());
151  t_end_info->tm_year = std::atoi(t_str.substr(6, 4).c_str()) - 1900;
152  t_end_info->tm_hour = std::atoi(t_str.substr(11, 2).c_str()) + 1;
153  t_end_info->tm_min = std::atoi(t_str.substr(14, 2).c_str());
154  t_end_info->tm_sec = 0;
155  t_end = mktime(t_end_info);
156  return t_end;
157 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284

◆ reg()

void Factory::reg ( std::shared_ptr< RegistryEntryBase obj)

Definition at line 22 of file Factory.C.

Referenced by Registry::registerObjectsTo().

23 {
24  const std::string obj_name = obj->name();
25  const std::string & label = obj->_label;
26  const std::string & deprecated_time = obj->_deprecated_time;
27  const std::string & replacement_name = obj->_replaced_by;
28  const std::string & file = obj->_file;
29  const int line = obj->_line;
30 
31  // do nothing if we have already added this exact object before
32  auto key = std::make_pair(label, obj_name);
33  if (_objects_by_label.find(key) != _objects_by_label.end())
34  return;
35 
36  /*
37  * If _registerable_objects has been set the user has requested that we only register some
38  * subset
39  * of the objects for a dynamically loaded application. The objects listed in *this*
40  * application's
41  * registerObjects() method will have already been registered before that member was set.
42  *
43  * If _registerable_objects is empty, the factory is unrestricted
44  */
45  if (_registerable_objects.empty() ||
46  _registerable_objects.find(obj_name) != _registerable_objects.end())
47  {
48  if (_name_to_object.find(obj_name) != _name_to_object.end())
49  mooseError("Object '" + obj_name + "' registered from multiple files: ",
50  file,
51  " and ",
52  _name_to_line.getInfo(obj_name).file());
53  _name_to_object[obj_name] = obj;
54  _objects_by_label.insert(key);
55  }
56  _name_to_line.addInfo(obj_name, file, line);
57 
58  if (!replacement_name.empty())
59  _deprecated_name[obj_name] = replacement_name;
60  if (!deprecated_time.empty())
61  _deprecated_time[obj_name] = parseTime(deprecated_time);
62 
63  // TODO: Possibly store and print information about objects that are skipped here?
64 }
std::set< std::pair< std::string, std::string > > _objects_by_label
set<label/appname, objectname> used to track if an object previously added is being added again - whi...
Definition: Factory.h:235
std::time_t parseTime(std::string)
Parse time string (mm/dd/yyyy HH:MM)
Definition: Factory.C:138
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
FileLineInfoMap _name_to_line
Definition: Factory.h:212
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::map< std::string, std::time_t > _deprecated_time
Storage for deprecated object experiation dates.
Definition: Factory.h:218
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:224
std::map< std::string, std::string > _deprecated_name
Storage for the deprecated objects that have replacements.
Definition: Factory.h:221
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:210
std::string file() const
Definition: FileLineInfo.C:29

◆ registeredObjects()

const auto& Factory::registeredObjects ( ) const
inline

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

Definition at line 147 of file Factory.h.

Referenced by MooseServer::addValuesToList(), Moose::Builder::buildFullTree(), and Moose::Builder::buildJsonSyntaxTree().

147 { return _name_to_object; }
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:210

◆ releaseSharedObjects()

void Factory::releaseSharedObjects ( const MooseObject moose_object,
THREAD_ID  tid = 0 
)

Releases any shared resources created as a side effect of creating an object through the Factory::create method(s).

Currently, this object just moves the InputParameters object from the InputParameterWarehouse. Normally this method does not need to be explicitly called during a normal simulation.

Definition at line 126 of file Factory.C.

Referenced by AdaptivityAction::act(), SetAdaptivityOptionsAction::act(), AddPeriodicBCAction::act(), CreateDisplacedProblemAction::addProxyRelationshipManagers(), Action::addRelationshipManager(), and MooseApp::removeRelationshipManager().

127 {
128  _app.getInputParameterWarehouse().removeInputParameters(moose_object, tid, {});
129 }
MooseApp & _app
Reference to the application.
Definition: Factory.h:207
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition: MooseApp.C:2218
void removeInputParameters(const MooseObject &moose_object, THREAD_ID tid, const AddRemoveParamsKey)
Allows for the deletion and cleanup of an object while the simulation is running. ...

◆ reportUnregisteredError()

void Factory::reportUnregisteredError ( const std::string &  obj_name) const
private

Prints error information when an object is not registered.

Definition at line 221 of file Factory.C.

Referenced by getValidParams(), and initialize().

222 {
223  std::ostringstream oss;
224  std::set<std::string> paths = _app.getLoadedLibraryPaths();
225 
226  oss << "A '" + obj_name + "' is not a registered object.\n";
227 
228  if (!paths.empty())
229  {
230  oss << "\nWe loaded objects from the following libraries and still couldn't find your "
231  "object:\n\t";
232  std::copy(paths.begin(), paths.end(), infix_ostream_iterator<std::string>(oss, "\n\t"));
233  oss << '\n';
234  }
235 
236  oss << "\nIf you are trying to find this object in a dynamically loaded library, make sure that\n"
237  "the library can be found either in your \"Problem/library_path\" parameter or in the\n"
238  "MOOSE_LIBRARY_PATH environment variable.";
239 
240  mooseError(oss.str());
241 }
MooseApp & _app
Reference to the application.
Definition: Factory.h:207
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
std::set< std::string > getLoadedLibraryPaths() const
Return the paths of loaded libraries.
Definition: MooseApp.C:2181

◆ restrictRegisterableObjects()

void Factory::restrictRegisterableObjects ( const std::vector< std::string > &  names)

Calling this object with a non-empty vector will cause this factory to ignore registrations from any object not contained within the list.

Parameters
namesa vector containing the names of objects that this factory will register

Definition at line 132 of file Factory.C.

Referenced by DynamicObjectRegistrationAction::DynamicObjectRegistrationAction().

133 {
134  _registerable_objects.insert(names.begin(), names.end());
135 }
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:224

Member Data Documentation

◆ _app

MooseApp& Factory::_app
private

Reference to the application.

Definition at line 207 of file Factory.h.

Referenced by app(), create(), getValidParams(), initialize(), releaseSharedObjects(), and reportUnregisteredError().

◆ _clone_counter

std::map<const MooseObject *, unsigned int> Factory::_clone_counter
private

Counter for keeping track of the number of times an object with a given name has been cloned so that we can continue to create objects with unique names.

Definition at line 244 of file Factory.h.

Referenced by clone().

◆ _constructed_types

std::set<std::string> Factory::_constructed_types
private

Constructed Moose Object types.

Definition at line 227 of file Factory.h.

Referenced by getConstructedObjects(), and initialize().

◆ _currently_constructing

std::vector<const InputParameters *> Factory::_currently_constructing
private

The object's parameters that are currently being constructed (if any).

This is a vector because we create within create, thus the last entry is the one that is being constructed at the moment

Definition at line 240 of file Factory.h.

Referenced by clone(), copyConstruct(), createUnique(), and currentlyConstructing().

◆ _deprecated_name

std::map<std::string, std::string> Factory::_deprecated_name
private

Storage for the deprecated objects that have replacements.

Definition at line 221 of file Factory.h.

Referenced by deprecatedMessage(), and reg().

◆ _deprecated_time

std::map<std::string, std::time_t> Factory::_deprecated_time
private

Storage for deprecated object experiation dates.

Definition at line 218 of file Factory.h.

Referenced by deprecatedMessage(), and reg().

◆ _deprecated_types

std::set<std::string> Factory::_deprecated_types
mutableprivate

Set of deprecated object types that have been printed.

Definition at line 230 of file Factory.h.

Referenced by deprecatedMessage().

◆ _name_to_class

std::map<std::string, std::string> Factory::_name_to_class
private

Object name to class name association.

Definition at line 215 of file Factory.h.

Referenced by associatedClassName(), and associateNameToClass().

◆ _name_to_line

FileLineInfoMap Factory::_name_to_line
private

Definition at line 212 of file Factory.h.

Referenced by getLineInfo(), and reg().

◆ _name_to_object

std::map<std::string, std::shared_ptr<RegistryEntryBase> > Factory::_name_to_object
private

Storage for pointers to the object registry entry.

Definition at line 210 of file Factory.h.

Referenced by createUnique(), getValidParams(), initialize(), isRegistered(), reg(), and registeredObjects().

◆ _objects_by_label

std::set<std::pair<std::string, std::string> > Factory::_objects_by_label
private

set<label/appname, objectname> used to track if an object previously added is being added again - which is okay/allowed, while still allowing us to detect/reject cases of duplicate object name registration where the label/appname is not identical.

Definition at line 235 of file Factory.h.

Referenced by reg().

◆ _registerable_objects

std::set<std::string> Factory::_registerable_objects
private

The list of objects that may be registered.

Definition at line 224 of file Factory.h.

Referenced by reg(), and restrictRegisterableObjects().


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