aGrUM 2.3.2
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-2025 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-2025 *
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#pragma once
41
42
50
51namespace gum {
52 namespace prm {
53
54 // Destructor.
55 template < typename GUM_SCALAR >
57 GUM_DESTRUCTOR(ClassDependencyGraph);
58
59 for (const auto& elt: _node_map_)
60 delete elt.second;
61
62 for (const auto& elt: _elt_map_)
63 delete elt.second;
64 }
65
66 // Build the class dependency graph.
67 template < typename GUM_SCALAR >
69 // First we add all nodes
70 for (const auto ci: prm.classes()) {
71 _node_map_.insert(ci, new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >());
72
73 for (const auto node: ci->containerDag().nodes())
74 _addNode_(ci, ci->get(node));
75 }
76
77 for (const auto ii: prm.interfaces()) {
78 _node_map_.insert(ii, new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >());
79
80 for (const auto node: ii->containerDag().nodes()) {
81 _addNode_(ii, ii->get(node));
82 }
83 }
84
85 // Then we add the arcs
86 for (const auto cc: prm.classes())
87 for (const auto node: cc->containerDag().nodes())
88 _addArcs_(*cc, node, *(_node_map_[cc]));
89 }
90
91 // Add arcs in _graph_.
92 template < typename GUM_SCALAR >
95 NodeId node,
97 switch (c.get(node).elt_type()) {
100 = static_cast< const PRMSlotChain< GUM_SCALAR >& >(c.get(node));
101
102 for (const auto chi: c.containerDag().children(node))
103 _graph_.addArc((*(_node_map_[&(sc.end())]))[&(sc.end().get(sc.lastElt().safeName()))],
104 map[&(c.get(chi))]);
105
106 break;
107 }
108
111 for (const auto chi: c.containerDag().children(node))
112 _graph_.addArc(map[&(c.get(node))], map[&(c.get(chi))]);
113
114 break;
115 }
116
117 default : { /* do nothing */ break;
118 }
119 }
120 }
121
122 template < typename GUM_SCALAR >
127
128 template < typename GUM_SCALAR >
132 GUM_CONS_CPY(ClassDependencyGraph);
133
134 for (const auto& elt: source._node_map_) {
135 _node_map_.insert(
136 elt.first,
137 new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >(*elt.second));
138 }
139 }
140
141 template < typename GUM_SCALAR >
143 return _graph_;
144 }
145
146 template < typename GUM_SCALAR >
149 return *(_elt_map_[id]);
150 }
151
152 template < typename GUM_SCALAR >
153 INLINE NodeId
158
159 template < typename GUM_SCALAR >
163
164 template < typename 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 children of a set of nodes
Base class for dag.
Definition DAG.h:121
virtual Size domainSize() const =0
The class for generic Hash Tables.
Definition hashTable.h:637
NodeProperty< EltPair * > _elt_map_
Mapping between the nodes in graph with the PRMClassElement<GUM_SCALAR> in the PRM<GUM_SCALAR>.
NodeProperty< Size > _modalitites_
The modalities map for each node in the ClassDependencyGraph<GUM_SCALAR>. This is useful when using a...
const NodeProperty< Size > & modalities() const
Returns a mapping between the ClassDependencyGraph<GUM_SCALAR>'s nodes and their modalities.
const EltPair & get(NodeId id) const
Returns a constant reference over the element assiociated with the node id in the ClassDependencyGrap...
void _buildGraph_(const PRM< GUM_SCALAR > &prm)
Build the class dependency graph.
ClassDependencyGraph(const PRM< GUM_SCALAR > &prm)
Default constructor.
void _addArcs_(const PRMClassElementContainer< GUM_SCALAR > &c, NodeId node, HashTable< const PRMClassElement< GUM_SCALAR > *, NodeId > &map)
Add arcs in graph.
const DAG & dag() const
Returns a constant reference over the graph of the DAG representing the ClassDependencyGraph<GUM_SCAL...
void _addNode_(const PRMClassElementContainer< GUM_SCALAR > *c, const PRMClassElement< GUM_SCALAR > &elt)
Add nodes in graph while updating consequently all the mappings.
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.
<agrum/PRM/classElementContainer.h>
virtual PRMClassElement< GUM_SCALAR > & get(const std::string &name)=0
Getter on a member of this PRMClassElementContainer.
virtual const DAG & containerDag() const
Returns the gum::DAG 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...
PRMClassElementContainer< GUM_SCALAR > & end()
Returns the PRMClassElement<GUM_SCALAR>Container over which this slot chain ends.
PRMClassElement< GUM_SCALAR > & lastElt()
Returns the last element of the slot chain, typically this is an gum::PRMAttribute or a gum::PRMAggre...
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition PRMType_inl.h:64
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