aGrUM 3.1.1
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-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
50
53
54namespace gum {
55
56 namespace prm {
57 template < GUM_Numeric GUM_SCALAR >
60 for (const auto& flag: c._IOFlags_)
61 setIOFlag_(get(flag.first), flag.second);
62 }
63
64 template < GUM_Numeric GUM_SCALAR >
69
70 template < GUM_Numeric GUM_SCALAR >
74
75 template < GUM_Numeric GUM_SCALAR >
80
81 template < GUM_Numeric GUM_SCALAR >
83 const PRMClassElementContainer< GUM_SCALAR >& source) : PRMObject(source) {
84 GUM_CONS_CPY(PRMClassElementContainer);
85 GUM_ERROR(FatalError, "illegal call to ClassElementContainer copy constructor")
86 }
87
88 template < GUM_Numeric GUM_SCALAR >
90 const PRMClassElement< GUM_SCALAR >& elt) const {
91 auto p = _IOFlags_.tryGet(elt.safeName());
92 if (!p) return false;
93 return p->first;
94 }
95
96 template < GUM_Numeric GUM_SCALAR >
99 bool b) {
100 if (!exists(elt.safeName())) {
101 GUM_ERROR(NotFound, ": <" + elt.safeName() + "> is not in <" + name() + ">")
104 if (auto p = _IOFlags_.tryGet(elt.safeName())) {
105 p->first = b;
106 } else {
107 setIOFlag_(elt, std::make_pair(b, false));
108 }
109 } else {
110 GUM_ERROR(WrongClassElement, "given id is not an PRMAttribute or an PRMAggregate")
111 }
112 }
113
114 template < GUM_Numeric GUM_SCALAR >
117 bool b) {
118 if (!exists(elt.safeName())) {
119 GUM_ERROR(NotFound, "<" + elt.safeName() + "> is not in <" + name() + ">")
122 if (auto p = _IOFlags_.tryGet(elt.safeName())) {
123 p->second = b;
124 } else {
125 setIOFlag_(elt, std::make_pair(false, b));
126 }
127
128 if (b) { updateDescendants_(elt); }
129 } else {
131 "given ClassElement<GUM_SCALAR> is not an "
132 "PRMAttribute or an PRMAggregate");
133 }
134 }
135
136 template < GUM_Numeric GUM_SCALAR >
138 const PRMClassElement< GUM_SCALAR >& elt) const {
139 auto p = _IOFlags_.tryGet(elt.safeName());
140 if (!p) return true;
141 return !(p->first || p->second);
142 }
143
144 template < GUM_Numeric GUM_SCALAR >
149
150 template < GUM_Numeric GUM_SCALAR >
153 auto p = _IOFlags_.tryGet(elt.safeName());
154 if (!p) GUM_ERROR(NotFound, "this ClassElement<GUM_SCALAR> does not have any IO flags")
155 return *p;
156 }
157
158 template < GUM_Numeric GUM_SCALAR >
160 const PRMClassElement< GUM_SCALAR >& elt) const {
161 auto p = _IOFlags_.tryGet(elt.safeName());
162 if (!p) GUM_ERROR(NotFound, "this ClassElement<GUM_SCALAR> does not have any IO flags")
163 return *p;
164 }
165
166 template < GUM_Numeric GUM_SCALAR >
167 void
169 const std::pair< bool, bool >& flags) {
170 if (auto p = _IOFlags_.tryGet(elt.safeName())) {
171 *p = flags;
172 } else {
173 _IOFlags_.insert(elt.safeName(), flags);
174 }
175 }
176
177 template < GUM_Numeric GUM_SCALAR >
179 return containerDag().exists(id);
180 }
181
182 template < GUM_Numeric GUM_SCALAR >
184 try {
185 get(name);
186 return true;
187 } catch (NotFound const&) { return false; }
188 }
189
190 template < GUM_Numeric GUM_SCALAR >
192 const PRMClassElement< GUM_SCALAR >& elt) const {
193 try {
194 return &elt == &(get(elt.safeName()));
195 } catch (NotFound const&) { return false; }
196 }
197
198 template < GUM_Numeric GUM_SCALAR >
202
203 } /* namespace prm */
204} /* namespace gum */
205
206template < gum::GUM_Numeric GUM_SCALAR >
207std::ostream& operator<<(std::ostream& output,
209 std::string tab = " ";
210 output << "digraph \"" << container.name() << "\" {" << std::endl;
211
212 for (const auto node: container.containerDag().nodes()) {
213 if (container.containerDag().children(node).size() > 0) {
214 for (const auto chi: container.containerDag().children(node)) {
215 output << tab << "\"" << container.get(node).name() << "\" -> "
216 << "\"" << container.get(chi).name() << "\";" << std::endl;
217 }
218 } else if (container.containerDag().parents(node).size() == 0) {
219 output << tab << "\"" << container.get(node).name() << "\";" << std::endl;
220 }
221 }
222
223 output << "}" << std::endl;
224 return output;
225}
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 nodes which consists in the node and its parents returns the set of children of a ...
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:607
Exception: wrong PRMClassElement for this operation.
<agrum/PRM/classElementContainer.h>
PRMClassElementContainer(std::string_view name)
Default constructor.
virtual bool isSuperTypeOf(const PRMClassElementContainer< GUM_SCALAR > &cec) const
Test if this PRMClassElementContainer is a super type of cec.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
virtual void copyIOFlags_(const PRMClassElementContainer< GUM_SCALAR > &c)
Copy the IO Flags of c in this PRMClassElementContainer.
virtual std::pair< bool, bool > & getIOFlag_(const PRMClassElement< GUM_SCALAR > &elt)
Returns the IO flags of a PRMClassElement<GUM_SCALAR>.
virtual bool exists(std::string_view name) const
Returns true if a member with the given name exists in this PRMClassElementContainer or in the PRMCla...
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...
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 void setIOFlag_(const PRMClassElement< GUM_SCALAR > &elt, const std::pair< bool, bool > &flags)
Defines the IO flags of a PRMClassElement<GUM_SCALAR>.
virtual bool isInputNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if the node is an input node.
virtual bool isInnerNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if the node is an inner node.
virtual void setInputNode(const PRMClassElement< GUM_SCALAR > &elt, bool b)
Set the input flag value of id at b.
PRMClassElementContainer< GUM_SCALAR > & operator=(const PRMClassElementContainer< GUM_SCALAR > &source)
Copy operator. Don't use it.
virtual void setOutputNode(const PRMClassElement< GUM_SCALAR > &elt, bool b)
Set the output flag value of id at b.
virtual bool belongsTo(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if elt belongs to this PRMClassElementContainer.
virtual PRMClassElement< GUM_SCALAR > & get(std::string_view name)=0
Getter on a member of this PRMClassElementContainer.
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.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
PRMObject(std::string_view name)
Constructor.
Definition PRMObject.cpp:62
const std::string & name() const
Returns the name of this object.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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