aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
dotWriter.h
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#ifndef GUM_DOT_WRITER_H
48#define GUM_DOT_WRITER_H
49
50#include <string>
51#include <vector>
52
53#include <agrum/agrum.h>
54
55#include <string_view>
56
57namespace gum::dot {
58
67 struct DotAttribute {
68 std::string key;
69 std::string value;
70 bool verbatim{false};
71 };
72
74 using DotAttributes = std::vector< DotAttribute >;
75
100 class GUM_SHARED_PUBLIC DotWriter {
101 public:
107 explicit DotWriter(bool directed = true, std::string_view name = "");
108
111
113 void graphAttributes(const DotAttributes& attributes);
116 void nodeDefaults(const DotAttributes& attributes);
119 void edgeDefaults(const DotAttributes& attributes);
120
122 void graphAttribute(const DotAttribute& attribute);
123
125 void node(std::string_view id, const DotAttributes& attributes = {});
128 void node(Size id, const DotAttributes& attributes = {});
129
131 void edge(std::string_view from, std::string_view to, const DotAttributes& attributes = {});
133 void edge(Size from, Size to, const DotAttributes& attributes = {});
134
142 void beginSubgraph(std::string_view name, const DotAttributes& attributes = {});
144 void endSubgraph();
146
149 std::string str() const;
150
153
156 static std::string quote(std::string_view text);
157
159 static std::string number(double value);
161
162 private:
163 std::string _buffer_;
165 int _depth_{0};
166
167 // the indentation of a statement
168 std::string _indent_() const;
169 // `[key="value", ...]`
170 static std::string _attributes_(const DotAttributes& attributes);
171 // `key="value"`
172 static std::string _attribute_(const DotAttribute& attribute);
173 // the statement `keyword [attributes];`
174 void _defaults_(const char* keyword, const DotAttributes& attributes);
175 // a node or an edge, whose identifiers are already written
176 void _node_(const std::string& id, const DotAttributes& attributes);
177 void _edge_(const std::string& from, const std::string& to, const DotAttributes& attributes);
178 };
179
180} // namespace gum::dot
181
182#endif // GUM_DOT_WRITER_H
void nodeDefaults(const DotAttributes &attributes)
node [attributes];, the default attributes of the nodes that follow (none : does nothing).
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 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.
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