aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
pattern.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 the Pattern class.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_PATTERN_H
30 #define GUM_PATTERN_H
31 
32 #include <agrum/agrum.h>
33 
34 #include <agrum/tools/graphs/diGraph.h>
35 
36 #include <agrum/PRM/gspan/DFSCode.h>
37 #include <agrum/PRM/gspan/interfaceGraph.h>
38 
39 namespace gum {
40  namespace prm {
41  namespace gspan {
42 
43  class NeighborIterator;
44 
45  /**
46  * @class Pattern
47  * @headerfile pattern.h <agrum/PRM/gspan/pattern.h>
48  * This contains all the information we want for a node in a DFSTree.
49  *
50  * Several rules are used regarding nodes in Pattern::graph.
51  * First nodes are added respecting a depth-first search, thus each node
52  * is labelled from 1 to n, where n is the number of nodes in
53  *Pattern::graph.
54  *
55  * Given two nodes id i and j, if i < j then i was discovered before j in
56  *the
57  * depth first search.
58  *
59  * To retrieve the depth first search tree from Pattern::graph simple
60  *consider
61  *only
62  * arcs (u, v) with u < v. The set of arcs { (u,v) | u < v} is called the
63  *forward edge
64  * set and the set of arcs { (u,v) | u > v} is called the backward edge
65  *set.
66  *
67  * The orientation in Pattern::graph is only used to differentiate forward
68  *edges
69  * from backward edges.
70  *
71  */
72  class Pattern: private DiGraph {
73  public:
74  // =========================================================================
75  /// @name Constructor and destructor.
76  // ==========================================================================
77  /// @{
78 
79  /// Default constructor.
80  Pattern();
81 
82  /// Copy constructor.
83  Pattern(const Pattern& source);
84 
85  /// Destructor.
86  ~Pattern();
87 
88  /// @}
89  // =========================================================================
90  /// @name Graphical getters and setters.
91  // ==========================================================================
92  /// @{
93 
94  /**
95  * @brief Insert a node with the given LabelData.
96  * @returns The id assigned to the inserted node.
97  */
99 
100  /// Returns the LabelData assigned to node.
102 
103  /// Returns the LabelData assigned to node.
104  const LabelData& label(NodeId node) const;
105 
106  /// Returns the LabelData assigned to arc.
108 
109  /// Returns the LabelData assigned to arc.
110  const LabelData& label(NodeId i, NodeId j) const;
111 
112  /// Returns the LabelData assigned to arc.
113  LabelData& label(const Arc& arc);
114 
115  /// Returns the LabelData assigned to arc.
116  const LabelData& label(const Arc& arc) const;
117 
118  // Returns the last added LabelData.
119  LabelData& lastAdded();
120 
121  // Returns the last added LabelData.
122  const LabelData& lastAdded() const;
123 
124  /**
125  * @brief Add an arc to this Pattern.
126  *
127  * This create an EdgeCode and check if it respects
128  * neighborhood restriction, if not an exception is raised.
129  *
130  * @param i The DFS subscript of the first node in the code.
131  * @param j The DFS subscript of the second node in the code.
132  * @param l The label data of the added edge.
133  *
134  * @throw OperationNotAllowed Raised if the neighborhood restriction
135  * is not respected.
136  */
137  void addArc(NodeId i, NodeId j, LabelData& l);
138 
139  /// Returns true if id is a node in this Pattern.
140  bool exists(NodeId id) const;
141 
142  /// Returns true if (tail, head) is an arc in this Pattern.
143  bool exists(NodeId tail, NodeId head) const;
144 
145  /// Returns the number of nodes in this Pattern.
146  Size size() const;
147 
148  /// Returns the number of arcs in this Pattern.
149  Size sizeArcs() const;
150 
151  /// Fill r_path with the rightmost path of this Pattern.
152  /// The list is supposed empty.
153  void rightmostPath(std::list< NodeId >& r_path) const;
154 
155  /// Print the pattern in the DOT syntax.
156  std::string toDot(size_t name) const;
157 
158  /// @}
159  // =========================================================================
160  /// @name Access to topology
161  // ==========================================================================
162  /// @{
163  const NodeGraphPart& nodes() const;
164 
165  const ArcSet& arcs() const;
166 
167  /// @}
168  // =========================================================================
169  /// @name DFSCode related methods.
170  // ==========================================================================
171  /// @{
172 
173  /// Returns the DFSCode of this Pattern.
174  DFSCode& code();
175 
176  /// Returns the DFSCode of this Pattern.
177  const DFSCode& code() const;
178 
179  /// Returns the EdgeCode of an edge of this Pattern.
181 
182  /// Returns the EdgeCode of an edge of this Pattern.
183  EdgeCode& edgeCode(const Arc& arc);
184 
185  /// Returns the EdgeCode of an edge of this Pattern.
186  const EdgeCode& edgeCode(NodeId tail, NodeId head) const;
187 
188  /// Returns the EdgeCode of an edge of this Pattern.
189  const EdgeCode& edgeCode(const Arc& arc) const;
190 
191  /// Remove the last EdgeCode of this pattern.
192  void pop_back();
193 
194  /// Remove a node if it has no neighbors, raise an OperationNotAllowed
195  /// otherwise
196  void remove(NodeId node);
197 
198  bool isMinimal();
199 
200  /// @}
201 
202  private:
203  /// The DFSCode of this Pattern.
205 
206  /// Mapping between nodes in this Pattern and their respective
207  /// LabelData.
209 
210  /// Mapping between edges in this Pattern and their respective
211  /// LabelData.
213 
214  /// The last LabelData added to this pattern.
216 
217  /// Returns true if the expand code by adding and edge betwenne u and v
218  /// is
219  /// minimal
220  /// with respect to _code_.
221  /// @param u A node in this Pattern.
222  /// @param v A node in this Pattern.
223  /// @returns true if the expand code by adding and edge betwenne u and v
224  /// is
225  /// minimal
226  /// with respect to _code_.
227  bool _expandCodeIsMinimal_(NodeId u, NodeId v);
228 
229  /// Recurisve method used by _expandCodeIsMinimal_.
230  /// @param p A Pattern.
231  /// @param node_map A bijection.
232  /// @param u A node in this Pattern.
233  /// @param v A node in this Pattern.
234  /// @return true if the expansion is minimal.
235  bool _rec_(Pattern& p, Bijection< NodeId, NodeId >& node_map, NodeId u, NodeId v);
236 
237  /// A non recursive bugged version of _rec_.
238  bool _not_rec_(Pattern& p, Bijection< NodeId, NodeId >& node_map, NodeId u, NodeId v);
239 
240  // to avoid clang++ warnings
241  using DiGraph::addNode;
242  using DiGraph::addArc;
243  using DiGraph::toDot;
244  using DiGraph::parents;
245  using DiGraph::children;
246  };
247  } /* namespace gspan */
248  } /* namespace prm */
249 } /* namespace gum */
250 #ifndef GUM_NO_INLINE
251 # include <agrum/PRM/gspan/pattern_inl.h>
252 #endif // GUM_NO_INLINE
253 
254 #endif /* GUM_PATTERN_H */
EdgeCode & edgeCode(NodeId tail, NodeId head)
Returns the EdgeCode of an edge of this Pattern.
Definition: pattern_inl.h:167
const LabelData & label(NodeId i, NodeId j) const
Returns the LabelData assigned to arc.
Definition: pattern_inl.h:90
const LabelData & label(const Arc &arc) const
Returns the LabelData assigned to arc.
Definition: pattern_inl.h:104
Pattern()
Default constructor.
Definition: pattern_inl.h:34
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
const LabelData & lastAdded() const
Insert a node with the given LabelData.
Definition: pattern_inl.h:76
DFSCode & code()
Returns the DFSCode of this Pattern.
Definition: pattern_inl.h:161
NodeId addNodeWithLabel(LabelData &l)
Insert a node with the given LabelData.
Definition: pattern_inl.h:46
bool exists(NodeId tail, NodeId head) const
Returns true if (tail, head) is an arc in this Pattern.
Definition: pattern_inl.h:132
bool isMinimal()
Returns the DFSCode of this Pattern.
Definition: pattern.cpp:53
~Pattern()
Destructor.
Definition: pattern_inl.h:40
LabelData * _last_
The last LabelData added to this pattern.
Definition: pattern.h:215
EdgeCode & edgeCode(const Arc &arc)
Returns the EdgeCode of an edge of this Pattern.
Definition: pattern_inl.h:174
LabelData & label(NodeId node)
Returns the LabelData assigned to node.
Definition: pattern_inl.h:55
Size size() const
Returns the number of nodes in this Pattern.
Definition: pattern_inl.h:137
std::string toDot(size_t name) const
Print the pattern in the DOT syntax.
Definition: pattern.cpp:83
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
const DFSCode & code() const
Returns the DFSCode of this Pattern.
Definition: pattern_inl.h:164
const EdgeCode & edgeCode(const Arc &arc) const
Returns the EdgeCode of an edge of this Pattern.
Definition: pattern_inl.h:188
LabelData & lastAdded()
Insert a node with the given LabelData.
Definition: pattern_inl.h:69
const NodeGraphPart & nodes() const
Definition: pattern_inl.h:156
void rightmostPath(std::list< NodeId > &r_path) const
Fill r_path with the rightmost path of this Pattern. The list is supposed empty.
Definition: pattern_inl.h:143
const ArcSet & arcs() const
Definition: pattern_inl.h:158
void pop_back()
Remove the last EdgeCode of this pattern.
Definition: pattern_inl.h:195
LabelData & label(const Arc &arc)
Returns the LabelData assigned to arc.
Definition: pattern_inl.h:97
bool _rec_(Pattern &p, Bijection< NodeId, NodeId > &node_map, NodeId u, NodeId v)
Recurisve method used by expandCodeIsMinimal.
Definition: pattern.cpp:125
DFSCode _code_
The DFSCode of this Pattern.
Definition: pattern.h:204
void addArc(NodeId i, NodeId j, LabelData &l)
Add an arc to this Pattern.
Definition: pattern_inl.h:111
NodeProperty< LabelData *> _node_map_
Mapping between nodes in this Pattern and their respective LabelData.
Definition: pattern.h:208
bool _expandCodeIsMinimal_(NodeId u, NodeId v)
Returns true if the expand code by adding and edge betwenne u and v is minimal with respect to code...
Definition: pattern.cpp:96
bool exists(NodeId id) const
Returns true if id is a node in this Pattern.
Definition: pattern_inl.h:129
const LabelData & label(NodeId node) const
Returns the LabelData assigned to node.
Definition: pattern_inl.h:62
ArcProperty< std::pair< LabelData *, EdgeCode *> > _arc_map_
Mapping between edges in this Pattern and their respective LabelData.
Definition: pattern.h:212
Pattern(const Pattern &source)
Copy constructor.
Definition: pattern.cpp:39
const EdgeCode & edgeCode(NodeId tail, NodeId head) const
Returns the EdgeCode of an edge of this Pattern.
Definition: pattern_inl.h:181
void remove(NodeId node)
Remove a node if it has no neighbors, raise an OperationNotAllowed otherwise.
Definition: pattern_inl.h:213
bool _not_rec_(Pattern &p, Bijection< NodeId, NodeId > &node_map, NodeId u, NodeId v)
A non recursive bugged version of rec.
Definition: pattern.cpp:183
Size sizeArcs() const
Returns the number of arcs in this Pattern.
Definition: pattern_inl.h:140
LabelData & label(NodeId i, NodeId j)
Returns the LabelData assigned to arc.
Definition: pattern_inl.h:83
INLINE std::ostream & operator<<(std::ostream &out, const EdgeData< GUM_SCALAR > &data)
Print a EdgeData<GUM_SCALAR> in out.