aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
dotWriter.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
47#include <charconv>
48
51
52namespace gum::dot {
53
54 namespace {
55 // letters, digits and underscores, not starting with a digit
56 bool _isIdentifier_(std::string_view key) {
57 if (key.empty() || (key.front() >= '0' && key.front() <= '9')) return false;
58 for (const char c: key) {
59 const bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
60 || c == '_';
61 if (!ok) return false;
62 }
63 return true;
64 }
65 } // namespace
66
67 DotWriter::DotWriter(bool directed, std::string_view name) : _directed_(directed) {
68 _buffer_ = directed ? "digraph" : "graph";
69 if (!name.empty()) {
70 _buffer_ += ' ';
71 _buffer_ += quote(name);
72 }
73 _buffer_ += " {\n";
74 }
75
76 std::string DotWriter::quote(std::string_view text) {
77 std::string result;
78 result.reserve(text.size() + 2);
79 result += '"';
80 for (const char c: text) {
81 switch (c) {
82 case '"' : result += "\\\""; break;
83 case '\\' : result += "\\\\"; break;
84 case '\n' : result += "\\n"; break;
85 case '\r' : result += "\\r"; break;
86 default : result += c;
87 }
88 }
89 result += '"';
90 return result;
91 }
92
93 std::string DotWriter::number(double value) {
94 char buffer[40];
95 const auto [end, error] = std::to_chars(buffer, buffer + sizeof(buffer), value);
96 return std::string(buffer, error == std::errc() ? end : buffer);
97 }
98
99 std::string DotWriter::_indent_() const { return std::string(2 * (_depth_ + 1), ' '); }
100
101 std::string DotWriter::_attribute_(const DotAttribute& attribute) {
102 if (!_isIdentifier_(attribute.key)) {
103 GUM_ERROR(InvalidArgument, "'" << attribute.key << "' is not a valid key of dot attribute.")
104 }
105 return attribute.key + '=' + (attribute.verbatim ? attribute.value : quote(attribute.value));
106 }
107
108 std::string DotWriter::_attributes_(const DotAttributes& attributes) {
109 std::string result = "[";
110 for (std::size_t i = 0; i < attributes.size(); ++i) {
111 if (i > 0) result += ", ";
112 result += _attribute_(attributes[i]);
113 }
114 return result + ']';
115 }
116
117 void DotWriter::_defaults_(const char* keyword, const DotAttributes& attributes) {
118 if (attributes.empty()) return;
119 _buffer_ += _indent_() + keyword + ' ' + _attributes_(attributes) + ";\n";
120 }
121
123 _defaults_("graph", attributes);
124 }
125
126 void DotWriter::nodeDefaults(const DotAttributes& attributes) { _defaults_("node", attributes); }
127
128 void DotWriter::edgeDefaults(const DotAttributes& attributes) { _defaults_("edge", attributes); }
129
131 _buffer_ += _indent_() + _attribute_(attribute) + ";\n";
132 }
133
134 void DotWriter::_node_(const std::string& id, const DotAttributes& attributes) {
135 std::string statement = _indent_() + id;
136 if (!attributes.empty()) statement += ' ' + _attributes_(attributes);
137 _buffer_ += statement + ";\n";
138 }
139
140 void DotWriter::_edge_(const std::string& from,
141 const std::string& to,
142 const DotAttributes& attributes) {
143 std::string statement = _indent_() + from + (_directed_ ? " -> " : " -- ") + to;
144 if (!attributes.empty()) statement += ' ' + _attributes_(attributes);
145 _buffer_ += statement + ";\n";
146 }
147
148 void DotWriter::node(std::string_view id, const DotAttributes& attributes) {
149 _node_(quote(id), attributes);
150 }
151
152 void DotWriter::node(Size id, const DotAttributes& attributes) {
153 _node_(std::to_string(id), attributes);
154 }
155
156 void
157 DotWriter::edge(std::string_view from, std::string_view to, const DotAttributes& attributes) {
158 _edge_(quote(from), quote(to), attributes);
159 }
160
161 void DotWriter::edge(Size from, Size to, const DotAttributes& attributes) {
162 _edge_(std::to_string(from), std::to_string(to), attributes);
163 }
164
165 void DotWriter::beginSubgraph(std::string_view name, const DotAttributes& attributes) {
166 // nothing is written if an attribute is refused
167 std::string statement = _indent_() + "subgraph ";
168 if (!name.empty()) statement += quote(name) + ' ';
169 statement += "{\n";
170 const std::string inner(2 * (_depth_ + 2), ' ');
171 for (const auto& attribute: attributes)
172 statement += inner + _attribute_(attribute) + ";\n";
173 _buffer_ += statement;
174 ++_depth_;
175 }
176
178 if (_depth_ == 0) { GUM_ERROR(OperationNotAllowed, "No subgraph to close.") }
179 --_depth_;
180 _buffer_ += _indent_() + "}\n";
181 }
182
183 std::string DotWriter::str() const {
184 if (_depth_ != 0) {
185 GUM_ERROR(OperationNotAllowed, "The graph is not complete : a subgraph is still open.")
186 }
187 return _buffer_ + "}\n";
188 }
189
190} // namespace gum::dot
Exception: at least one argument passed to a function is not what was expected.
Exception : operation not allowed.
void nodeDefaults(const DotAttributes &attributes)
node [attributes];, the default attributes of the nodes that follow (none : does nothing).
void _edge_(const std::string &from, const std::string &to, const DotAttributes &attributes)
std::string _buffer_
Definition dotWriter.h:163
void graphAttributes(const DotAttributes &attributes)
graph [attributes];, the attributes of the graph (does nothing if there is none).
void edgeDefaults(const DotAttributes &attributes)
edge [attributes];, the default attributes of the edges that follow (none : does nothing).
void edge(std::string_view from, std::string_view to, const DotAttributes &attributes={})
an edge from from to to (-> in a directed graph, -- in an undirected one).
void endSubgraph()
Closes the last subgraph that is open.
static std::string number(double value)
The shortest text that reads back as value (0.5, 1, 1e-05).
Definition dotWriter.cpp:93
DotWriter(bool directed=true, std::string_view name="")
Starts a graph.
Definition dotWriter.cpp:67
static std::string _attributes_(const DotAttributes &attributes)
void _defaults_(const char *keyword, const DotAttributes &attributes)
std::string _indent_() const
Definition dotWriter.cpp:99
void _node_(const std::string &id, const DotAttributes &attributes)
static std::string quote(std::string_view text)
text between double quotes, in which ", \, line feed and carriage return are escaped.
Definition dotWriter.cpp:76
std::string str() const
The graph written so far, closed. It can be called at any moment, and the writer can be used after.
void node(std::string_view id, const DotAttributes &attributes={})
a node, whose identifier is id (quoted).
void beginSubgraph(std::string_view name, const DotAttributes &attributes={})
Opens a subgraph : subgraph "name" { key="value"; ... .
void graphAttribute(const DotAttribute &attribute)
key="value";, an attribute of the graph or of the subgraph being written.
static std::string _attribute_(const DotAttribute &attribute)
A minimal writer of graphs in the dot language of graphviz.
aGrUM's exceptions
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
std::vector< DotAttribute > DotAttributes
a list of attributes
Definition dotWriter.h:74
An attribute of a graph, a node or an edge : key="value".
Definition dotWriter.h:67