aGrUM  0.16.0
classDependencyGraph_tpl.h
Go to the documentation of this file.
1 
30 
31 namespace gum {
32  namespace prm {
33 
34  // Destructor.
35  template < typename GUM_SCALAR >
37  GUM_DESTRUCTOR(ClassDependencyGraph);
38 
39  for (const auto& elt : __node_map)
40  delete elt.second;
41 
42  for (const auto& elt : __elt_map)
43  delete elt.second;
44  }
45 
46  // Build the class dependency graph.
47  template < typename GUM_SCALAR >
49  const PRM< GUM_SCALAR >& prm) {
50  // First we add all nodes
51  for (const auto ci : prm.classes()) {
52  __node_map.insert(
53  ci, new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >());
54 
55  for (const auto node : ci->containerDag().nodes())
56  __addNode(ci, ci->get(node));
57  }
58 
59  for (const auto ii : prm.interfaces()) {
60  __node_map.insert(
61  ii, new HashTable< const PRMClassElement< GUM_SCALAR >*, NodeId >());
62 
63  for (const auto node : ii->containerDag().nodes()) {
64  __addNode(ii, ii->get(node));
65  }
66  }
67 
68  // Then we add the arcs
69  for (const auto cc : prm.classes())
70  for (const auto node : cc->containerDag().nodes())
71  __addArcs(*cc, node, *(__node_map[cc]));
72  }
73 
74  // Add arcs in __graph.
75  template < typename GUM_SCALAR >
78  NodeId node,
80  switch (c.get(node).elt_type()) {
82  const PRMSlotChain< GUM_SCALAR >& sc =
83  static_cast< const PRMSlotChain< GUM_SCALAR >& >(c.get(node));
84 
85  for (const auto chi : c.containerDag().children(node))
86  __graph.addArc((*(__node_map[&(sc.end())]))[&(
87  sc.end().get(sc.lastElt().safeName()))],
88  map[&(c.get(chi))]);
89 
90  break;
91  }
92 
95  for (const auto chi : c.containerDag().children(node))
96  __graph.addArc(map[&(c.get(node))], map[&(c.get(chi))]);
97 
98  break;
99  }
100 
101  default: { /* do nothing */ break;
102  }
103  }
104  }
105 
106  template < typename GUM_SCALAR >
108  const PRM< GUM_SCALAR >& prm) {
109  GUM_CONSTRUCTOR(ClassDependencyGraph);
110  __buildGraph(prm);
111  }
112 
113  template < typename GUM_SCALAR >
115  const ClassDependencyGraph< GUM_SCALAR >& source) :
116  __graph(source.__graph),
117  __modalitites(source.__modalitites), __elt_map(source.__elt_map) {
118  GUM_CONS_CPY(ClassDependencyGraph);
119 
120  for (const auto elt : source.__node_map) {
122  elt.first,
124  *elt.second));
125  }
126  }
127 
128  template < typename GUM_SCALAR >
130  return __graph;
131  }
132 
133  template < typename GUM_SCALAR >
134  INLINE const typename ClassDependencyGraph< GUM_SCALAR >::EltPair&
136  return *(__elt_map[id]);
137  }
138 
139  template < typename GUM_SCALAR >
142  const PRMClassElement< GUM_SCALAR >& elt) const {
143  return (*(__node_map[&c]))[&elt];
144  }
145 
146  template < typename GUM_SCALAR >
147  INLINE const NodeProperty< Size >&
149  return __modalitites;
150  }
151 
152  template < typename GUM_SCALAR >
155  const PRMClassElement< GUM_SCALAR >& elt) {
156  switch (elt.elt_type()) {
159  NodeId id = __graph.addNode();
160  __elt_map.insert(
162  __node_map[c]->insert(&elt, id);
163  __modalitites.insert(id, elt.type().variable().domainSize());
164  break;
165  }
166 
167  default: { /* do nothing */ break;
168  }
169  }
170  }
171 
172  } /* namespace prm */
173 } /* 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:122
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:140
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual NodeId addNode()
insert a new node and return its id
ClassDependencyGraph(const PRM< GUM_SCALAR > &prm)
Default constructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
The class for generic Hash Tables.
Definition: hashTable.h:679
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:221
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:66
<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:102
Size NodeId
Type for node ids.
Definition: graphElements.h:98
NodeProperty< EltPair *> __elt_map
Mapping between the nodes in __graph with the PRMClassElement<GUM_SCALAR> in the PRM<GUM_SCALAR>.