aGrUM  0.18.2
a C++ library for (probabilistic) graphical models
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 */
void buildGraph__(const PRM< GUM_SCALAR > &prm)
Build the class dependency graph.
const DAG & dag() const
Returns a constant reference over the graph of the DAG representing the ClassDependencyGraph<GUM_SCAL...
NodeProperty< Size > modalitites__
The modalities map for each node in the ClassDependencyGraph<GUM_SCALAR>. This is useful when using a...
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.
void addNode__(const PRMClassElementContainer< GUM_SCALAR > *c, const PRMClassElement< GUM_SCALAR > &elt)
Add nodes in graph__ while updating consequently all the mappings.
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...
virtual PRMClassElement< GUM_SCALAR > & get(const std::string &name)=0
Getter on a member of this PRMClassElementContainer.
void addArcs__(const PRMClassElementContainer< GUM_SCALAR > &c, NodeId node, HashTable< const PRMClassElement< GUM_SCALAR > *, NodeId > &map)
Add arcs in graph__.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
NodeMap node_map__
Map each Class to a HashTable mapping the Class&#39;s ClassElements to their assigned NodeId in graph__...
virtual NodeId addNode()
insert a new node and return its id
ClassDependencyGraph(const PRM< GUM_SCALAR > &prm)
Default constructor.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
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.
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
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
<agrum/PRM/classElementContainer.h>
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>&#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