https://mooseframework.inl.gov
Public Member Functions | Private Member Functions | 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

template<class T , class SmartPtr >
void createCheckObjectType (const SmartPtr &object, const std::string &obj_name) const
 Internal method for checking if the created smart pointer is of the correct expected type. More...
 
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 expiration 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...
 
template<class ptr_type >
ptr_type createTempl (const std::string &obj_name, const std::string &name, const InputParameters &parameters, const THREAD_ID tid, const std::optional< std::string > &deprecated_method_name)
 Internal method for creating a MooseObject, either as a shared_ptr or as a unique_ptr. More...
 
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...
 

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 18 of file Factory.C.

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

◆ ~Factory()

Factory::~Factory ( )
virtual

Definition at line 20 of file Factory.C.

20 {}

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:247

◆ 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 301 of file Factory.C.

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

302 {
303  auto it = _name_to_class.find(name);
304  if (it == _name_to_class.end())
305  return "";
306  else
307  return it->second;
308 }
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:255

◆ 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 295 of file Factory.C.

Referenced by Registry::registerObjectsTo().

296 {
297  _name_to_class[name] = class_name;
298 }
std::string name(const ElemQuality q)
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:255

◆ 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 323 of file Factory.h.

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

324 {
325  static_assert(std::is_base_of_v<MooseObject, T>, "Not a MooseObject");
326 
327  const auto tid = object.template getParam<THREAD_ID>("_tid");
328  if (tid != 0)
329  mooseError("Factory::clone(): The object ",
330  object.typeAndName(),
331  " is threaded but cloning does not work with threaded objects");
332 
333  // Clone the parameters; we can't copy construct InputParameters
334  InputParameters cloned_params = emptyInputParameters();
335  cloned_params += object.parameters();
336  if (const auto hit_node = object.parameters().getHitNode())
337  cloned_params.setHitNode(*hit_node, {});
338 
339  // Fill the new parameters in the warehouse
340  const auto type = static_cast<const MooseBase &>(object).type();
341  const auto clone_count = _clone_counter[&object]++;
342  const auto name = object.name() + "_clone" + std::to_string(clone_count);
343  const auto & params = initialize(type, name, cloned_params, 0);
344 
345  // Construct the object
346  _currently_constructing.push_back(&params);
347  auto cloned_object = std::make_unique<T>(params);
348  _currently_constructing.pop_back();
349 
350  // Do some sanity checking
351  finalize(type, *cloned_object);
352 
353  return cloned_object;
354 }
std::string name(const ElemQuality q)
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:49
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
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:311
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
Definition: Factory.h:280
void finalize(const std::string &type, const MooseObject &object)
Finalizes the creaction of object of type type.
Definition: Factory.C:355
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:284
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 358 of file Factory.h.

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

359 {
360  static_assert(std::is_base_of_v<MooseObject, T>, "Not a MooseObject");
361 
362  const auto type = static_cast<const MooseBase &>(object).type();
363  if (object.hasBase())
364  {
365  const auto & base = object.getBase();
366  if (base != "MooseMesh" && base != "RelationshipManager")
367  mooseError("Copy construction of ", type, " objects is not supported.");
368  }
369 
370  _currently_constructing.push_back(&object.parameters());
371  auto cloned_object = std::make_unique<T>(object);
372  _currently_constructing.pop_back();
373 
374  finalize(type, *cloned_object);
375 
376  return cloned_object;
377 }
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:49
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
Definition: Factory.h:280
void finalize(const std::string &type, const MooseObject &object)
Finalizes the creaction of object of type type.
Definition: Factory.C:355
const std::string & getBase() const
Definition: MooseBase.h:147

◆ 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 142 of file Factory.C.

Referenced by CreateExecutionerAction::act(), PartitionerAction::act(), CreateProblemAction::act(), CreateProblemDefaultAction::act(), SetupMeshAction::act(), SetupPredictorAction::act(), SetupPreconditionerAction::act(), CreateDisplacedProblemAction::act(), MaterialDerivativeTestAction::act(), SetAdaptivityOptionsAction::act(), AddControlAction::act(), MooseEigenSystem::addKernel(), FEProblem::addLineSearch(), FEProblemBase::addObject(), CreateDisplacedProblemAction::addProxyRelationshipManagers(), Action::addRelationshipManager(), MooseObjectUnitTest::buildObjects(), and create().

147 {
148  std::optional<std::string> deprecated_method_name;
149  if (print_deprecated)
150  deprecated_method_name = "create";
151  return createTempl<std::shared_ptr<MooseObject>>(
152  obj_name, name, parameters, tid, deprecated_method_name);
153 }
std::string name(const ElemQuality q)

◆ 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 311 of file Factory.h.

315 {
316  auto object = create(obj_name, name, parameters, tid, false);
317  createCheckObjectType<T>(object, obj_name);
318  return std::static_pointer_cast<T>(object);
319 }
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Definition: Factory.C:142

◆ createCheckObjectType()

template<class T , class SmartPtr >
void Factory::createCheckObjectType ( const SmartPtr &  object,
const std::string &  obj_name 
) const
private

Internal method for checking if the created smart pointer is of the correct expected type.

Used in create<T> and createUnique<T>.

Definition at line 289 of file Factory.h.

290 {
291  if (!dynamic_cast<T *>(object.get()))
292  mooseError("We expected to create an object of type '" + MooseUtils::prettyCppType<T>() +
293  "'.\nInstead we received a parameters object for type '" + obj_name +
294  "'.\nDid you call the wrong \"add\" method in your Action?");
295 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323

◆ createTempl()

template<class ptr_type >
ptr_type Factory::createTempl ( const std::string &  obj_name,
const std::string &  name,
const InputParameters parameters,
const THREAD_ID  tid,
const std::optional< std::string > &  deprecated_method_name 
)
private

Internal method for creating a MooseObject, either as a shared_ptr or as a unique_ptr.

This is needed because we need to explicitly create objects that are requested as a shared_ptr with make_shared, so that the std::enable_shared_from_this interface will work for those objects.

We avoid code duplication by putting the creation logic for shared_ptr and unique_ptr in the same method.

This is explicitly instantiated for std::unique_ptr<MooseObject> and std::shared_ptr<MooseObject> in Factory.C.

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
deprecated_method_nameThe name of the method to throw a deprecated warning for, if any
Returns
The created MooseObject

Definition at line 88 of file Factory.C.

93 {
94  static_assert(std::is_same_v<ptr_type, std::unique_ptr<MooseObject>> ||
95  std::is_same_v<ptr_type, std::shared_ptr<MooseObject>>,
96  "Invalid ptr_type");
97 
98  if (deprecated_method_name)
99  {
100  const std::string name = "Factory::" + *deprecated_method_name;
101  mooseDeprecated(name + "() is deprecated, please use name" + "<T>() instead");
102  }
103 
104  // Build the parameters that are stored in the InputParameterWarehouse for this
105  // object, set a few other things and do a little error checking
106  auto & warehouse_params = initialize(obj_name, name, parameters, tid);
107 
108  // Mark that we're constructing this object
109  _currently_constructing.push_back(&warehouse_params);
110 
111  // Construct the object
112  auto & registry_entry = *_name_to_object.at(obj_name);
113  ptr_type obj;
114  if constexpr (std::is_same_v<ptr_type, std::unique_ptr<MooseObject>>)
115  obj = registry_entry.build(warehouse_params);
116  else
117  obj = registry_entry.buildShared(warehouse_params);
118 
119  // Done constructing the object
120  _currently_constructing.pop_back();
121 
122  finalize(obj_name, *obj);
123 
124  return obj;
125 }
std::string name(const ElemQuality q)
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:311
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:374
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
Definition: Factory.h:280
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:250
void finalize(const std::string &type, const MooseObject &object)
Finalizes the creaction of object of type type.
Definition: Factory.C:355

◆ 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 128 of file Factory.C.

Referenced by MooseObjectUnitTest::buildObjects(), and createUnique().

133 {
134  std::optional<std::string> deprecated_method_name;
135  if (print_deprecated)
136  deprecated_method_name = "createUnique";
137  return createTempl<std::unique_ptr<MooseObject>>(
138  obj_name, name, parameters, tid, deprecated_method_name);
139 }
std::string name(const ElemQuality q)

◆ 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 299 of file Factory.h.

303 {
304  auto object = createUnique(obj_name, name, parameters, tid, false);
305  createCheckObjectType<T>(object, obj_name);
306  return std::unique_ptr<T>(static_cast<T *>(object.release()));
307 }
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:128

◆ 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 283 of file Factory.C.

Referenced by MooseObject::MooseObject().

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

◆ 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 190 of file Factory.C.

Referenced by getValidParams(), and initialize().

191 {
192  const auto time_it = _deprecated_time.find(obj_name);
193 
194  // If the object is not deprecated return
195  if (time_it == _deprecated_time.end())
196  return;
197 
198  // If the message has already been printed, return
199  if (_deprecated_types.count(obj_name))
200  return;
201  _deprecated_types.emplace(obj_name);
202 
203  // We dont need a backtrace on this, this is user-facing
204  const auto current_show_trace = Moose::show_trace;
205  Moose::show_trace = false;
206 
207  // Get the current time
208  std::time_t now;
209  time(&now);
210 
211  // Get the stop time
212  std::time_t t_end = time_it->second;
213 
214  // Message storage
215  std::ostringstream msg;
216 
217  const auto name_it = _deprecated_name.find(obj_name);
218 
219  // Expired object
220  if (now > t_end)
221  {
222  msg << "***** Invalid Object: " << obj_name << " *****\n";
223  msg << "Expired on " << ctime(&t_end);
224 
225  // Append replacement object, if it exists
226  if (name_it != _deprecated_name.end())
227  msg << "Update your application using the '" << name_it->second << "' object";
228 
229  // Produce the error message
230  mooseDeprecationExpired(msg.str());
231  }
232 
233  // Expiring object
234  else
235  {
236  // Build the basic message
237  msg << "Deprecated Object: " << obj_name << "\n";
238  msg << "This object will be removed on " << ctime(&t_end);
239 
240  // Append replacement object, if it exists
241  if (name_it != _deprecated_name.end())
242  msg << "Replace " << obj_name << " with " << name_it->second;
243 
244  // Produce the error message
245  mooseDeprecated(msg.str());
246  }
247  Moose::show_trace = current_show_trace;
248 }
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:800
std::set< std::string > _deprecated_types
Set of deprecated object types that have been printed.
Definition: Factory.h:270
void mooseDeprecationExpired(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:382
std::map< std::string, std::time_t > _deprecated_time
Storage for deprecated object expiration dates.
Definition: Factory.h:258
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:374
std::map< std::string, std::string > _deprecated_name
Storage for the deprecated objects that have replacements.
Definition: Factory.h:261

◆ 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 355 of file Factory.C.

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

356 {
357  // Make sure no unexpected parameters were added by the object's constructor or by the action
358  // initiating this create call. All parameters modified by the constructor must have already
359  // been specified in the object's validParams function.
360  InputParameters orig_params = getValidParams(type);
361  const auto & object_params = object.parameters();
362  if (orig_params.n_parameters() != object_params.n_parameters())
363  {
364  std::set<std::string> orig, populated;
365  for (const auto & it : orig_params)
366  orig.emplace(it.first);
367  for (const auto & it : object_params)
368  populated.emplace(it.first);
369 
370  std::set<std::string> diff;
371  std::set_difference(populated.begin(),
372  populated.end(),
373  orig.begin(),
374  orig.end(),
375  std::inserter(diff, diff.begin()));
376 
377  if (!diff.empty())
378  {
379  std::stringstream ss;
380  for (const auto & name : diff)
381  ss << ", " << name;
382  object.mooseError("Attempted to set unregistered parameter(s):\n ", ss.str().substr(2));
383  }
384  }
385 }
std::string name(const ElemQuality q)
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:68
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::size_t n_parameters() const

◆ getConstructedObjects()

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

Get a list of all constructed Moose Object types.

Definition at line 274 of file Factory.C.

275 {
276  std::vector<std::string> list;
277  for (const auto & name : _constructed_types)
278  list.push_back(name);
279  return list;
280 }
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:267

◆ 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 289 of file Factory.C.

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

290 {
291  return _name_to_line.getInfo(name);
292 }
FileLineInfoMap _name_to_line
Definition: Factory.h:252
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 68 of file Factory.C.

Referenced by ElementIDOutputAction::act(), AutoCheckpointAction::act(), CreateProblemDefaultAction::act(), CombineComponentsMeshes::act(), SetupMeshAction::act(), SetupTimeStepperAction::act(), ComposeTimeStepperAction::act(), SetupResidualDebugAction::act(), CreateDisplacedProblemAction::act(), MaterialDerivativeTestAction::act(), SetAdaptivityOptionsAction::act(), DisplayGhostingAction::act(), MaterialOutputAction::act(), CommonOutputAction::act(), DiffusionCG::addBoundaryConditionsFromComponents(), DiffusionCG::addFEBCs(), DiffusionCG::addFEKernels(), DiffusionFV::addFVBCs(), DiffusionFV::addFVKernels(), DiffusionPhysicsBase::addInitialConditions(), DiffusionPhysicsBase::addInitialConditionsFromComponents(), FEProblem::addLineSearch(), ComponentMaterialPropertyInterface::addMaterials(), ComponentMeshTransformHelper::addMeshGenerators(), CylinderComponent::addMeshGenerators(), BatchMeshGeneratorAction::addMeshGenerators(), MeshGenerator::addMeshSubgenerator(), MFEMProblem::addMFEMFESpaceFromMOOSEVariable(), DiffusionPhysicsBase::addPostprocessors(), CreateDisplacedProblemAction::addProxyRelationshipManagers(), Action::addRelationshipManager(), DiffusionCG::addSolverVariables(), DiffusionFV::addSolverVariables(), BatchMeshGeneratorAction::BatchMeshGeneratorAction(), MooseObjectUnitTest::buildObjects(), ExtraIDIntegralReporter::ExtraIDIntegralReporter(), finalize(), FunctorSmootherTempl< T >::FunctorSmootherTempl(), DiffusionFV::getAdditionalRMParams(), MooseServer::getHoverDisplayText(), FEProblemBase::getKokkosFunction(), MooseServer::getObjectParameters(), MaterialOutputAction::getParams(), OverlayMeshGenerator::OverlayMeshGenerator(), ProjectedStatefulMaterialStorageAction::processProperty(), TransientBase::setupTimeIntegrator(), and SideSetExtruderGenerator::SideSetExtruderGenerator().

69 {
70  const auto it = _name_to_object.find(obj_name);
71 
72  // Check if the object is registered
73  if (it == _name_to_object.end())
74  reportUnregisteredError(obj_name);
75 
76  // Print out deprecated message, if it exists
77  deprecatedMessage(obj_name);
78 
79  // Return the parameters
80  auto params = it->second->buildParameters();
81  params.addPrivateParam(MooseBase::app_param, &_app);
82 
83  return params;
84 }
void reportUnregisteredError(const std::string &obj_name) const
Prints error information when an object is not registered.
Definition: Factory.C:251
static const std::string app_param
The name of the parameter that contains the MooseApp.
Definition: MooseBase.h:59
MooseApp & _app
Reference to the application.
Definition: Factory.h:247
void deprecatedMessage(const std::string obj_name) const
Show the appropriate message for deprecated objects.
Definition: Factory.C:190
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:250

◆ 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 311 of file Factory.C.

Referenced by clone(), and createTempl().

315 {
316  // Pointer to the object constructor
317  const auto it = _name_to_object.find(type);
318 
319  // Check if the object is registered
320  if (it == _name_to_object.end())
322 
323  // Print out deprecated message, if it exists
324  deprecatedMessage(type);
325 
326  // Create the actual parameters object that the object will reference
327  InputParameters & params =
328  _app.getInputParameterWarehouse().addInputParameters(name, from_params, tid, {});
329 
330  // Add the hit node from the action if it isn't set already (it might be set
331  // already because someone had a better option than just the action)
332  // If it isn't set, it typically means that this object was created by a
333  // non-MooseObjectAction Action
334  if (!params.getHitNode() || params.getHitNode()->isRoot())
335  if (const auto hit_node = _app.getCurrentActionHitNode())
336  params.setHitNode(*hit_node, {});
337 
338  // Set the type parameter
339  params.set<std::string>(MooseBase::type_param) = type;
340 
341  // Check to make sure that all required parameters are supplied
342  params.finalize(name);
343 
344  // register type name as constructed
345  _constructed_types.insert(type);
346 
347  // add FEProblem pointers to object's params object
349  _app.actionWarehouse().problemBase()->setInputParametersFEProblem(params);
350 
351  return params;
352 }
void reportUnregisteredError(const std::string &obj_name) const
Prints error information when an object is not registered.
Definition: Factory.C:251
const hit::Node * getHitNode(const std::string &param) const
MooseApp & _app
Reference to the application.
Definition: Factory.h:247
static const std::string type_param
The name of the parameter that contains the object type.
Definition: MooseBase.h:53
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition: MooseApp.C:2983
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:3178
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:190
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:211
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:250
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:267
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:250

◆ 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 168 of file Factory.C.

Referenced by reg().

169 {
170  // The string must be a certain length to be valid
171  if (t_str.size() != 16)
172  mooseError("The deprecated time not formatted correctly; it must be given as mm/dd/yyyy HH:MM");
173 
174  // Store the time, the time must be specified as: mm/dd/yyyy HH:MM
175  std::time_t t_end;
176  struct tm * t_end_info;
177  time(&t_end);
178  t_end_info = localtime(&t_end);
179  t_end_info->tm_mon = std::atoi(t_str.substr(0, 2).c_str()) - 1;
180  t_end_info->tm_mday = std::atoi(t_str.substr(3, 2).c_str());
181  t_end_info->tm_year = std::atoi(t_str.substr(6, 4).c_str()) - 1900;
182  t_end_info->tm_hour = std::atoi(t_str.substr(11, 2).c_str()) + 1;
183  t_end_info->tm_min = std::atoi(t_str.substr(14, 2).c_str());
184  t_end_info->tm_sec = 0;
185  t_end = mktime(t_end_info);
186  return t_end;
187 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323

◆ reg()

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

Definition at line 23 of file Factory.C.

Referenced by Registry::registerObjectsTo().

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

◆ 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 156 of file Factory.C.

Referenced by SetAdaptivityOptionsAction::act(), CreateDisplacedProblemAction::addProxyRelationshipManagers(), and Action::addRelationshipManager().

157 {
158  _app.getInputParameterWarehouse().removeInputParameters(moose_object, tid, {});
159 }
MooseApp & _app
Reference to the application.
Definition: Factory.h:247
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition: MooseApp.C:2983
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 251 of file Factory.C.

Referenced by getValidParams(), and initialize().

252 {
253  std::ostringstream oss;
254  std::set<std::string> paths = _app.getLoadedLibraryPaths();
255 
256  oss << "A '" + obj_name + "' is not a registered object.\n";
257 
258  if (!paths.empty())
259  {
260  oss << "\nWe loaded objects from the following libraries and still couldn't find your "
261  "object:\n\t";
262  std::copy(paths.begin(), paths.end(), infix_ostream_iterator<std::string>(oss, "\n\t"));
263  oss << '\n';
264  }
265 
266  oss << "\nIf you are trying to find this object in a dynamically loaded library, make sure that\n"
267  "the library can be found either in your \"Problem/library_path\" parameter or in the\n"
268  "MOOSE_LIBRARY_PATH environment variable.";
269 
270  mooseError(oss.str());
271 }
MooseApp & _app
Reference to the application.
Definition: Factory.h:247
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
std::set< std::string > getLoadedLibraryPaths() const
Return the paths of loaded libraries.
Definition: MooseApp.C:2946

◆ 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 162 of file Factory.C.

Referenced by DynamicObjectRegistrationAction::DynamicObjectRegistrationAction().

163 {
164  _registerable_objects.insert(names.begin(), names.end());
165 }
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:264

Member Data Documentation

◆ _app

MooseApp& Factory::_app
private

Reference to the application.

Definition at line 247 of file Factory.h.

Referenced by app(), 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 284 of file Factory.h.

Referenced by clone().

◆ _constructed_types

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

Constructed Moose Object types.

Definition at line 267 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 280 of file Factory.h.

Referenced by clone(), copyConstruct(), createTempl(), 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 261 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 expiration dates.

Definition at line 258 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 270 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 255 of file Factory.h.

Referenced by associatedClassName(), and associateNameToClass().

◆ _name_to_line

FileLineInfoMap Factory::_name_to_line
private

Definition at line 252 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 250 of file Factory.h.

Referenced by createTempl(), 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 275 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 264 of file Factory.h.

Referenced by reg(), and restrictRegisterableObjects().


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