20#include <unordered_set>
62 virtual std::string
add_node(
const std::shared_ptr<Node> &p_node,
63 const std::string &
id =
"");
73 template <
typename U,
typename... Args> std::string
add_node(Args... args)
75 return this->
add_node(std::make_shared<U>(args...));
96 std::string
get_id()
const {
return this->
id; };
110 const std::string &to,
124 const std::string &port_label_from,
125 const std::string &to,
126 const std::string &port_label_to);
140 const std::string &to,
154 const std::string &port_label_from,
155 const std::string &to,
156 const std::string &port_label_to);
165 const std::string &graph_label =
"graph");
174 const std::string &graph_label =
"graph");
190 const std::string &node_id)
const;
206 const std::string &node_id)
const;
247 template <
typename T = Node>
250 auto it =
nodes.find(node_id);
251 if (it ==
nodes.end())
return nullptr;
253 T *ptr =
dynamic_cast<T *
>(it->second.get());
255 throw std::runtime_error(
"Failed to cast node with ID: " + node_id +
256 " to the specified type.");
266 const std::map<std::string, std::shared_ptr<Node>> &
get_nodes()
273 const std::vector<std::string> &node_ids);
300 const std::string &target,
301 std::unordered_set<std::string> visited = {})
const;
328 void set_id(
const std::string &new_id) { this->
id = new_id; };
338 const std::vector<std::string> &,
346 const std::vector<std::string> &dirty_node_ids);
361 virtual void update(
const std::string &node_id);
368 virtual void update(
const std::vector<std::string> &node_ids);
374 std::map<std::string, std::shared_ptr<Node>>
nodes;
392 std::function<void(
const std::string ¤t_id,
393 const std::vector<std::string> &sorted_ids,
400template <
typename T>
bool contains(
const std::vector<T> &vec,
const T &value)
402 return std::find(vec.begin(), vec.end(), value) != vec.end();
The Graph class provides methods for manipulating nodes and connections in a directed graph structure...
Definition graph.hpp:36
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.
std::vector< LinkView > get_link_views(const std::string &node_id) const
Returns the link views connected to a node.
virtual ~Graph()=default
Destroy the Graph object.
std::string add_node(Args... args)
Add a new node of a specific type to the graph.
Definition graph.hpp:73
virtual void update()
Mark all nodes as dirty and update the entire graph.
void set_id(const std::string &new_id)
Set the graph ID.
Definition graph.hpp:328
std::vector< Link > links
A list of links between nodes in the graph.
Definition graph.hpp:379
std::map< std::string, std::vector< std::string > > get_connectivity_downstream()
Get the downstream connectivity of the graph.
uint * get_id_count_ref()
Definition graph.hpp:215
uint get_id_count() const
Get the current count of unique identifiers.
Definition graph.hpp:213
void set_update_callback(std::function< void(const std::string &, const std::vector< std::string > &, bool)> new_callback)
Definition graph.hpp:337
std::string id
Graph id.
Definition graph.hpp:390
std::vector< std::string > get_connectivity_downstream(const std::string &node_id) const
Returns the downstream nodes connected to a node.
void print()
Print the current graph structure.
void export_to_mermaid(const std::string &fname="export.mmd", const std::string &graph_label="graph")
Export the graph to a Mermaid file.
std::vector< Point > compute_graph_layout_sugiyama()
Compute the layout of the graph using the Sugiyama algorithm.
uint id_count
Keep track of unique identifiers.
Definition graph.hpp:385
std::vector< std::string > get_nodes_to_update(const std::string &node_id)
void clear()
Clear the graph, remove all the nodes and the links.
bool is_node_id_available(const std::string &node_id)
Check if a node ID is available in the graph.
virtual void remove_node(const std::string &id)
Remove a node from the graph by its ID.
std::map< std::string, std::shared_ptr< Node > > nodes
A map of node IDs to shared pointers of Node objects.
Definition graph.hpp:374
virtual void update(const std::vector< std::string > &node_ids)
Update a specific list of node IDs.
std::vector< std::string > get_nodes_to_update(const std::vector< std::string > &node_ids)
Get node list for update priority.
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.
std::string get_id() const
Return the graph ID.
Definition graph.hpp:96
Graph(const std::string &id)
Construct a new Graph object.
Definition graph.hpp:48
const std::vector< Link > & get_links() const
Get the link storage.
Definition graph.hpp:222
virtual void post_update()
Method called after the graph update process is completed.
Definition graph.hpp:309
std::function< void(const std::string ¤t_id, const std::vector< std::string > &sorted_ids, bool before_update)> update_callback
Definition graph.hpp:395
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.
Graph()=default
Construct a new Graph object.
const std::map< std::string, std::shared_ptr< Node > > & get_nodes()
Get a reference to the nodes map.
Definition graph.hpp:266
virtual std::string add_node(const std::shared_ptr< Node > &p_node, const std::string &id="")
Add a new node to the graph.
std::vector< std::string > topological_sort(const std::vector< std::string > &dirty_node_ids)
Kahn's algorithm for node sorting for update priority.
T * get_node_ref_by_id(const std::string &node_id) const
Get a pointer to a node by its ID.
Definition graph.hpp:248
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.
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.
std::vector< std::string > get_connectivity_upstream(const std::string &node_id) const
Returns the upstream nodes connected to a node.
virtual void update(const std::string &node_id)
Update a specific node by its ID.
void set_id_count(uint new_id_count)
Set the current count of unique identifiers.
Definition graph.hpp:335
std::map< std::string, std::vector< std::string > > get_connectivity_upstream()
Get the upstream connectivity of the graph.
unsigned int uint
Definition graph.hpp:26
bool contains(const std::vector< T > &vec, const T &value)
Definition graph.hpp:400
Defines the Point struct used to represent a 2D point in space.