GNode library (C++)
Loading...
Searching...
No Matches
gnode::Graph Class Reference

The Graph class provides methods for manipulating nodes and connections in a directed graph structure. More...

#include <graph.hpp>

Public Member Functions

 Graph ()=default
 Construct a new Graph object.
 
 Graph (const std::string &id)
 Construct a new Graph object.
 
virtual ~Graph ()=default
 Destroy the Graph object.
 
virtual std::string add_node (const std::shared_ptr< Node > &p_node, const std::string &id="")
 Add a new node to the graph.
 
template<typename U , typename... Args>
std::string add_node (Args... args)
 Add a new node of a specific type to the graph.
 
void clear ()
 Clear the graph, remove all the nodes and the links.
 
std::vector< Pointcompute_graph_layout_sugiyama ()
 Compute the layout of the graph using the Sugiyama algorithm.
 
std::string get_id () const
 Return the graph ID.
 
bool new_link (const std::string &from, int port_from, const std::string &to, int port_to)
 Connect two nodes in the graph using port indices.
 
bool new_link (const std::string &from, const std::string &port_label_from, const std::string &to, const std::string &port_label_to)
 Connect two nodes in the graph using port labels.
 
bool remove_link (const std::string &from, int port_from, const std::string &to, int port_to)
 Disconnect two nodes in the graph using port indices.
 
bool remove_link (const std::string &from, const std::string &port_label_from, const std::string &to, const std::string &port_label_to)
 Disconnect two nodes in the graph using port labels.
 
void export_to_graphviz (const std::string &fname="export.dot", const std::string &graph_label="graph")
 Export the graph to a Graphviz DOT file.
 
void export_to_mermaid (const std::string &fname="export.mmd", const std::string &graph_label="graph")
 Export the graph to a Mermaid file.
 
std::map< std::string, std::vector< std::string > > get_connectivity_downstream ()
 Get the downstream connectivity of the graph.
 
std::map< std::string, std::vector< std::string > > get_connectivity_upstream ()
 Get the upstream connectivity of the graph.
 
uint get_id_count () const
 Get the current count of unique identifiers.
 
uintget_id_count_ref ()
 
const std::vector< Link > & get_links () const
 Get the link storage.
 
template<typename T = Node>
T * get_node_ref_by_id (const std::string &node_id) const
 Get a pointer to a node by its ID.
 
const std::map< std::string, std::shared_ptr< Node > > & get_nodes ()
 Get a reference to the nodes map.
 
std::vector< std::string > get_nodes_to_update (const std::string &node_id)
 Get node list for update priority.
 
bool is_node_id_available (const std::string &node_id)
 Check if a node ID is available in the graph.
 
virtual void post_update ()
 Method called after the graph update process is completed.
 
void print ()
 Print the current graph structure.
 
virtual void remove_node (const std::string &id)
 Remove a node from the graph by its ID.
 
void set_id (const std::string &new_id)
 Set the graph ID.
 
void set_id_count (uint new_id_count)
 Set the current count of unique identifiers.
 
void set_update_callback (std::function< void(const std::string &, const std::vector< std::string > &, bool)> new_callback)
 
std::vector< std::string > topological_sort (const std::vector< std::string > &dirty_node_ids)
 Kahn's algorithm for node sorting for update priority.
 
virtual void update ()
 Mark all nodes as dirty and update the entire graph.
 
virtual void update (const std::string &node_id)
 Update a specific node by its ID.
 

Protected Attributes

std::map< std::string, std::shared_ptr< Node > > nodes
 A map of node IDs to shared pointers of Node objects.
 
std::vector< Linklinks
 A list of links between nodes in the graph.
 

Private Attributes

uint id_count = 0
 Keep track of unique identifiers.
 
std::string id = ""
 Graph id.
 
std::function< void(const std::string &current_id, const std::vector< std::string > &sorted_ids, bool before_update)> update_callback = nullptr
 

Detailed Description

The Graph class provides methods for manipulating nodes and connections in a directed graph structure.

Constructor & Destructor Documentation

◆ Graph() [1/2]

gnode::Graph::Graph ( )
default

Construct a new Graph object.

◆ Graph() [2/2]

gnode::Graph::Graph ( const std::string &  id)
inline

Construct a new Graph object.

Parameters
idGraph ID.

◆ ~Graph()

virtual gnode::Graph::~Graph ( )
virtualdefault

Destroy the Graph object.

Member Function Documentation

◆ add_node() [1/2]

template<typename U , typename... Args>
std::string gnode::Graph::add_node ( Args...  args)
inline

Add a new node of a specific type to the graph.

Template Parameters
UNode type derived from Node class.
ArgsArguments for the constructor of the node.
Parameters
argsArguments to pass to the node constructor.
Returns
std::string The ID of the added node.

◆ add_node() [2/2]

virtual std::string gnode::Graph::add_node ( const std::shared_ptr< Node > &  p_node,
const std::string &  id = "" 
)
virtual

Add a new node to the graph.

Parameters
p_nodeShared pointer to the node to be added.
idOptional ID for the node. If empty, an ID will be generated.
Returns
std::string The ID of the added node.

◆ clear()

void gnode::Graph::clear ( )

Clear the graph, remove all the nodes and the links.

◆ compute_graph_layout_sugiyama()

std::vector< Point > gnode::Graph::compute_graph_layout_sugiyama ( )

Compute the layout of the graph using the Sugiyama algorithm.

Returns
std::vector<Point> A vector of points representing the node positions.

◆ export_to_graphviz()

void gnode::Graph::export_to_graphviz ( const std::string &  fname = "export.dot",
const std::string &  graph_label = "graph" 
)

Export the graph to a Graphviz DOT file.

Parameters
fnameFilename of the DOT file.
graph_labelLabel for the graph.

◆ export_to_mermaid()

void gnode::Graph::export_to_mermaid ( const std::string &  fname = "export.mmd",
const std::string &  graph_label = "graph" 
)

Export the graph to a Mermaid file.

Parameters
fnameFilename of the Mermaid file.
graph_labelLabel for the graph.

◆ get_connectivity_downstream()

std::map< std::string, std::vector< std::string > > gnode::Graph::get_connectivity_downstream ( )

Get the downstream connectivity of the graph.

Returns
std::map<std::string, std::vector<std::string>> A map of node IDs and their downstream connections.

◆ get_connectivity_upstream()

std::map< std::string, std::vector< std::string > > gnode::Graph::get_connectivity_upstream ( )

Get the upstream connectivity of the graph.

Returns
std::map<std::string, std::vector<std::string>> A map of node IDs and their upstream connections.

◆ get_id()

std::string gnode::Graph::get_id ( ) const
inline

Return the graph ID.

Returns
std::string ID.

◆ get_id_count()

uint gnode::Graph::get_id_count ( ) const
inline

Get the current count of unique identifiers.

Returns
An available unique identifier.

◆ get_id_count_ref()

uint * gnode::Graph::get_id_count_ref ( )
inline

◆ get_links()

const std::vector< Link > & gnode::Graph::get_links ( ) const
inline

Get the link storage.

Returns
Links.

◆ get_node_ref_by_id()

template<typename T = Node>
T * gnode::Graph::get_node_ref_by_id ( const std::string &  node_id) const
inline

Get a pointer to a node by its ID.

Template Parameters
TNode type, default is Node.
Parameters
node_idID of the node.
Returns
T* Pointer to the node (returns nullptr if the node ID is not found).
Exceptions
std::runtime_errorIf casting the node to the specified type fails.

◆ get_nodes()

const std::map< std::string, std::shared_ptr< Node > > & gnode::Graph::get_nodes ( )
inline

Get a reference to the nodes map.

Returns
Nodes map.

◆ get_nodes_to_update()

std::vector< std::string > gnode::Graph::get_nodes_to_update ( const std::string &  node_id)

Get node list for update priority.

◆ is_node_id_available()

bool gnode::Graph::is_node_id_available ( const std::string &  node_id)

Check if a node ID is available in the graph.

Parameters
idNode ID to check.
Returns
true If the ID is available.
false If the ID is already taken.

◆ new_link() [1/2]

bool gnode::Graph::new_link ( const std::string &  from,
const std::string &  port_label_from,
const std::string &  to,
const std::string &  port_label_to 
)

Connect two nodes in the graph using port labels.

Parameters
fromID of the source node.
port_label_fromLabel of the source node's output port.
toID of the destination node.
port_label_toLabel of the destination node's input port.
Returns
true If the connection was successful.
false If the connection failed.

◆ new_link() [2/2]

bool gnode::Graph::new_link ( const std::string &  from,
int  port_from,
const std::string &  to,
int  port_to 
)

Connect two nodes in the graph using port indices.

Parameters
fromID of the source node.
port_fromIndex of the source node's output port.
toID of the destination node.
port_toIndex of the destination node's input port.
Returns
true If the connection was successful.
false If the connection failed.

◆ post_update()

virtual void gnode::Graph::post_update ( )
inlinevirtual

Method called after the graph update process is completed.

This method can be overridden in derived classes for custom post-update behavior.

◆ print()

void gnode::Graph::print ( )

Print the current graph structure.

◆ remove_link() [1/2]

bool gnode::Graph::remove_link ( const std::string &  from,
const std::string &  port_label_from,
const std::string &  to,
const std::string &  port_label_to 
)

Disconnect two nodes in the graph using port labels.

Parameters
fromThe label of the source node.
port_label_fromThe label of the source node's output port.
toThe label of the destination node.
port_label_toThe label of the destination node's input port.
Returns
true If the disconnection was successful.
false If the disconnection failed.

◆ remove_link() [2/2]

bool gnode::Graph::remove_link ( const std::string &  from,
int  port_from,
const std::string &  to,
int  port_to 
)

Disconnect two nodes in the graph using port indices.

Parameters
fromID of the source node.
port_fromIndex of the source node's output port.
toID of the destination node.
port_toIndex of the destination node's input port.
Returns
true If the disconnection was successful.
false If the disconnection failed.

◆ remove_node()

virtual void gnode::Graph::remove_node ( const std::string &  id)
virtual

Remove a node from the graph by its ID.

Parameters
idID of the node to remove.

◆ set_id()

void gnode::Graph::set_id ( const std::string &  new_id)
inline

Set the graph ID.

Parameters
new_idID.

◆ set_id_count()

void gnode::Graph::set_id_count ( uint  new_id_count)
inline

Set the current count of unique identifiers.

Parameters
new_idAn available unique identifier.

◆ set_update_callback()

void gnode::Graph::set_update_callback ( std::function< void(const std::string &, const std::vector< std::string > &, bool)>  new_callback)
inline

◆ topological_sort()

std::vector< std::string > gnode::Graph::topological_sort ( const std::vector< std::string > &  dirty_node_ids)

Kahn's algorithm for node sorting for update priority.

◆ update() [1/2]

virtual void gnode::Graph::update ( )
virtual

Mark all nodes as dirty and update the entire graph.

This method forces a complete update of the graph, which may be inefficient for large graphs.

◆ update() [2/2]

virtual void gnode::Graph::update ( const std::string &  node_id)
virtual

Update a specific node by its ID.

Parameters
idID of the node to update.
Returns
true If the update was successful.

Member Data Documentation

◆ id

std::string gnode::Graph::id = ""
private

Graph id.

◆ id_count

uint gnode::Graph::id_count = 0
private

Keep track of unique identifiers.

◆ links

std::vector<Link> gnode::Graph::links
protected

A list of links between nodes in the graph.

◆ nodes

std::map<std::string, std::shared_ptr<Node> > gnode::Graph::nodes
protected

A map of node IDs to shared pointers of Node objects.

◆ update_callback

std::function<void(const std::string &current_id, const std::vector<std::string> &sorted_ids, bool before_update)> gnode::Graph::update_callback = nullptr
private

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