Construct version 5.4.4
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");
dynet::ParameterMap my_attributes{{"my att1 name", "my att1 value"}, {"my att2 name", "my att2 value"}}
my_nodeset.add_node("Mary", my_attributes);
my_nodeset.add_node("Joe", my_attributes);
my_nodeset.add_node("John", my_attributes);
my_nodeset.turn_to_const();
if (construct.ns_manager.does_nodeset_exist("my_nodeset"))
std::cout << "my_nodeset was loaded" << std::endl;
my_nodeset.add_node("Sam", my_attributes); // raises an assertion
A container for nodes.
Definition: NodesetManager.h:192
void add_node(const dynet::ParameterMap &attributes)
Adds a node to the Nodeset.
Definition: NodesetManager.cpp:16
Definition: utils.h:87

Output:

my_nodeset was loaded

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

Nodeset& my_nodeset = *construct.ns_manager.create_nodeset("my_nodeset");
dynet::ParameterMap my_attributes{{"my att1 name", "my att1 value"}, {"my att2 name", "my att2 value"}}
my_nodeset.add_node("Mary", my_attributes);
my_nodeset.add_node("Joe", my_attributes);
my_nodeset.add_node("John", my_attributes);
my_nodeset.turn_to_const();
if (construct.ns_manager.does_nodeset_exist("my_nodeset"))
std::cout << "my_nodeset was loaded" << std::endl;
my_nodeset.add_node("Sam", my_attributes); // raises an assertion

Output:

my_nodeset was loaded

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;
const std::string agents
Definition: NodesetManager.h:14
Nodes are any type of entity with or without attributes.
Definition: NodesetManager.h:109
const unsigned int index
Node index of the node
Definition: NodesetManager.h:124

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 Nodeset::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.