/
huginParser.y
537 lines (478 loc) · 14.1 KB
/
huginParser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
%{
/*
* SQLassie - database firewall
* Copyright (C) 2011 Brandon Skari <brandon.skari@gmail.com>
*
* This file is part of SQLassie.
*
* SQLassie is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SQLassie is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SQLassie. If not, see <http://www.gnu.org/licenses/>.
*/
#include "clearStack.hpp"
#include "Logger.hpp"
#include "warnUnusedResult.h"
#include "dlib/dlib/bayes_utils.h"
#include "dlib/dlib/graph_utils.h"
#include "dlib/dlib/graph.h"
#include "dlib/dlib/directed_graph.h"
#include <stack>
#include <map>
#include <string>
#include <vector>
#include <boost/lexical_cast.hpp>
/**
* Parser for Hugin-formatted Bayesian network files.
* @author Brandon Skari
* @date May 8 2011
*/
const double PROBABILITY_SUM_TOLERANCE = 0.0000001;
typedef dlib::directed_graph<dlib::bayes_node>::kernel_1a_c bayes_net;
/* These declarations are needed so the compiler doesn't barf */
int hugin_lex(void* scanner);
void hugin_error(bayes_net*, bool, void*, const char* s);
/**
* Sets the probabilities for a given node. The names of the parents should be
* in the parents vector and the probability values in the probabilities
* vector.
* @return 0 on success.
*/
///@{
static int setProbabilities(
const std::string& node,
bayes_net* network
) WARN_UNUSED_RESULT;
static int setProbabilities(
const std::string& node,
bayes_net* network,
dlib::assignment* parentStates,
const int parentNumber
) WARN_UNUSED_RESULT;
///@}
extern char* hugin_text;
extern std::stack<std::string> hugin_identifiers;
extern std::map<std::string, int> hugin_nodesNumbers;
extern std::stack<std::string> hugin_numbers;
static int nodeCount;
static std::vector<std::string> parents;
static std::map<std::string, int> numberOfStates;
static std::stack<double> probabilities;
static bool networkSizeHasBeenSet;
static int statesCount;
%}
%name-prefix="hugin_"
%parse-param { bayes_net* network }
%parse-param { bool firstTime }
%parse-param { void* scanner }
%lex-param { void* scanner }
/* Tokens */
%token STRING
%token IDENTIFIER
%token NUMBER
%token NET
%token POTENTIAL
%token DATA
%token STATES
%token LABEL
%token NODE
%token EQUAL
%token LEFT_BRACE RIGHT_BRACE
%token LEFT_PARENTHESE RIGHT_PARENTHESE
%token SEMICOLON
%token PIPE
%token ERROR
%%
begin:
NET LEFT_BRACE netParametersList RIGHT_BRACE nodesList
{
}
;
netParametersList:
netParameter
{
// I need to reset these at the beginning of any parsing.
// Because the netParameter action is always guaranteed to run
// before the node parsing actions, I can put it here and it should
// work okay - I just don't know where else I can put this code and
// have it work without requiring external action by the user.
nodeCount = 0;
parents.clear();
numberOfStates.clear();
clearStack(&probabilities);
networkSizeHasBeenSet = false;
statesCount = 0;
// These are external, but should be cleared too
clearStack(&hugin_identifiers);
hugin_nodesNumbers.clear();
clearStack(&hugin_numbers);
}
| netParameter netParametersList
{
}
;
netParameter:
IDENTIFIER EQUAL STRING SEMICOLON
{
hugin_identifiers.pop();
}
| IDENTIFIER EQUAL LEFT_PARENTHESE numbersList RIGHT_PARENTHESE SEMICOLON
{
hugin_identifiers.pop();
}
;
nodesList:
node nodesList
{
}
| potentialList
{
}
;
node:
NODE IDENTIFIER LEFT_BRACE nodeParametersList RIGHT_BRACE
{
const std::string nodeName(hugin_identifiers.top());
hugin_identifiers.pop();
// Save the number of states this node has
std::map<std::string, int>::const_iterator statesIter(
numberOfStates.find(nodeName)
);
if (numberOfStates.end() == statesIter)
{
numberOfStates.insert(std::pair<std::string, int>(nodeName, statesCount));
}
// This can't be set until the network's structure has been created
if (!firstTime)
{
dlib::bayes_node_utils::set_node_num_values(*network, nodeCount, statesCount);
}
statesCount = 0;
// Save the name of the node
std::map<std::string, int>::const_iterator nodeIter(
hugin_nodesNumbers.find(nodeName)
);
if (hugin_nodesNumbers.end() != nodeIter)
{
YYERROR;
}
hugin_nodesNumbers.insert(std::pair<std::string, int>(nodeName, nodeCount));
++nodeCount;
}
;
nodeParametersList:
nodeParameter
{
}
| nodeParameter nodeParametersList
{
}
;
nodeParameter:
STATES EQUAL statesList SEMICOLON
{
}
| LABEL EQUAL STRING SEMICOLON
{
}
| IDENTIFIER EQUAL LEFT_PARENTHESE numbersList RIGHT_PARENTHESE SEMICOLON
{
hugin_identifiers.pop();
}
| IDENTIFIER EQUAL STRING SEMICOLON
{
hugin_identifiers.pop();
}
;
statesList:
LEFT_PARENTHESE statesStringsList RIGHT_PARENTHESE
{
}
;
statesStringsList:
STRING
{
++statesCount;
}
| STRING statesStringsList
{
++statesCount;
}
;
numbersList:
NUMBER
{
hugin_numbers.pop();
}
| NUMBER numbersList
{
hugin_numbers.pop();
}
;
potentialList:
potential
{
}
| potential potentialList
{
}
;
potential:
POTENTIAL LEFT_PARENTHESE IDENTIFIER PIPE parentList RIGHT_PARENTHESE
LEFT_BRACE DATA EQUAL dataList SEMICOLON RIGHT_BRACE
{
const std::string nodeName(hugin_identifiers.top());
hugin_identifiers.pop();
// The first call to the parser will just build the structure of
// the network
if (firstTime)
{
if (!networkSizeHasBeenSet)
{
networkSizeHasBeenSet = true;
network->set_number_of_nodes(nodeCount);
}
// Add edges between all the parents to this node
std::map<std::string, int>::const_iterator nodeIter(
hugin_nodesNumbers.find(nodeName)
);
if (hugin_nodesNumbers.end() == nodeIter)
{
YYERROR;
}
const int nodeNumber = nodeIter->second;
const std::vector<std::string>::const_iterator end(parents.end());
for (
std::vector<std::string>::const_iterator i(parents.begin());
i != end;
++i
)
{
const std::map<std::string, int>::const_iterator parentIter(
hugin_nodesNumbers.find(*i)
);
if (hugin_nodesNumbers.end() == parentIter)
{
YYERROR;
}
const int parentNumber = parentIter->second;
network->add_edge(parentNumber, nodeNumber);
}
}
// The second call to the parser will set the probabilities
else
{
const int status = setProbabilities(nodeName, network);
if (0 != status)
{
Logger::log(Logger::FATAL) <<
"Error parsing Hugin Bayes net file: " <<
"unable to set probabilities for node " <<
nodeName;
}
}
// Done adding the parents for this node, so clear the parents
parents.clear();
}
;
parentList:
/* empty - root node (and possibly others) have no parents */
{
}
| IDENTIFIER parentList
{
parents.push_back(hugin_identifiers.top());
hugin_identifiers.pop();
}
;
dataList:
LEFT_PARENTHESE dataList dataList RIGHT_PARENTHESE
{
}
| LEFT_PARENTHESE probabilityList RIGHT_PARENTHESE
{
}
;
probabilityList:
NUMBER
{
try
{
probabilities.push(boost::lexical_cast<double>(hugin_numbers.top()));
hugin_numbers.pop();
}
catch (...)
{
YYERROR;
}
}
| NUMBER probabilityList
{
try
{
probabilities.push(boost::lexical_cast<double>(hugin_numbers.top()));
hugin_numbers.pop();
}
catch (...)
{
YYERROR;
}
}
;
%%
void hugin_error(bayes_net*, bool, void*, const char* s)
{
Logger::log(Logger::ERROR) << "Hugin parser error: " << s;
}
int setProbabilities(const std::string& node, bayes_net* network)
{
// Add the parent states for the node
dlib::assignment parentStates;
std::vector<std::string>::const_iterator end(parents.end());
for (
std::vector<std::string>::const_iterator i(parents.begin());
i != end;
++i
)
{
const std::map<std::string, int>::const_iterator parentIter(
hugin_nodesNumbers.find(*i)
);
if (hugin_nodesNumbers.end() == parentIter)
{
return 1;
}
const int parentNumber = parentIter->second;
parentStates.add(parentNumber, 0);
}
const int status = setProbabilities(
node,
network,
&parentStates,
parentStates.size() - 1
);
if (0 == status && probabilities.empty())
{
return 0;
}
return 1;
}
int setProbabilities(
const std::string& node,
bayes_net* network,
dlib::assignment* parentStates,
const int parentNumber
)
{
if (parentNumber < 0)
{
// All parent states are set, so set the probability
const std::map<std::string, int>::const_iterator nodeNumberIter(
hugin_nodesNumbers.find(node)
);
if (hugin_nodesNumbers.end() == nodeNumberIter)
{
Logger::log(Logger::FATAL) <<
"Error parsing Hugin Bayes net file: " <<
"invalid node number: " <<
node;
return 1;
}
const int nodeNumber = nodeNumberIter->second;
const std::map<std::string, int>::const_iterator nodeStatesIter(
numberOfStates.find(node)
);
if (numberOfStates.end() == nodeStatesIter)
{
Logger::log(Logger::FATAL) <<
"Error parsing Hugin Bayes net file: " <<
"invalid node number: " <<
node;
return 1;
}
const int numberOfNodeStates = nodeStatesIter->second;
double totalProbability = 0.0;
for (int state = 0; state < numberOfNodeStates; ++state)
{
assert(
!probabilities.empty() &&
"Needed probabilities and there were none in the stack"
);
if (probabilities.empty())
{
Logger::log(Logger::FATAL) <<
"Error parsing Hugin Bayes net file: " <<
"list of probabilities was too short";
return 1;
}
const double probability = probabilities.top();
probabilities.pop();
totalProbability += probability;
// Set the probability of the node having a given state, given the
// states of its parents
dlib::bayes_node_utils::set_node_probability(
*network,
nodeNumber,
state,
*parentStates,
probability
);
}
if (abs(totalProbability - 1.0) > PROBABILITY_SUM_TOLERANCE)
{
Logger::log(Logger::FATAL) <<
"Error parsing Hugin Bayes net file: " <<
"probabilities list for a given state didn't sum to 1, but to " <<
abs(totalProbability - 1.0);
}
return 0;
}
const std::string parentNodeName(parents.at(parentNumber));
const std::map<std::string, int>::const_iterator parentStatesIter(
numberOfStates.find(parentNodeName)
);
if (numberOfStates.end() == parentStatesIter)
{
Logger::log(Logger::FATAL) <<
"Error parsing Hugin Bayes net file: " <<
"parent node not found";
return 1;
}
const int numberParentStates = parentStatesIter->second;
const std::map<std::string, int>::const_iterator parentNodeIter(
hugin_nodesNumbers.find(parentNodeName)
);
if (hugin_nodesNumbers.end() == parentNodeIter)
{
Logger::log(Logger::FATAL) <<
"Error parsing Hugin Bayes net file: " <<
"parent node not found";
return 1;
}
const int parentNodeNumber = parentNodeIter->second;
// This has to be done in reverse because the probabilities are stored in a
// stack and so they need to be handled in reverse order
for (int i = numberParentStates - 1; i >= 0; --i)
{
(*parentStates)[parentNodeNumber] = i;
const int status = setProbabilities(
node,
network,
parentStates,
parentNumber - 1
);
if (0 != status)
{
return status;
}
}
return 0;
}