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
>
34
INLINE
std
::
string
35
O3prmBNReader
<
GUM_SCALAR
>::
getVariableName__
(
const
std
::
string
&
path
,
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
50
O3prmBNReader
<
GUM_SCALAR
>::
getInstanceName__
(
const
std
::
string
&
classname
) {
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
58
O3prmBNReader
<
GUM_SCALAR
>::
getEntityName__
(
const
std
::
string
&
filename
) {
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
>
66
O3prmBNReader
<
GUM_SCALAR
>::
O3prmBNReader
(
BayesNet
<
GUM_SCALAR
>*
bn
,
67
const
std
::
string
&
filename
,
68
const
std
::
string
&
entityName
,
69
const
std
::
string
&
classpath
) :
70
BNReader
<
GUM_SCALAR
>(
bn
,
filename
) {
71
GUM_CONSTRUCTOR
(
O3prmBNReader
);
72
bn__
=
bn
;
73
filename__
=
filename
;
74
entityName__
=
entityName
==
""
?
getEntityName__
(
filename
) :
entityName
;
75
classpath__
=
classpath
;
76
}
77
78
template
<
typename
GUM_SCALAR
>
79
O3prmBNReader
<
GUM_SCALAR
>::~
O3prmBNReader
() {
80
GUM_DESTRUCTOR
(
O3prmBNReader
);
81
}
82
83
/// parse.
84
/// @return the number of detected errors
85
/// @throws IOError if file not exists
86
template
<
typename
GUM_SCALAR
>
87
Size
O3prmBNReader
<
GUM_SCALAR
>::
proceed
() {
88
prm
::
o3prm
::
O3prmReader
<
GUM_SCALAR
>
reader
;
89
if
(
classpath__
!=
""
) {
reader
.
addClassPath
(
classpath__
); }
90
reader
.
readFile
(
filename__
);
91
gum
::
prm
::
PRM
<
GUM_SCALAR
>*
prm
=
reader
.
prm
();
92
errors__
=
reader
.
errorsContainer
();
93
94
95
if
(
errors
() == 0) {
96
std
::
string
instanceName
=
""
;
97
if
(
prm
->
isSystem
(
entityName__
)) {
98
generateBN__
(
prm
->
getSystem
(
entityName__
));
99
}
else
if
(
prm
->
isClass
(
entityName__
)) {
100
ParseError
warn
(
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__
);
108
instanceName
=
getInstanceName__
(
entityName__
);
109
auto
i
110
=
new
gum
::
prm
::
PRMInstance
<
GUM_SCALAR
>(
instanceName
,
111
prm
->
getClass
(
entityName__
));
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
);
125
instanceName
=
getInstanceName__
(
entityName
);
126
auto
i
127
=
new
gum
::
prm
::
PRMInstance
<
GUM_SCALAR
>(
instanceName
,
128
prm
->
getClass
(
entityName
));
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
();
150
bn__
->
variable
(
node
).
setDescription
(
nn
);
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
160
std
::
string
newNameRadical
161
=
getVariableName__
(
path
,
type
,
name
,
instanceName
);
162
163
std
::
string
newName
=
newNameRadical
;
164
// forcing newName to be unique
165
int
num
= 0;
166
while
(
names
.
contains
(
newName
)) {
167
newName
=
newNameRadical
+
std
::
to_string
(++
num
);
168
}
169
170
names
.
insert
(
newName
);
171
bn__
->
changeVariableName
(
node
,
newName
);
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
>
189
void
O3prmBNReader
<
GUM_SCALAR
>::
generateBN__
(
190
prm
::
PRMSystem
<
GUM_SCALAR
>&
system
) {
191
system
.
instantiate
();
192
BayesNetFactory
<
GUM_SCALAR
>
factory
(
bn__
);
193
system
.
groundedBN
(
factory
);
194
bn__
->
setProperty
(
"name"
,
entityName__
);
195
}
196
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669