Construct version 5.3.9
An agent based modeling framework
NodesetManager Class Reference

A container for nodesets. More...

Public Member Functions

Nodesetcreate_nodeset (const std::string &name)
 Creates a new nodeset and adds it to the NodesetManager. More...
 
const Nodesetget_nodeset (const std::string &name) const
 Finds the specified nodeset. More...
 
bool does_nodeset_exist (const std::string &name) const noexcept
 Determines if the constant nodeset exists. More...
 
void import_nodeset (const Nodeset *nodeset)
 Causes this manager to take ownership of the nodeset. More...
 
void export_nodeset (const Nodeset *nodeset) noexcept
 Causes this manager to relieve its ownership of the nodeset. More...
 

Detailed Description

A container for nodesets.

The nodeset manager contains all nodesets in Construct. Only one instance of a nodeset manager is expected in an instance of Construct. The nodeset manager can be called from Construct using Construct::get_NodesetManager.

Member Function Documentation

◆ create_nodeset()

Nodeset * NodesetManager::create_nodeset ( const std::string &  name)

Creates a new nodeset and adds it to the NodesetManager.

Nodeset pointers are owned by the NodesetManager and should not be deallocated by anything other than the NodesetManager.

Parameters
nameThe nodeset's name.
Returns
A pointer to the newly created nodeset.

Example

Nodeset* my_nodeset = construct->ns_manager->create_nodeset("my_nodeset");
my_attributes->insert(std::make_pair("my att1 name", "my att1 value"));
//At this point we know that the nodeset will take ownership of the submitted attribute pointer
//This is because only one pointer has been submitted in the nodeset's history.
my_nodeset->add_node("Mary", my_attributes);
//Once the nodeset takes ownership of the pointer, it can be repeatedly submitted to the function.
my_nodeset->add_node("Joe", my_attributes);
my_attributes = new dynet::ParameterMap();
my_attributes->insert(std::make_pair("my att1 name", "my att1 value"));
//because the nodeset will attempt to optimize memory usage, it will reject ownership of this pointer
//as its contents are equal to the previous node's (Joe) attributes.
//The nodeset will then use that attribute pointer rather than the submitted pointer.
if (my_nodeset->add_node("John", my_attributes)) {
std::cout << "attribute pointer not added and requires deallocation" << std::endl;
delete my_attributes;
}
my_attributes = new dynet::ParameterMap();
my_attributes->insert(std::make_pair("my att2 name", "my att2 value"));
//if you're sure the nodeset will not reject ownership you need not check each function return
my_nodeset->add_node("Rebecca", my_attributes);
//any previous pointer that the nodeset has taken ownership of can be used with a check on the function return
//if no name is given a unique name is given based on the nodeset name and the node's index
auto first_node = my_nodeset->begin();
my_nodeset->add_node(first_node->attributes);
my_nodeset->turn_to_const();
const Nodeset* loaded_nodeset = construct->ns_manager->get_nodeset("my_nodeset");
for (auto node = loaded_nodeset->begin(); node != loaded_nodeset->end(); ++node) {
std::cout << "Node name, index: " << node->name << ", " << std::to_string(node->index) << std::endl;
std::cout << "Node attributes:" << std::endl;
for (auto att = node->attributes->begin(); att != node->attributes->end(); ++att) {
std::cout << "/t" << att->first << " -> " << att->second << std::endl;
}
}
A container for nodes.
Definition: NodesetManager.h:145
const std::string name
Definition: NodesetManager.h:156
bool add_node(const dynet::ParameterMap *attributes)
Adds a node to the Nodeset.
Definition: NodesetManager.cpp:16
void turn_to_const() noexcept
Modifies the nodeset such that is findable by this class and can no longer have nodes added to it.
Definition: NodesetManager.cpp:76
iterator end(void) const
Returns an iterator pointing the beyond the last node in the vector of nodes.
Definition: NodesetManager.cpp:9
iterator begin(void) const
Returns an iterator pointing the beginning of the vector of nodes.
Definition: NodesetManager.cpp:8
Definition: utils.h:96

Output:

attribute pointer not added and requires deallocation
Node name, index: Mary, 0
Node attributes:
    my att1 name -> my att1 value
Node name, index: Joe, 1
Node attributes:
    my att1 name -> my att1 value
Node name, index: John, 2
Node attributes:
    my att1 name -> my att1 value
Node name, index: Rebecca, 3
Node attributes:
    my att2 name -> my att2 value
Node name, index: my_nodeset_4, 4
Node attributes:
    my att1 name -> my att1 value

Complexity

Constant.

Exception Safety

A dynet::already_exists exception is thrown if a Nodeset with the same name already exists.

◆ does_nodeset_exist()

bool NodesetManager::does_nodeset_exist ( const std::string &  name) const
noexcept

Determines if the constant nodeset exists.

A nodeset can be created, but it is not discoverable until Nodeset::turn_to_const has been called. Some functions may have variable functionality depending on whether a nodeset is present.

Parameters
nameThe nodeset's name.
Returns
True if the nodeset is found, false otherwise.

Example

//agent_group nodeset has been loaded but my_nodeset has not
if (construct->ns_manager->does_nodeset_exist(nodeset_names::agent_group)) {
std::cout << "Calculating statistics based on an agent's group" << std::endl;
}
else {
std::cout << "Calculating individual statistics" << std::endl;
}
if (construct->ns_manager->does_nodeset_exist("my_nodeset")) {
std::cout << "Calculating statistics based on my_nodeset" << std::endl;
}
else {
std::cout << "Calculating statistics without my_nodeset" << std::endl;
}

Output:

Calculating statistics based on an agent's group 
Calculating statistics without my_nodeset 

Complexity

Average is constant. The number of nodesets is not expected to be large. Underlying function uses unordered_map::find

Exception Safety

No-throw guarantee: this member function never throws exceptions.

Here is the caller graph for this function:

◆ export_nodeset()

void NodesetManager::export_nodeset ( const Nodeset nodeset)
noexcept

Causes this manager to relieve its ownership of the nodeset.

This Nodeset is removed from the container and will not be deallocated when this manager is deallocated. This can be used to save the Nodeset for a future Construct instance without copying the old contents.

Parameters
nodesetThe Nodeset that this manager is relieving its owership of.

Exception Safety

No-throw guarantee: this member function never throws exceptions.

◆ get_nodeset()

const Nodeset * NodesetManager::get_nodeset ( const std::string &  name) const

Finds the specified nodeset.

Nodeset pointers are owned by the NodesetManager and should not be deallocated by anything other than the NodesetManager.

Parameters
nameThe nodeset's name.
Returns
A pointer to the constant nodeset with name matching the parameter name.

Example

//nodeset has been preloaded with some agents; Mary, John, and Joe.
const Nodeset* agents = construct->ns_manager->get_nodeset(nodeset_names::agent);
const Node* agent = agents->get_node_by_name("Mary");
std::cout << "Node Mary is at index " << agent->index << std::endl;
Nodes are any type of entity with or without attributes.
Definition: NodesetManager.h:67
const unsigned int index
Node index of the node
Definition: NodesetManager.h:82

Output:

Node Mary is at index 0 

Complexity

Expected to be constant. The number of nodesets is not expected to be large. Underlying function uses unordered_map::find

Iterator validity

No changes

Exception Safety

A dynet::could_not_find_nodeset is thrown if the nodeset does not exist or it has not yet had #turn_to_const called on it.

Here is the caller graph for this function:

◆ import_nodeset()

void NodesetManager::import_nodeset ( const Nodeset nodeset)

Causes this manager to take ownership of the nodeset.

This Nodeset is added to the container and it is deallocated when this manager is deallocated. This can be used to import a Nodeset from a previous Construct instance without copying the old contents.

Parameters
nodesetThe Nodeset that this manager is taking owership of.

Exception Safety

A dynet::already_exists exception is thrown if a Nodeset with the same name already exists.