aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
PRMInterface.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 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 >&
139  get(const std::string& name) const;
140 
141  /**
142  * Returns the set of PRMAttribute<GUM_SCALAR> of this Class<GUM_SCALAR>.
143  * @return Returns the set of PRMAttribute<GUM_SCALAR> of this
144  * Class<GUM_SCALAR>.
145  */
146  const Set< PRMAttribute< GUM_SCALAR >* >& attributes() const;
147 
148  /**
149  * Returns the set of PRMAggregate of this Class<GUM_SCALAR>.
150  * @return Returns the set of PRMAggregate of this Class<GUM_SCALAR>.
151  */
152  const Set< PRMReferenceSlot< GUM_SCALAR >* >& referenceSlots() const;
153 
154  /// See
155  /// gum::prm::PRMClassElementContainer<GUM_SCALAR>::add(PRMClassElement<GUM_SCALAR>*).
157 
158  /**
159  * @brief Add a new PRMClassElement<GUM_SCALAR> which overload an
160  *inherited
161  *PRMClassElement<GUM_SCALAR>.
162  *
163  * The pointer is "given" to this class, which will delete it when
164  * ~Class<GUM_SCALAR>() is called.
165  *
166  * The NodeId of elt is defined when it is added to this, discarding any
167  * previous value. There is no garanty that elt will have the same NodeId
168  * than the PRMClassElement<GUM_SCALAR> it overloaded.
169  *
170  * You can only overload inherited PRMClassElement<GUM_SCALAR> and only if
171  *elt is
172  *a subtype
173  * of the inherited PRMClassElement<GUM_SCALAR>. You do not define
174  *dependencies in
175  *an PRMInterface
176  * so it is useless to overload an PRMAttribute<GUM_SCALAR> with another
177  *sharing
178  *the same type,
179  * and if tried it will raise an OperationNotAllowed exception.
180  *
181  * @param elt The new PRMClassElement<GUM_SCALAR> overloading an inherited
182  *PRMClassElement<GUM_SCALAR> in this.
183  * @return the NodeId assigned to elt.
184  * @throw NotFound Raised if no overloaded PRMClassElement<GUM_SCALAR> is
185  *found.
186  * @throw OperationNotAllowed Raised if the overloading is impossible.
187  */
189 
190  /// @}
191  // ========================================================================
192  /// @name Inheritance methods
193  // ========================================================================
194  /// @{
195 
196  /**
197  * @brief Test if this PRMInterface is a sub PRMInterface of cec.
198  *
199  * PRMInterface can not be a sub PRMInterface of a Class<GUM_SCALAR>, so
200  *if cec
201  *is a
202  *Class<GUM_SCALAR>
203  * this method will return false.
204  *
205  * If cec is an PRMInterface then this PRMInterface is a sub PRMInterface
206  *of cec if
207  *they
208  *are
209  * equal or there exists a super PRMInterface of this PRMInterface which
210  *is
211  *equal to
212  *cec.
213  *
214  * @param cec The PRMClassElementContainer<GUM_SCALAR> for which we
215  *determine
216  *if
217  *this
218  * PRMInterface is a sub PRMInterface of it.
219  * @return Returns true if this Class<GUM_SCALAR> is a subclass of cec.
220  */
221  virtual bool
222  isSubTypeOf(const PRMClassElementContainer< GUM_SCALAR >& cec) const;
223 
224  /**
225  * @brief Returns the superInterface of this PRMInterface.
226  * @return Returns the super PRMInterface of this PRMInterface.
227  * @throw NotFound Raised if this has no super PRMInterface.
228  */
229  PRMInterface< GUM_SCALAR >& super();
230 
231  /**
232  * @brief Returns the superInterface of this PRMInterface.
233  * @return Returns the super PRMInterface of this PRMInterface.
234  * @throw NotFound Raised if this has no super PRMInterface.
235  */
236  const PRMInterface< GUM_SCALAR >& super() const;
237 
238  /**
239  * Returns the set of Class<GUM_SCALAR> implementing this PRMInterface.
240  */
242 
243  /**
244  * Returns the set of Class<GUM_SCALAR> implementing this PRMInterface.
245  */
246  const Set< PRMClass< GUM_SCALAR >* >& implementations() const;
247 
248  /**
249  * Inherits from this interface super interface, this should only be done
250  * when this inteface inheritance was delayed.
251  */
252  void inheritInterface();
253 
254  /// @}
255  // ========================================================================
256  /// @name Getters & setters operators
257  // ========================================================================
258  /// @{
259 
260  /// See
261  /// gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](NodeId).
263 
264  /// See
265  /// gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](NodeId).
266  const PRMClassElement< GUM_SCALAR >& operator[](NodeId id) const;
267 
268  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](const
269  /// std::string&).
271 
272  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](const
273  /// std::string&).
274  const PRMClassElement< GUM_SCALAR >&
275  operator[](const std::string& name) const;
276 
277  /// @}
278  // ========================================================================
279  /// @name Iterators
280  // ========================================================================
281  /// @{
282 
283  typedef typename NodeProperty< PRMClassElement< GUM_SCALAR >* >::iterator
286  const ClassEltIterator& end();
287 
288  typedef
292  const const_ClassEltIterator& end() const;
293 
294  /// @}
295  protected:
296  /// Returns a constant reference over this PRMInterface's DAG.
297  const DAG& dag_() const;
298 
299  /// Returns a non constant reference over this PRMInterface's DAG.
300  DAG& dag_();
301 
302  /// Fills set with all the subtypes of this PRMInterface, this includes
303  /// extensions
304  /// and implementations.
306 
307  /// See gum::prm::PRMClassElementContainer<GUM_SCALAR>(const
308  /// PRMClassElement<GUM_SCALAR>&).
309  void updateDescendants_(const PRMClassElement< GUM_SCALAR >& elt);
310 
311  private:
312  /// Copy operator. Don't use it.
313  PRMInterface< GUM_SCALAR >&
314  operator=(const PRMInterface< GUM_SCALAR >& source);
315 
316  /// Proceed with the copy of i in this.
317  void inheritInterface__(const PRMInterface< GUM_SCALAR >& i);
318 
319  // ========================================================================
320  /// @name Graphical model members
321  // ========================================================================
322  /// @{
323 
324  /// The dag representing dependencies between formal attributes and
325  /// slots.
327 
328  /// Mapping between node's id and their name (being an attribute or a
329  /// slot). Used for fast access to a member given it's node id.
331 
332  /// @}
333  // ========================================================================
334  /// @name PRMClassElement<GUM_SCALAR> members
335  // ========================================================================
336  /// @{
337 
338  /// Mapping between a member's name and itself.
339  /// Used for fast access to a member given it's name.
341 
342  /// The sequence of PRMAttribute<GUM_SCALAR>s.
344 
345  /// The sequence of PRMReferenceSlot<GUM_SCALAR>.
347 
348  /// @}
349  // ========================================================================
350  /// @name Inheritance members
351  // ========================================================================
352  /// @{
353 
354  /// @brief The alternate PRMClassElementContainer<GUM_SCALAR> searched for
355  /// elements defined in
356  /// this.
357  /// Note that this is first searched for gum::PRMClassElement<GUM_SCALAR>.
359 
360  /// The set of Class<GUM_SCALAR> which implements this PRMInterface.
362 
363  /// Add an Class<GUM_SCALAR> to the set of Class<GUM_SCALAR> which
364  /// implements
365  /// this PRMInterface.
366  void addImplementation__(PRMClass< GUM_SCALAR >* c);
367 
368  /// The set of Class<GUM_SCALAR> which implements this PRMInterface.
370 
371  /// Add an Class<GUM_SCALAR> to the set of Class<GUM_SCALAR> which
372  /// implements
373  /// this PRMInterface.
374  void addExtension__(PRMInterface< GUM_SCALAR >* c);
375 
376  bool
377  checkOverloadLegality__(const PRMClassElement< GUM_SCALAR >* overloaded,
378  const PRMClassElement< GUM_SCALAR >* overloader);
379 
380  void overloadAttribute__(PRMAttribute< GUM_SCALAR >* overloader,
381  PRMAttribute< GUM_SCALAR >* overloaded);
382 
383  void overloadReferenceSlot__(PRMReferenceSlot< GUM_SCALAR >* overloader,
384  PRMReferenceSlot< GUM_SCALAR >* overloaded);
385 
386  void addCastDescendants__(PRMAttribute< GUM_SCALAR >* start,
387  PRMAttribute< GUM_SCALAR >* end);
388 
389  /// @}
390  };
391 
392 
393 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
394  extern template class PRMInterface< double >;
395 #endif
396 
397 
398  } /* namespace prm */
399 } // namespace gum
400 
401 #include <agrum/PRM/elements/PRMInterface_tpl.h>
402 
403 #endif /* GUM_INTERFACE_H */
const DAG & dag_() const
Returns a constant reference over this PRMInterface&#39;s DAG.
void inheritInterface__(const PRMInterface< GUM_SCALAR > &i)
Proceed with the copy of i in this.
const const_ClassEltIterator & end() const
virtual PRMClassElement< GUM_SCALAR > & get(const std::string &name)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(const std::string&).
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.
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()
Set< PRMInterface< GUM_SCALAR > *> extensions__
The set of Class<GUM_SCALAR> which implements this PRMInterface.
Definition: PRMInterface.h:369
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.
void overloadAttribute__(PRMAttribute< GUM_SCALAR > *overloader, PRMAttribute< GUM_SCALAR > *overloaded)
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
const Set< PRMSlotChain< GUM_SCALAR > *> & slotChains() const
Returns the set of PRMSlotChain<GUM_SCALAR> of this Class<GUM_SCALAR>.
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&).
void addImplementation__(PRMClass< GUM_SCALAR > *c)
Add an Class<GUM_SCALAR> to the set of Class<GUM_SCALAR> which implements this PRMInterface.
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&).
Set< PRMClass< GUM_SCALAR > *> implementations__
The set of Class<GUM_SCALAR> which implements this PRMInterface.
Definition: PRMInterface.h:361
void overloadReferenceSlot__(PRMReferenceSlot< GUM_SCALAR > *overloader, PRMReferenceSlot< GUM_SCALAR > *overloaded)
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.
bool checkOverloadLegality__(const PRMClassElement< GUM_SCALAR > *overloaded, const PRMClassElement< GUM_SCALAR > *overloader)
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
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&).
PRMInterface< GUM_SCALAR > & super()
Returns the superInterface of this PRMInterface.
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.
DAG dag__
The dag representing dependencies between formal attributes and slots.
Definition: PRMInterface.h:326
void findAllSubtypes_(Set< PRMClassElementContainer< GUM_SCALAR > * > &set)
Fills set with all the subtypes of this PRMInterface, this includes extensions and implementations...
PRMInterface< GUM_SCALAR > * superInterface__
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
Definition: PRMInterface.h:358
PRMClassElement< GUM_SCALAR > & operator[](NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::operator[](NodeId).
void addCastDescendants__(PRMAttribute< GUM_SCALAR > *start, PRMAttribute< GUM_SCALAR > *end)
The alternate PRMClassElementContainer<GUM_SCALAR> searched for elements defined in this...
void addExtension__(PRMInterface< GUM_SCALAR > *c)
Add an Class<GUM_SCALAR> to the set of Class<GUM_SCALAR> which implements this PRMInterface.