Construct version 5.3.9
An agent based modeling framework
ModelManager Class Reference

Container for Construct models. More...

Public Member Functions

void move_model_to_front (Model *model)
 Moves the specified model pointer to front of the execution order. More...
 
Modelget_model_by_name (const std::string &model_name) noexcept
 Finds the named model's pointer. More...
 
const Modelget_model_by_name (const std::string &model_name) const noexcept
 Finds the named model's pointer. More...
 
void add_model (Model *model) noexcept
 Adds a model pointer. More...
 

Detailed Description

Container for Construct models.

The model manager stores all models as pointers in an unordered_map. As the number of active models is not expected to be large, unordered_map is used instead of map. The model manager creates pointers for models based on the XML input. A model's primary method of interacting with the model manager should be either checking to see if a model is active, or to add a PlaceHolder model when there is a model in a model.

Member Function Documentation

◆ add_model()

void ModelManager::add_model ( Model model)
noexcept

Adds a model pointer.

Takes a model pointer and adds it to the member container. If another model by the same name is present, the submitted pointer is deleted.

Excessive model creation can cause a rehash in the container. This will cause the iterators calling the core Model functions to become invalid. If a model causes a rehash during its initialize function, for example, this will cause undefined behavior. With current default bucket count a rehash will not happen, however the possibility still exists.

Parameters
modelPointer to the created model.

Example

construct->model_manager->add_model(new PlaceHolder(model_names::SIM));
if (construct->model_manager->get_model_by_name(model_names::SIM)) {
std::cout << "Standard Interaction Model currently active" << std::endl;
}
const std::string SIM
Definition: Model.h:186
A model that won't do anything.
Definition: Model.h:174

Output:

Standard Interaction Model currently active 

Complexity

First the model name is searched for in the unordered set. If the model name is not found it is inserted. Both of these operations take on average constant time, and worst case linear time in number of models.

Iterator validity

All iterators are valid unless insertion causes a rehash of the unordered_set, in which case all iterators are invalidated. References to elements remain valid in either case.

Exception Safety

No-throw guarantee: this member function never throws exceptions. A warning is displayed if another model by the same name is already present. In this case, the submitted model is deallocated and the pre-existing model in unchanged.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_model_by_name() [1/2]

const Model * ModelManager::get_model_by_name ( const std::string &  model_name) const
noexcept

Finds the named model's pointer.

This function primarily serves to identify the existence of a model (see KnowledgeTransactiveMemory). In addition this function can allow for the access of member objects such as loaded graphs (see Location).

Parameters
model_nameThe name of the model that is being searched for.
Returns
If the model exists, a constant pointer to that model is returned. If not, a null pointer is returned.

Example

//SIM has been loaded but the twitter model hasn't
Model* SIM = construct->model_manager->get_model_by_name(model_names::SIM);
Model* TWIT = construct->model_manager->get_model_by_name(models_names::TWIT);
if (SIM) std::cout << "Standard Interaction Model located, adjusting behavior." << std::endl;
else std::cout << "Standard Interaction Model not found, substituting relevant functions." << std::endl;
if (TWIT) std::cout << "Twitter Interaction Model located, adjusting behavior." << std::endl;
else std::cout << "Twitter Interaction Model not found, substituting relevant functions." << std::endl;
const std::string TWIT
Definition: Model.h:207
Parent class for Construct models.
Definition: Model.h:70

Output:

Standard Interaction Model not found, substituting relevant functions.
Twitter Interaction Model not found, substituting relevant functions.

Complexity

Complexity follows unordered_map::find.

Iterator validity

No Changes

Exception Safety

No-throw guarantee: this member function never throws exceptions. If the model name is not found, a null pointer is returned.

◆ get_model_by_name() [2/2]

Model * ModelManager::get_model_by_name ( const std::string &  model_name)
noexcept

Finds the named model's pointer.

This function primarily serves to identify the existence of a model (see KnowledgeTransactiveMemory). In addition this function can allow for the access of member objects such as loaded graphs (see Location).

Parameters
model_nameThe name of the model that is being searched for.
Returns
If the model exists, a pointer to that model is returned. If not, a null pointer is returned.

Example

//SIM has been loaded but the twitter model hasn't
Model* SIM = construct->model_manager->get_model_by_name(model_names::SIM);
Model* TWIT = construct->model_manager->get_model_by_name(models_names::TWIT);
if (SIM) std::cout << "Standard Interaction Model located, adjusting behavior." << std::endl;
else std::cout << "Standard Interaction Model not found, substituting relevant functions." << std::endl;
if (TWIT) std::cout << "Twitter Interaction Model located, adjusting behavior." << std::endl;
else std::cout << "Twitter Interaction Model not found, substituting relevant functions." << std::endl;

Output:

Standard Interaction Model not found, substituting relevant functions.
Twitter Interaction Model not found, substituting relevant functions.

Complexity

Complexity follows unordered_map::find.

Iterator validity

No Changes

Exception Safety

No-throw guarantee: this member function never throws exceptions. If the model name is not found, a null pointer is returned.

Here is the caller graph for this function:

◆ move_model_to_front()

void ModelManager::move_model_to_front ( Model model)

Moves the specified model pointer to front of the execution order.

This function moves the specified model pointer from its current location to the beginning of the execution order. All models that were above the specified model pointer are shifted down one element. A model that uses this function should only move their pointer in the execution order, otherwise it may be possible some model initializations are skipped. This function can only be used before all models have completed their initialization. If the model pointer is not found, no changes occur.

Parameters
modelPointer of the model being moved.

Example

//custom model 1 needs to be in the execution order before custom model 2
Model* custom_model_1 = construct->model_manager->get_model_by_name("custom model 1");
if (custom_model_1) {
Model* custom_model_2 = construct->model_manager->get_model_by_name("custom model 2");
if (custom_model_2) construct->model_manager->move_model_to_front(custom_model_1);
}

Complexity

Number of elements between the specified model pointer and the front of the list.

Iterator validity

No Changes

Exception Safety

If this function is called after all initializations have been called, an assertion is raised.

Here is the caller graph for this function: