aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
O3prmBNReader_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 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
>
34
INLINE
std
::
string
O3prmBNReader
<
GUM_SCALAR
>::
_getVariableName_
(
const
std
::
string
&
path
,
35
const
std
::
string
&
type
,
36
const
std
::
string
&
name
,
37
const
std
::
string
&
toRemove
) {
38
auto
res
=
path
+
name
;
// path ends up with a "."
39
if
(
toRemove
!=
""
) {
40
if
(
res
.
substr
(0,
toRemove
.
size
()) ==
toRemove
) {
res
=
res
.
substr
(
toRemove
.
size
()); }
41
}
42
return
res
;
43
}
44
45
template
<
typename
GUM_SCALAR
>
46
std
::
string
O3prmBNReader
<
GUM_SCALAR
>::
_getInstanceName_
(
const
std
::
string
&
classname
) {
47
auto
res
=
classname
.
substr
(0, 4);
48
std
::
transform
(
res
.
begin
(),
res
.
end
(),
res
.
begin
(), ::
tolower
);
49
return
res
;
50
}
51
52
template
<
typename
GUM_SCALAR
>
53
std
::
string
O3prmBNReader
<
GUM_SCALAR
>::
_getEntityName_
(
const
std
::
string
&
filename
) {
54
auto
b
=
filename
.
find_last_of
(
"/\\"
);
55
auto
e
=
filename
.
find_last_of
(
"."
) - 1;
56
GUM_ASSERT
(
e
>
b
);
// we are waiting ../../basename.o3prm
57
return
filename
.
substr
(
b
+ 1,
e
-
b
);
58
}
59
60
template
<
typename
GUM_SCALAR
>
61
O3prmBNReader
<
GUM_SCALAR
>::
O3prmBNReader
(
BayesNet
<
GUM_SCALAR
>*
bn
,
62
const
std
::
string
&
filename
,
63
const
std
::
string
&
entityName
,
64
const
std
::
string
&
classpath
) :
65
BNReader
<
GUM_SCALAR
>(
bn
,
filename
) {
66
GUM_CONSTRUCTOR
(
O3prmBNReader
);
67
_bn_
=
bn
;
68
_filename_
=
filename
;
69
_entityName_
=
entityName
==
""
?
_getEntityName_
(
filename
) :
entityName
;
70
_classpath_
=
classpath
;
71
}
72
73
template
<
typename
GUM_SCALAR
>
74
O3prmBNReader
<
GUM_SCALAR
>::~
O3prmBNReader
() {
75
GUM_DESTRUCTOR
(
O3prmBNReader
);
76
}
77
78
/// parse.
79
/// @return the number of detected errors
80
/// @throws IOError if file not exists
81
template
<
typename
GUM_SCALAR
>
82
Size
O3prmBNReader
<
GUM_SCALAR
>::
proceed
() {
83
prm
::
o3prm
::
O3prmReader
<
GUM_SCALAR
>
reader
;
84
if
(
_classpath_
!=
""
) {
reader
.
addClassPath
(
_classpath_
); }
85
reader
.
readFile
(
_filename_
);
86
gum
::
prm
::
PRM
<
GUM_SCALAR
>*
prm
=
reader
.
prm
();
87
_errors_
=
reader
.
errorsContainer
();
88
89
90
if
(
errors
() == 0) {
91
std
::
string
instanceName
=
""
;
92
if
(
prm
->
isSystem
(
_entityName_
)) {
93
_generateBN_
(
prm
->
getSystem
(
_entityName_
));
94
}
else
if
(
prm
->
isClass
(
_entityName_
)) {
95
ParseError
warn
(
false
,
96
"No system '"
+
_entityName_
97
+
"' found but class found. Generating unnamed instance."
,
98
_filename_
,
99
0);
100
_errors_
.
add
(
warn
);
101
gum
::
prm
::
PRMSystem
<
GUM_SCALAR
>
s
(
"S_"
+
_entityName_
);
102
instanceName
=
_getInstanceName_
(
_entityName_
);
103
auto
i
=
new
gum
::
prm
::
PRMInstance
<
GUM_SCALAR
>(
instanceName
,
prm
->
getClass
(
_entityName_
));
104
s
.
add
(
i
);
105
_generateBN_
(
s
);
106
instanceName
+=
"."
;
// to be removed in _getVariableName_
107
}
else
if
(
prm
->
classes
().
size
() == 1) {
108
const
std
::
string
&
entityName
= (*
prm
->
classes
().
begin
())->
name
();
109
ParseError
warn
(
false
,
110
"Unique class '"
+
entityName
+
"' found. Generating unnamed instance."
,
111
_filename_
,
112
0);
113
_errors_
.
add
(
warn
);
114
115
gum
::
prm
::
PRMSystem
<
GUM_SCALAR
>
s
(
"S_"
+
entityName
);
116
instanceName
=
_getInstanceName_
(
entityName
);
117
auto
i
=
new
gum
::
prm
::
PRMInstance
<
GUM_SCALAR
>(
instanceName
,
prm
->
getClass
(
entityName
));
118
s
.
add
(
i
);
119
_generateBN_
(
s
);
120
121
// force the name of the BN to be the name of the class instead of the name
122
// of the file
123
_bn_
->
setProperty
(
"name"
,
entityName
);
124
instanceName
+=
"."
;
// to be removed in _getVariableName_
125
}
else
{
126
ParseError
err
(
true
,
127
"Neither system nor class '"
+
_entityName_
+
"' and more than one class."
,
128
_filename_
,
129
0);
130
_errors_
.
add
(
err
);
131
}
132
133
// renaming variables in th BN
134
gum
::
Set
<
std
::
string
>
names
;
135
for
(
auto
node
:
_bn_
->
nodes
()) {
136
// keeping the complete name in description
137
const
std
::
string
&
nn
=
_bn_
->
variable
(
node
).
name
();
138
_bn_
->
variable
(
node
).
setDescription
(
nn
);
139
140
// trying to simplify the
141
auto
start
=
nn
.
find_first_of
(
'('
);
142
auto
end
=
nn
.
find_first_of
(
')'
);
143
if
(0 <
start
&&
start
<
end
&&
end
<
nn
.
size
()) {
144
auto
path
=
nn
.
substr
(0,
start
);
145
auto
type
=
nn
.
substr
(
start
+ 1,
end
-
start
- 1);
146
auto
name
=
nn
.
substr
(
end
+ 1,
std
::
string
::
npos
);
147
148
std
::
string
newNameRadical
=
_getVariableName_
(
path
,
type
,
name
,
instanceName
);
149
150
std
::
string
newName
=
newNameRadical
;
151
// forcing newName to be unique
152
int
num
= 0;
153
while
(
names
.
contains
(
newName
)) {
154
newName
=
newNameRadical
+
std
::
to_string
(++
num
);
155
}
156
157
names
.
insert
(
newName
);
158
_bn_
->
changeVariableName
(
node
,
newName
);
159
}
else
{
160
ParseError
warn
(
false
,
"Name "
+
nn
+
" cannot be simplified."
,
_filename_
, 0);
161
_errors_
.
add
(
warn
);
162
}
163
}
164
}
165
166
delete
prm
;
167
168
return
errors
();
169
}
170
171
172
template
<
typename
GUM_SCALAR
>
173
void
O3prmBNReader
<
GUM_SCALAR
>::
_generateBN_
(
prm
::
PRMSystem
<
GUM_SCALAR
>&
system
) {
174
system
.
instantiate
();
175
BayesNetFactory
<
GUM_SCALAR
>
factory
(
_bn_
);
176
system
.
groundedBN
(
factory
);
177
_bn_
->
setProperty
(
"name"
,
_entityName_
);
178
}
179
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643