aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
PRMSlotChain_tpl.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#pragma once
41
42
50
51namespace gum {
52 namespace prm {
53
54 template < typename GUM_SCALAR >
56 const std::string& name,
58 PRMClassElement< GUM_SCALAR >(name),
59 _chain_(new Sequence< PRMClassElement< GUM_SCALAR >* >(chain)), _isMultiple_(false) {
60 GUM_CONSTRUCTOR(PRMSlotChain);
61
62 if (_chain_->size() < 2) {
63 GUM_ERROR(OperationNotAllowed, "chain must contain at least two ClassElement")
64 }
65
66 for (Size i = 0; i < _chain_->size() - 1; ++i) {
67 if (_chain_->atPos(i)->elt_type() != PRMClassElement< GUM_SCALAR >::prm_refslot) {
68 GUM_ERROR(WrongClassElement, "illegal ClassElement in chain")
69 } else {
72 || static_cast< PRMReferenceSlot< GUM_SCALAR >* >(_chain_->atPos(i))->isArray();
73 }
74 }
75
77
78 this->safeName_
79 = PRMObject::LEFT_CAST() + lastElt().type().name() + PRMObject::RIGHT_CAST() + name;
80 this->lastElt().type().variable().setName(name);
81 }
82
83 // Parameters are inverse to prevent unwanted constructors calls (it
84 // happened)
85 template < typename GUM_SCALAR >
87 const std::string& name) : PRMSlotChain(name, *chain) {
88 // No need to
89 // GUM_CONSTRUCTOR(PRMSlotChain);
90
91 // if ( _chain_->size() < 2) {
92 // GUM_ERROR(OperationNotAllowed,
93 // "chain must containt at least two PRMClassElement")
94 //}
95
96 // for (Size i = 0; i < _chain_->size() - 1; ++i) {
97 // if (not( _chain_->atPos(i)->elt_type() !=
98 // PRMClassElement<GUM_SCALAR>::prm_refslot)) {
99 // GUM_ERROR(WrongClassElement, "illegal PRMClassElement in chain")
100 // } else {
101 // _isMultiple_ =
102 // _isMultiple_ or
103 // static_cast<PRMReferenceSlot<GUM_SCALAR>
104 // *>( _chain_->atPos(i))->isArray();
105 // }
106 //}
107
108 // __copyLastElt();
109 // this->safeName_ = PRMObject::LEFT_CAST() + lastElt().type().name() +
110 // PRMObject::RIGHT_CAST() + name;
111 }
112
113 template < typename GUM_SCALAR >
116
117 switch (_chain_->back()->elt_type()) {
119 auto old_attr = static_cast< const PRMAttribute< GUM_SCALAR >* >(_chain_->back());
120
122 for (auto var: old_attr->cpf().variablesSequence()) {
123 if (var != &(old_attr->type().variable())) { bij.insert(var, var); }
124 }
125
126 new_elt = old_attr->copy(bij);
127 break;
128 }
129
131 const PRMAggregate< GUM_SCALAR >* c_agg
132 = static_cast< const PRMAggregate< GUM_SCALAR >* >(_chain_->back());
134 c_agg->agg_type(),
135 c_agg->type(),
136 c_agg->id());
137 new_elt = agg;
138 break;
139 }
140
141 default : {
142 GUM_ERROR(WrongClassElement, "last element of chain is not valid")
143 }
144 }
146 new_elt->setId(_chain_->back()->id());
147 _chain_->setAtPos(_chain_->size() - 1, new_elt);
148 }
149
150 template < typename GUM_SCALAR >
152 GUM_DESTRUCTOR(PRMSlotChain);
153 delete _chain_->back();
154 delete _chain_;
155 }
156
157 template < typename GUM_SCALAR >
159 PRMClassElement< GUM_SCALAR >(source.name()),
160 _chain_(new Sequence< PRMClassElement< GUM_SCALAR >* >(source.chain())),
161 _isMultiple_(source.isMultiple()) {
162 GUM_CONS_CPY(PRMSlotChain);
164 }
165
166 template < typename GUM_SCALAR >
169 GUM_ERROR(FatalError, "Illegal call to gum::SlotChain<GUM_SCALAR> copy constructor.")
171
172 template < typename GUM_SCALAR >
175 return this->prm_slotchain;
176 }
177
178 template < typename GUM_SCALAR >
180 return _chain_->back()->type();
181 }
183 template < typename GUM_SCALAR >
185 return _chain_->back()->type();
186 }
187
188 template < typename GUM_SCALAR >
189 INLINE const Tensor< GUM_SCALAR >& PRMSlotChain< GUM_SCALAR >::cpf() const {
190 return _chain_->back()->cpf();
191 }
192
193 template < typename GUM_SCALAR >
198
199 template < typename GUM_SCALAR >
201 return static_cast< PRMReferenceSlot< GUM_SCALAR >* >(_chain_->atPos(_chain_->size() - 2))
202 ->slotType();
203 }
205 template < typename GUM_SCALAR >
210 template < typename GUM_SCALAR >
212 return *(_chain_->back());
213 }
214
215 template < typename GUM_SCALAR >
219
220 template < typename GUM_SCALAR >
225
226 template < typename GUM_SCALAR >
228
229 template < typename GUM_SCALAR >
231
232 template < typename GUM_SCALAR >
234 return _isMultiple_;
235 }
236
237 template < typename GUM_SCALAR >
241
242 } /* namespace prm */
243} /* namespace gum */
Headers of gum::PRMSlotChain.
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1594
Exception : fatal (unknown ?) error.
Exception : operation not allowed.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Exception: wrong PRMClassElement for this operation.
virtual PRMType & type()
See gum::PRMClassElement::type().
AggregateType agg_type() const
Returns the aggregate of *this.
PRMAttribute is a member of a Class in a PRM.
<agrum/PRM/classElementContainer.h>
PRMClassElement(const std::string &name)
Default constructor of a PRMClassElement.
virtual void setId(NodeId id)
Used to assign the id of this element.
NodeId id() const
Returns the NodeId of this element in it's class DAG.
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.
std::string safeName_
The safe name of this PRMClassElement.
const std::string & name() const
Returns the name of this object.
static std::string LEFT_CAST()
Enumeration of the different types of objects handled by a PRM.
Definition PRMObject.h:90
static std::string RIGHT_CAST()
Enumeration of the different types of objects handled by a PRM.
Definition PRMObject.h:92
A PRMReferenceSlot represent a relation between two PRMClassElementContainer.
PRMClassElementContainer< GUM_SCALAR > & slotType()
Returns the type of this slot, which is a PRMClassElementContainer (it is not the type of PRMObject).
bool isArray() const
Returns true if this reference slot is an array.
PRMSlotChain(const std::string &name, const Sequence< PRMClassElement< GUM_SCALAR > * > &chain)
Default constructor.
bool _isMultiple_
Flag indicating if this slot chain is multiple or not.
void _copyLastElt_()
Copy the last element, this prevents unwanted DuplicateElement exceptions.
Sequence< PRMClassElement< GUM_SCALAR > * > & chain()
Return the sequence representing the chain of elements in this PRMSlotChain.
virtual void addChild(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement<GUM_SCALAR>::addChild_().
Sequence< PRMClassElement< GUM_SCALAR > * > * _chain_
The sequence of PRMClassElement<GUM_SCALAR> composing the slot chain.
virtual void addParent(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement<GUM_SCALAR>::addParent_().
PRMClassElementContainer< GUM_SCALAR > & end()
Returns the PRMClassElement<GUM_SCALAR>Container over which this slot chain ends.
virtual PRMAttribute< GUM_SCALAR > * getCastDescendant() const
Raise a gum::OperationNotAllowed exception See gum::PRMClassElement<GUM_SCALAR>::getCastDescendant().
PRMClassElement< GUM_SCALAR > & lastElt()
Returns the last element of the slot chain, typically this is an gum::PRMAttribute or a gum::PRMAggre...
virtual PRMType & type()
This is similar to the following call: this->lastElt().type().
virtual PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const
See gum::PRMClassElement<GUM_SCALAR>::elt_type().
PRMSlotChain & operator=(const PRMSlotChain &source)
Copy operator. Don't use it.
virtual ~PRMSlotChain()
Destructor.
virtual const Tensor< GUM_SCALAR > & cpf() const
This is similar to the following call: this->lastElt().cpf().
bool isMultiple() const
Return true if this slot chain contains at least one multiple reference slot.
This is a decoration of the DiscreteVariable class.
Definition PRMType.h:78
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46