aGrUM 3.1.1
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-2026 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-2026 *
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#pragma once
42
43
51
52namespace gum {
53 namespace prm {
54
55 template < GUM_Numeric GUM_SCALAR >
57 std::string_view name,
59 PRMClassElement< GUM_SCALAR >(name),
60 _chain_(new Sequence< PRMClassElement< GUM_SCALAR >* >(chain)), _isMultiple_(false) {
61 GUM_CONSTRUCTOR(PRMSlotChain);
62
63 if (_chain_->size() < 2) {
64 GUM_ERROR(OperationNotAllowed, "chain must contain at least two ClassElement")
65 }
66
67 for (Size i = 0; i < _chain_->size() - 1; ++i) {
68 if (_chain_->atPos(i)->elt_type() != PRMClassElement< GUM_SCALAR >::prm_refslot) {
69 GUM_ERROR(WrongClassElement, "illegal ClassElement in chain")
70 } else {
73 || static_cast< PRMReferenceSlot< GUM_SCALAR >* >(_chain_->atPos(i))->isArray();
74 }
75 }
76
78
79 this->safeName_ = (PRMObject::LEFT_CAST() + lastElt().type().name() + PRMObject::RIGHT_CAST())
80 .append(name);
81 this->lastElt().type().variable().setName(name);
82 }
83
84 // Parameters are inverse to prevent unwanted constructors calls (it
85 // happened)
86 template < GUM_Numeric GUM_SCALAR >
88 std::string_view name) : PRMSlotChain(name, *chain) {
89 // No need to
90 // GUM_CONSTRUCTOR(PRMSlotChain);
91
92 // if ( _chain_->size() < 2) {
93 // GUM_ERROR(OperationNotAllowed,
94 // "chain must containt at least two PRMClassElement")
95 //}
96
97 // for (Size i = 0; i < _chain_->size() - 1; ++i) {
98 // if (not( _chain_->atPos(i)->elt_type() !=
99 // PRMClassElement<GUM_SCALAR>::prm_refslot)) {
100 // GUM_ERROR(WrongClassElement, "illegal PRMClassElement in chain")
101 // } else {
102 // _isMultiple_ =
103 // _isMultiple_ or
104 // static_cast<PRMReferenceSlot<GUM_SCALAR>
105 // *>( _chain_->atPos(i))->isArray();
106 // }
107 //}
108
109 // __copyLastElt();
110 // this->safeName_ = PRMObject::LEFT_CAST() + lastElt().type().name() +
111 // PRMObject::RIGHT_CAST() + name;
112 }
113
114 template < GUM_Numeric GUM_SCALAR >
116 PRMClassElement< GUM_SCALAR >* new_elt = nullptr;
117
118 switch (_chain_->back()->elt_type()) {
120 auto old_attr = static_cast< const PRMAttribute< GUM_SCALAR >* >(_chain_->back());
121
123 for (auto var: old_attr->cpf().variablesSequence()) {
124 if (var != &(old_attr->type().variable())) { bij.insert(var, var); }
125 }
126
127 new_elt = old_attr->copy(bij);
128 break;
129 }
130
132 const PRMAggregate< GUM_SCALAR >* c_agg
133 = static_cast< const PRMAggregate< GUM_SCALAR >* >(_chain_->back());
135 c_agg->agg_type(),
136 c_agg->type(),
137 c_agg->id());
138 new_elt = agg;
139 break;
140 }
141
142 default : {
143 GUM_ERROR(WrongClassElement, "last element of chain is not valid")
144 }
145 }
146
147 new_elt->setId(_chain_->back()->id());
148 _chain_->setAtPos(_chain_->size() - 1, new_elt);
149 }
150
151 template < GUM_Numeric GUM_SCALAR >
153 GUM_DESTRUCTOR(PRMSlotChain);
154 delete _chain_->back();
155 delete _chain_;
156 }
157
158 template < GUM_Numeric GUM_SCALAR >
160 PRMClassElement< GUM_SCALAR >(source.name()),
161 _chain_(new Sequence< PRMClassElement< GUM_SCALAR >* >(source.chain())),
162 _isMultiple_(source.isMultiple()) {
163 GUM_CONS_CPY(PRMSlotChain);
165 }
166
167 template < GUM_Numeric GUM_SCALAR >
170 GUM_ERROR(FatalError, "Illegal call to gum::SlotChain<GUM_SCALAR> copy constructor.")
171 }
172
173 template < GUM_Numeric GUM_SCALAR >
176 return this->prm_slotchain;
177 }
178
179 template < GUM_Numeric GUM_SCALAR >
181 return _chain_->back()->type();
182 }
183
184 template < GUM_Numeric GUM_SCALAR >
186 return _chain_->back()->type();
187 }
188
189 template < GUM_Numeric GUM_SCALAR >
190 const Tensor< GUM_SCALAR >& PRMSlotChain< GUM_SCALAR >::cpf() const {
191 return _chain_->back()->cpf();
192 }
193
194 template < GUM_Numeric GUM_SCALAR >
199
200 template < GUM_Numeric GUM_SCALAR >
202 return static_cast< PRMReferenceSlot< GUM_SCALAR >* >(_chain_->atPos(_chain_->size() - 2))
203 ->slotType();
204 }
205
206 template < GUM_Numeric GUM_SCALAR >
210
211 template < GUM_Numeric GUM_SCALAR >
213 return *(_chain_->back());
214 }
215
216 template < GUM_Numeric GUM_SCALAR >
220
221 template < GUM_Numeric GUM_SCALAR >
225
226 template < GUM_Numeric GUM_SCALAR >
228
229 template < GUM_Numeric GUM_SCALAR >
231
232 template < GUM_Numeric GUM_SCALAR >
234 return _isMultiple_;
235 }
236
237 template < GUM_Numeric 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:1640
Exception : fatal (unknown ?) error.
Exception : operation not allowed.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
Exception: wrong PRMClassElement for this operation.
PRMType & type() override
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(std::string_view name)
Default constructor of a PRMClassElement.
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.
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.
std::string safeName_
The safe name of this PRMClassElement.
const std::string & name() const
Returns the name of this object.
static std::string RIGHT_CAST()
Enumeration of the different types of objects handled by a PRM.
static std::string LEFT_CAST()
Enumeration of the different types of objects handled by a PRM.
A PRMReferenceSlot represent a relation between two PRMClassElementContainer.
bool isArray() const
Returns true if this reference slot is an array.
PRMClassElementContainer< GUM_SCALAR > & slotType()
Returns the type of this slot, which is a PRMClassElementContainer (it is not the type of PRMObject).
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
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_
The sequence of PRMClassElement<GUM_SCALAR> composing the slot chain.
PRMType & type() override
This is similar to the following call: this->lastElt().type().
PRMClassElement< GUM_SCALAR > & lastElt()
Returns the last element of the slot chain, typically this is an gum::PRMAttribute or a gum::PRMAggre...
Sequence< PRMClassElement< GUM_SCALAR > * > & chain()
Return the sequence representing the chain of elements in this PRMSlotChain.
void addParent(const PRMClassElement< GUM_SCALAR > &elt) override
See gum::PRMClassElement<GUM_SCALAR>::addParent_().
void addChild(const PRMClassElement< GUM_SCALAR > &elt) override
See gum::PRMClassElement<GUM_SCALAR>::addChild_().
PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const override
See gum::PRMClassElement<GUM_SCALAR>::elt_type().
PRMSlotChain(std::string_view name, const Sequence< PRMClassElement< GUM_SCALAR > * > &chain)
Default constructor.
bool isMultiple() const
Return true if this slot chain contains at least one multiple reference slot.
~PRMSlotChain() override
Destructor.
const Tensor< GUM_SCALAR > & cpf() const override
This is similar to the following call: this->lastElt().cpf().
PRMClassElementContainer< GUM_SCALAR > & end()
Returns the PRMClassElement<GUM_SCALAR>Container over which this slot chain ends.
PRMAttribute< GUM_SCALAR > * getCastDescendant() const override
Raise a gum::OperationNotAllowed exception See gum::PRMClassElement<GUM_SCALAR>::getCastDescendant().
PRMSlotChain & operator=(const PRMSlotChain &source)
Copy operator. Don't use it.
This is a decoration of the DiscreteVariable class.
Definition PRMType.h:78
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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