aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
PRMInterface.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 gum::prm::PRMInterface.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_INTERFACE_H
30 #define GUM_INTERFACE_H
31 
32 #include <set>
33 #include <utility>
34 #include <string>
35 
36 #include <agrum/agrum.h>
37 #include <agrum/tools/core/sequence.h>
38 #include <agrum/tools/core/set.h>
39 
40 #include <agrum/PRM/elements/PRMClassElement.h>
41 #include <agrum/PRM/elements/PRMClassElementContainer.h>
42 #include <agrum/PRM/elements/PRMAttribute.h>
43 #include <agrum/PRM/elements/PRMReferenceSlot.h>
44 #include <agrum/PRM/elements/PRMScalarAttribute.h>
45 
46 
47 namespace gum {
48  namespace prm {
49 
50  template < typename GUM_SCALAR >
51  class PRMClass;
52 
53  /**
54  * @class PRMInterface
55  * @headerfile interface.h <agrum/PRM/interface.h>
56  * @brief An PRMInterface is implemented by a Class<GUM_SCALAR> and defines
57  *a
58  *set of
59  * PRMReferenceSlot<GUM_SCALAR> and PRMAttribute<GUM_SCALAR> which
60  *the
61  *implementing Class<GUM_SCALAR> must contain.
62  *
63  * @see PRM Class<GUM_SCALAR> PRMClassElement<GUM_SCALAR>
64  */
65  template < typename GUM_SCALAR >
67  // ========================================================================
68  friend class PRMClass< GUM_SCALAR >;
69  // ========================================================================
70  public:
71  // ========================================================================
72  /// @name Constructor & destructor.
73  // ========================================================================
74  /// @{
75 
76  /**
77  * Default constructor.
78  * @param name The interface name.
79  */
80  explicit PRMInterface(const std::string& name);
81 
82  /**
83  * Constructor for building a subclass of super.
84  * @param name The sub-interface name.
85  * @param super The super PRMInterface of this.
86  * @param delayInheritance If true, inheritance will be delayed.
87  */
88  PRMInterface(const std::string& name,
89  PRMInterface< GUM_SCALAR >& super,
90  bool delayInheritance = false);
91 
92  /// Copy constructor.
93  PRMInterface(const PRMInterface< GUM_SCALAR >& source);
94 
95  /// Destructor.
96  virtual ~PRMInterface();
97 
98  /// @}
99  // ========================================================================
100  /// @name Graphical model getters and setters
101  // ========================================================================
102  /// @{
103 
104  /// Implementation of pure virtual method of PRMObject.
105  virtual typename PRMObject::prm_type obj_type() const;
106 
107  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
108  virtual PRMClassElement< GUM_SCALAR >& get(NodeId id);
109 
110  /// Se gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
111  virtual const PRMClassElement< GUM_SCALAR >& get(NodeId id) const;
112 
113  /**
114  * @brief An Interfance doesn't have any arc, this will raise an
115  * OperationNotAllowed exception.
116  *
117  * @param tail The tail of the added arc.
118  * @param head The head of the added arc.
119  * @throw OperationNotAllowed Systematically throws an
120  *OperationNotAllowed.
121  */
122  void addArc(const std::string& tail, const std::string& head);
123 
124  /// @}
125  // ========================================================================
126  /// @name PRMClassElement<GUM_SCALAR> getters and setters
127  // ========================================================================
128  /// @{
129 
130  virtual bool isOutputNode(const PRMClassElement< GUM_SCALAR >& elt) const;
131 
132  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(const
133  /// std::string&).
134  virtual PRMClassElement< GUM_SCALAR >& get(const std::string& name);
135 
136  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(const
137  /// std::string&).
138  virtual const PRMClassElement< GUM_SCALAR >& get(const std::string& name) const;
139 
140  /**
141  * Returns the set of PRMAttribute<GUM_SCALAR> of this Class<GUM_SCALAR>.
142  * @return Returns the set of PRMAttribute<GUM_SCALAR> of this
143  * Class<GUM_SCALAR>.
144  */
145  const Set< PRMAttribute< GUM_SCALAR >* >& attributes() const;
146 
147  /**
148  * Returns the set of PRMAggregate of this Class<GUM_SCALAR>.
149  * @return Returns the set of PRMAggregate of this Class<GUM_SCALAR>.
150  */
151  const Set< PRMReferenceSlot< GUM_SCALAR >* >& referenceSlots() const;
152 
153  /// See
154  /// gum::prm::PRMClassElementContainer<GUM_SCALAR>::add(PRMClassElement<GUM_SCALAR>*).
156 
157  /**
158  * @brief Add a new PRMClassElement<GUM_SCALAR> which overload an
159  *inherited
160  *PRMClassElement<GUM_SCALAR>.
161  *
162  * The pointer is "given" to this class, which will delete it when
163  * ~Class<GUM_SCALAR>() is called.
164  *
165  * The NodeId of elt is defined when it is added to this, discarding any
166  * previous value. There is no garanty that elt will have the same NodeId
167  * than the PRMClassElement<GUM_SCALAR> it overloaded.
168  *
169  * You can only overload inherited PRMClassElement<GUM_SCALAR> and only if
170  *elt is
171  *a subtype
172  * of the inherited PRMClassElement<GUM_SCALAR>. You do not define
173  *dependencies in
174  *an PRMInterface
175  * so it is useless to overload an PRMAttribute<GUM_SCALAR> with another
176  *sharing
177  *the same type,
178  * and if tried it will raise an OperationNotAllowed exception.
179  *
180  * @param elt The new PRMClassElement<GUM_SCALAR> overloading an inherited
181  *PRMClassElement<GUM_SCALAR> in this.
182  * @return the NodeId assigned to elt.
183  * @throw NotFound Raised if no overloaded PRMClassElement<GUM_SCALAR> is
184  *found.
185  * @throw OperationNotAllowed Raised if the overloading is impossible.
186  */
188 
189  /// @}
190  // ========================================================================
191  /// @name Inheritance methods
192  // ========================================================================
193  /// @{
194 
195  /**
196  * @brief Test if this PRMInterface is a sub PRMInterface of cec.
197  *
198  * PRMInterface can not be a sub PRMInterface of a Class<GUM_SCALAR>, so
199  *if cec
200  *is a
201  *Class<GUM_SCALAR>
202  * this method will return false.
203  *
204  * If cec is an PRMInterface then this PRMInterface is a sub PRMInterface
205  *of cec if
206  *they
207  *are
208  * equal or there exists a super PRMInterface of this PRMInterface which
209  *is
210  *equal to
211  *cec.
212  *
213  * @param cec The PRMClassElementContainer<GUM_SCALAR> for which we
214  *determine
215  *if
216  *this
217  * PRMInterface is a sub PRMInterface of it.
218  * @return Returns true if this Class<GUM_SCALAR> is a subclass of cec.
219  */
220  virtual bool isSubTypeOf(const PRMClassElementContainer< GUM_SCALAR >& cec) const;
221 
222  /**
223  * @brief Returns the superInterface of this PRMInterface.
224  * @return Returns the super PRMInterface of this PRMInterface.
225  * @throw NotFound Raised if this has no super PRMInterface.
226  */
227  PRMInterface< GUM_SCALAR >& super();
228 
229  /**
230  * @brief Returns the superInterface of this PRMInterface.
231  * @return Returns the super PRMInterface of this PRMInterface.
232  * @throw NotFound Raised if this has no super PRMInterface.
233  */
234  const PRMInterface< GUM_SCALAR >& super() const;
235 
236  /**
237  * Returns the set of Class<GUM_SCALAR> implementing this PRMInterface.
238  */
240 
241  /**
242  * Returns the set of Class<GUM_SCALAR> implementing this PRMInterface.
243  */
244  const Set< PRMClass< GUM_SCALAR >* >& implementations() const;
245 
246  /**
247  * Inherits from this interface super interface, this should only be done
248  * when this inteface inheritance was delayed.
249  */
250  void inheritInterface();
251 
252  /// @}
253  // ========================================================================
254  /// @name Getters & setters operators
255  // ========================================================================
256  /// @{
257 
258  /// See
259  /// gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](NodeId).
261 
262  /// See
263  /// gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](NodeId).
264  const PRMClassElement< GUM_SCALAR >& operator[](NodeId id) const;
265 
266  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](const
267  /// std::string&).
269 
270  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](const
271  /// std::string&).
272  const PRMClassElement< GUM_SCALAR >& operator[](const std::string& name) const;
273 
274  /// @}
275  // ========================================================================
276  /// @name Iterators
277  // ========================================================================
278  /// @{
279 
280  typedef typename NodeProperty< PRMClassElement< GUM_SCALAR >* >::iterator ClassEltIterator;
282  const ClassEltIterator& end();
283 
284  typedef typename NodeProperty< PRMClassElement< GUM_SCALAR >* >::const_iterator
287  const const_ClassEltIterator& end() const;
288 
289  /// @}
290  protected:
291  /// Returns a constant reference over this PRMInterface's DAG.
292  const DAG& dag_() const;
293 
294  /// Returns a non constant reference over this PRMInterface's DAG.
295  DAG& dag_();
296 
297  /// Fills set with all the subtypes of this PRMInterface, this includes
298  /// extensions
299  /// and implementations.
301 
302  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>(const
303  /// PRMClassElement<GUM_SCALAR>&).
304  void updateDescendants_(const PRMClassElement< GUM_SCALAR >& elt);
305 
306  private:
307  /// Copy operator. Don't use it.
308  PRMInterface< GUM_SCALAR >& operator=(const PRMInterface< GUM_SCALAR >& source);
309 
310  /// Proceed with the copy of i in this.
311  void _inheritInterface_(const PRMInterface< GUM_SCALAR >& i);
312 
313  // ========================================================================
314  /// @name Graphical model members
315  // ========================================================================
316  /// @{
317 
318  /// The dag representing dependencies between formal attributes and
319  /// slots.
321 
322  /// Mapping between node's id and their name (being an attribute or a
323  /// slot). Used for fast access to a member given it's node id.
325 
326  /// @}
327  // ========================================================================
328  /// @name PRMClassElement<GUM_SCALAR> members
329  // ========================================================================
330  /// @{
331 
332  /// Mapping between a member's name and itself.
333  /// Used for fast access to a member given it's name.
335 
336  /// The sequence of PRMAttribute<GUM_SCALAR>s.
338 
339  /// The sequence of PRMReferenceSlot<GUM_SCALAR>.
341 
342  /// @}
343  // ========================================================================
344  /// @name Inheritance members
345  // ========================================================================
346  /// @{
347 
348  /// @brief The alternate PRMClassElementContainer<GUM_SCALAR> searched for
349  /// elements defined in
350  /// this.
351  /// Note that this is first searched for gum::PRMClassElement<GUM_SCALAR>.
353 
354  /// The set of Class<GUM_SCALAR> which implements this PRMInterface.
356 
357  /// Add an Class<GUM_SCALAR> to the set of Class<GUM_SCALAR> which
358  /// implements
359  /// this PRMInterface.
360  void _addImplementation_(PRMClass< GUM_SCALAR >* c);
361 
362  /// The set of Class<GUM_SCALAR> which implements this PRMInterface.
364 
365  /// Add an Class<GUM_SCALAR> to the set of Class<GUM_SCALAR> which
366  /// implements
367  /// this PRMInterface.
368  void _addExtension_(PRMInterface< GUM_SCALAR >* c);
369 
370  bool _checkOverloadLegality_(const PRMClassElement< GUM_SCALAR >* overloaded,
371  const PRMClassElement< GUM_SCALAR >* overloader);
372 
373  void _overloadAttribute_(PRMAttribute< GUM_SCALAR >* overloader,
374  PRMAttribute< GUM_SCALAR >* overloaded);
375 
376  void _overloadReferenceSlot_(PRMReferenceSlot< GUM_SCALAR >* overloader,
377  PRMReferenceSlot< GUM_SCALAR >* overloaded);
378 
379  void _addCastDescendants_(PRMAttribute< GUM_SCALAR >* start, PRMAttribute< GUM_SCALAR >* end);
380 
381  /// @}
382  };
383 
384 
385 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
386  extern template class PRMInterface< double >;
387 #endif
388 
389 
390  } /* namespace prm */
391 } // namespace gum
392 
393 #include <agrum/PRM/elements/PRMInterface_tpl.h>
394 
395 #endif /* GUM_INTERFACE_H */
bool _checkOverloadLegality_(const PRMClassElement< GUM_SCALAR > *overloaded, const PRMClassElement< GUM_SCALAR > *overloader)
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
const DAG & dag_() const
Returns a constant reference over this PRMInterface&#39;s DAG.
const const_ClassEltIterator & end() const
virtual PRMClassElement< GUM_SCALAR > & get(const std::string &name)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(const std::string&).
PRMInterface< GUM_SCALAR > * _superInterface_
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
Definition: PRMInterface.h:352
const PRMClassElement< GUM_SCALAR > & operator[](NodeId id) const
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](NodeId).
virtual bool isSubTypeOf(const PRMClassElementContainer< GUM_SCALAR > &cec) const
Test if this PRMInterface is a sub PRMInterface of cec.
void _inheritInterface_(const PRMInterface< GUM_SCALAR > &i)
Proceed with the copy of i in this.
Set< PRMClass< GUM_SCALAR > *> & implementations()
Returns the set of Class<GUM_SCALAR> implementing this PRMInterface.
PRMInterface(const PRMInterface< GUM_SCALAR > &source)
Copy constructor.
ClassEltIterator begin()
PRMInterface(const std::string &name, PRMInterface< GUM_SCALAR > &super, bool delayInheritance=false)
Constructor for building a subclass of super.
NodeId add(PRMClassElement< GUM_SCALAR > *elt)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::add(PRMClassElement<GUM_SCALAR>*).
const PRMInterface< GUM_SCALAR > & super() const
Returns the superInterface of this PRMInterface.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void _addImplementation_(PRMClass< GUM_SCALAR > *c)
Add an Class<GUM_SCALAR> to the set of Class<GUM_SCALAR> which implements this PRMInterface.
const Set< PRMSlotChain< GUM_SCALAR > *> & slotChains() const
Returns the set of PRMSlotChain<GUM_SCALAR> of this Class<GUM_SCALAR>.
void _overloadAttribute_(PRMAttribute< GUM_SCALAR > *overloader, PRMAttribute< GUM_SCALAR > *overloaded)
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
Set< PRMClass< GUM_SCALAR > *> _implementations_
The set of Class<GUM_SCALAR> which implements this PRMInterface.
Definition: PRMInterface.h:355
NodeId overload(PRMClassElement< GUM_SCALAR > *elt)
Add a new PRMClassElement<GUM_SCALAR> which overload an inherited PRMClassElement<GUM_SCALAR>.
const_ClassEltIterator begin() const
virtual const PRMClassElement< GUM_SCALAR > & get(const std::string &name) const
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(const std::string&).
virtual ~PRMInterface()
Destructor.
DAG & dag_()
Returns a non constant reference over this PRMInterface&#39;s DAG.
const PRMClassElement< GUM_SCALAR > & operator[](const std::string &name) const
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](const std::string&).
void _overloadReferenceSlot_(PRMReferenceSlot< GUM_SCALAR > *overloader, PRMReferenceSlot< GUM_SCALAR > *overloaded)
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
void _addCastDescendants_(PRMAttribute< GUM_SCALAR > *start, PRMAttribute< GUM_SCALAR > *end)
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
virtual bool isOutputNode(const PRMClassElement< GUM_SCALAR > &elt) const
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(const std::string&).
void addArc(const std::string &tail, const std::string &head)
An Interfance doesn&#39;t have any arc, this will raise an OperationNotAllowed exception.
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
PRMInterface(const std::string &name)
Default constructor.
const Set< PRMReferenceSlot< GUM_SCALAR > *> & referenceSlots() const
Returns the set of PRMAggregate of this Class<GUM_SCALAR>.
PRMClassElement< GUM_SCALAR > & operator[](const std::string &name)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](const std::string&).
void _addExtension_(PRMInterface< GUM_SCALAR > *c)
Add an Class<GUM_SCALAR> to the set of Class<GUM_SCALAR> which implements this PRMInterface.
PRMInterface< GUM_SCALAR > & super()
Returns the superInterface of this PRMInterface.
DAG _dag_
The dag representing dependencies between formal attributes and slots.
Definition: PRMInterface.h:320
PRMInterface< GUM_SCALAR > & operator=(const PRMInterface< GUM_SCALAR > &source)
Copy operator. Don&#39;t use it.
void inheritInterface()
Inherits from this interface super interface, this should only be done when this inteface inheritance...
virtual PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
virtual const PRMClassElement< GUM_SCALAR > & get(NodeId id) const
Se gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
void updateDescendants_(const PRMClassElement< GUM_SCALAR > &elt)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>(const PRMClassElement<GUM_SCALAR>&).
const ClassEltIterator & end()
const Set< PRMClass< GUM_SCALAR > *> & implementations() const
Returns the set of Class<GUM_SCALAR> implementing this PRMInterface.
void findAllSubtypes_(Set< PRMClassElementContainer< GUM_SCALAR > * > &set)
Fills set with all the subtypes of this PRMInterface, this includes extensions and implementations...
PRMClassElement< GUM_SCALAR > & operator[](NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](NodeId).
Set< PRMInterface< GUM_SCALAR > *> _extensions_
The set of Class<GUM_SCALAR> which implements this PRMInterface.
Definition: PRMInterface.h:363