aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
PRMClassElementContainer_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
49
52
53namespace gum {
54
55 namespace prm {
56 template < typename GUM_SCALAR >
59 for (const auto& flag: c._IOFlags_)
60 setIOFlag_(get(flag.first), flag.second);
61 }
62
63 template < typename GUM_SCALAR >
64 INLINE
69
70 template < typename GUM_SCALAR >
74
75 template < typename GUM_SCALAR >
79 GUM_ERROR(FatalError, "illegal call to ClassElementContainer copy operator")
80 }
81
82 template < typename GUM_SCALAR >
84 const PRMClassElementContainer< GUM_SCALAR >& source) : PRMObject(source) {
85 GUM_CONS_CPY(PRMClassElementContainer);
86 GUM_ERROR(FatalError, "illegal call to ClassElementContainer copy constructor")
87 }
88
89 template < typename GUM_SCALAR >
91 const PRMClassElement< GUM_SCALAR >& elt) const {
92 try {
93 return getIOFlag_(elt).first;
94 } catch (NotFound const&) { return false; }
95 }
96
97 template < typename GUM_SCALAR >
100 bool b) {
101 if (!exists(elt.safeName())) {
102 GUM_ERROR(NotFound, ": <" + elt.safeName() + "> is not in <" + name() + ">")
105 try {
106 getIOFlag_(elt).first = b;
107 } catch (NotFound const&) { setIOFlag_(elt, std::make_pair(b, false)); }
108 } else {
109 GUM_ERROR(WrongClassElement, "given id is not an PRMAttribute or an PRMAggregate")
110 }
111 }
112
113 template < typename GUM_SCALAR >
116 bool b) {
117 if (!exists(elt.safeName())) {
118 GUM_ERROR(NotFound, "<" + elt.safeName() + "> is not in <" + name() + ">")
121 try {
122 getIOFlag_(elt).second = b;
123 } catch (NotFound const&) { setIOFlag_(elt, std::make_pair(false, b)); }
124
125 if (b) { updateDescendants_(elt); }
126 } else {
128 "given ClassElement<GUM_SCALAR> is not an "
129 "PRMAttribute or an PRMAggregate");
130 }
131 }
132
133 template < typename GUM_SCALAR >
135 const PRMClassElement< GUM_SCALAR >& elt) const {
136 try {
137 return !(getIOFlag_(elt).first || getIOFlag_(elt).second);
138 } catch (NotFound const&) { return true; }
139 }
140
141 template < typename GUM_SCALAR >
146
147 template < typename GUM_SCALAR >
150 try {
151 return _IOFlags_[elt.safeName()];
152 } catch (NotFound const&) {
153 GUM_ERROR(NotFound, "this ClassElement<GUM_SCALAR> does not have any IO flags")
154 }
155 }
156
157 template < typename GUM_SCALAR >
158 INLINE const std::pair< bool, bool >& PRMClassElementContainer< GUM_SCALAR >::getIOFlag_(
159 const PRMClassElement< GUM_SCALAR >& elt) const {
160 try {
161 return _IOFlags_[elt.safeName()];
162 } catch (NotFound const&) {
163 GUM_ERROR(NotFound, "this ClassElement<GUM_SCALAR> does not have any IO flags")
164 }
165 }
166
167 template < typename GUM_SCALAR >
168 INLINE void
170 const std::pair< bool, bool >& flags) {
171 try {
172 _IOFlags_[elt.safeName()] = flags;
173 } catch (NotFound const&) { _IOFlags_.insert(elt.safeName(), flags); }
174 }
175
176 template < typename GUM_SCALAR >
178 return containerDag().exists(id);
179 }
180
181 template < typename GUM_SCALAR >
182 INLINE bool PRMClassElementContainer< GUM_SCALAR >::exists(const std::string& name) const {
183 try {
184 get(name);
185 return true;
186 } catch (NotFound const&) { return false; }
187 }
188
189 template < typename GUM_SCALAR >
191 const PRMClassElement< GUM_SCALAR >& elt) const {
192 try {
193 return &elt == &(get(elt.safeName()));
194 } catch (NotFound const&) { return false; }
195 }
196
197 template < typename GUM_SCALAR >
199 return dag_();
200 }
201
202 } /* namespace prm */
203} /* namespace gum */
204
205template < typename GUM_SCALAR >
206std::ostream& operator<<(std::ostream& output,
208 std::string tab = " ";
209 output << "digraph \"" << container.name() << "\" {" << std::endl;
210
211 for (const auto node: container.containerDag().nodes()) {
212 if (container.containerDag().children(node).size() > 0) {
213 for (const auto chi: container.containerDag().children(node)) {
214 output << tab << "\"" << container.get(node).name() << "\" -> "
215 << "\"" << container.get(chi).name() << "\";" << std::endl;
216 }
217 } else if (container.containerDag().parents(node).size() == 0) {
218 output << tab << "\"" << container.get(node).name() << "\";" << std::endl;
219 }
220 }
221
222 output << "}" << std::endl;
223 return output;
224}
Headers of gum::prm::PRMClassElementContainer.
std::ostream & operator<<(std::ostream &output, const gum::prm::PRMClassElementContainer< GUM_SCALAR > &container)
An << operator for PRMClassElementContainer. Output in the graphviz-dot format.
Headers of gum::PRMClassElement.
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
Base class for dag.
Definition DAG.h:121
Exception : fatal (unknown ?) error.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Exception : the element we looked for cannot be found.
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:636
Exception: wrong PRMClassElement for this operation.
<agrum/PRM/classElementContainer.h>
virtual PRMClassElement< GUM_SCALAR > & get(const std::string &name)=0
Getter on a member of this PRMClassElementContainer.
virtual bool isInnerNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if the node is an inner node.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
virtual std::pair< bool, bool > & getIOFlag_(const PRMClassElement< double > &elt)
virtual void copyIOFlags_(const PRMClassElementContainer< GUM_SCALAR > &c)
Copy the IO Flags of c in this PRMClassElementContainer.
PRMClassElementContainer< GUM_SCALAR > & operator=(const PRMClassElementContainer< GUM_SCALAR > &source)
Copy operator. Don't use it.
virtual void updateDescendants_(const PRMClassElement< GUM_SCALAR > &elt)=0
When a PRMClassElement<GUM_SCALAR> becomes an Output node we must update any the IO flags of every de...
virtual bool belongsTo(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if elt belongs to this PRMClassElementContainer.
HashTable< std::string, std::pair< bool, bool > > _IOFlags_
input / output flags, useful when inheriting or copying.
virtual bool isSubTypeOf(const PRMClassElementContainer< GUM_SCALAR > &cec) const =0
Test if this PRMClassElementContainer is a subtype of cec.
virtual bool isSuperTypeOf(const PRMClassElementContainer< GUM_SCALAR > &cec) const
Test if this PRMClassElementContainer is a super type of cec.
virtual void setIOFlag_(const PRMClassElement< GUM_SCALAR > &elt, const std::pair< bool, bool > &flags)
Defines the IO flags of a PRMClassElement<GUM_SCALAR>.
PRMClassElementContainer(const std::string &name)
Default constructor.
virtual bool isInputNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if the node is an input node.
virtual bool exists(const std::string &name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
virtual void setInputNode(const PRMClassElement< GUM_SCALAR > &elt, bool b)
Set the input flag value of id at b.
virtual void setOutputNode(const PRMClassElement< GUM_SCALAR > &elt, bool b)
Set the output flag value of id at b.
virtual const DAG & dag_() const =0
Abstract class representing an element of PRM class.
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
const std::string & name() const
Returns the name of this object.
PRMObject(const std::string &name)
Constructor.
Definition PRMObject.cpp:62
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size NodeId
Type for node ids.
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46