aGrUM  0.14.2
classDependencyGraph_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 
28 namespace gum {
29  namespace prm {
30 
31  // Destructor.
32  template < typename GUM_SCALAR >
34  GUM_DESTRUCTOR(ClassDependencyGraph);
35 
36  for (const auto& elt : __node_map)
37  delete elt.second;
38 
39  for (const auto& elt : __elt_map)
40  delete elt.second;
41  }
42 
43  // Build the class dependency graph.
44  template < typename GUM_SCALAR >
46  const PRM< GUM_SCALAR >& prm) {
47  // First we add all nodes
48  for (const auto ci : prm.classes()) {
49  __node_map.insert(
50  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(
58  ii, new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >());
59 
60  for (const auto node : ii->containerDag().nodes()) {
61  __addNode(ii, ii->get(node));
62  }
63  }
64 
65  // Then we add the arcs
66  for (const auto cc : prm.classes())
67  for (const auto node : cc->containerDag().nodes())
68  __addArcs(*cc, node, *(__node_map[cc]));
69  }
70 
71  // Add arcs in __graph.
72  template < typename GUM_SCALAR >
75  NodeId node,
77  switch (c.get(node).elt_type()) {
79  const PRMSlotChain< GUM_SCALAR >& sc =
80  static_cast< const PRMSlotChain< GUM_SCALAR >& >(c.get(node));
81 
82  for (const auto chi : c.containerDag().children(node))
83  __graph.addArc((*(__node_map[&(sc.end())]))[&(
84  sc.end().get(sc.lastElt().safeName()))],
85  map[&(c.get(chi))]);
86 
87  break;
88  }
89 
92  for (const auto chi : c.containerDag().children(node))
93  __graph.addArc(map[&(c.get(node))], map[&(c.get(chi))]);
94 
95  break;
96  }
97 
98  default: { /* do nothing */ break;
99  }
100  }
101  }
102 
103  template < typename GUM_SCALAR >
105  const PRM< GUM_SCALAR >& prm) {
106  GUM_CONSTRUCTOR(ClassDependencyGraph);
107  __buildGraph(prm);
108  }
109 
110  template < typename GUM_SCALAR >
112  const ClassDependencyGraph< GUM_SCALAR >& source) :
113  __graph(source.__graph),
114  __modalitites(source.__modalitites), __elt_map(source.__elt_map) {
115  GUM_CONS_CPY(ClassDependencyGraph);
116 
117  for (const auto elt : source.__node_map) {
119  elt.first,
121  *elt.second));
122  }
123  }
124 
125  template < typename GUM_SCALAR >
127  return __graph;
128  }
129 
130  template < typename GUM_SCALAR >
131  INLINE const typename ClassDependencyGraph< GUM_SCALAR >::EltPair&
133  return *(__elt_map[id]);
134  }
135 
136  template < typename GUM_SCALAR >
139  const PRMClassElement< GUM_SCALAR >& elt) const {
140  return (*(__node_map[&c]))[&elt];
141  }
142 
143  template < typename GUM_SCALAR >
144  INLINE const NodeProperty< Size >&
146  return __modalitites;
147  }
148 
149  template < typename GUM_SCALAR >
152  const PRMClassElement< GUM_SCALAR >& elt) {
153  switch (elt.elt_type()) {
156  NodeId id = __graph.addNode();
157  __elt_map.insert(
159  __node_map[c]->insert(&elt, id);
160  __modalitites.insert(id, elt.type().variable().domainSize());
161  break;
162  }
163 
164  default: { /* do nothing */ break;
165  }
166  }
167  }
168 
169  } /* namespace prm */
170 } /* namespace gum */
const DAG & dag() const
Returns a constant reference over the graph of the DAG representing the ClassDependencyGraph<GUM_SCAL...
const Set< PRMClass< GUM_SCALAR > *> & classes() const
Returns the Set of all Class<GUM_SCALAR> in this PRM.
Definition: PRM_tpl.h:119
Abstract class representing an element of PRM class.
const Set< PRMInterface< GUM_SCALAR > *> & interfaces() const
Returns the Set of all Class<GUM_SCALAR> in this PRM.
Definition: PRM_tpl.h:137
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&#39;s ClassElements to their assigned NodeId in __graph...
virtual PRMClassElement< GUM_SCALAR > & get(const std::string &name)=0
Getter on a member of this PRMClassElementContainer.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
virtual NodeId addNode()
insert a new node and return its id
ClassDependencyGraph(const PRM< GUM_SCALAR > &prm)
Default constructor.
Headers of ClassDependencyGraph<GUM_SCALAR>.
The class for generic Hash Tables.
Definition: hashTable.h:676
std::pair< const PRMClassElementContainer< GUM_SCALAR > *, const PRMClassElement< GUM_SCALAR > *> EltPair
Association between a class element and it&#39;s holding class.
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.
PRMClassElementContainer< GUM_SCALAR > & end()
Returns the PRMClassElement<GUM_SCALAR>Container over which this slot chain ends. ...
This class represent the dependencies of all classes in a PRM<GUM_SCALAR>.
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...
Definition: PRMObject.h:218
void __addNode(const PRMClassElementContainer< GUM_SCALAR > *c, const PRMClassElement< GUM_SCALAR > &elt)
Add nodes in __graph while updating consequently all the mappings.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:63
<agrum/PRM/classElementContainer.h>
void __addArcs(const PRMClassElementContainer< GUM_SCALAR > &c, NodeId node, HashTable< const PRMClassElement< GUM_SCALAR > *, NodeId > &map)
Add arcs in __graph.
const NodeProperty< Size > & modalities() const
Returns a mapping between the ClassDependencyGraph<GUM_SCALAR>&#39;s nodes and their modalities.
PRMClassElement< GUM_SCALAR > & lastElt()
Returns the last element of the slot chain, typically this is an gum::PRMAttribute or a gum::PRMAggre...
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
Base class for dag.
Definition: DAG.h:99
Size NodeId
Type for node ids.
Definition: graphElements.h:97
NodeProperty< EltPair *> __elt_map
Mapping between the nodes in __graph with the PRMClassElement<GUM_SCALAR> in the PRM<GUM_SCALAR>.