|
GNode library (C++)
|
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< Point > | compute_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::vector< std::string > | get_connectivity_downstream (const std::string &node_id) const |
| Returns the downstream nodes connected to a node. | |
| std::map< std::string, std::vector< std::string > > | get_connectivity_upstream () |
| Get the upstream connectivity of the graph. | |
| std::vector< std::string > | get_connectivity_upstream (const std::string &node_id) const |
| Returns the upstream nodes connected to a node. | |
| uint | get_id_count () const |
| Get the current count of unique identifiers. | |
| uint * | get_id_count_ref () |
| const std::vector< Link > & | get_links () const |
| Get the link storage. | |
| std::vector< LinkView > | get_link_views (const std::string &node_id) const |
| Returns the link views connected to a node. | |
| 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::vector< std::string > &node_ids) |
| Get node list for update priority. | |
| std::vector< std::string > | get_nodes_to_update (const std::string &node_id) |
| bool | is_node_id_available (const std::string &node_id) |
| Check if a node ID is available in the graph. | |
| bool | is_reachable (const std::string &start, const std::string &target, std::unordered_set< std::string > visited={}) const |
| Checks whether a target node is reachable from a start node. | |
| 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. | |
| virtual void | update (const std::vector< std::string > &node_ids) |
| Update a specific list of node IDs. | |
Protected Attributes | |
| std::map< std::string, std::shared_ptr< Node > > | nodes |
| A map of node IDs to shared pointers of Node objects. | |
| std::vector< Link > | links |
| 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 ¤t_id, const std::vector< std::string > &sorted_ids, bool before_update)> | update_callback = nullptr |
The Graph class provides methods for manipulating nodes and connections in a directed graph structure.
|
default |
Construct a new Graph object.
|
inline |
|
virtualdefault |
Destroy the Graph object.
|
inline |
|
virtual |
Add a new node to the graph.
| p_node | Shared pointer to the node to be added. |
| id | Optional ID for the node. If empty, an ID will be generated. |
| void gnode::Graph::clear | ( | ) |
Clear the graph, remove all the nodes and the links.
| std::vector< Point > gnode::Graph::compute_graph_layout_sugiyama | ( | ) |
Compute the layout of the graph using the Sugiyama algorithm.
| 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.
| fname | Filename of the DOT file. |
| graph_label | Label for the graph. |
| 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.
| fname | Filename of the Mermaid file. |
| graph_label | Label for the graph. |
| std::map< std::string, std::vector< std::string > > gnode::Graph::get_connectivity_downstream | ( | ) |
Get the downstream connectivity of the graph.
| std::vector< std::string > gnode::Graph::get_connectivity_downstream | ( | const std::string & | node_id | ) | const |
Returns the downstream nodes connected to a node.
| node_id | Identifier of the source node. |
| std::map< std::string, std::vector< std::string > > gnode::Graph::get_connectivity_upstream | ( | ) |
Get the upstream connectivity of the graph.
| std::vector< std::string > gnode::Graph::get_connectivity_upstream | ( | const std::string & | node_id | ) | const |
Returns the upstream nodes connected to a node.
| node_id | Identifier of the destination node. |
|
inline |
Return the graph ID.
|
inline |
Get the current count of unique identifiers.
|
inline |
| std::vector< LinkView > gnode::Graph::get_link_views | ( | const std::string & | node_id | ) | const |
Returns the link views connected to a node.
This function collects all links where the specified node is either the source or destination node and converts them into LinkView objects enriched with node and port information.
Invalid links referencing missing nodes are ignored.
| node_id | Identifier of the target node. |
LinkView objects.
|
inline |
Get the link storage.
|
inline |
|
inline |
Get a reference to the nodes map.
| std::vector< std::string > gnode::Graph::get_nodes_to_update | ( | const std::string & | node_id | ) |
| std::vector< std::string > gnode::Graph::get_nodes_to_update | ( | const std::vector< std::string > & | node_ids | ) |
Get node list for update priority.
| bool gnode::Graph::is_node_id_available | ( | const std::string & | node_id | ) |
Check if a node ID is available in the graph.
| id | Node ID to check. |
| bool gnode::Graph::is_reachable | ( | const std::string & | start, |
| const std::string & | target, | ||
| std::unordered_set< std::string > | visited = {} |
||
| ) | const |
Checks whether a target node is reachable from a start node.
This function recursively traverses downstream connections to determine whether a path exists between two nodes. It can also be used to detect graph cycles (using start = node_to and target = node_from, i.e. "backwards").
| start | Identifier of the starting node. |
| target | Identifier of the target node. |
| visited | Set of already visited node identifiers. |
true if the target node is reachable, otherwise false. | 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.
| from | ID of the source node. |
| port_label_from | Label of the source node's output port. |
| to | ID of the destination node. |
| port_label_to | Label of the destination node's input port. |
| 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.
| from | ID of the source node. |
| port_from | Index of the source node's output port. |
| to | ID of the destination node. |
| port_to | Index of the destination node's input port. |
|
inlinevirtual |
Method called after the graph update process is completed.
This method can be overridden in derived classes for custom post-update behavior.
| void gnode::Graph::print | ( | ) |
Print the current graph structure.
| 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.
| from | The label of the source node. |
| port_label_from | The label of the source node's output port. |
| to | The label of the destination node. |
| port_label_to | The label of the destination node's input port. |
| 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.
| from | ID of the source node. |
| port_from | Index of the source node's output port. |
| to | ID of the destination node. |
| port_to | Index of the destination node's input port. |
|
virtual |
Remove a node from the graph by its ID.
| id | ID of the node to remove. |
|
inline |
Set the graph ID.
| new_id | ID. |
|
inline |
Set the current count of unique identifiers.
| new_id | An available unique identifier. |
|
inline |
| 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.
|
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.
|
virtual |
Update a specific node by its ID.
| id | ID of the node to update. |
|
virtual |
Update a specific list of node IDs.
| id | IDs of the nodes to update. |
|
private |
Graph id.
|
private |
Keep track of unique identifiers.
|
protected |
A list of links between nodes in the graph.
|
protected |
A map of node IDs to shared pointers of Node objects.
|
private |