The Gaudi Framework
master (9cc87df9)
Toggle main menu visibility
Loading...
Searching...
No Matches
GraphDumper.cpp
Go to the documentation of this file.
1
/***********************************************************************************\
2
* (c) Copyright 1998-2026 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
#include "
GraphDumper.h
"
12
13
namespace
{
14
std::string escapeSpecialCharactersForMl( std::string_view view ) {
15
std::string result;
16
result.
reserve
( view.size() * 6 );
// Worst case: all characters become """ (6 chars)
17
for
(
char
c : view ) {
18
switch
( c ) {
19
case
'&'
:
20
result +=
"&"
;
21
break
;
22
case
'<'
:
23
result +=
"<"
;
24
break
;
25
case
'>'
:
26
result +=
">"
;
27
break
;
28
case
'"'
:
29
result +=
"""
;
30
break
;
31
default
:
32
result.
push_back
( c );
33
break
;
34
}
35
}
36
37
return
std::string{ result };
38
}
39
}
// namespace
40
41
namespace
Gaudi::Hive
{
42
43
Graph::Graph
( std::string_view
fileName
,
FileType
type ) :
m_fileName
{
fileName
},
m_type
{ type } {
44
// find out type of file if not given
45
if
( type ==
FileType::UNKNOWN
) {
46
// Check file extension
47
if
(
m_fileName
.ends_with(
".dot"
) ) {
48
m_type
=
FileType::DOT
;
49
}
else
if
(
m_fileName
.ends_with(
".md"
) ) {
50
m_type
=
FileType::MD
;
51
}
else
if
(
m_fileName
.ends_with(
".graphml"
) ) {
52
m_type
=
FileType::ML
;
53
}
else
{
54
m_type
=
FileType::DOT
;
55
m_fileName
+=
".dot"
;
56
}
57
}
58
// open file
59
m_stream
= std::ofstream{
m_fileName
,
std::ofstream::out
};
60
writeHeader
();
61
}
62
63
Graph::~Graph
() {
64
writeTrailer
();
65
m_stream
.
close
();
66
}
67
68
std::string
const
&
Graph::fileName
() {
return
m_fileName
; }
69
70
void
Graph::addNode
( std::string_view
id
, std::string_view name ) {
71
switch
(
m_type
) {
72
case
FileType::DOT
:
73
m_stream
<<
" "
<<
id
<<
" [label=\""
<< name <<
"\";shape=box];\n"
;
74
break
;
75
case
FileType::MD
:
76
m_stream
<<
" "
<<
id
<<
"{{"
<< name <<
"}}\n"
;
77
break
;
78
case
FileType::ML
:
79
m_stream
<<
" <node id=\""
<<
id
80
<<
"\">\n"
81
" <data key=\"d0\">"
82
<< escapeSpecialCharactersForMl( name )
83
<<
"</data>\n"
84
" </node>\n"
;
85
break
;
86
case
FileType::UNKNOWN
:
87
break
;
88
}
89
};
90
91
void
Graph::addEdge
( std::string_view srcId, std::string_view tgtId, std::string_view label ) {
92
switch
(
m_type
) {
93
case
FileType::DOT
:
94
m_stream
<<
" "
<< srcId <<
" -> "
<< tgtId;
95
if
( label !=
""
)
m_stream
<<
" [label=\""
<< label <<
"\"]"
;
96
m_stream
<<
";\n"
;
97
break
;
98
case
FileType::MD
:
99
m_stream
<<
" "
<< srcId <<
" --> "
<< tgtId;
100
if
( label !=
""
)
m_stream
<<
" : "
<< label;
101
m_stream
<<
"\n"
;
102
break
;
103
case
FileType::ML
:
104
if
( label !=
""
) {
105
m_stream
<<
" <edge source=\""
<< srcId <<
"\" target=\""
<< tgtId
106
<<
"\">\n"
107
" <data key=\"d1\">"
108
<< escapeSpecialCharactersForMl( label )
109
<<
"</data>\n"
110
" </edge>\n"
;
111
}
else
{
112
m_stream
<<
" <edge source=\""
<< srcId <<
"\" target=\""
<< tgtId <<
"\"/>\n"
;
113
}
114
break
;
115
case
FileType::UNKNOWN
:
116
break
;
117
}
118
};
119
120
//---------------------------------------------------------------------------
121
122
void
Graph::writeHeader
() {
123
switch
(
m_type
) {
124
case
FileType::DOT
:
125
m_stream
<<
"digraph datadeps {\n rankdir=\"LR\";\n"
;
126
break
;
127
case
FileType::MD
:
128
m_stream
<<
"```mermaid\ngraph LR;\n\n"
;
129
break
;
130
case
FileType::ML
:
131
m_stream
<<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
132
"<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n"
133
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
134
" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
135
"http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n"
136
" <key id=\"d1\" for=\"edge\" attr.name=\"label\" attr.type=\"string\"/>\n"
137
" <key id=\"d0\" for=\"node\" attr.name=\"label\" attr.type=\"string\"/>\n"
138
" <graph id=\"Dependencies\" edgedefault=\"directed\">\n"
;
139
break
;
140
case
FileType::UNKNOWN
:
141
break
;
142
}
143
}
144
145
void
Graph::writeTrailer
() {
146
switch
(
m_type
) {
147
case
FileType::DOT
:
148
m_stream
<<
"}\n"
;
149
break
;
150
case
FileType::MD
:
151
m_stream
<<
"```\n"
;
152
break
;
153
case
FileType::ML
:
154
m_stream
<<
" </graph>\n</graphml>"
;
155
break
;
156
case
FileType::UNKNOWN
:
157
break
;
158
}
159
}
160
161
}
// end namespace Gaudi::Hive
GraphDumper.h
std::basic_ofstream::close
void close()
std::basic_string::push_back
void push_back(_CharT __c)
std::basic_string::reserve
void reserve()
std::basic_ofstream< char >::out
static const openmode out
Gaudi::Hive::Graph::addNode
void addNode(std::string_view id, std::string_view name)
Definition
GraphDumper.cpp:70
Gaudi::Hive::Graph::~Graph
~Graph()
Definition
GraphDumper.cpp:63
Gaudi::Hive::Graph::m_type
FileType m_type
type of file used
Definition
GraphDumper.h:60
Gaudi::Hive::Graph::addEdge
void addEdge(std::string_view srcId, std::string_view tgtId, std::string_view label="")
Definition
GraphDumper.cpp:91
Gaudi::Hive::Graph::m_fileName
std::string m_fileName
name of the filed used for storing the graph
Definition
GraphDumper.h:56
Gaudi::Hive::Graph::fileName
std::string const & fileName()
Definition
GraphDumper.cpp:68
Gaudi::Hive::Graph::writeHeader
void writeHeader()
Definition
GraphDumper.cpp:122
Gaudi::Hive::Graph::Graph
Graph(std::string_view fileName, FileType type=FileType::UNKNOWN)
Creates a graph with given type and given file name.
Definition
GraphDumper.cpp:43
Gaudi::Hive::Graph::writeTrailer
void writeTrailer()
Definition
GraphDumper.cpp:145
Gaudi::Hive::Graph::m_stream
std::ofstream m_stream
stream to the graph file
Definition
GraphDumper.h:58
Gaudi::Hive
Definition
FetchDataFromFile.cpp:14
Gaudi::Hive::FileType
FileType
Definition
GraphDumper.h:19
Gaudi::Hive::FileType::DOT
@ DOT
Definition
GraphDumper.h:19
Gaudi::Hive::FileType::UNKNOWN
@ UNKNOWN
Definition
GraphDumper.h:19
Gaudi::Hive::FileType::MD
@ MD
Definition
GraphDumper.h:19
Gaudi::Hive::FileType::ML
@ ML
Definition
GraphDumper.h:19
GaudiHive
src
GraphDumper.cpp
Generated on
for The Gaudi Framework by
1.18.0