aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
interfaceGraph.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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,
108  const NodeData< GUM_SCALAR >& data);
109 
110  /**
111  * @struct EdgeData interfaceGraph.h <agrum/PRM/gspan/interfaceGraph.h>
112  * Inner class to handle data about edges in graph__.
113  */
114  template < typename GUM_SCALAR >
115  class EdgeData {
116  public:
117  /// Constructor.
118  EdgeData();
119  /// Copy constructor.
120  EdgeData(const EdgeData< GUM_SCALAR >& from);
121  /// Destructor.
122  ~EdgeData();
123  /// One of the two instance represented by this edge.
124  PRMInstance< GUM_SCALAR >* u;
125  /// The label data of u.
126  LabelData* l_u;
127  /// The other instance represented by thus edge
128  PRMInstance< GUM_SCALAR >* v;
129  /// The label data of v.
130  LabelData* l_v;
131  /// The labal data of this edge.
132  LabelData* l;
133  /// Equality operator.
134  bool operator==(const EdgeData< GUM_SCALAR >& from) const;
135  /// Difference operator.
136  bool operator!=(const EdgeData< GUM_SCALAR >& from) const;
137  };
138 
139  /**
140  * Print a EdgeData<GUM_SCALAR> in out.
141  * @param out The stream in which data is printed.
142  * @param data The data printed.
143  * @return Returns out.
144  */
145  template < typename GUM_SCALAR >
146  std::ostream& operator<<(std::ostream& out,
147  const EdgeData< GUM_SCALAR >& data);
148 
149  /**
150  * @class InterfaceGraph
151  * @headerfile interfaceGraph.h <agrum/PRM/gspan/interfaceGraph.h>
152  *
153  * @brief This class represent the interface graph of a given
154  * gum::prm::PRMSystem<GUM_SCALAR>.
155  *
156  * An interface graph is a labelled graph over the instances of a
157  * gum::prm::PRMSystem<GUM_SCALAR>, where there exists an edge between two
158  * instance i and j if and only if their shared interface is nonempty.
159  *
160  * Labels assigned to edges and nodes in the interface graph are
161  * technically strings, however since we need a linear oder each label is
162  * assigned a unique id.
163  *
164  */
165  template < typename GUM_SCALAR >
166  class InterfaceGraph {
167  friend class gum::prm::GSpan< GUM_SCALAR >;
168 
169  public:
170  /// Default constructor.
171  explicit InterfaceGraph(const PRMSystem< GUM_SCALAR >& sys);
172 
173  /// Copy constructor, proceeds with a shallow copy so for friends only.
174  InterfaceGraph(const InterfaceGraph& source);
175 
176  /// Copy operator.
177  InterfaceGraph& operator=(const InterfaceGraph& source);
178 
179  /// Destructor.
180  ~InterfaceGraph();
181 
182  /// Returns the graph of this interface graph
183  UndiGraph& graph();
184 
185  /// Returns the graph of this interface graph
186  const UndiGraph& graph() const;
187 
188  /// Returns the bijection between LabelData and their string
189  /// representation.
190  Bijection< Idx, LabelData* >& labels();
191 
192  /// Returns the bijection between LabelData and their string
193  /// representation.
194  const Bijection< Idx, LabelData* >& labels() const;
195 
196  /// Returns the number of node or edges labelled by l.
197  Size size(const LabelData* l) const;
198 
199  /// Returns the set of nodes labelled by l.
200  Set< NodeData< GUM_SCALAR >* >& nodes(const LabelData* l);
201 
202  /// Returns the set of nodes labelled by l.
203  const Set< NodeData< GUM_SCALAR >* >& nodes(const LabelData* l) const;
204 
205  /// Returns the set of nodes labelled by l.
206  Set< EdgeData< GUM_SCALAR >* >& edges(const LabelData* l);
207 
208  /// Returns the set of nodes labelled by l.
209  const Set< EdgeData< GUM_SCALAR >* >& edges(const LabelData* l) const;
210 
211  /// Returns a label given its id
212  LabelData* label(Idx id);
213 
214  /// Returns the id of i in this interface graph
215  NodeId id(const PRMInstance< GUM_SCALAR >& i) const;
216 
217  /// Returns the id of i in this interface graph
218  NodeId id(const PRMInstance< GUM_SCALAR >* i) const;
219 
220  /// Returns data about a node.
221  /// @throw NotFound
222  NodeData< GUM_SCALAR >& node(const PRMInstance< GUM_SCALAR >* i);
223 
224  /// Returns data about a node.
225  /// @throw NotFound
226  const NodeData< GUM_SCALAR >&
227  node(const PRMInstance< GUM_SCALAR >* i) const;
228 
229  /// Returns data about a node.
230  /// @throw NotFound
231  NodeData< GUM_SCALAR >& node(NodeId id);
232 
233  /// Returns data about a node.
234  /// @throw NotFound
235  const NodeData< GUM_SCALAR >& node(NodeId id) const;
236 
237  /// Returns data about an edge.
238  /// @throw NotFound
239  EdgeData< GUM_SCALAR >& edge(NodeId u, NodeId v);
240 
241  /// Returns data about an edge.
242  /// @throw NotFound
243  const EdgeData< GUM_SCALAR >& edge(NodeId u, NodeId v) const;
244 
245  private:
246  /// The gum::prm::PRMSystem<GUM_SCALAR> represented by this interface
247  /// graph.
248  const PRMSystem< GUM_SCALAR >* sys__;
249 
250  /// The interface graph.
251  UndiGraph graph__;
252 
253  /// Data associated with a node in graph__.
254  NodeProperty< NodeData< GUM_SCALAR >* > nodes__;
255 
256  /// Mapping between PRMInstance<GUM_SCALAR> dans their id in graph__.
257  HashTable< PRMInstance< GUM_SCALAR >*, NodeId > idMap__;
258 
259  /// Data associated with edges in graph__.
260  EdgeProperty< EdgeData< GUM_SCALAR >* > edges__;
261 
262  /// Bijection between labels and their ids.
263  Bijection< Idx, LabelData* >* labels__;
264 
265  /// Mapping between a LabelData and the set of NodeData<GUM_SCALAR> with
266  /// that
267  /// label.
268  HashTable< LabelData*, Set< NodeData< GUM_SCALAR >* >* > nodeMap__;
269 
270  /// Mapping between a LabelData and the set of EdgeData<GUM_SCALAR> with
271  /// that
272  /// label.
273  HashTable< LabelData*, Set< EdgeData< GUM_SCALAR >* >* > edgeMap__;
274 
275  /// A counter used of assigning ids to labels.
276  Idx counter__;
277 
278  /// For shallow copies
279  bool erase_flag__;
280 
281  /// Compute the label of node and add it to labels__ if it does not
282  /// exists yet. Update node with the correct label's id.
283  void label__(NodeData< GUM_SCALAR >* node,
284  HashTable< std::string, LabelData* >& label_map);
285 
286  /// Compute the label of edge and add it to labels__ if it does not
287  /// exists yet. Update edge with the correct label's id.
288  void label__(EdgeData< GUM_SCALAR >* edge,
289  HashTable< std::string, LabelData* >& label_map);
290  };
291 
292 
293 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
294 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
295 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
296  extern template class NodeData< double >;
297 # endif
298 # endif
299 #endif
300 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
301 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
302 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
303  extern template class EdgeData< double >;
304 # endif
305 # endif
306 #endif
307 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
308 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
309 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
310  extern template class InterfaceGraph< double >;
311 # endif
312 # endif
313 #endif
314 
315 
316  } /* namespace gspan */
317  } /* namespace prm */
318 } /* namespace gum */
319 
320 #include <agrum/PRM/gspan/interfaceGraph_tpl.h>
321 
322 #endif /* GUM_INTERFACE_GRAPH_H */