61 virtual std::string
add_node(
const std::shared_ptr<Node> &p_node,
62 const std::string &
id =
"");
72 template <
typename U,
typename... Args> std::string
add_node(Args... args)
74 return this->
add_node(std::make_shared<U>(args...));
95 std::string
get_id()
const {
return this->
id; };
109 const std::string &to,
123 const std::string &port_label_from,
124 const std::string &to,
125 const std::string &port_label_to);
139 const std::string &to,
153 const std::string &port_label_from,
154 const std::string &to,
155 const std::string &port_label_to);
164 const std::string &graph_label =
"graph");
173 const std::string &graph_label =
"graph");
216 template <
typename T = Node>
219 auto it =
nodes.find(node_id);
220 if (it ==
nodes.end())
return nullptr;
222 T *ptr =
dynamic_cast<T *
>(it->second.get());
224 throw std::runtime_error(
"Failed to cast node with ID: " + node_id +
225 " to the specified type.");
235 const std::map<std::string, std::shared_ptr<Node>> &
get_nodes()
277 void set_id(
const std::string &new_id) { this->
id = new_id; };
287 const std::vector<std::string> &,
295 const std::vector<std::string> &dirty_node_ids);
311 virtual void update(
const std::string &node_id);
317 std::map<std::string, std::shared_ptr<Node>>
nodes;
335 std::function<void(
const std::string ¤t_id,
336 const std::vector<std::string> &sorted_ids,
343template <
typename T>
bool contains(
const std::vector<T> &vec,
const T &value)
345 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: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 ¤t_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
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.