GNode library (C++)
Loading...
Searching...
No Matches
graph.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 * Public License. The full license is in the file LICENSE, distributed with
3 * this software. */
4
16#pragma once
17#include <functional>
18#include <map>
19#include <memory>
20#include <unordered_set>
21
22#include "gnode/link.hpp"
23#include "gnode/node.hpp"
24#include "gnode/point.hpp"
25
26typedef unsigned int uint;
27
28namespace gnode
29{
30
35class Graph
36{
37public:
41 Graph() = default;
42
48 Graph(const std::string &id) : id(id) {}
49
53 virtual ~Graph() = default;
54
62 virtual std::string add_node(const std::shared_ptr<Node> &p_node,
63 const std::string &id = "");
64
73 template <typename U, typename... Args> std::string add_node(Args... args)
74 {
75 return this->add_node(std::make_shared<U>(args...));
76 }
77
81 void clear();
82
89 std::vector<Point> compute_graph_layout_sugiyama();
90
96 std::string get_id() const { return this->id; };
97
108 bool new_link(const std::string &from,
109 int port_from,
110 const std::string &to,
111 int port_to);
112
123 bool new_link(const std::string &from,
124 const std::string &port_label_from,
125 const std::string &to,
126 const std::string &port_label_to);
127
138 bool remove_link(const std::string &from,
139 int port_from,
140 const std::string &to,
141 int port_to);
142
153 bool remove_link(const std::string &from,
154 const std::string &port_label_from,
155 const std::string &to,
156 const std::string &port_label_to);
157
164 void export_to_graphviz(const std::string &fname = "export.dot",
165 const std::string &graph_label = "graph");
166
173 void export_to_mermaid(const std::string &fname = "export.mmd",
174 const std::string &graph_label = "graph");
175
182 std::map<std::string, std::vector<std::string>> get_connectivity_downstream()
183 const;
184
190 std::vector<std::string> get_connectivity_downstream(
191 const std::string &node_id) const;
192
199 std::map<std::string, std::vector<std::string>> get_connectivity_upstream()
200 const;
201
207 std::vector<std::string> get_connectivity_upstream(
208 const std::string &node_id) const;
209
215 uint get_id_count() const { return this->id_count; }
216
217 uint *get_id_count_ref() { return &this->id_count; }
218
224 const std::vector<Link> &get_links() const { return this->links; }
225
238 std::vector<LinkView> get_link_views(const std::string &node_id) const;
239
244 Node *get_node(const std::string &node_id) const
245 {
246 auto it = nodes.find(node_id);
247 return (it != nodes.end()) ? it->second.get() : nullptr;
248 }
249
259 template <typename T = Node>
260 T *get_node_ref_by_id(const std::string &node_id) const
261 {
262 auto it = nodes.find(node_id);
263 if (it == nodes.end()) return nullptr;
264
265 T *ptr = dynamic_cast<T *>(it->second.get());
266 if (!ptr)
267 throw std::runtime_error("Failed to cast node with ID: " + node_id +
268 " to the specified type.");
269
270 return ptr;
271 }
272
278 const std::map<std::string, std::shared_ptr<Node>> &get_nodes()
279 {
280 return this->nodes;
281 }
282
284 std::vector<std::string> get_nodes_to_update(
285 const std::vector<std::string> &node_ids);
286
287 std::vector<std::string> get_nodes_to_update(const std::string &node_id);
288
308 bool has_cycle() const;
309
317 bool is_node_id_available(const std::string &node_id);
318
332 bool is_reachable(const std::string &start,
333 const std::string &target,
334 std::unordered_set<std::string> visited = {}) const;
335
342 virtual void post_update() {}
343
347 void print();
348
354 virtual void remove_node(const std::string &id);
355
361 void set_id(const std::string &new_id) { this->id = new_id; };
362
368 void set_id_count(uint new_id_count) { this->id_count = new_id_count; }
369
370 void set_update_callback(std::function<void(const std::string &,
371 const std::vector<std::string> &,
372 bool)> new_callback)
373 {
374 this->update_callback = new_callback;
375 }
376
378 std::vector<std::string> topological_sort(
379 const std::vector<std::string> &dirty_node_ids) const;
380
387 virtual void update();
388
394 virtual void update(const std::string &node_id);
395
401 virtual void update(const std::vector<std::string> &node_ids);
402
403protected:
407 std::map<std::string, std::shared_ptr<Node>> nodes;
408
412 std::vector<Link> links;
413
414private:
419
423 std::string id = "";
424
425 std::function<void(const std::string &current_id,
426 const std::vector<std::string> &sorted_ids,
427 bool before_update)>
429};
430
431// helper
432
433template <typename T> bool contains(const std::vector<T> &vec, const T &value)
434{
435 return std::find(vec.begin(), vec.end(), value) != vec.end();
436}
437
438} // namespace gnode
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:361
std::vector< Link > links
A list of links between nodes in the graph.
Definition graph.hpp:412
uint * get_id_count_ref()
Definition graph.hpp:217
uint get_id_count() const
Get the current count of unique identifiers.
Definition graph.hpp:215
void set_update_callback(std::function< void(const std::string &, const std::vector< std::string > &, bool)> new_callback)
Definition graph.hpp:370
std::string id
Graph id.
Definition graph.hpp:423
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:418
std::vector< std::string > get_nodes_to_update(const std::string &node_id)
bool has_cycle() const
Checks whether the graph contains a cycle.
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.
Node * get_node(const std::string &node_id) const
Get a pointer to a node by its ID.
Definition graph.hpp:244
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:407
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:224
virtual void post_update()
Method called after the graph update process is completed.
Definition graph.hpp:342
std::function< void(const std::string &current_id, const std::vector< std::string > &sorted_ids, bool before_update)> update_callback
Definition graph.hpp:428
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:278
virtual std::string add_node(const std::shared_ptr< Node > &p_node, const std::string &id="")
Add a new node to the graph.
T * get_node_ref_by_id(const std::string &node_id) const
Get a pointer to a node by its ID.
Definition graph.hpp:260
std::map< std::string, std::vector< std::string > > get_connectivity_upstream() const
Get the upstream connectivity of 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.
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 > topological_sort(const std::vector< std::string > &dirty_node_ids) const
Kahn's algorithm for node sorting for update priority.
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.
std::map< std::string, std::vector< std::string > > get_connectivity_downstream() const
Get the downstream connectivity of the graph.
void set_id_count(uint new_id_count)
Set the current count of unique identifiers.
Definition graph.hpp:368
Abstract Node class that represents a basic building block in a graph-based system.
Definition node.hpp:34
unsigned int uint
Definition graph.hpp:26
Definition data.hpp:23
bool contains(const std::vector< T > &vec, const T &value)
Definition graph.hpp:433
Defines the Point struct used to represent a 2D point in space.