aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
classBayesNet_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Inline implementation of ClassBayesNet.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #include <agrum/PRM/classBayesNet.h> // to ease IDE parser
29 #include <agrum/tools/core/inline.h>
30 
31 namespace gum {
32  namespace prm {
33 
34  template < typename GUM_SCALAR >
35  void ClassBayesNet< GUM_SCALAR >::_init_(const PRMClass< GUM_SCALAR >& c) {
36  for (const auto node: c.containerDag().nodes()) {
37  try {
38  // Adding the attribute
41  const PRMClassElement< GUM_SCALAR >& elt = c.get(node);
42  this->dag_.addNodeWithId(elt.id());
43  this->_varNodeMap_.insert(&(elt.type().variable()), &elt);
44  }
45  } catch (NotFound&) {
46  // Not an attribute
47  }
48  }
49 
50  for (const auto& arc: c.containerDag().arcs()) {
51  try {
52  this->dag_.addArc(arc.tail(), arc.head());
53  } catch (InvalidNode&) {
54  // Not added means not an attribute
55  }
56  }
57  }
58 
59  template < typename GUM_SCALAR >
61  IBayesNet< GUM_SCALAR >(), _class_(&c) {
63  _init_(c);
64  }
65 
66  template < typename GUM_SCALAR >
70  }
71 
72  template < typename GUM_SCALAR >
75  }
76 
77  template < typename GUM_SCALAR >
80  if (this != &from) {
82 
84  }
85 
86  return *this;
87  }
88 
89  template < typename GUM_SCALAR >
91  return _get_(varId).cpf();
92  }
93 
94  template < typename GUM_SCALAR >
96  GUM_ERROR(FatalError, "Sorry no VarMap in a ClassBayesNet.")
97  }
98 
99  template < typename GUM_SCALAR >
101  return _get_(id).type().variable();
102  }
103 
104  template < typename GUM_SCALAR >
106  return _varNodeMap_[&var]->id();
107  }
108 
109  template < typename GUM_SCALAR >
111  return _get_(name).id();
112  }
113 
114  template < typename GUM_SCALAR >
115  INLINE const DiscreteVariable&
117  return _get_(name).type().variable();
118  }
119 
120  template < typename GUM_SCALAR >
123  if (this->dag_.exists(id)) {
124  return _class_->get(id);
125  } else {
126  GUM_ERROR(NotFound, "no element found with that id.")
127  }
128  }
129 
130  template < typename GUM_SCALAR >
133  try {
134  return _class_->get(name);
135  } catch (NotFound&) { GUM_ERROR(NotFound, "no element found with that id.") }
136  }
137 
138  template < typename GUM_SCALAR >
140  if (_modalities_.empty()) {
141  for (const auto node: this->nodes()) {
143  }
144  }
145 
146  return _modalities_;
147  }
148 
149  template < typename GUM_SCALAR >
151  std::string tab = " ";
153  output << "digraph \"";
154  output << _class_->name() << "\" {" << std::endl;
155 
156  for (const auto node: this->nodes()) {
157  if (this->children(node).size() > 0)
158  for (const auto chi: this->children(node)) {
159  output << tab << "\"" << variable(node).name() << "\" -> ";
160  output << "\"" << variable(chi).name() << "\";" << std::endl;
161  }
162  else if (this->parents(node).size() == 0) {
163  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
164  }
165  }
166 
167  output << "}" << std::endl;
168  return output.str();
169  }
170 
171  } /* namespace prm */
172 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)