aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
classDependencyGraph_tpl.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
41#pragma once
42
43
51
52namespace gum {
53 namespace prm {
54
55 // Destructor.
56 template < GUM_Numeric GUM_SCALAR >
58 GUM_DESTRUCTOR(ClassDependencyGraph);
59
60 for (const auto& elt: _node_map_)
61 delete elt.second;
62
63 for (const auto& elt: _elt_map_)
64 delete elt.second;
65 }
66
67 // Build the class dependency graph.
68 template < GUM_Numeric GUM_SCALAR >
70 // First we add all nodes
71 for (const auto ci: prm.classes()) {
72 _node_map_.insert(ci, new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >());
73
74 for (const auto node: ci->containerDag().nodes())
75 _addNode_(ci, ci->get(node));
76 }
77
78 for (const auto ii: prm.interfaces()) {
79 _node_map_.insert(ii, new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >());
80
81 for (const auto node: ii->containerDag().nodes()) {
82 _addNode_(ii, ii->get(node));
83 }
84 }
85
86 // Then we add the arcs
87 for (const auto cc: prm.classes())
88 for (const auto node: cc->containerDag().nodes())
89 _addArcs_(*cc, node, *(_node_map_[cc]));
90 }
91
92 // Add arcs in _graph_.
93 template < GUM_Numeric GUM_SCALAR >
96 NodeId node,
98 switch (c.get(node).elt_type()) {
101 = static_cast< const PRMSlotChain< GUM_SCALAR >& >(c.get(node));
102
103 for (const auto chi: c.containerDag().children(node))
104 _graph_.addArc((*(_node_map_[&(sc.end())]))[&(sc.end().get(sc.lastElt().safeName()))],
105 map[&(c.get(chi))]);
106
107 break;
108 }
109
112 for (const auto chi: c.containerDag().children(node))
113 _graph_.addArc(map[&(c.get(node))], map[&(c.get(chi))]);
114
115 break;
116 }
117
118 default : { /* do nothing */ break;
119 }
120 }
121 }
122
123 template < GUM_Numeric GUM_SCALAR >
128
129 template < GUM_Numeric GUM_SCALAR >
133 GUM_CONS_CPY(ClassDependencyGraph);
134
135 for (const auto& elt: source._node_map_) {
136 _node_map_.insert(
137 elt.first,
138 new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >(*elt.second));
139 }
140 }
141
142 template < GUM_Numeric GUM_SCALAR >
146
147 template < GUM_Numeric GUM_SCALAR >
150 return *(_elt_map_[id]);
151 }
152
153 template < GUM_Numeric GUM_SCALAR >
158
159 template < GUM_Numeric GUM_SCALAR >
163
164 template < GUM_Numeric GUM_SCALAR >
168 switch (elt.elt_type()) {
171 NodeId id = _graph_.addNode();
173 _node_map_[c]->insert(&elt, id);
174 _modalitites_.insert(id, elt.type().variable().domainSize());
175 break;
176 }
177
178 default : { /* do nothing */ break;
179 }
180 }
181 }
182
183 } /* namespace prm */
184} /* namespace gum */
Headers of ClassDependencyGraph<GUM_SCALAR>.
NodeSet children(const NodeSet &ids) const
returns the set of nodes which consists in the node and its parents returns the set of children of a ...
Base class for dag.
Definition DAG.h:121
virtual Size domainSize() const =0
The class for generic Hash Tables.
Definition hashTable.h:640
ClassDependencyGraph(const PRM< GUM_SCALAR > &prm)
Default constructor.
NodeProperty< EltPair * > _elt_map_
Mapping between the nodes in graph with the PRMClassElement<GUM_SCALAR> in the PRM<GUM_SCALAR>.
const NodeProperty< Size > & modalities() const
Returns a mapping between the ClassDependencyGraph<GUM_SCALAR>'s nodes and their modalities.
void _addNode_(const PRMClassElementContainer< GUM_SCALAR > *c, const PRMClassElement< GUM_SCALAR > &elt)
Add nodes in graph while updating consequently all the mappings.
NodeProperty< Size > _modalitites_
The modalities map for each node in the ClassDependencyGraph<GUM_SCALAR>. This is useful when using a...
void _buildGraph_(const PRM< GUM_SCALAR > &prm)
Build the class dependency graph.
void _addArcs_(const PRMClassElementContainer< GUM_SCALAR > &c, NodeId node, HashTable< const PRMClassElement< GUM_SCALAR > *, NodeId > &map)
Add arcs in graph.
const EltPair & get(NodeId id) const
Returns a constant reference over the element assiociated with the node id in the ClassDependencyGrap...
NodeMap _node_map_
Map each Class to a HashTable mapping the Class's ClassElements to their assigned NodeId in graph.
std::pair< const PRMClassElementContainer< GUM_SCALAR > *, const PRMClassElement< GUM_SCALAR > * > EltPair
Association between a class element and it's holding class.
const DAG & internalDag() const
Returns a constant reference over the graph of the DAG representing the ClassDependencyGraph<GUM_SCAL...
<agrum/PRM/classElementContainer.h>
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
virtual PRMClassElement< GUM_SCALAR > & get(std::string_view name)=0
Getter on a member of this PRMClassElementContainer.
Abstract class representing an element of PRM class.
virtual ClassElementType elt_type() const =0
Return the type of class element this object is.
virtual PRMType & type()=0
Return a reference over the gum::PRMType of this class element.
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
PRMClassElement< GUM_SCALAR > & lastElt()
Returns the last element of the slot chain, typically this is an gum::PRMAttribute or a gum::PRMAggre...
PRMClassElementContainer< GUM_SCALAR > & end()
Returns the PRMClassElement<GUM_SCALAR>Container over which this slot chain ends.
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition PRMType_inl.h:65
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition PRM.h:74
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46