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
21#include "gnode/link.hpp"
22#include "gnode/node.hpp"
23#include "gnode/point.hpp"
24
25typedef unsigned int uint;
26
27namespace gnode
28{
29
34class Graph
35{
36public:
40 Graph() = default;
41
47 Graph(const std::string &id) : id(id) {}
48
52 virtual ~Graph() = default;
53
61 virtual std::string add_node(const std::shared_ptr<Node> &p_node,
62 const std::string &id = "");
63
72 template <typename U, typename... Args> std::string add_node(Args... args)
73 {
74 return this->add_node(std::make_shared<U>(args...));
75 }
76
80 void clear();
81
88 std::vector<Point> compute_graph_layout_sugiyama();
89
95 std::string get_id() const { return this->id; };
96
107 bool new_link(const std::string &from,
108 int port_from,
109 const std::string &to,
110 int port_to);
111
122 bool new_link(const std::string &from,
123 const std::string &port_label_from,
124 const std::string &to,
125 const std::string &port_label_to);
126
137 bool remove_link(const std::string &from,
138 int port_from,
139 const std::string &to,
140 int port_to);
141
152 bool remove_link(const std::string &from,
153 const std::string &port_label_from,
154 const std::string &to,
155 const std::string &port_label_to);
156
163 void export_to_graphviz(const std::string &fname = "export.dot",
164 const std::string &graph_label = "graph");
165
172 void export_to_mermaid(const std::string &fname = "export.mmd",
173 const std::string &graph_label = "graph");
174
181 std::map<std::string, std::vector<std::string>> get_connectivity_downstream();
182
189 std::map<std::string, std::vector<std::string>> get_connectivity_upstream();
190
196 uint get_id_count() const { return this->id_count; }
197
198 uint *get_id_count_ref() { return &this->id_count; }
199
205 const std::vector<Link> &get_links() const { return this->links; }
206
216 template <typename T = Node>
217 T *get_node_ref_by_id(const std::string &node_id) const
218 {
219 auto it = nodes.find(node_id);
220 if (it == nodes.end()) return nullptr;
221
222 T *ptr = dynamic_cast<T *>(it->second.get());
223 if (!ptr)
224 throw std::runtime_error("Failed to cast node with ID: " + node_id +
225 " to the specified type.");
226
227 return ptr;
228 }
229
235 const std::map<std::string, std::shared_ptr<Node>> &get_nodes()
236 {
237 return this->nodes;
238 }
239
241 std::vector<std::string> get_nodes_to_update(const std::string &node_id);
242
250 bool is_node_id_available(const std::string &node_id);
251
258 virtual void post_update() {}
259
263 void print();
264
270 virtual void remove_node(const std::string &id);
271
277 void set_id(const std::string &new_id) { this->id = new_id; };
278
284 void set_id_count(uint new_id_count) { this->id_count = new_id_count; }
285
286 void set_update_callback(std::function<void(const std::string &,
287 const std::vector<std::string> &,
288 bool)> new_callback)
289 {
290 this->update_callback = new_callback;
291 }
292
294 std::vector<std::string> topological_sort(
295 const std::vector<std::string> &dirty_node_ids);
296
303 virtual void update();
304
311 virtual void update(const std::string &node_id);
312
313protected:
317 std::map<std::string, std::shared_ptr<Node>> nodes;
318
322 std::vector<Link> links;
323
324private:
329
333 std::string id = "";
334
335 std::function<void(const std::string &current_id,
336 const std::vector<std::string> &sorted_ids,
337 bool before_update)>
339};
340
341// helper
342
343template <typename T> bool contains(const std::vector<T> &vec, const T &value)
344{
345 return std::find(vec.begin(), vec.end(), value) != vec.end();
346}
347
348} // namespace gnode
The Graph class provides methods for manipulating nodes and connections in a directed graph structure...
Definition graph.hpp:35
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.
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:72
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:277
std::vector< Link > links
A list of links between nodes in the graph.
Definition graph.hpp:322
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:198
uint get_id_count() const
Get the current count of unique identifiers.
Definition graph.hpp:196
void set_update_callback(std::function< void(const std::string &, const std::vector< std::string > &, bool)> new_callback)
Definition graph.hpp:286
std::string id
Graph id.
Definition graph.hpp:333
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:328
std::vector< std::string > get_nodes_to_update(const std::string &node_id)
Get node list for update priority.
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:317
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:95
Graph(const std::string &id)
Construct a new Graph object.
Definition graph.hpp:47
const std::vector< Link > & get_links() const
Get the link storage.
Definition graph.hpp:205
virtual void post_update()
Method called after the graph update process is completed.
Definition graph.hpp:258
std::function< void(const std::string &current_id, const std::vector< std::string > &sorted_ids, bool before_update)> update_callback
Definition graph.hpp:338
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:235
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:217
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.
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:284
std::map< std::string, std::vector< std::string > > get_connectivity_upstream()
Get the upstream connectivity of the graph.
unsigned int uint
Definition graph.hpp:25
Definition data.hpp:23
bool contains(const std::vector< T > &vec, const T &value)
Definition graph.hpp:343
Defines the Point struct used to represent a 2D point in space.