aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
O3prmBNReader_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
50// to ease Parser
52
53namespace gum {
54 template < typename GUM_SCALAR >
55 INLINE std::string O3prmBNReader< GUM_SCALAR >::_getVariableName_(const std::string& path,
56 const std::string& type,
57 const std::string& name,
58 const std::string& toRemove) {
59 auto res = path + name; // path ends up with a "."
60 if (toRemove != "") {
61 if (res.substr(0, toRemove.size()) == toRemove) { res = res.substr(toRemove.size()); }
62 }
63 return res;
64 }
65
66 template < typename GUM_SCALAR >
67 std::string O3prmBNReader< GUM_SCALAR >::_getInstanceName_(const std::string& classname) {
68 auto res = classname.substr(0, 4);
69 std::transform(res.begin(), res.end(), res.begin(), ::tolower);
70 return res;
71 }
72
73 template < typename GUM_SCALAR >
74 std::string O3prmBNReader< GUM_SCALAR >::_getEntityName_(const std::string& filename) {
75 auto b = filename.find_last_of("/\\");
76 auto e = filename.find_last_of(".") - 1;
77 GUM_ASSERT(e > b); // we are waiting ../../basename.o3prm
78 return filename.substr(b + 1, e - b);
79 }
80
81 template < typename GUM_SCALAR >
83 const std::string& filename,
84 const std::string& entityName,
85 const std::string& classpath) :
86 BNReader< GUM_SCALAR >(bn, filename) {
87 GUM_CONSTRUCTOR(O3prmBNReader);
88 _bn_ = bn;
89 _filename_ = filename;
90 _entityName_ = entityName.empty() ? _getEntityName_(filename) : entityName;
91 _classpath_ = classpath;
92 }
93
94 template < typename GUM_SCALAR >
98
102 template < typename GUM_SCALAR >
105 if (_classpath_ != "") { reader.addClassPath(_classpath_); }
106 reader.readFile(_filename_);
108 _errors_ = reader.errorsContainer();
109
110
111 if (errors() == 0) {
112 std::string instanceName = "";
113 if (prm->isSystem(_entityName_)) {
114 _generateBN_(prm->getSystem(_entityName_));
115 } else if (prm->isClass(_entityName_)) {
116 ParseError warn(false,
117 "No system '" + _entityName_
118 + "' found but class found. Generating unnamed instance.",
120 0);
121 _errors_.add(warn);
123 instanceName = _getInstanceName_(_entityName_);
124 auto i = new gum::prm::PRMInstance< GUM_SCALAR >(instanceName, prm->getClass(_entityName_));
125 s.add(i);
126 _generateBN_(s);
127 instanceName += "."; // to be removed in _getVariableName_
128 } else if (prm->classes().size() == 1) {
129 const std::string& entityName = (*prm->classes().begin())->name();
130 ParseError warn(false,
131 "Unique class '" + entityName + "' found. Generating unnamed instance.",
133 0);
134 _errors_.add(warn);
135
136 gum::prm::PRMSystem< GUM_SCALAR > s("S_" + entityName);
137 instanceName = _getInstanceName_(entityName);
138 auto i = new gum::prm::PRMInstance< GUM_SCALAR >(instanceName, prm->getClass(entityName));
139 s.add(i);
140 _generateBN_(s);
141
142 // force the name of the BN to be the name of the class instead of the name
143 // of the file
144 _bn_->setProperty("name", entityName);
145 instanceName += "."; // to be removed in _getVariableName_
146 } else {
147 ParseError err(true,
148 "Neither system nor class '" + _entityName_ + "' and more than one class.",
150 0);
151 _errors_.add(err);
152 }
153
154 // renaming variables in th BN
156 for (auto node: _bn_->nodes()) {
157 // keeping the complete name in description
158 const std::string& nn = _bn_->variable(node).name();
159 _bn_->variable(node).setDescription(nn);
160
161 // trying to simplify the
162 auto start = nn.find_first_of('(');
163 auto end = nn.find_first_of(')');
164 if (0 < start && start < end && end < nn.size()) {
165 auto path = nn.substr(0, start);
166 auto type = nn.substr(start + 1, end - start - 1);
167 auto name = nn.substr(end + 1, std::string::npos);
168
169 std::string newNameRadical = _getVariableName_(path, type, name, instanceName);
170
171 std::string newName = newNameRadical;
172 // forcing newName to be unique
173 int num = 0;
174 while (names.contains(newName)) {
175 newName = newNameRadical + std::to_string(++num);
176 }
177
178 names.insert(newName);
179 _bn_->changeVariableName(node, newName);
180 } else {
181 ParseError warn(false, "Name " + nn + " cannot be simplified.", _filename_, 0);
182 _errors_.add(warn);
183 }
184 }
185 }
186
187 delete prm;
188
189 return errors();
190 }
191
192 template < typename GUM_SCALAR >
194 system.instantiate();
196 system.groundedBN(factory);
197 _bn_->setProperty("name", _entityName_);
198 }
199} // namespace gum
Inline implementation of O3prBNmReader : reader for BN using o3prm syntaxt.
BNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename)
Constructor A reader is defined for reading a defined file.
A factory class to ease BayesNet construction.
Size errors()
publishing Errors API
std::string _filename_
static std::string _getInstanceName_(const std::string &classname)
std::string _entityName_
ErrorsContainer _errors_
O3prmBNReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename, const std::string &entityName="", const std::string &classPath="")
void _generateBN_(prm::PRMSystem< GUM_SCALAR > &system)
std::string _classpath_
BayesNet< GUM_SCALAR > * _bn_
static std::string _getVariableName_(const std::string &path, const std::string &type, const std::string &name, const std::string &toRemove="")
static std::string _getEntityName_(const std::string &filename)
Size proceed()
parse the file
This class is used to represent parsing errors for the different parser implemented in aGrUM.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:497
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
Definition PRMInstance.h:79
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition PRMSystem.h:70
void instantiate()
Instantiate all the PRMInstance in this PRMSystem.
void groundedBN(BayesNetFactory< GUM_SCALAR > &factory) const
Returns the grounded Bayesian network of this system.
NodeId add(PRMInstance< GUM_SCALAR > *i)
Add an PRMInstance to this system.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition PRM.h:74
This class read O3PRM files and creates the corresponding gum::prm::PRM.
Definition O3prmReader.h:84
const ErrorsContainer & errorsContainer() const
publishing Errors API
void addClassPath(const std::string &class_path)
Add a list of paths to look for o3prm files.
gum::prm::PRM< GUM_SCALAR > * prm()
Size readFile(const std::string &file, const std::string &module="")
Read file and load its content using a PRMFactory. The package parameter set the file's content packa...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46