DPsim
Graph.h
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #pragma once
10 
11 #include <map>
12 
13 #include <graphviz/cgraph.h>
14 
15 #include <dpsim-models/Definitions.h>
16 
17 namespace CPS {
18 namespace Graph {
19 
20 enum class Type { undirected, directed };
21 
22 class Graph;
23 
24 class Element {
25 
26 protected:
27  void *mPtr;
28  int mKind;
29 
30 public:
31  void set(const String &key, const String &value, bool html = false);
32 };
33 
34 class Node : public Element {
35 public:
36  Agnode_t *mNode;
37 
38  Node(Graph *g, const String &name);
39 };
40 
41 class Edge : public Element {
42 public:
43  Agedge_t *mEdge;
44 
45  Edge(Graph *g, const String &name, Node *head, Node *tail);
46 };
47 
52 class Graph : public Element {
53 
54 protected:
55  std::map<String, Node *> mNodes;
56  std::map<String, Edge *> mEdges;
57 
58 public:
59  Agraph_t *mGraph;
60 
61  Graph(const String &name, Type type, bool strict = false);
62  ~Graph();
63 
64  void render(std::ostream &os, const String &layout = "dot",
65  const String &format = "svg");
66 
67  Node *addNode(const String &name);
68  Edge *addEdge(const String &name, Node *head, Node *tail);
69 
70  Node *node(const String &name);
71  Edge *edge(const String &name);
72 };
73 } // namespace Graph
74 } // namespace CPS