aGrUM  0.14.2
structuralConstraintSetStatic.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
38 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H
39 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H
40 
41 #include <type_traits>
42 
43 #include <agrum/agrum.h>
44 #include <agrum/graphs/diGraph.h>
46 
47 namespace gum {
48 
49  namespace learning {
50 
51 #ifndef DOXYGEN_SHOULD_SKIP_THIS
52 
53  // a class that indicates the we inherit from a
54  // StructuralConstraintSetStatic
55  struct __StructuralRoot {};
56 
57  // a temporary structure used to help computing the minimal set of
58  // constraints
59  template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
60  struct __ConstraintSet;
61 
62  // a structure to concatenate __ConstraintSets or simply constraints and
63  // produce as a result a new __ConstraintSet
64  template < typename SET1, typename SET2 >
65  struct __ConcatConstraintSet;
66 
67  // a helper function to create minimum structural constraint sets and the
68  // methods actually used on all these constraints. This is a helper for
69  // the class that the user should use, i.e., StructuralConstraintSetStatic
70  template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
71  class __StructuralConstraintSetStatic;
72 
73  // ============================================================================
74  // checks whether a given structural constraint belongs to a given set of
75  // structural constraints
76  template < typename CONSTRAINT, typename SET >
77  struct __IsInConstraintSet;
78 
79  template < typename CONSTRAINT, typename SET >
80  struct __IsInConstraintSet< CONSTRAINT, __ConstraintSet< SET > > {
81  constexpr static bool value = std::is_same< CONSTRAINT, SET >::value;
82  };
83 
84  template < typename CONSTRAINT, typename SET1, typename... SETS >
85  struct __IsInConstraintSet< CONSTRAINT, __ConstraintSet< SET1, SETS... > > {
86  constexpr static bool value =
87  std::is_same< CONSTRAINT, SET1 >::value
88  || __IsInConstraintSet< CONSTRAINT, __ConstraintSet< SETS... > >::value;
89  };
90 
91  // ============================================================================
92  // a temporary structure used to help computing the minimal set of
93  // constraints
94  // (removing all duplicates) belonging to a given set of structural
95  // constraints. For instance, if we have the following structural hierarchy:
96  // Z->Y->X, T->Y->X and we have the set of constraints S = <Z,T>, the set
97  // of
98  // all structural constraints reachable from S is S' = <Z,Y,X,T,Y,X>. The
99  // goal
100  // of the following class is to transform S' into S'' = <Z,T,Y,X>, i.e., the
101  // set S' without any duplicates.
102  template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
103  struct __ConstraintSet : public __ConstraintSet< OTHER_CONSTRAINTS... > {
104  using minset = typename std::conditional<
105  __IsInConstraintSet< FIRST_CONSTRAINT,
106  __ConstraintSet< OTHER_CONSTRAINTS... > >::value,
107  typename __ConstraintSet< OTHER_CONSTRAINTS... >::minset,
108  typename __ConcatConstraintSet<
109  FIRST_CONSTRAINT,
110  typename __ConstraintSet< OTHER_CONSTRAINTS... >::minset >::type >::
111  type;
112  using set =
113  __StructuralConstraintSetStatic< FIRST_CONSTRAINT, OTHER_CONSTRAINTS... >;
114  };
115 
116  template < typename CONSTRAINT >
117  struct __ConstraintSet< CONSTRAINT > {
118  using minset = __ConstraintSet< CONSTRAINT >;
119  using set = __StructuralConstraintSetStatic< CONSTRAINT >;
120  };
121 
122  // ============================================================================
123  // a structure to concatenate __ConstraintSets or simply constraints and
124  // produce as a result a new __ConstraintSet
125  template < typename SET1, typename SET2 >
126  struct __ConcatConstraintSet;
127 
128  template < typename CONSTRAINT1, typename CONSTRAINT2 >
129  struct __ConcatConstraintSet< CONSTRAINT1, __ConstraintSet< CONSTRAINT2 > > {
130  using type = __ConstraintSet< CONSTRAINT1, CONSTRAINT2 >;
131  };
132 
133  template < typename CONSTRAINT1, typename CONSTRAINT2 >
134  struct __ConcatConstraintSet< __ConstraintSet< CONSTRAINT1 >,
135  __ConstraintSet< CONSTRAINT2 > > {
136  using type = __ConstraintSet< CONSTRAINT1, CONSTRAINT2 >;
137  };
138 
139  template < typename CONSTRAINT1,
140  typename CONSTRAINT2,
141  typename... OTHER_CONSTRAINT2 >
142  struct __ConcatConstraintSet<
143  CONSTRAINT1,
144  __ConstraintSet< CONSTRAINT2, OTHER_CONSTRAINT2... > > {
145  using type =
146  __ConstraintSet< CONSTRAINT1, CONSTRAINT2, OTHER_CONSTRAINT2... >;
147  };
148 
149  template < typename CONSTRAINT1,
150  typename CONSTRAINT2,
151  typename... OTHER_CONSTRAINT1 >
152  struct __ConcatConstraintSet<
153  __ConstraintSet< CONSTRAINT1, OTHER_CONSTRAINT1... >,
154  __ConstraintSet< CONSTRAINT2 > > {
155  using type =
156  __ConstraintSet< CONSTRAINT1, OTHER_CONSTRAINT1..., CONSTRAINT2 >;
157  };
158 
159  template < typename CONSTRAINT1,
160  typename CONSTRAINT2,
161  typename... OTHER_CONSTR1,
162  typename... OTHER_CONSTR2 >
163  struct __ConcatConstraintSet<
164  __ConstraintSet< CONSTRAINT1, OTHER_CONSTR1... >,
165  __ConstraintSet< CONSTRAINT2, OTHER_CONSTR2... > > {
166  using type = __ConstraintSet< CONSTRAINT1,
167  OTHER_CONSTR1...,
168  CONSTRAINT2,
169  OTHER_CONSTR2... >;
170  };
171 
172  // ============================================================================
173  // a helper function to create minimum structural constraint sets and the
174  // methods actually used on all these constraints. This is a helper for
175  // the class that the user should use, i.e., StructuralConstraintSetStatic
176  template < typename CONSTRAINT1, typename... OTHER_CONSTRAINTS >
177  class __StructuralConstraintSetStatic
178  : public virtual CONSTRAINT1
179  , public virtual __StructuralConstraintSetStatic< OTHER_CONSTRAINTS... > {
180  public:
182  using first_constraint = CONSTRAINT1;
183 
185  using next_constraints =
186  __StructuralConstraintSetStatic< OTHER_CONSTRAINTS... >;
187 
188  // determines the set of all constraints in the set (included inherited
189  // ones)
190  using allConstraints = typename __ConcatConstraintSet<
191  typename std::conditional<
192  std::is_base_of< __StructuralRoot, CONSTRAINT1 >::value,
193  typename __ConcatConstraintSet<
194  CONSTRAINT1,
195  typename CONSTRAINT1::allConstraints >::type,
196  __ConstraintSet< CONSTRAINT1 > >::type,
197  typename next_constraints::allConstraints >::type;
198 
207  using minConstraints = typename allConstraints::minset::set;
208 
209  // ##########################################################################
211  // ##########################################################################
213 
215  __StructuralConstraintSetStatic();
216 
218  __StructuralConstraintSetStatic(
219  const __StructuralConstraintSetStatic< CONSTRAINT1,
220  OTHER_CONSTRAINTS... >&);
221 
223  ~__StructuralConstraintSetStatic();
224 
226 
227  // ##########################################################################
229  // ##########################################################################
231 
233  __StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... >&
234  operator=(const __StructuralConstraintSetStatic< CONSTRAINT1,
235  OTHER_CONSTRAINTS... >&);
236 
238 
239  // ##########################################################################
241  // ##########################################################################
243 
245  void setGraph(const DiGraph& graph);
246 
248  void modifyGraph(const ArcAddition& change);
249 
251  void modifyGraph(const ArcDeletion& change);
252 
254  void modifyGraph(const ArcReversal& change);
255 
257  void modifyGraph(const GraphChange& change);
258 
260  bool isAlwaysInvalid(const GraphChange& change) const;
261 
263  bool checkArcAddition(NodeId x, NodeId y) const;
264 
266  bool checkArcDeletion(NodeId x, NodeId y) const;
267 
269  bool checkArcReversal(NodeId x, NodeId y) const;
270 
272  bool checkModification(const ArcAddition& change) const;
273 
275  bool checkModification(const ArcDeletion& change) const;
276 
278  bool checkModification(const ArcReversal& change) const;
279 
281  bool checkModification(const GraphChange& change) const;
282 
284  };
285 
286  template < typename CONSTRAINT >
287  class __StructuralConstraintSetStatic< CONSTRAINT >
288  : public virtual CONSTRAINT
289  , public virtual __StructuralRoot {
290  public:
292  using first_constraint = CONSTRAINT;
293 
295  using next_constraints = __StructuralRoot;
296 
297  // determines the set of all constraints in the set (included inherited
298  // ones)
301  using allConstraints = typename std::conditional<
302  std::is_base_of< __StructuralRoot, CONSTRAINT >::value,
303  typename __ConcatConstraintSet<
304  CONSTRAINT,
305  typename CONSTRAINT::allConstraints >::type,
306  __ConstraintSet< CONSTRAINT > >::type;
307 
316  using minConstraints = typename allConstraints::minset::set;
317 
318  // ##########################################################################
320  // ##########################################################################
322 
324  __StructuralConstraintSetStatic();
325 
327  __StructuralConstraintSetStatic(
328  const __StructuralConstraintSetStatic< CONSTRAINT >&);
329 
331  ~__StructuralConstraintSetStatic();
332 
334 
335  // ##########################################################################
337  // ##########################################################################
339 
341  __StructuralConstraintSetStatic< CONSTRAINT >&
342  operator=(const __StructuralConstraintSetStatic< CONSTRAINT >&);
343 
345 
346  // ##########################################################################
348  // ##########################################################################
350 
352  void setGraph(const DiGraph& graph);
353 
355  void modifyGraph(const ArcAddition& change);
356 
358  void modifyGraph(const ArcDeletion& change);
359 
361  void modifyGraph(const ArcReversal& change);
362 
364  void modifyGraph(const GraphChange& change);
365 
367  bool isAlwaysInvalid(const GraphChange& change) const;
368 
370  bool checkArcAddition(NodeId x, NodeId y) const;
371 
373  bool checkArcDeletion(NodeId x, NodeId y) const;
374 
376  bool checkArcReversal(NodeId x, NodeId y) const;
377 
379  bool checkModification(const ArcAddition& change) const;
380 
382  bool checkModification(const ArcDeletion& change) const;
383 
385  bool checkModification(const ArcReversal& change) const;
386 
388  bool checkModification(const GraphChange& change) const;
389 
391  };
392 
393 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
394 
422  template < typename CONSTRAINT1, typename... OTHER_CONSTRAINTS >
424  : public virtual __StructuralConstraintSetStatic<
425  CONSTRAINT1,
426  OTHER_CONSTRAINTS... >::minConstraints {
427  public:
428  using constraints = typename __StructuralConstraintSetStatic<
429  CONSTRAINT1,
430  OTHER_CONSTRAINTS... >::minConstraints;
431 
432  // ##########################################################################
434  // ##########################################################################
436 
439 
442  const StructuralConstraintSetStatic< CONSTRAINT1,
443  OTHER_CONSTRAINTS... >&);
444 
447 
449 
450  // ##########################################################################
452  // ##########################################################################
454 
456  StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... >&
457  operator=(const StructuralConstraintSetStatic< CONSTRAINT1,
458  OTHER_CONSTRAINTS... >&);
459 
461 
462  // ##########################################################################
464  // ##########################################################################
466 
468  void setGraph(const DiGraph& graph);
469 
471  void modifyGraph(const ArcAddition& change);
472 
474  void modifyGraph(const ArcDeletion& change);
475 
477  void modifyGraph(const ArcReversal& change);
478 
480  void modifyGraph(const GraphChange& change);
481 
483  bool isAlwaysInvalid(const GraphChange& change) const;
484 
486  bool checkArcAddition(NodeId x, NodeId y) const;
487 
489  bool checkArcDeletion(NodeId x, NodeId y) const;
490 
492  bool checkArcReversal(NodeId x, NodeId y) const;
493 
495  bool checkModification(const ArcAddition& change) const;
496 
498  bool checkModification(const ArcDeletion& change) const;
499 
501  bool checkModification(const ArcReversal& change) const;
502 
504  bool checkModification(const GraphChange& change) const;
505 
507  };
508 
509 #ifndef DOXYGEN_SHOULD_SKIP_THIS
510 
511  template < typename CONSTRAINT >
512  class StructuralConstraintSetStatic< CONSTRAINT >
513  : public virtual __StructuralConstraintSetStatic<
514  CONSTRAINT >::minConstraints {
515  public:
516  using constraints =
517  typename __StructuralConstraintSetStatic< CONSTRAINT >::minConstraints;
518 
519  // ##########################################################################
521  // ##########################################################################
523 
526 
530 
533 
535 
536  // ##########################################################################
538  // ##########################################################################
540 
544 
546 
547  // ##########################################################################
549  // ##########################################################################
551 
553  void setGraph(const DiGraph& graph);
554 
556  void modifyGraph(const ArcAddition& change);
557 
559  void modifyGraph(const ArcDeletion& change);
560 
562  void modifyGraph(const ArcReversal& change);
563 
565  void modifyGraph(const GraphChange& change);
566 
568  bool isAlwaysInvalid(const GraphChange& change) const;
569 
571  bool checkArcAddition(NodeId x, NodeId y) const;
572 
574  bool checkArcDeletion(NodeId x, NodeId y) const;
575 
577  bool checkArcReversal(NodeId x, NodeId y) const;
578 
580  bool checkModification(const ArcAddition& change) const;
581 
583  bool checkModification(const ArcDeletion& change) const;
584 
586  bool checkModification(const ArcReversal& change) const;
587 
589  bool checkModification(const GraphChange& change) const;
590 
592  };
593 
594 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
595 
596  } /* namespace learning */
597 
598 } /* namespace gum */
599 
602 
603 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H */
Base classes for oriented graphs.
the classes to account for structure changes in a graph
The class for notifying learning algorithms of new arc additionsThis class is convenient to know at c...
Definition: graphChange.h:147
The class for notifying learning algorithms of arc removalsThis class is convenient to know at compil...
Definition: graphChange.h:213
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
typename __StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... >::minConstraints constraints
the "meta-programming" class for storing structural constraintsIn aGrUM, there are two ways to store ...
Base class for all oriented graphs.
Definition: diGraph.h:108
The class for notifying learning algorithms of arc reversalsThis class is convenient to know at compi...
Definition: graphChange.h:279
Size NodeId
Type for node ids.
Definition: graphElements.h:97
the "meta-programming" class for storing several structural constraints