9 #include <graphviz/gvc.h>
11 #include <dpsim-models/Graph.h>
14 using namespace CPS::Graph;
16 CPS::Graph::Graph::Graph(
const CPS::String &name, Type type,
bool strict) {
21 desc = strict ? Agstrictdirected : Agdirected;
23 case Type::undirected:
24 desc = strict ? Agstrictundirected : Agundirected;
28 mGraph = agopen((
char *)name.c_str(), desc, NULL);
33 CPS::Graph::Graph::~Graph() { agclose(mGraph); }
35 void CPS::Graph::Graph::render(std::ostream &os,
const CPS::String &layout,
36 const CPS::String &format) {
43 gvLayout(gvc, mGraph, layout.c_str());
44 gvRenderData(gvc, mGraph, format.c_str(), &data, &len);
48 gvFreeRenderData(data);
49 gvFreeLayout(gvc, mGraph);
54 Node *CPS::Graph::Graph::addNode(
const CPS::String &name) {
55 auto n =
new Node(
this, name);
62 Edge *CPS::Graph::Graph::addEdge(
const CPS::String &name,
Node *head,
64 auto e =
new Edge(
this, name, head, tail);
71 Node *CPS::Graph::Graph::node(
const CPS::String &name) {
return mNodes[name]; }
73 Edge *CPS::Graph::Graph::edge(
const CPS::String &name) {
return mEdges[name]; }
75 void CPS::Graph::Element::set(
const CPS::String &key,
const CPS::String &value,
77 Agraph_t *g = agraphof(mPtr);
80 char *k = (
char *)key.c_str();
81 char *v = (
char *)value.c_str();
82 char *vd = html ? agstrdup_html(g, v) : agstrdup(g, v);
84 agsafeset(mPtr, k, vd, d);
87 CPS::Graph::Node::Node(
Graph *g,
const String &name) {
88 mNode = agnode(g->mGraph, (
char *)name.c_str(), 1);
93 CPS::Graph::Edge::Edge(
Graph *g,
const CPS::String &name,
Node *head,
95 mEdge = agedge(g->mGraph, head->mNode, tail->mNode, (
char *)name.c_str(), 1);