The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
GraphDumper.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
13#include <fstream>
14#include <string>
15#include <string_view>
16
17namespace Gaudi::Hive {
18
19 enum class FileType : short { UNKNOWN, DOT, MD, ML };
20
30 class Graph {
31
32 public:
40 Graph( std::string_view fileName, FileType type = FileType::UNKNOWN ) : m_fileName{ fileName }, m_type{ type } {
41 // find out type of file if not given
42 if ( type == FileType::UNKNOWN ) {
43 // Check file extension
44 if ( m_fileName.ends_with( ".dot" ) ) {
46 } else if ( m_fileName.ends_with( ".md" ) ) {
48 } else if ( m_fileName.ends_with( ".graphml" ) ) {
50 } else {
52 m_fileName += ".dot";
53 }
54 }
55 // open file
56 m_stream = std::ofstream{ m_fileName, std::ofstream::out };
58 }
59
62 m_stream.close();
63 }
64
65 std::string const& fileName() { return m_fileName; }
66
67 void addNode( std::string_view name, std::string_view id ) {
68 switch ( m_type ) {
69 case FileType::DOT:
70 m_stream << " " << id << " [label=\"" << name << "\";shape=box];\n";
71 break;
72 case FileType::MD:
73 m_stream << " " << id << "{{" << name << "}}\n";
74 break;
75 case FileType::ML:
76 m_stream << " <node id=\"" << name << "\"/>\n";
77 break;
79 break;
80 }
81 };
82
83 void addEdge( std::string_view srcName, std::string_view srcId, std::string_view tgtName, std::string_view tgtId,
84 std::string_view label = "" ) {
85 switch ( m_type ) {
86 case FileType::DOT:
87 m_stream << " " << srcId << " -> " << tgtId;
88 if ( label != "" ) m_stream << " [label=\"" << label << "\"]";
89 m_stream << ";\n";
90 break;
91 case FileType::MD:
92 m_stream << " " << srcId << " --> " << tgtId;
93 if ( label != "" ) m_stream << " : " << label;
94 m_stream << "\n";
95 break;
96 case FileType::ML:
97 m_stream << " <edge source=\"" << srcName << "\" target=\"" << tgtName << "\"/>\n";
98 break;
100 break;
101 }
102 };
103
104 private:
105 void writeHeader() {
106 switch ( m_type ) {
107 case FileType::DOT:
108 m_stream << "digraph datadeps {\n rankdir=\"LR\";\n";
109 break;
110 case FileType::MD:
111 m_stream << "```mermaid\ngraph LR;\n\n";
112 break;
113 case FileType::ML:
114 m_stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
115 "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n"
116 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
117 " xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
118 "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n"
119 " <graph id=\"Data dependencies\" edgedefault=\"directed\">\n";
120 break;
122 break;
123 }
124 }
125
127 switch ( m_type ) {
128 case FileType::DOT:
129 m_stream << "}\n";
130 break;
131 case FileType::MD:
132 m_stream << "```\n";
133 break;
134 case FileType::ML:
135 m_stream << " </graph>\n</graphml>";
136 break;
138 break;
139 }
140 }
141
143 std::string m_fileName;
145 std::ofstream m_stream;
148 };
149
150} // end namespace Gaudi::Hive
FileType m_type
type of file used
std::string m_fileName
name of the filed used for storing the graph
std::string const & fileName()
Definition GraphDumper.h:65
void addEdge(std::string_view srcName, std::string_view srcId, std::string_view tgtName, std::string_view tgtId, std::string_view label="")
Definition GraphDumper.h:83
void addNode(std::string_view name, std::string_view id)
Definition GraphDumper.h:67
Graph(std::string_view fileName, FileType type=FileType::UNKNOWN)
Creates a graph with given type and given file name.
Definition GraphDumper.h:40
std::ofstream m_stream
stream to the graph file