aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
dotOptions.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 <algorithm>
48#include <cmath>
49
52
53namespace gum::dot {
54
55 void DotOptions::setSize(const std::string& size) { _size_ = size; }
56
57 void DotOptions::setRankDir(const std::string& rankDir) { _rankDir_ = rankDir; }
58
59 void DotOptions::setLayout(const std::string& layout) { _layout_ = layout; }
60
61 void DotOptions::setLabel(const std::string& label) { _label_ = label; }
62
63 const std::string& DotOptions::size() const { return _size_; }
64
65 const std::string& DotOptions::rankDir() const { return _rankDir_; }
66
67 const std::string& DotOptions::layout() const { return _layout_; }
68
69 const std::string& DotOptions::label() const { return _label_; }
70
71 void DotOptions::setNodeBgColor(Size node, const std::string& color) {
72 _nodes_[node].bgColor = color;
73 }
74
75 void DotOptions::setNodeFgColor(Size node, const std::string& color) {
76 _nodes_[node].fgColor = color;
77 }
78
79 void DotOptions::setNodeLabel(Size node, const std::string& label) {
80 _nodes_[node].label = label;
81 }
82
83 void DotOptions::setNodeTooltip(Size node, const std::string& tooltip) {
84 _nodes_[node].tooltip = tooltip;
85 }
86
88 const std::string& key,
89 const std::string& value,
90 bool verbatim) {
91 _nodes_[node].attributes.push_back({key, value, verbatim});
92 }
93
95 const auto it = _nodes_.find(node);
96 return it == _nodes_.end() ? nullptr : &it->second;
97 }
98
99 void DotOptions::setArcColor(Size tail, Size head, const std::string& color) {
100 _arcs_[{tail, head}].color = color;
101 }
102
103 void DotOptions::setArcWidth(Size tail, Size head, double width) {
104 if (!std::isfinite(width) || width < 0) {
106 "The width of an arc must be a finite number, at least 0 (not " << width << ").")
107 }
108 _arcs_[{tail, head}].width = width;
109 }
110
111 void DotOptions::setArcLabel(Size tail, Size head, const std::string& label) {
112 _arcs_[{tail, head}].label = label;
113 }
114
115 void DotOptions::setArcTooltip(Size tail, Size head, const std::string& tooltip) {
116 _arcs_[{tail, head}].tooltip = tooltip;
117 }
118
120 Size head,
121 const std::string& key,
122 const std::string& value,
123 bool verbatim) {
124 _arcs_[{tail, head}].attributes.push_back({key, value, verbatim});
125 }
126
127 const DotArcOptions* DotOptions::arc(Size tail, Size head) const {
128 const auto it = _arcs_.find({tail, head});
129 return it == _arcs_.end() ? nullptr : &it->second;
130 }
131
133 if (const auto* options = arc(a, b)) return options;
134 return arc(b, a);
135 }
136
137 std::vector< Size > DotOptions::_key_(std::vector< Size > variables) {
138 std::sort(variables.begin(), variables.end());
139 variables.erase(std::unique(variables.begin(), variables.end()), variables.end());
140 return variables;
141 }
142
143 void DotOptions::setFactorBgColor(const std::vector< Size >& variables,
144 const std::string& color) {
145 _factors_[_key_(variables)].bgColor = color;
146 }
147
148 void DotOptions::setFactorFgColor(const std::vector< Size >& variables,
149 const std::string& color) {
150 _factors_[_key_(variables)].fgColor = color;
151 }
152
153 void DotOptions::setFactorLabel(const std::vector< Size >& variables, const std::string& label) {
154 _factors_[_key_(variables)].label = label;
155 }
156
157 void DotOptions::setFactorTooltip(const std::vector< Size >& variables,
158 const std::string& tooltip) {
159 _factors_[_key_(variables)].tooltip = tooltip;
160 }
161
162 void DotOptions::setFactorAttribute(const std::vector< Size >& variables,
163 const std::string& key,
164 const std::string& value,
165 bool verbatim) {
166 _factors_[_key_(variables)].attributes.push_back({key, value, verbatim});
167 }
168
169 const DotNodeOptions* DotOptions::factor(const std::vector< Size >& variables) const {
170 const auto it = _factors_.find(_key_(variables));
171 return it == _factors_.end() ? nullptr : &it->second;
172 }
173
174 bool DotOptions::empty() const {
175 return _size_.empty() && _rankDir_.empty() && _layout_.empty() && _label_.empty()
176 && _nodes_.empty() && _arcs_.empty() && _factors_.empty();
177 }
178
179} // namespace gum::dot
Exception: at least one argument passed to a function is not what was expected.
void setArcColor(Size tail, Size head, const std::string &color)
std::map< std::pair< Size, Size >, DotArcOptions > _arcs_
Definition dotOptions.h:195
void setRankDir(const std::string &rankDir)
direction of the layout, TB or LR
void setNodeAttribute(Size node, const std::string &key, const std::string &value, bool verbatim=false)
any other attribute of the node ; written as is if verbatim, see DotAttribute
void setFactorTooltip(const std::vector< Size > &variables, const std::string &tooltip)
any other attribute of the node of the factor ; written as is if verbatim, see DotAttribute
const std::string & layout() const
direction of the layout, TB or LR
void setArcWidth(Size tail, Size head, double width)
void setArcAttribute(Size tail, Size head, const std::string &key, const std::string &value, bool verbatim=false)
any other attribute of the arc ; written as is if verbatim, see DotAttribute
void setNodeBgColor(Size node, const std::string &color)
any other attribute of the node ; written as is if verbatim, see DotAttribute
void setFactorAttribute(const std::vector< Size > &variables, const std::string &key, const std::string &value, bool verbatim=false)
any other attribute of the node of the factor ; written as is if verbatim, see DotAttribute
const std::string & label() const
direction of the layout, TB or LR
const std::string & size() const
direction of the layout, TB or LR
const DotArcOptions * arc(Size tail, Size head) const
the options of the arc from tail to head, or nullptr if none was given for it
std::map< std::vector< Size >, DotNodeOptions > _factors_
Definition dotOptions.h:196
void setArcTooltip(Size tail, Size head, const std::string &tooltip)
void setLabel(const std::string &label)
label of the graph (by default, the name of the graph if it has one)
std::map< Size, DotNodeOptions > _nodes_
Definition dotOptions.h:194
std::string _rankDir_
Definition dotOptions.h:190
void setNodeTooltip(Size node, const std::string &tooltip)
any other attribute of the node ; written as is if verbatim, see DotAttribute
void setSize(const std::string &size)
direction of the layout, TB or LR
void setFactorFgColor(const std::vector< Size > &variables, const std::string &color)
any other attribute of the node of the factor ; written as is if verbatim, see DotAttribute
bool empty() const
true if nothing was given
const DotArcOptions * edge(Size a, Size b) const
the options of the undirected edge between a and b, given for (a, b) or for (b, a),...
static std::vector< Size > _key_(std::vector< Size > variables)
void setLayout(const std::string &layout)
graphviz layout engine
void setNodeFgColor(Size node, const std::string &color)
any other attribute of the node ; written as is if verbatim, see DotAttribute
std::string _label_
Definition dotOptions.h:192
const std::string & rankDir() const
direction of the layout, TB or LR
const DotNodeOptions * factor(const std::vector< Size > &variables) const
the options of the factor on these variables (in any order), or nullptr if none was given
std::string _layout_
Definition dotOptions.h:191
const DotNodeOptions * node(Size node) const
the options of a node, or nullptr if none was given for it
void setFactorBgColor(const std::vector< Size > &variables, const std::string &color)
any other attribute of the node of the factor ; written as is if verbatim, see DotAttribute
void setArcLabel(Size tail, Size head, const std::string &label)
void setFactorLabel(const std::vector< Size > &variables, const std::string &label)
any other attribute of the node of the factor ; written as is if verbatim, see DotAttribute
void setNodeLabel(Size node, const std::string &label)
any other attribute of the node ; written as is if verbatim, see DotAttribute
What the caller gives to a toDot() beyond the configuration: sizes, labels, colors of some nodes and ...
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
the options of an arc or an edge : an empty text (no value for the width) means "not given"
Definition dotOptions.h:79
the options of a node : an empty text means "not given", so that the value of the configuration is us...
Definition dotOptions.h:65