aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
O3prmBNReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 O3prmReader.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Lionel TORTI
27  */
28 
29 // to ease Parser
30 #include <agrum/PRM/o3prm/O3prmBNReader.h>
31 
32 namespace gum {
33  template < typename GUM_SCALAR >
36  const std::string& type,
37  const std::string& name,
38  const std::string& toRemove) {
39  auto res = path + name; // path ends up with a "."
40  if (toRemove != "") {
41  if (res.substr(0, toRemove.size()) == toRemove) {
42  res = res.substr(toRemove.size());
43  }
44  }
45  return res;
46  }
47 
48  template < typename GUM_SCALAR >
49  std::string
51  auto res = classname.substr(0, 4);
52  std::transform(res.begin(), res.end(), res.begin(), ::tolower);
53  return res;
54  }
55 
56  template < typename GUM_SCALAR >
57  std::string
59  auto b = filename.find_last_of("/\\");
60  auto e = filename.find_last_of(".") - 1;
61  GUM_ASSERT(e > b); // we are waiting ../../basename.o3prm
62  return filename.substr(b + 1, e - b);
63  }
64 
65  template < typename GUM_SCALAR >
67  const std::string& filename,
68  const std::string& entityName,
69  const std::string& classpath) :
72  bn__ = bn;
76  }
77 
78  template < typename GUM_SCALAR >
81  }
82 
83  /// parse.
84  /// @return the number of detected errors
85  /// @throws IOError if file not exists
86  template < typename GUM_SCALAR >
91  gum::prm::PRM< GUM_SCALAR >* prm = reader.prm();
93 
94 
95  if (errors() == 0) {
96  std::string instanceName = "";
97  if (prm->isSystem(entityName__)) {
99  } else if (prm->isClass(entityName__)) {
101  false,
102  "No system '" + entityName__
103  + "' found but class found. Generating unnamed instance.",
104  filename__,
105  0);
106  errors__.add(warn);
107  gum::prm::PRMSystem< GUM_SCALAR > s("S_" + entityName__);
109  auto i
112  s.add(i);
113  generateBN__(s);
114  instanceName += "."; // to be removed in getVariableName__
115  } else if (prm->classes().size() == 1) {
116  const std::string& entityName = (*prm->classes().begin())->name();
117  ParseError warn(false,
118  "Unique class '" + entityName
119  + "' found. Generating unnamed instance.",
120  filename__,
121  0);
122  errors__.add(warn);
123 
124  gum::prm::PRMSystem< GUM_SCALAR > s("S_" + entityName);
126  auto i
129  s.add(i);
130  generateBN__(s);
131 
132  // force the name of the BN to be the name of the class instead of the name
133  // of the file
134  bn__->setProperty("name", entityName);
135  instanceName += "."; // to be removed in getVariableName__
136  } else {
137  ParseError err(true,
138  "Neither system nor class '" + entityName__
139  + "' and more than one class.",
140  filename__,
141  0);
142  errors__.add(err);
143  }
144 
145  // renaming variables in th BN
146  gum::Set< std::string > names;
147  for (auto node: bn__->nodes()) {
148  // keeping the complete name in description
149  const std::string& nn = bn__->variable(node).name();
151 
152  // trying to simplify the
153  auto start = nn.find_first_of('(');
154  auto end = nn.find_first_of(')');
155  if (0 < start && start < end && end < nn.size()) {
156  auto path = nn.substr(0, start);
157  auto type = nn.substr(start + 1, end - start - 1);
158  auto name = nn.substr(end + 1, std::string::npos);
159 
162 
164  // forcing newName to be unique
165  int num = 0;
166  while (names.contains(newName)) {
168  }
169 
172  } else {
173  ParseError warn(false,
174  "Name " + nn + " cannot be simplified.",
175  filename__,
176  0);
177  errors__.add(warn);
178  }
179  }
180  }
181 
182  delete prm;
183 
184  return errors();
185  }
186 
187 
188  template < typename GUM_SCALAR >
194  bn__->setProperty("name", entityName__);
195  }
196 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669