aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
classDependencyGraph_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4
* info_at_agrum_dot_org
5
*
6
* This library is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18
*
19
*/
20
21
22
/**
23
* @file
24
* @brief Inline implementation of ClassDependencyGraph.
25
*
26
* @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27
*/
28
#
include
<
agrum
/
PRM
/
classDependencyGraph
.
h
>
29
30
namespace
gum
{
31
namespace
prm
{
32
33
// Destructor.
34
template
<
typename
GUM_SCALAR
>
35
ClassDependencyGraph
<
GUM_SCALAR
>::~
ClassDependencyGraph
() {
36
GUM_DESTRUCTOR
(
ClassDependencyGraph
);
37
38
for
(
const
auto
&
elt
:
_node_map_
)
39
delete
elt
.
second
;
40
41
for
(
const
auto
&
elt
:
_elt_map_
)
42
delete
elt
.
second
;
43
}
44
45
// Build the class dependency graph.
46
template
<
typename
GUM_SCALAR
>
47
void
ClassDependencyGraph
<
GUM_SCALAR
>::
_buildGraph_
(
const
PRM
<
GUM_SCALAR
>&
prm
) {
48
// First we add all nodes
49
for
(
const
auto
ci
:
prm
.
classes
()) {
50
_node_map_
.
insert
(
ci
,
new
HashTable
<
const
PRMClassElement
<
GUM_SCALAR
>*,
NodeId
>());
51
52
for
(
const
auto
node
:
ci
->
containerDag
().
nodes
())
53
_addNode_
(
ci
,
ci
->
get
(
node
));
54
}
55
56
for
(
const
auto
ii
:
prm
.
interfaces
()) {
57
_node_map_
.
insert
(
ii
,
new
HashTable
<
const
PRMClassElement
<
GUM_SCALAR
>*,
NodeId
>());
58
59
for
(
const
auto
node
:
ii
->
containerDag
().
nodes
()) {
60
_addNode_
(
ii
,
ii
->
get
(
node
));
61
}
62
}
63
64
// Then we add the arcs
65
for
(
const
auto
cc
:
prm
.
classes
())
66
for
(
const
auto
node
:
cc
->
containerDag
().
nodes
())
67
_addArcs_
(*
cc
,
node
, *(
_node_map_
[
cc
]));
68
}
69
70
// Add arcs in _graph_.
71
template
<
typename
GUM_SCALAR
>
72
void
ClassDependencyGraph
<
GUM_SCALAR
>::
_addArcs_
(
73
const
PRMClassElementContainer
<
GUM_SCALAR
>&
c
,
74
NodeId
node
,
75
HashTable
<
const
PRMClassElement
<
GUM_SCALAR
>*,
NodeId
>&
map
) {
76
switch
(
c
.
get
(
node
).
elt_type
()) {
77
case
PRMClassElement
<
GUM_SCALAR
>::
prm_slotchain
: {
78
const
PRMSlotChain
<
GUM_SCALAR
>&
sc
79
=
static_cast
<
const
PRMSlotChain
<
GUM_SCALAR
>& >(
c
.
get
(
node
));
80
81
for
(
const
auto
chi
:
c
.
containerDag
().
children
(
node
))
82
_graph_
.
addArc
((*(
_node_map_
[&(
sc
.
end
())]))[&(
sc
.
end
().
get
(
sc
.
lastElt
().
safeName
()))],
83
map
[&(
c
.
get
(
chi
))]);
84
85
break
;
86
}
87
88
case
PRMClassElement
<
GUM_SCALAR
>::
prm_aggregate
:
89
case
PRMClassElement
<
GUM_SCALAR
>::
prm_attribute
: {
90
for
(
const
auto
chi
:
c
.
containerDag
().
children
(
node
))
91
_graph_
.
addArc
(
map
[&(
c
.
get
(
node
))],
map
[&(
c
.
get
(
chi
))]);
92
93
break
;
94
}
95
96
default
: {
/* do nothing */
97
break
;
98
}
99
}
100
}
101
102
template
<
typename
GUM_SCALAR
>
103
INLINE
ClassDependencyGraph
<
GUM_SCALAR
>::
ClassDependencyGraph
(
const
PRM
<
GUM_SCALAR
>&
prm
) {
104
GUM_CONSTRUCTOR
(
ClassDependencyGraph
);
105
_buildGraph_
(
prm
);
106
}
107
108
template
<
typename
GUM_SCALAR
>
109
INLINE
ClassDependencyGraph
<
GUM_SCALAR
>::
ClassDependencyGraph
(
110
const
ClassDependencyGraph
<
GUM_SCALAR
>&
source
) :
111
_graph_
(
source
.
_graph_
),
112
_modalitites_
(
source
.
_modalitites_
),
_elt_map_
(
source
.
_elt_map_
) {
113
GUM_CONS_CPY
(
ClassDependencyGraph
);
114
115
for
(
const
auto
elt
:
source
.
_node_map_
) {
116
_node_map_
.
insert
(
117
elt
.
first
,
118
new
HashTable
<
const
PRMClassElement
<
GUM_SCALAR
>*,
NodeId
>(*
elt
.
second
));
119
}
120
}
121
122
template
<
typename
GUM_SCALAR
>
123
INLINE
const
DAG
&
ClassDependencyGraph
<
GUM_SCALAR
>::
dag
()
const
{
124
return
_graph_
;
125
}
126
127
template
<
typename
GUM_SCALAR
>
128
INLINE
const
typename
ClassDependencyGraph
<
GUM_SCALAR
>::
EltPair
&
129
ClassDependencyGraph
<
GUM_SCALAR
>::
get
(
NodeId
id
)
const
{
130
return
*(
_elt_map_
[
id
]);
131
}
132
133
template
<
typename
GUM_SCALAR
>
134
INLINE
NodeId
135
ClassDependencyGraph
<
GUM_SCALAR
>::
get
(
const
PRMClassElementContainer
<
GUM_SCALAR
>&
c
,
136
const
PRMClassElement
<
GUM_SCALAR
>&
elt
)
const
{
137
return
(*(
_node_map_
[&
c
]))[&
elt
];
138
}
139
140
template
<
typename
GUM_SCALAR
>
141
INLINE
const
NodeProperty
<
Size
>&
ClassDependencyGraph
<
GUM_SCALAR
>::
modalities
()
const
{
142
return
_modalitites_
;
143
}
144
145
template
<
typename
GUM_SCALAR
>
146
INLINE
void
ClassDependencyGraph
<
GUM_SCALAR
>::
_addNode_
(
147
const
PRMClassElementContainer
<
GUM_SCALAR
>*
c
,
148
const
PRMClassElement
<
GUM_SCALAR
>&
elt
) {
149
switch
(
elt
.
elt_type
()) {
150
case
PRMClassElement
<
GUM_SCALAR
>::
prm_attribute
:
151
case
PRMClassElement
<
GUM_SCALAR
>::
prm_aggregate
: {
152
NodeId
id
=
_graph_
.
addNode
();
153
_elt_map_
.
insert
(
id
,
new
ClassDependencyGraph
<
GUM_SCALAR
>::
EltPair
(
c
, &
elt
));
154
_node_map_
[
c
]->
insert
(&
elt
,
id
);
155
_modalitites_
.
insert
(
id
,
elt
.
type
().
variable
().
domainSize
());
156
break
;
157
}
158
159
default
: {
/* do nothing */
160
break
;
161
}
162
}
163
}
164
165
}
/* namespace prm */
166
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643
gum::prm::ParamScopeData::ParamScopeData
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
Definition:
PRMClass_tpl.h:1032