aGrUM  0.14.2
PRMClassElementContainer_tpl.h
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
29 
30 namespace gum {
31 
32  namespace prm {
33  template < typename GUM_SCALAR >
36  for (const auto& flag : c.__IOFlags)
37  _setIOFlag(get(flag.first), flag.second);
38  }
39 
40  template < typename GUM_SCALAR >
42  const std::string& name) :
43  PRMObject(name) {
44  GUM_CONSTRUCTOR(PRMClassElementContainer);
45  }
46 
47  template < typename GUM_SCALAR >
49  GUM_DESTRUCTOR(PRMClassElementContainer);
50  }
51 
52  template < typename GUM_SCALAR >
56  GUM_ERROR(FatalError, "illegal call to ClassElementContainer copy operator");
57  }
58 
59  template < typename GUM_SCALAR >
62  PRMObject(source) {
63  GUM_CONS_CPY(PRMClassElementContainer);
65  "illegal call to ClassElementContainer copy constructor");
66  }
67 
68  template < typename GUM_SCALAR >
70  const PRMClassElement< GUM_SCALAR >& elt) const {
71  try {
72  return _getIOFlag(elt).first;
73  } catch (NotFound&) { return false; }
74  }
75 
76  template < typename GUM_SCALAR >
78  const PRMClassElement< GUM_SCALAR >& elt, bool b) {
79  if (!exists(elt.safeName())) {
81  ": <" + elt.safeName() + "> is not in <" + name() + ">");
84  try {
85  _getIOFlag(elt).first = b;
86  } catch (NotFound&) { _setIOFlag(elt, std::make_pair(b, false)); }
87  } else {
89  "given id is not an PRMAttribute or an PRMAggregate");
90  }
91  }
92 
93  template < typename GUM_SCALAR >
95  const PRMClassElement< GUM_SCALAR >& elt, bool b) {
96  if (!exists(elt.safeName())) {
97  GUM_ERROR(NotFound, "<" + elt.safeName() + "> is not in <" + name() + ">");
100  try {
101  _getIOFlag(elt).second = b;
102  } catch (NotFound&) { _setIOFlag(elt, std::make_pair(false, b)); }
103 
104  if (b) { _updateDescendants(elt); }
105  } else {
107  "given ClassElement<GUM_SCALAR> is not an "
108  "PRMAttribute or an PRMAggregate");
109  }
110  }
111 
112  template < typename GUM_SCALAR >
114  const PRMClassElement< GUM_SCALAR >& elt) const {
115  try {
116  return !(_getIOFlag(elt).first || _getIOFlag(elt).second);
117  } catch (NotFound&) { return true; }
118  }
119 
120  template < typename GUM_SCALAR >
122  const PRMClassElementContainer< GUM_SCALAR >& cec) const {
123  return cec.isSubTypeOf(*this);
124  }
125 
126  template < typename GUM_SCALAR >
127  INLINE std::pair< bool, bool >&
129  const PRMClassElement< GUM_SCALAR >& elt) {
130  try {
131  return __IOFlags[elt.safeName()];
132  } catch (NotFound&) {
134  "this ClassElement<GUM_SCALAR> does not have any IO flags");
135  }
136  }
137 
138  template < typename GUM_SCALAR >
139  INLINE const std::pair< bool, bool >&
141  const PRMClassElement< GUM_SCALAR >& elt) const {
142  try {
143  return __IOFlags[elt.safeName()];
144  } catch (NotFound&) {
146  "this ClassElement<GUM_SCALAR> does not have any IO flags");
147  }
148  }
149 
150  template < typename GUM_SCALAR >
153  const std::pair< bool, bool >& flags) {
154  try {
155  __IOFlags[elt.safeName()] = flags;
156  } catch (NotFound&) { __IOFlags.insert(elt.safeName(), flags); }
157  }
158 
159  template < typename GUM_SCALAR >
161  return containerDag().exists(id);
162  }
163 
164  template < typename GUM_SCALAR >
166  const std::string& name) const {
167  try {
168  get(name);
169  return true;
170  } catch (NotFound&) { return false; }
171  }
172 
173  template < typename GUM_SCALAR >
175  const PRMClassElement< GUM_SCALAR >& elt) const {
176  try {
177  return &elt == &(get(elt.safeName()));
178  } catch (NotFound&) { return false; }
179  }
180 
181  template < typename GUM_SCALAR >
182  INLINE const DAG&
184  return _dag();
185  }
186 
187  } /* namespace prm */
188 } /* namespace gum */
189 
190 template < typename GUM_SCALAR >
191 std::ostream&
192  operator<<(std::ostream& output,
194  std::string tab = " ";
195  output << "digraph \"" << container.name() << "\" {" << std::endl;
196 
197  for (const auto node : container.containerDag().nodes()) {
198  if (container.containerDag().children(node).size() > 0) {
199  for (const auto chi : container.containerDag().children(node)) {
200  output << tab << "\"" << container.get(node).name() << "\" -> "
201  << "\"" << container.get(chi).name() << "\";" << std::endl;
202  }
203  } else if (container.containerDag().parents(node).size() == 0) {
204  output << tab << "\"" << container.get(node).name() << "\";" << std::endl;
205  }
206  }
207 
208  output << "}" << std::endl;
209  return output;
210 }
PRMClassElementContainer(const std::string &name)
Default constructor.
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:32
Abstract class representing an element of PRM class.
virtual PRMClassElement< GUM_SCALAR > & get(const std::string &name)=0
Getter on a member of this PRMClassElementContainer.
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...
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
virtual void _copyIOFlags(const PRMClassElementContainer< GUM_SCALAR > &c)
Copy the IO Flags of c in this PRMClassElementContainer.
virtual bool isSubTypeOf(const PRMClassElementContainer< GUM_SCALAR > &cec) const =0
Test if this PRMClassElementContainer is a subtype of cec.
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
std::ostream & operator<<(std::ostream &output, const gum::prm::PRMClassElementContainer< GUM_SCALAR > &container)
An << operator for PRMClassElementContainer. Output in the graphviz-dot format.
HashTable< std::string, std::pair< bool, bool > > __IOFlags
input / output flags, useful when inheriting or copying.
Abstract base class for any element defined in a PRM.
Definition: PRMObject.h:53
Headers of gum::PRMClassElement.
<agrum/PRM/classElementContainer.h>
Headers of gum::prm::PRMClassElementContainer.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
Base class for dag.
Definition: DAG.h:99
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52