aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
interfaceGraph.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of InterfaceGraph.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_INTERFACE_GRAPH_H
29 #define GUM_INTERFACE_GRAPH_H
30 
31 #include <list>
32 #include <sstream>
33 #include <string>
34 
35 #include <agrum/PRM/PRM.h>
36 #include <agrum/tools/core/hashTable.h>
37 #include <agrum/tools/graphs/undiGraph.h>
38 
39 namespace gum {
40  namespace prm {
41  template < typename GUM_SCALAR >
42  class GSpan;
43 
44  namespace gspan {
45 
46  /**
47  * @struct LabelData interfaceGraph.h <agrum/PRM/gspan/interfaceGraph.h>
48  * Inner class to handle data about labels in this interface graph.
49  */
50  struct LabelData {
51  /// Constructor.
52  LabelData();
53  /// Copy constructor.
54  LabelData(const LabelData& from);
55  /// Destructor.
56  ~LabelData();
57  /// An unique identifier for this label.
58  Idx id;
59  /// The string version of this label.
60  std::string l;
61  /// The size in terms of tree width of the given label.
62  Size tree_width;
63  /// Equality operator.
64  bool operator==(const LabelData& from) const;
65  /// Difference operator.
66  bool operator!=(const LabelData& from) const;
67  };
68 
69  /**
70  * Print a LabelData in out.
71  * @param out The stream in which data is printed.
72  * @param data The data printed.
73  * @return Returns out.
74  */
75  std::ostream& operator<<(std::ostream& out, const LabelData& data);
76 
77  /**
78  * @struct NodeData interfaceGraph.h <agrum/PRM/gspan/interfaceGraph.h>
79  * Inner class to handle data about nodes in _graph_.
80  */
81  template < typename GUM_SCALAR >
82  class NodeData {
83  public:
84  /// Constructor.
85  NodeData< GUM_SCALAR >();
86  /// Copy Constructor.
87  NodeData< GUM_SCALAR >(const NodeData< GUM_SCALAR >& from);
88  /// Destructor.
89  ~NodeData< GUM_SCALAR >();
90  /// The instance represented by this node.
91  PRMInstance< GUM_SCALAR >* n;
92  /// The label of this node.
93  LabelData* l;
94  /// Equality operator.
95  bool operator==(const NodeData< GUM_SCALAR >& from) const;
96  /// Difference operator.
97  bool operator!=(const NodeData< GUM_SCALAR >& from) const;
98  };
99 
100  /**
101  * Print a NodeData<GUM_SCALAR> in out.
102  * @param out The stream in which data is printed.
103  * @param data The data printed.
104  * @return Returns out.
105  */
106  template < typename GUM_SCALAR >
107  std::ostream& operator<<(std::ostream& out, const NodeData< GUM_SCALAR >& data);
108 
109  /**
110  * @struct EdgeData interfaceGraph.h <agrum/PRM/gspan/interfaceGraph.h>
111  * Inner class to handle data about edges in _graph_.
112  */
113  template < typename GUM_SCALAR >
114  class EdgeData {
115  public:
116  /// Constructor.
117  EdgeData();
118  /// Copy constructor.
119  EdgeData(const EdgeData< GUM_SCALAR >& from);
120  /// Destructor.
121  ~EdgeData();
122  /// One of the two instance represented by this edge.
123  PRMInstance< GUM_SCALAR >* u;
124  /// The label data of u.
125  LabelData* l_u;
126  /// The other instance represented by thus edge
127  PRMInstance< GUM_SCALAR >* v;
128  /// The label data of v.
129  LabelData* l_v;
130  /// The labal data of this edge.
131  LabelData* l;
132  /// Equality operator.
133  bool operator==(const EdgeData< GUM_SCALAR >& from) const;
134  /// Difference operator.
135  bool operator!=(const EdgeData< GUM_SCALAR >& from) const;
136  };
137 
138  /**
139  * Print a EdgeData<GUM_SCALAR> in out.
140  * @param out The stream in which data is printed.
141  * @param data The data printed.
142  * @return Returns out.
143  */
144  template < typename GUM_SCALAR >
145  std::ostream& operator<<(std::ostream& out, const EdgeData< GUM_SCALAR >& data);
146 
147  /**
148  * @class InterfaceGraph
149  * @headerfile interfaceGraph.h <agrum/PRM/gspan/interfaceGraph.h>
150  *
151  * @brief This class represent the interface graph of a given
152  * gum::prm::PRMSystem<GUM_SCALAR>.
153  *
154  * An interface graph is a labelled graph over the instances of a
155  * gum::prm::PRMSystem<GUM_SCALAR>, where there exists an edge between two
156  * instance i and j if and only if their shared interface is nonempty.
157  *
158  * Labels assigned to edges and nodes in the interface graph are
159  * technically strings, however since we need a linear oder each label is
160  * assigned a unique id.
161  *
162  */
163  template < typename GUM_SCALAR >
164  class InterfaceGraph {
165  friend class gum::prm::GSpan< GUM_SCALAR >;
166 
167  public:
168  /// Default constructor.
169  explicit InterfaceGraph(const PRMSystem< GUM_SCALAR >& sys);
170 
171  /// Copy constructor, proceeds with a shallow copy so for friends only.
172  InterfaceGraph(const InterfaceGraph& source);
173 
174  /// Copy operator.
175  InterfaceGraph& operator=(const InterfaceGraph& source);
176 
177  /// Destructor.
178  ~InterfaceGraph();
179 
180  /// Returns the graph of this interface graph
181  UndiGraph& graph();
182 
183  /// Returns the graph of this interface graph
184  const UndiGraph& graph() const;
185 
186  /// Returns the bijection between LabelData and their string
187  /// representation.
188  Bijection< Idx, LabelData* >& labels();
189 
190  /// Returns the bijection between LabelData and their string
191  /// representation.
192  const Bijection< Idx, LabelData* >& labels() const;
193 
194  /// Returns the number of node or edges labelled by l.
195  Size size(const LabelData* l) const;
196 
197  /// Returns the set of nodes labelled by l.
198  Set< NodeData< GUM_SCALAR >* >& nodes(const LabelData* l);
199 
200  /// Returns the set of nodes labelled by l.
201  const Set< NodeData< GUM_SCALAR >* >& nodes(const LabelData* l) const;
202 
203  /// Returns the set of nodes labelled by l.
204  Set< EdgeData< GUM_SCALAR >* >& edges(const LabelData* l);
205 
206  /// Returns the set of nodes labelled by l.
207  const Set< EdgeData< GUM_SCALAR >* >& edges(const LabelData* l) const;
208 
209  /// Returns a label given its id
210  LabelData* label(Idx id);
211 
212  /// Returns the id of i in this interface graph
213  NodeId id(const PRMInstance< GUM_SCALAR >& i) const;
214 
215  /// Returns the id of i in this interface graph
216  NodeId id(const PRMInstance< GUM_SCALAR >* i) const;
217 
218  /// Returns data about a node.
219  /// @throw NotFound
220  NodeData< GUM_SCALAR >& node(const PRMInstance< GUM_SCALAR >* i);
221 
222  /// Returns data about a node.
223  /// @throw NotFound
224  const NodeData< GUM_SCALAR >& node(const PRMInstance< GUM_SCALAR >* i) const;
225 
226  /// Returns data about a node.
227  /// @throw NotFound
228  NodeData< GUM_SCALAR >& node(NodeId id);
229 
230  /// Returns data about a node.
231  /// @throw NotFound
232  const NodeData< GUM_SCALAR >& node(NodeId id) const;
233 
234  /// Returns data about an edge.
235  /// @throw NotFound
236  EdgeData< GUM_SCALAR >& edge(NodeId u, NodeId v);
237 
238  /// Returns data about an edge.
239  /// @throw NotFound
240  const EdgeData< GUM_SCALAR >& edge(NodeId u, NodeId v) const;
241 
242  private:
243  /// The gum::prm::PRMSystem<GUM_SCALAR> represented by this interface
244  /// graph.
245  const PRMSystem< GUM_SCALAR >* _sys_;
246 
247  /// The interface graph.
248  UndiGraph _graph_;
249 
250  /// Data associated with a node in _graph_.
251  NodeProperty< NodeData< GUM_SCALAR >* > _nodes_;
252 
253  /// Mapping between PRMInstance<GUM_SCALAR> dans their id in _graph_.
254  HashTable< PRMInstance< GUM_SCALAR >*, NodeId > _idMap_;
255 
256  /// Data associated with edges in _graph_.
257  EdgeProperty< EdgeData< GUM_SCALAR >* > _edges_;
258 
259  /// Bijection between labels and their ids.
260  Bijection< Idx, LabelData* >* _labels_;
261 
262  /// Mapping between a LabelData and the set of NodeData<GUM_SCALAR> with
263  /// that
264  /// label.
265  HashTable< LabelData*, Set< NodeData< GUM_SCALAR >* >* > _nodeMap_;
266 
267  /// Mapping between a LabelData and the set of EdgeData<GUM_SCALAR> with
268  /// that
269  /// label.
270  HashTable< LabelData*, Set< EdgeData< GUM_SCALAR >* >* > _edgeMap_;
271 
272  /// A counter used of assigning ids to labels.
273  Idx _counter_;
274 
275  /// For shallow copies
276  bool _erase_flag_;
277 
278  /// Compute the label of node and add it to _labels_ if it does not
279  /// exists yet. Update node with the correct label's id.
280  void _label_(NodeData< GUM_SCALAR >* node, HashTable< std::string, LabelData* >& label_map);
281 
282  /// Compute the label of edge and add it to _labels_ if it does not
283  /// exists yet. Update edge with the correct label's id.
284  void _label_(EdgeData< GUM_SCALAR >* edge, HashTable< std::string, LabelData* >& label_map);
285  };
286 
287 
288 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
289 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
290 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
291  extern template class NodeData< double >;
292 # endif
293 # endif
294 #endif
295 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
296 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
297 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
298  extern template class EdgeData< double >;
299 # endif
300 # endif
301 #endif
302 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
303 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
304 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
305  extern template class InterfaceGraph< double >;
306 # endif
307 # endif
308 #endif
309 
310 
311  } /* namespace gspan */
312  } /* namespace prm */
313 } /* namespace gum */
314 
315 #include <agrum/PRM/gspan/interfaceGraph_tpl.h>
316 
317 #endif /* GUM_INTERFACE_GRAPH_H */