aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
PRMInstance.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49#ifndef GUM_INSTANCE_H
50#define GUM_INSTANCE_H
51
52#include <set>
53#include <utility>
54
56
57namespace gum {
58 namespace prm {
59
60
78 template < typename GUM_SCALAR >
79 class PRMInstance: public PRMObject {
80 public:
81 // ========================================================================
83 // ========================================================================
85
99 PRMInstance(const std::string& name, PRMClass< GUM_SCALAR >& type);
100
102 virtual ~PRMInstance();
103
105 // ========================================================================
107 // ========================================================================
109
113 virtual prm_type obj_type() const;
114
119
123 const PRMClass< GUM_SCALAR >& type() const;
124
129 bool exists(NodeId id) const;
130
135 bool exists(const std::string& name) const;
136
154
163 PRMAttribute< GUM_SCALAR >& get(const std::string& name);
164
181 const PRMAttribute< GUM_SCALAR >& get(NodeId id) const;
182
191 const PRMAttribute< GUM_SCALAR >& get(const std::string& name) const;
192
197 Size size() const;
198
200 // ========================================================================
202 // ========================================================================
204
217 void instantiate();
218
232
234 // ========================================================================
236 // ========================================================================
238
267 void add(NodeId id, PRMInstance< GUM_SCALAR >& instance);
268
287
299
306 bool hasRefAttr(NodeId id) const;
307
311 std::vector< std::pair< PRMInstance< GUM_SCALAR >*, std::string > >& getRefAttr(NodeId id);
312
316 const std::vector< std::pair< PRMInstance< GUM_SCALAR >*, std::string > >&
317 getRefAttr(NodeId id) const;
318
320 // ========================================================================
322 // ========================================================================
324
328
332 iterator begin();
333
336 const iterator& end();
337
341
345 const_iterator begin() const;
346
349 const const_iterator& end() const;
350
357 public:
358 explicit RefIterator(Set< PRMInstance< GUM_SCALAR >* >& set);
359
360 RefIterator(const RefIterator& from);
361
362 ~RefIterator();
363
364 RefIterator& operator=(const RefIterator& from);
365
367
368 bool isEnd() const;
369
370 bool operator!=(const RefIterator& from) const;
371
372 bool operator==(const RefIterator& from) const;
373
376
377 private:
380 };
381
401
408 public:
409 explicit RefConstIterator(const Set< PRMInstance< GUM_SCALAR >* >& set);
410
412
414
416
418
419 bool isEnd() const;
420
421 bool operator!=(const RefConstIterator& from) const;
422
423 bool operator==(const RefConstIterator& from) const;
424
427
428 private:
431 };
432
451 RefConstIterator begin(NodeId id) const;
452
453 using InvRefIterator = typename NodeProperty<
454 std::vector< std::pair< PRMInstance< GUM_SCALAR >*, std::string > >* >::iterator;
456 std::vector< std::pair< PRMInstance< GUM_SCALAR >*, std::string > >* >::const_iterator;
457
459 const InvRefIterator& endInvRef();
460
462 const InvRefConstIterator& endInvRef() const;
463
465
466 private:
469
472
473 // ========================================================================
475 // ========================================================================
477
480
484
488 void _doInstantiate_();
489
494
502
508
510 // ========================================================================
512 // ========================================================================
514
517
520
524
529
531 using pair = std::pair< PRMInstance< GUM_SCALAR >*, std::string >;
532
536
539
541 };
542
543
544#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
545 extern template class PRMInstance< double >;
546#endif
547
548
549 } /* namespace prm */
550} /* namespace gum */
551
553
554#endif /* GUM_INSTANCE_H */
Headers of gum::prm::Class<GUM_SCALAR>.
Inline implementation of gum::PRMInstance.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1594
Representation of a set.
Definition set.h:131
PRMAttribute is a member of a Class in a PRM.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition PRMClass.h:75
Nested class to iterate over PRMReferenceSlot and PRMSlotChain<GUM_SCALAR> instantiations.
const Set< PRMInstance< GUM_SCALAR > * > & _set_
bool operator!=(const RefConstIterator &from) const
const PRMInstance< GUM_SCALAR > * operator->() const
RefConstIterator & operator=(const RefConstIterator &from)
const PRMInstance< GUM_SCALAR > & operator*() const
bool operator==(const RefConstIterator &from) const
RefConstIterator(const Set< PRMInstance< GUM_SCALAR > * > &set)
Set< PRMInstance< GUM_SCALAR > * >::const_iterator _iter_
Nested class to iterate over PRMReferenceSlot and PRMSlotChain<GUM_SCALAR> instantiations.
PRMInstance< GUM_SCALAR > & operator*() const
RefIterator(Set< PRMInstance< GUM_SCALAR > * > &set)
RefIterator & operator=(const RefIterator &from)
Set< PRMInstance< GUM_SCALAR > * >::iterator _iter_
Set< PRMInstance< GUM_SCALAR > * > & _set_
bool operator==(const RefIterator &from) const
bool operator!=(const RefIterator &from) const
PRMInstance< GUM_SCALAR > * operator->() const
std::pair< PRMInstance< GUM_SCALAR > *, std::string > pair
Code alias.
NodeProperty< PRMAttribute< GUM_SCALAR > * > _nodeIdMap_
The gum::prm::PRMAttribute<GUM_SCALAR> and gum::prm::PRMAggregate<GUM_SCALAR> of this PRMInstance<GUM...
void instantiate()
Instantiate all nodes which requires it.
const iterator & end()
Returns a reference over the iterator at the end of the list of gum::prm::PRMAttribute<GUM_SCALAR> in...
void _copyAttributeCPF_(PRMAttribute< GUM_SCALAR > *attr)
Copy the content of an PRMAttribute<GUM_SCALAR> from its Class<GUM_SCALAR> counterpart.
typename NodeProperty< PRMAttribute< GUM_SCALAR > * >::const_iterator const_iterator
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
NodeProperty< Set< PRMInstance< GUM_SCALAR > * > * > _referenceMap_
Mapping between the gum::prm::PRMReferenceSlot and gum::prm::PRMSlotChain<GUM_SCALAR> in type / and t...
PRMInstance< GUM_SCALAR > & operator=(const PRMClass< GUM_SCALAR > &from)
Copy operator. Don't use it.
virtual prm_type obj_type() const
Returns the PRM type of this object.
const Bijection< const DiscreteVariable *, const DiscreteVariable * > & bijection() const
Returns a mapping between DiscreteVariable used in this and the ones used in this PRMInstance<GUM_SCA...
InvRefIterator beginInvRef()
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
bool _instantiated_
True if this instance has been instantiated.
Bijection< const DiscreteVariable *, const DiscreteVariable * > _bijection_
A bijection used for MultiDim handling.
const InvRefIterator & endInvRef()
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
bool exists(NodeId id) const
Returns true if id matches an PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
std::vector< std::pair< PRMInstance< GUM_SCALAR > *, std::string > > & getRefAttr(NodeId id)
Returns a vector of pairs of refering attributes of id.
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
PRMClass< GUM_SCALAR > * _type_
The type of this PRMInstance<GUM_SCALAR>.
const Set< PRMInstance< GUM_SCALAR > * > & getInstances(NodeId id) const
Returns the Set of PRMInstance<GUM_SCALAR> referenced by id.
void _copyAttribute_(PRMAttribute< GUM_SCALAR > *source)
Used at construction to instantiate attributes.
void _instantiateSlotChain_(PRMSlotChain< GUM_SCALAR > *sc)
Retrieve all instances referred by sc.
typename NodeProperty< std::vector< std::pair< PRMInstance< GUM_SCALAR > *, std::string > > * >::const_iterator InvRefConstIterator
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
typename NodeProperty< PRMAttribute< GUM_SCALAR > * >::iterator iterator
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
void _addReferingInstance_(PRMSlotChain< GUM_SCALAR > *sc, PRMInstance< GUM_SCALAR > *i)
Add this as a referring instance over the attribute pointed by sc in i.
bool hasRefAttr(NodeId id) const
Returns true if id has at least one referring PRMAttribute<GUM_SCALAR>.
PRMInstance(const std::string &name, PRMClass< GUM_SCALAR > &type)
Default constructor of an PRMInstance<GUM_SCALAR>.
typename NodeProperty< std::vector< std::pair< PRMInstance< GUM_SCALAR > *, std::string > > * >::iterator InvRefIterator
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
const PRMInstance< GUM_SCALAR > & getInstance(NodeId id) const
Fast access to the first instance in a PRMReferenceSlot or PRMSlotChain<GUM_SCALAR>.
iterator begin()
Returns an iterator at the begining of the list of gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInst...
virtual ~PRMInstance()
Destructor.
void add(NodeId id, PRMInstance< GUM_SCALAR > &instance)
Add an PRMInstance<GUM_SCALAR> to a given PRMReferenceSlot, PRMSlotChain<GUM_SCALAR> or output node.
NodeProperty< std::vector< pair > * > _referingAttr_
The set of pair (instance, attribute) referring an attribute of this instance.
Size size() const
Returns the number of attributes in this PRMInstance<GUM_SCALAR>.
void _copyAggregates_(PRMAggregate< GUM_SCALAR > *source)
Used at construction to instantiate aggregates.
void _doInstantiate_()
Starts this instance instantiations.
const std::string & name() const
Returns the name of this object.
prm_type
Enumeration of the different types of objects handled by a PRM.
Definition PRMObject.h:88
PRMObject(const std::string &name)
Constructor.
Definition PRMObject.cpp:62
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46