The Gaudi Framework  master (d98a2936)
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 
17 namespace Gaudi::Hive {
18 
19  enum class FileType : short { UNKNOWN, DOT, MD, ML };
20 
30  class Graph {
31 
32  public:
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 };
57  writeHeader();
58  }
59 
60  ~Graph() {
61  writeTrailer();
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;
78  case FileType::UNKNOWN:
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;
99  case FileType::UNKNOWN:
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;
121  case FileType::UNKNOWN:
122  break;
123  }
124  }
125 
126  void writeTrailer() {
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;
137  case FileType::UNKNOWN:
138  break;
139  }
140  }
141 
143  std::string m_fileName;
145  std::ofstream m_stream;
148  };
149 
150 } // end namespace Gaudi::Hive
Gaudi::Hive
Definition: FetchDataFromFile.cpp:14
Gaudi::Hive::Graph::addEdge
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
Gaudi::Hive::Graph::addNode
void addNode(std::string_view name, std::string_view id)
Definition: GraphDumper.h:67
Gaudi::Hive::Graph::m_stream
std::ofstream m_stream
stream to the graph file
Definition: GraphDumper.h:145
Gaudi::Hive::FileType
FileType
Definition: GraphDumper.h:19
Gaudi::Hive::Graph
utilities to dump graphs in different formats
Definition: GraphDumper.h:30
Gaudi::Hive::Graph::m_type
FileType m_type
type of file used
Definition: GraphDumper.h:147
Gaudi::Hive::Graph::fileName
std::string const & fileName()
Definition: GraphDumper.h:65
Gaudi::Hive::FileType::DOT
@ DOT
Gaudi::Hive::FileType::UNKNOWN
@ UNKNOWN
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.h:40
Gaudi::Hive::Graph::writeHeader
void writeHeader()
Definition: GraphDumper.h:105
Gaudi::Hive::Graph::writeTrailer
void writeTrailer()
Definition: GraphDumper.h:126
Gaudi::Hive::Graph::~Graph
~Graph()
Definition: GraphDumper.h:60
gaudirun.type
type
Definition: gaudirun.py:160
Gaudi::Hive::FileType::ML
@ ML
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Gaudi::Hive::FileType::MD
@ MD
Gaudi::Hive::Graph::m_fileName
std::string m_fileName
name of the filed used for storing the graph
Definition: GraphDumper.h:143
Gaudi::Functional::details::out
OptOut && out
Definition: details.h:179