The Gaudi Framework master (9cc87df9)
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
13namespace {
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 += "&lt;";
24 break;
25 case '>':
26 result += "&gt;";
27 break;
28 case '"':
29 result += "&quot;";
30 break;
31 default:
32 result.push_back( c );
33 break;
34 }
35 }
36
37 return std::string{ result };
38 }
39} // namespace
40
41namespace 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" ) ) {
49 } else if ( m_fileName.ends_with( ".md" ) ) {
51 } else if ( m_fileName.ends_with( ".graphml" ) ) {
53 } else {
55 m_fileName += ".dot";
56 }
57 }
58 // open file
59 m_stream = std::ofstream{ m_fileName, std::ofstream::out };
61 }
62
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;
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;
116 break;
117 }
118 };
119
120 //---------------------------------------------------------------------------
121
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;
141 break;
142 }
143 }
144
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;
157 break;
158 }
159 }
160
161} // end namespace Gaudi::Hive
void push_back(_CharT __c)
static const openmode out
void addNode(std::string_view id, std::string_view name)
FileType m_type
type of file used
Definition GraphDumper.h:60
void addEdge(std::string_view srcId, std::string_view tgtId, std::string_view label="")
std::string m_fileName
name of the filed used for storing the graph
Definition GraphDumper.h:56
std::string const & fileName()
Graph(std::string_view fileName, FileType type=FileType::UNKNOWN)
Creates a graph with given type and given file name.
std::ofstream m_stream
stream to the graph file
Definition GraphDumper.h:58