Menu

[r45]: / trunk / libtop_engine / kb_parser.cpp  Maximize  Restore  History

Download this file

1431 lines (1163 with data), 55.4 kB

   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
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
#include "kb_parser.h"
#include "kb_term_builder.h"
#include "owl_import.h"
#include "psearch_db.h"
namespace parser {
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_schema_statement
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_schema_statement(std::string const& line)
{
string_vector_t v;
tokenize(line, v, "(), =\t");
if(v.size() != 3) throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P1, invalid schema statement");
if(v[1] == "resource") create_resource(v[0], v[2]);
else if(v[1] == "text") create_text (v[0], v[2]);
else if(v[1] == "bool") create_bool (v[0], v[2]);
else if(v[1] == "int") create_int (v[0], v[2]);
else if(v[1] == "uint") create_uint (v[0], v[2]);
else if(v[1] == "real") create_real (v[0], v[2]);
else if(v[1] == "duration") create_duration(v[0], v[2]);
else if(v[1] == "time") create_time (v[0], v[2]);
else if(v[1] == "date") create_date (v[0], v[2]);
else if(v[1] == "unset") create_unset (v[0]);
else {
std::string msg("ERROR-P3, unknown data type '"+v[1]+"' in schema statement");
std::cout << msg << std::endl;
std::cout << "\tKnown data type are 'resource', 'text', 'bool', 'int', 'uint', 'real', 'duration', 'time', 'date'\n";
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_assert_triple
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_assert_triple(std::string const& line)
{
string_vector_t v;
tokenize(line, v, "(), =\t");
rdf::index_type s=0, p=0, o=0;
if(v[0] == "assert_triple" or v[0] == "expected_result" or v[0] == "failure_result") {
if(v[1].find("bnode:") == 0) s = get_bnode(v[1]);
else s = create_resource(v[1], v[1]);
p = create_resource(v[2], v[2]);
if(v[3].find("bnode:") == 0) o = get_bnode(v[3]);
else o = create_resource(v[3], v[3]);
}
if(v[0] == "assert_triple") {
m_meta_graph_p->insert(s, p, o);
} else if(v[0] == "expected_result") {
add_expected_triple(rdf::index_triple(s, p, o));
} else if(v[0] == "failure_result") {
add_failure_triple(rdf::index_triple(s, p, o));
} else {
std::string msg("ERROR-P3, unknown directive '"+v[0]+"' in assert triples section");
std::cout << msg << std::endl;
std::cout << "\tKnown directives are 'assert_triple', 'expected_result', 'failure_result'\n";
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_import_owl
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_import_owl(std::string const& line)
{
string_vector_t v;
tokenize(line, v, "\"(), =\t");
if(v[0] == "owl_import") {
parser::xml::import_owl_model(v[1], m_meta_graph_p, false);
} else {
std::string msg("ERROR-P3, unknown directive '"+v[0]+"' in owl_import section");
std::cout << msg << std::endl;
std::cout << "\tKnown directives is 'owl_import'\n";
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_psearch_db
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_psearch_db(std::string const& line)
{
string_vector_t v;
char * pEnd;
tokenize(line, v, "{};");
psearch::psearch_db_ptr_type db_p = m_kbase_p->get_psearch_db_ptr();
for(string_vector_t::iterator itor=v.begin(); itor!=v.end(); ++itor) boost::trim(*itor);
if(v[0] == "rule") {
rdf::index_type index=NULL;
unsigned int weight=0;
psearch::category_set_type ex_categories;
psearch::node_set_type c1_nodes;
psearch::node_set_type c2_nodes;
psearch::node_set_type negated_nodes;
for(unsigned int item=1; item<v.size(); ++item) {
string_vector_t v2;
tokenize(v[item], v2, "(), \t");
if(v2[0] == "index") {
index = create_resource(v2[1], v2[1]);
} else if(v2[0] == "ex_category") {
for(unsigned int item2=1; item2<v2.size(); ++item2) {
rdf::index_type idx = create_resource(v2[item2], v2[item2]);
ex_categories.insert(db_p->add_category(idx, idx->get_name()));
}
} else if(v2[0] == "has_c1") {
for(unsigned int item2=1; item2<v2.size(); ++item2) {
rdf::index_type idx = create_resource(v2[item2], v2[item2]);
c1_nodes.insert(db_p->add_node(idx, idx->get_name()));
}
} else if(v2[0] == "has_c2") {
for(unsigned int item2=1; item2<v2.size(); ++item2) {
rdf::index_type idx = create_resource(v2[item2], v2[item2]);
c2_nodes.insert(db_p->add_node(idx, idx->get_name()));
}
} else if(v2[0] == "has_not") {
for(unsigned int item2=1; item2<v2.size(); ++item2) {
rdf::index_type idx = create_resource(v2[item2], v2[item2]);
negated_nodes.insert(db_p->add_node(idx, idx->get_name()));
}
} else if(v2[0] == "weight") {
weight = strtol(v2[1].c_str(), &pEnd, 0);
}
}
db_p->add_rule(index, index->get_name(), weight, ex_categories, c1_nodes, c2_nodes, negated_nodes);
} else if(v[0] == "node") {
rdf::index_type index=NULL;
bool is_skip_pattern_activation=false;
for(unsigned int item=1; item<v.size(); ++item) {
string_vector_t v2;
tokenize(v[item], v2, "(), \t");
if(v2[0] == "index") {
index = create_resource(v2[1], v2[1]);
} else if(v2[0] == "skip_pattern_activation") {
is_skip_pattern_activation = v2[1] == "true";
}
}
psearch::node_index_type node_index = db_p->add_node(index, index->get_name());
db_p->set_node_skip_activation(node_index, is_skip_pattern_activation);
} else {
std::string msg("ERROR-P3, unknown directive '"+v[0]+"' in process_psearch_db");
std::cout << msg << std::endl;
std::cout << "\tKnown directives are: 'pattern'\n";
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_psearch_session
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_psearch_session(std::string const& line)
{
if(not m_test_data_map_p) {
std::cout << "ERROR-P4, kb_builder not properly initialized for process_psearch_session section, ignore section";
return;
}
string_vector_t v;
tokenize(line, v, "{};");
std::string key = boost::trim_copy(v[0]);
if(key == "session" or key.find("expected_nsc") == 0 or key.find("expected_psc") == 0) {
string_vector_t & properties = (*m_test_data_map_p)[key];
for(unsigned int item=1; item<v.size(); ++item) {
properties.push_back(boost::trim_copy(v[item]));
}
} else {
std::string msg("ERROR-P3, unknown directive '"+key+"' in process_psearch_session");
std::cout << msg << std::endl;
std::cout << "\tKnown directives are: 'session', 'expected_nsc', 'expected_psc'\n";
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_graph_internal
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_graph_internal(rdf::rdf_graph_ptr_type & graph_p, std::string const& line)
{
string_vector_t v;
tokenize(line, v, "(),");
for(string_vector_t::iterator itor=v.begin(); itor!=v.end(); ++itor) boost::trim(*itor);
if(v[0] == "triple") {
if(v.size() != 4) {
std::string msg("ERROR-P3, triple of incorrect size in process_graph_internal");
std::cout << msg << std::endl;
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
rdf::index_type s=0, o=0;
if(v[1].find("bnode:") == 0) s = get_bnode(v[1]);
else s = create_resource(v[1], v[1]);
rdf::index_type p = create_resource(v[2], v[2]);
if(v[3].find("bnode:") == 0) o = get_bnode(v[3]);
else o = create_resource(v[3], v[3]);
graph_p->insert(s, p, o);
} else {
std::string msg("ERROR-P3, unknown directive '"+v[0]+"' in process_graph_internal (will ignore)");
std::cout << msg << std::endl;
std::cout << "\tKnown directives are: 'triple'\n";
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_meta_graph
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_meta_graph(std::string const& line)
{
rdf::rdf_graph_ptr_type meta_graph_p = m_kbase_p->get_meta_graph();
process_graph_internal(meta_graph_p, line);
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_asserted_graph
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_asserted_graph(std::string const& line)
{
process_graph_internal(m_asserted_graph_p, line);
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_knowledge_base_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_knowledge_base_section(std::string const& line)
{
string_vector_t v;
tokenize(line, v, "(), =\t");
if(v.size() != 2) throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P1, invalid schema statement");
if(v[0] == "default-explain") m_kbase_p->set_has_explain_info(v[1]=="true" or v[1]=="t" ? true:false);
else if(v[0] == "default-optimization") m_kbase_p->set_optimization_flag(v[1]=="true" or v[1]=="t" ? true:false);
else if(v[0] == "default-lookup-index") m_kbase_p->set_lookup_index_flag(v[1]=="true" or v[1]=="t" ? true:false);
else if(v[0] == "max-rule-visit") m_kbase_p->set_max_rule_visit(boost::lexical_cast<unsigned int>(v[1]));
else {
std::cout << "WARNING: unknown directive in knowledge base configuration section, skipping";
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::create_literal_as_index
//
// 'l' is a copy of the string from which to create the literal resource,
// the literal will be taken as text in double quotes, the remainder will be assigned to
// 'rest'.
// works only for date, time, duration and numbers
/////////////////////////////////////////////////////////////////////////////////////////
rdf::index_type
kb_builder::create_literal_as_index(std::string line, std::string & rest)
{
line = parse_param(line, rest);
unsigned int sz = line.size();
if(sz == 0) throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P16, invalid text for literal");
// check for date
if(sz>4 and sz<11 and line[4]=='-') return create_date(line, line);
// check for time
if(sz>15 and sz<20 and
line.find('-') != std::string::npos and
line.find(' ') != std::string::npos and
line.find(':') != std::string::npos) return create_time(line, line);
// check for duration
if(sz>2 and line.find(':') != std::string::npos) return create_duration(line, line);
// check for real
if(sz>1 and line.find('.') != std::string::npos) return create_real(line, line);
// check for unsigned int
if(sz>1 and (line[sz-1]=='u' or line[sz-1]=='U')) {
line[sz-1] = 'u';
return create_uint(line, line.substr(0, sz-1));
}
// well, gotta be int!
return create_int(line, line);
};
template<class F>
rule::knowledge_rule::xprsn_ptr_t
kb_builder::create_binary_expression_helper(rule::knowledge_rule_ptr_type & rule_p, F const& first, std::string & str)
{
// the second term is the operator - can't be an expression or variable
std::string oper = next_token(str, str, " ()");
if(oper[0]=='?' or oper[0]=='_' or oper[0]=='\"' or oper[0]=='(')
throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P15, invalid rule syntax, cannot have binary expression with a variable, literal, or another expression as operator.");
// now the second operant
boost::trim(str);
if(str[0] == '(') { // oh no, another term!
str = str.substr(1);
rule::knowledge_rule::xprsn_ptr_t second = create_expression(rule_p, str);
boost::trim(str);
if(str[0] == ')') str.erase(0, 1);
return create_binary_term(&*rule_p, first, oper, second);
} else if(str[0]=='?' or str[0]=='_') {
std::string second = next_token(str, str, " ()");
if(second.find_first_of(" \t()")!=std::string::npos) {
std::string msg("ERROR-P13, invalid rule syntax, variable '"+second+"' is not valid. Forgot '.'?");
std::cout << msg << std::endl;
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
boost::trim(str);
if(str[0] == ')') str.erase(0, 1);
return create_binary_term(&*rule_p, first, oper, second);
} else if(str[0] == '\"') {
rdf::index_type second = create_literal_as_index(str, str);
boost::trim(str);
if(str[0] == ')') str.erase(0, 1);
return create_binary_term(&*rule_p, first, oper, second);
}
// must be a named resource/literal
std::string second = next_token(str, str, " ()");
boost::trim(str);
if(str[0] == ')') str.erase(0, 1);
return create_binary_term(&*rule_p, first, oper, get_index(second));
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::create_expression
//
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_rule::xprsn_ptr_t
kb_builder::create_expression(rule::knowledge_rule_ptr_type & rule_p, std::string & str)
{
boost::trim(str);
///////////////////////////////////////////////////////////
// get the first term of the expression
///////////////////////////////////////////////////////////
if(str[0] == '(') { // oh no, another term!
str = str.substr(1);
rule::knowledge_rule::xprsn_ptr_t first = create_expression(rule_p, str);
boost::trim(str);
if(str[0] == ')') {
str.erase(0, 1);
return first;
}
return create_binary_expression_helper(rule_p, first, str);
} else if(str[0]=='?' or str[0]=='_') {
std::string first = next_token(str, str, " ()");
if(first.find_first_of(" \t()")!=std::string::npos) {
std::string msg("ERROR-P13, invalid rule syntax, variable '"+first+"' is not valid. Forgot '.'?");
std::cout << msg << std::endl;
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
return create_binary_expression_helper(rule_p, first, str);
} else if(str[0] == '\"') {
rdf::index_type l = create_literal_as_index(str, str);
return create_binary_expression_helper(rule_p, l, str);
}
// gotta be named literal if binary term or operator if unary term
std::string first = next_token(str, str, " ()");
// check if is resource
if(is_resource(first)) {
// we have a binary term with a resource as first operand
return create_binary_expression_helper(rule_p, get_index(first), str);
} else {
// we have a unary term - get the object
boost::trim(str);
///////////////////////////////////////////////////////////
// It's a unary term
// 'first' is operator,
// finding operand of unary term
///////////////////////////////////////////////////////////
if(str[0] == '(') { // oh no, another term!
str = str.substr(1);
rule::knowledge_rule::xprsn_ptr_t operant = create_expression(rule_p, str);
boost::trim(str);
if(str[0] == ')') str.erase(0, 1);
return create_unary_term(&*rule_p, first, operant);
} else if(str[0]=='?' or str[0]=='_') {
std::string operant = next_token(str, str, " ()");
if(operant.find_first_of(" \t()")!=std::string::npos) {
std::string msg("ERROR-P13, invalid rule syntax, variable '"+operant+"' is not valid. Forgot '.'?");
std::cout << msg << std::endl;
throw rdf::rdf_exception(rdf::parsing_error, msg);
}
boost::trim(str);
if(str[0] == ')') str.erase(0, 1);
return create_unary_term(&*rule_p, first, operant);
} else if(str[0] == '\"') {
rdf::index_type operant = create_literal_as_index(str, str);
boost::trim(str);
if(str[0] == ')') str.erase(0, 1);
return create_unary_term(&*rule_p, first, operant);
}
// gotta be named resource/literal
std::string operant = next_token(str, str, " ()");
boost::trim(str);
if(str[0] == ')') str.erase(0, 1);
return create_unary_term(&*rule_p, first, get_index(operant));
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_knowledge_rule
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_knowledge_rule(std::string const& line)
{
string_vector_t v;
// std::cout << "kb_builder::process_knowledge_rule('" << line << "'\n";
// get the rule attributes
std::string params, body;
split_at(line, params, body, ":");
tokenize(params, v, "[] =,");
unsigned int sz = v.size();
if(sz == 0) throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P10, invalid rule syntax");
if(sz > 1 and sz%2 == 1) throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P10, invalid rule syntax");
std::string k, name, val;
std::map<std::string, std::string> p;
if(sz == 1) {
name = v[0];
p.insert(std::make_pair("n", name));
} else {
for(string_vector_t::iterator itor=v.begin(); itor!=v.end(); ++itor) {
k = *itor;
if(k == "name") k = "n";
p.insert(std::make_pair(k, *(++itor)));
}
}
rule::rule_type type = rule::query_rule;
if(body.find("->") != std::string::npos) type = rule::forward_chaining_rule;
else if(body.find("<-") != std::string::npos) type = rule::backward_chaining_rule;
name = p.find("n")->second;
rule::knowledge_rule_ptr_type rule_p = m_kbase_p->create_knowledge_rule(type, name);
rule_p->set_rule_text(line);
for(std::map<std::string, std::string>::iterator itor=p.begin(); itor!=p.end(); ++itor) {
k = itor->first;
val = itor->second;
if(k=="salience" or k=="s") rule_p->set_rule_salience(boost::lexical_cast<unsigned int>(val));
if(k=="optimize" or k=="o") rule_p->set_optimization_flag(val=="true" or val=="t" ? true:false);
if(k=="explain_info" or k=="x") rule_p->keep_explain_info(val=="true" or val=="t" ? true:false);
}
// ready for the rule body
switch(type) {
case rule::query_rule: process_for_query (rule_p, body); break;
case rule::forward_chaining_rule: process_for_forward (rule_p, body); break;
case rule::backward_chaining_rule: process_for_backward(rule_p, body); break;
case rule::head_rule_rt: break;
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_for_forward
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_for_forward(rule::knowledge_rule_ptr_type & rule_p, std::string const& line)
{
// get the body terms of the rule and the consequent terms
std::string body, consequent;
split_at(line, body, consequent, "->");
process_body_terms(rule_p, body);
process_consequent_terms(rule_p, consequent);
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_for_query
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_for_query(rule::knowledge_rule_ptr_type & rule_p, std::string line)
{
// parse the select * from section
std::string token = next_token(line, line, " ");
if(!boost::algorithm::iequals(token, "select"))
throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P25, invalid rule syntax - Expecting SELECT keyword.");
token = next_token(line, line, " ");
if(token != "*") {
int count = 0;
while(!boost::algorithm::iequals(token, "from") and ++count<100) {
if(token[0] != '?' and token[0] != '_')
throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P25, invalid rule syntax - Variable must start with \"?\" or \"_\" - error while within SELECT clause.");
rule_p->add_select_variable(token);
token = next_token(line, line, " ");
}
if(count == 100)
throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P25, invalid rule syntax - Error - too many variables (limit 100 exceeded) or FROM keyword missing in SELECT clause.");
} else {
// consume the from keyword
token = next_token(line, line, " ");
if(!boost::algorithm::iequals(token, "from"))
throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P25, invalid rule syntax - Expecting FROM keyword.");
}
boost::trim(line);
process_body_terms(rule_p, line);
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_for_backward
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_for_backward(rule::knowledge_rule_ptr_type & rule_p, std::string const& line)
{
// get the head and body terms
std::string head, body;
split_at(line, head, body, "<-");
rule::kterm_ptr_type head_term_p =
process_term(rule_p, head, head_functor());
process_body_terms(rule_p, body);
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_body_terms
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_body_terms(rule::knowledge_rule_ptr_type & rule_p, std::string const& line)
{
// process each term
std::string term;
string_vector_t v;
tokenize(line, v, ".");
rule::kterm_ptr_type rule_term_p;
rule::knowledge_rule::xprsn_ptr_t filter_p;
for(string_vector_t::iterator itor=v.begin(); itor!=v.end(); ++itor) {
term = *itor;
boost::trim(term);
if(term[0] == '(') {
rule_term_p = process_term(rule_p, term, body_functor());
} else if(term[0] == 'n' or term[0] == 'N') {
rule_term_p = process_term(rule_p, term, neg_body_functor());
} else if(term[0] == '[') {
filter_p = process_filter(rule_p, term);
if(rule_term_p) rule_p->set_filter(rule_term_p, filter_p);
else throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P11, invalid rule syntax - cannot attach filter to rule term");
rule_term_p.reset(); // it's consumed; cannot attach another filter to it
} else {
throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P12, invalid rule syntax - body term must start with either '(' or '['");
}
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// kb_builder::process_consequent_terms
//
/////////////////////////////////////////////////////////////////////////////////////////
void
kb_builder::process_consequent_terms(rule::knowledge_rule_ptr_type & rule_p, std::string const& line)
{
// process each term
std::string term;
string_vector_t v;
tokenize(line, v, ".");
rule::kterm_ptr_type rule_term_p;
for(string_vector_t::iterator itor=v.begin(); itor!=v.end(); ++itor) {
term = *itor;
boost::trim(term);
if(term[0] == '(') {
rule_term_p = process_term(rule_p, term, consequent_functor());
} else {
throw rdf::rdf_exception(rdf::parsing_error, "ERROR-P12, invalid rule syntax - consequent term must start with either '(' or '['");
}
}
};
//---------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base
//
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_base_ptr_type
load_knowledge_base(std::string const& fname, bool verbose)
{
// parameter can be set here such as pool_size, triple_size, sessions_map_size
// leaving to default for now.
rdf::rdf_graph_ptr_type meta_graph_p = rdf::create_rdf_graph();
rule::knowledge_base_ptr_type knowledge_base_p = rule::knowledge_base_ptr_type(new rule::knowledge_base(meta_graph_p));
kb_builder builder(knowledge_base_p, verbose);
load_knowledge_base_internal(fname, builder, verbose);
return knowledge_base_p;
};
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base
//
// use meta_graph_p as the meta data graph for the knowledge base
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_base_ptr_type
load_knowledge_base(std::string const& fname, rdf::rdf_graph_ptr_type & meta_graph_p, bool verbose)
{
rule::knowledge_base_ptr_type knowledge_base_p = rule::knowledge_base_ptr_type(new rule::knowledge_base(meta_graph_p));
kb_builder builder(knowledge_base_p, verbose);
load_knowledge_base_internal(fname, builder, verbose);
return knowledge_base_p;
};
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base
//
// Method for loading knowledge_base with additional asserted_graph to load a
// completed knowledge session from file
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_base_ptr_type
load_knowledge_base(std::string const& fname, rdf::rdf_graph_ptr_type & meta_graph_p, rdf::rdf_graph_ptr_type & asserted_graph_p, bool verbose)
{
rule::knowledge_base_ptr_type knowledge_base_p = rule::knowledge_base_ptr_type(new rule::knowledge_base(meta_graph_p));
kb_builder builder(knowledge_base_p, asserted_graph_p, verbose);
load_knowledge_base_internal(fname, builder, verbose);
return knowledge_base_p;
};
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base
//
// Method for loading knowledge_base with provisions for test cases.
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_base_ptr_type
load_knowledge_base(std::string const& fname, triple_array_type & expected_triples, triple_array_type & failure_triples, bool verbose)
{
rdf::rdf_graph_ptr_type meta_graph_p = rdf::create_rdf_graph();
rule::knowledge_base_ptr_type knowledge_base_p = rule::knowledge_base_ptr_type(new rule::knowledge_base(meta_graph_p));
kb_builder builder(knowledge_base_p, &expected_triples, &failure_triples, verbose);
load_knowledge_base_internal(fname, builder, verbose);
return knowledge_base_p;
};
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base
//
// Method for loading knowledge_base with provisions for test cases targeted to psearch.
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_base_ptr_type
load_knowledge_base(std::string const& fname, test_data_map_type & test_data_map, bool verbose)
{
rdf::rdf_graph_ptr_type meta_graph_p = rdf::create_rdf_graph();
rule::knowledge_base_ptr_type knowledge_base_p = rule::knowledge_base_ptr_type(new rule::knowledge_base(meta_graph_p));
kb_builder builder(knowledge_base_p, &test_data_map, verbose);
load_knowledge_base_internal(fname, builder, verbose);
return knowledge_base_p;
};
//---------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base_from_buffer
//
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_base_ptr_type
load_knowledge_base_from_buffer(std::string const& mem_buffer, bool verbose)
{
// parameter can be set here such as pool_size, triple_size, sessions_map_size
// leaving to default for now.
rdf::rdf_graph_ptr_type meta_graph_p = rdf::create_rdf_graph();
rule::knowledge_base_ptr_type knowledge_base_p = rule::knowledge_base_ptr_type(new rule::knowledge_base(meta_graph_p));
kb_builder builder(knowledge_base_p, verbose);
load_knowledge_base_from_buffer_internal(mem_buffer, builder, verbose);
return knowledge_base_p;
};
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base_from_buffer
//
// use meta_graph_p as the meta data graph for the knowledge base
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_base_ptr_type
load_knowledge_base_from_buffer(std::string const& mem_buffer, rdf::rdf_graph_ptr_type & meta_graph_p, bool verbose)
{
rule::knowledge_base_ptr_type knowledge_base_p = rule::knowledge_base_ptr_type(new rule::knowledge_base(meta_graph_p));
kb_builder builder(knowledge_base_p, verbose);
load_knowledge_base_from_buffer_internal(mem_buffer, builder, verbose);
return knowledge_base_p;
};
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base_from_buffer
//
// Method for loading knowledge_base with additional asserted_graph to load a
// completed knowledge session from file
/////////////////////////////////////////////////////////////////////////////////////////
rule::knowledge_base_ptr_type
load_knowledge_base_from_buffer(std::string const& mem_buffer, rdf::rdf_graph_ptr_type & meta_graph_p, rdf::rdf_graph_ptr_type & asserted_graph_p, bool verbose)
{
rule::knowledge_base_ptr_type knowledge_base_p = rule::knowledge_base_ptr_type(new rule::knowledge_base(meta_graph_p));
kb_builder builder(knowledge_base_p, asserted_graph_p, verbose);
load_knowledge_base_from_buffer_internal(mem_buffer, builder, verbose);
return knowledge_base_p;
};
//---------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base_internal
//
// main function to load knowledge base
/////////////////////////////////////////////////////////////////////////////////////////
void
load_knowledge_base_internal(std::string const& fname, kb_builder & builder, bool verbose)
{
if(verbose) std::cout << "Reading knowledge base from file " << fname << std::endl;
if(fname.size() == 0) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, invalid file name");
process_file(fname, builder);
post_load_knowledge_base_internal(fname, builder, verbose);
};
/////////////////////////////////////////////////////////////////////////////////////////
// load_knowledge_base_from_buffer_internal
//
// main function to load knowledge base
/////////////////////////////////////////////////////////////////////////////////////////
void
load_knowledge_base_from_buffer_internal(std::string const& mem_buffer, kb_builder & builder, bool verbose)
{
if(verbose) std::cout << "Reading knowledge base from buffer " << mem_buffer << std::endl;
if(mem_buffer.size() == 0) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, invalid mem_buffer");
std::istringstream in(mem_buffer, std::istringstream::in);
if(!in) {
std::cout << "*** Cannot open buffer '" << mem_buffer << "' for reading!\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, invalid mem_buffer");
}
process_stream(in, "", builder);
post_load_knowledge_base_internal("", builder, verbose);
};
/////////////////////////////////////////////////////////////////////////////////////////
// post_load_knowledge_base_internal
//
// main function to load knowledge base
/////////////////////////////////////////////////////////////////////////////////////////
void
post_load_knowledge_base_internal(std::string const& fname, kb_builder & builder, bool verbose)
{
// compile the rules
rule::knowledge_base_ptr_type kbase_p = builder.get_knowledge_base_ptr();
kbase_p->activate_knowledge_rules(verbose);
if(builder.get_parsed_meta_graph()) kbase_p->get_psearch_db_ptr()->load_from_meta_graph(verbose);
if(verbose) {
rule_priority_queue_type queue;
rule::knowledge_base::knowledge_rule_ptr_const_iterator itor = kbase_p->get_knowledge_rules_begin();
rule::knowledge_base::knowledge_rule_ptr_const_iterator end = kbase_p->get_knowledge_rules_end();
int rules_count = 0;
for(; itor!=end; ++itor) {
queue.push(*itor);
rules_count += 1;
}
std::cout << std::endl << "The knowledge base contains "<< rules_count << " rules, they are (sorted by salience):" << std::endl;
std::cout << std::endl;
while(!queue.empty()) {
rule::knowledge_rule_ptr_type rule_p = queue.top();
std::cout << *rule_p << std::endl;
std::cout << std::endl;
queue.pop();
}
std::cout << std::endl << "The psearch db contains the following patterns:" << std::endl;
kbase_p->get_psearch_db_ptr()->printAllPatterns();
std::cout << std::endl;
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// process_file
//
/////////////////////////////////////////////////////////////////////////////////////////
void
process_file(std::string const& fname, kb_builder & builder)
{
std::ifstream in;
in.open(fname.c_str());
if(!in) {
std::cout << "*** Cannot open file '" << fname << "' for reading!\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, invalid file name");
}
process_stream(in, fname, builder);
};
/////////////////////////////////////////////////////////////////////////////////////////
// process_stream
//
/////////////////////////////////////////////////////////////////////////////////////////
void
process_stream(std::istream & in, std::string const& fname, kb_builder & builder)
{
// main control
std::string line;
unsigned int lnum=0;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*include-file*") == 0) {
// std::cout << "*include-file* found at line number " << lnum << std::endl;
std::string name = parse_param(line);
process_file(name, builder);
} else if(line.find("*schema-begin*") == 0) {
// std::cout << "*schema-begin* found at line number " << lnum << std::endl;
parse_schema_section(in, builder, lnum);
} else if(line.find("*knowledge-base-begin*") == 0) {
// std::cout << "*knowledge-base-begin* found at line number " << lnum << std::endl;
parse_knowledge_base_section(in, builder, lnum);
} else if(line.find("*knowledge-rules-begin*") == 0) {
// std::cout << "*knowledge-rules-begin* found at line number " << lnum << std::endl;
parse_knowledge_rule_section(in, builder, lnum);
} else if(line.find("*assert-triples-begin*") == 0) {
// std::cout << "*assert-triples-begin* found at line number " << lnum << std::endl;
parse_assert_triples_section(in, builder, lnum);
} else if(line.find("*import-owl-begin*") == 0) {
parse_import_owl_section(in, builder, lnum);
} else if(line.find("*psearch-db-begin*") == 0) {
parse_psearch_db_section(in, builder, lnum);
} else if(line.find("*psearch-session-begin*") == 0) {
parse_psearch_session_section(in, builder, lnum);
} else if(line.find("*meta-graph-begin*") == 0) {
builder.set_parsed_meta_graph(true);
parse_meta_graph_section(in, builder, lnum);
} else if(line.find("*asserted-graph-begin*") == 0) {
parse_asserted_graph_section(in, builder, lnum);
}
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught with message: '" << e.what();
std::cout << "' near line " << lnum << " of file " << fname << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught ";
std::cout << "near line " << lnum << " of file " << fname << std::endl;
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_knowledge_rule_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_knowledge_rule_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
builder.process_knowledge_rule_section_begin();
std::string line;
try {
while (read_line(in, line, lnum, " ")) {
if(line.find("*knowledge-rules-end*") == 0) {
builder.process_knowledge_rule_section_end();
return;
}
builder.process_knowledge_rule(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in knowledge rule section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in knowledge rule section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in schema section!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_knowledge_base_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_knowledge_base_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
std::string line;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*knowledge-base-end*") == 0) return;
builder.process_knowledge_base_section(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in knowledge base section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in knowledge base section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in schema section!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_schema_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_schema_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
std::string line;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*schema-end*") == 0) return;
builder.process_schema_statement(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in schema section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in schema section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in schema section!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_assert_triples_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_assert_triples_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
std::string line;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*assert-triples-end*") == 0) return;
builder.process_assert_triple(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in assert triple section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in assert triple section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in assert triple section!");
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// parse_import_owl_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_import_owl_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
std::string line;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*import-owl-end*") == 0) return;
builder.process_import_owl(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in import-owl section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in import-owl section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in import-owl section!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_psearch_session_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_psearch_session_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
std::string line;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*psearch-session-end*") == 0) return;
builder.process_psearch_session(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in parse_psearch_session_section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in parse_psearch_session_section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in parse_psearch_session_section!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_meta_graph_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_meta_graph_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
std::string line;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*meta-graph-end*") == 0) return;
builder.process_meta_graph(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in parse_meta_graph_section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in parse_meta_graph_section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in parse_meta_graph_section!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_asserted_graph_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_asserted_graph_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
bool process_lines = true;
if(not builder.has_asserted_graph()) {
process_lines = false;
std::cout << "ERROR, Builder does not have an asserted graph - triples will be ignored" << std::endl;
}
std::string line;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*asserted-graph-end*") == 0) return;
if(process_lines) builder.process_asserted_graph(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in parse_asserted_graph_section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in parse_asserted_graph_section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in parse_asserted_graph_section!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_psearch_db_section
//
/////////////////////////////////////////////////////////////////////////////////////////
void
parse_psearch_db_section(std::istream & in, kb_builder & builder, unsigned int & lnum)
{
std::string line;
try {
while (read_line(in, line, lnum, "")) {
if(line.find("*psearch-db-end*") == 0) return;
builder.process_psearch_db(line);
}
} catch(rdf::rdf_exception const& e) {
std::cout << "ERROR, exception caught in parse_psearch_db_section. . ." << e << std::endl;
throw e;
} catch(...) {
std::cout << "ERROR, unknown exception caught in parse_psearch_db_section . . .\n";
throw rdf::rdf_exception(rdf::parsing_error, "ERROR, unknown exception caught in parse_psearch_db_section!");
}
};
/////////////////////////////////////////////////////////////////////////////////////////
// read_line
//
/////////////////////////////////////////////////////////////////////////////////////////
bool
read_line(std::istream & in, std::string & line, unsigned int & lnum, std::string const& multi_line_delemit)
{
std::string str;
while(getline(in, line)) {
++lnum;
boost::trim(line);
if(line.length()==0 or '#' == line[0]) continue;
if(*line.rbegin() == '\\') {
while(*line.rbegin() == '\\') {
if(!getline(in, str)) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, end of file reached while reading line");
if(str.length()==0 or '#' == str[0]) continue;
++lnum;
(*line.rbegin()) = ' ';
boost::trim(line);
boost::trim(str);
line.append(multi_line_delemit).append(str);
}
std::string::size_type pos = 0;
int count=0;
while((pos=line.find("\"\"", pos)) != std::string::npos and ++count<50) {
if(pos==0 or line[pos-1] != '\\') line.erase(pos, 2);
else ++pos;
}
if(count == 50) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, while looking for \"\" (infinite loop!)");
}
// std::cout << "line is '"<< line << "' at line number "<< lnum << std::endl;
return true;
}
return false;
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_param
//
// jpos will be set to the end \" of 'line', so that line[jpos]=='\"'
// consume_quotes indicates if first and last quote are consumed
/////////////////////////////////////////////////////////////////////////////////////////
std::string
parse_param(std::string line, std::string::size_type & jpos, bool consume_quotes)
{
// find the first "
std::string::size_type ipos = line.find('\"');
if(ipos == std::string::npos) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, missing \" to delimit argument");
// find the next un-escaped one
jpos = ipos+1;
int count=0;
while((jpos=line.find('\"', jpos)) != std::string::npos and ++count<50) {
if(line[jpos-1] != '\\') break;
++jpos;
}
if(jpos == std::string::npos or count == 50) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, missing \" to delimit argument");
if(!consume_quotes) return line.substr(ipos, jpos-ipos+1);
// consume the quotes
if(jpos == ipos+1) return "";
std::string str = line.substr(ipos+1, jpos-ipos-1);
// now changed the escaped quotes into quotes
//ie, change '\\\"' into '\"'
if(str.find("\\\"") != std::string::npos) {
std::string::size_type pos = 0;
while((pos=str.find("\\\""), pos) != std::string::npos) str.erase(pos, 1);
// std::cout << "quoted-param: "<< str << std::endl;
}
return str;
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_param
//
// 'rest' is assigned the text following the end '\"'
/////////////////////////////////////////////////////////////////////////////////////////
std::string
parse_param(std::string line, std::string & rest, bool consume_quotes)
{
std::string::size_type pos = 0;
std::string params = parse_param(line, pos, consume_quotes);
if(pos == line.size()-1) rest = "";
else rest = line.substr(pos+1);
return params;
};
/////////////////////////////////////////////////////////////////////////////////////////
// parse_param
//
// just extract the double-quoted params - don't care about the rest of the line
// - will consume the quotes
/////////////////////////////////////////////////////////////////////////////////////////
std::string
parse_param(std::string line)
{
std::string::size_type pos = 0;
std::string params = parse_param(line, pos, true);
return params;
};
/////////////////////////////////////////////////////////////////////////////////////////
// tokenize
//
// tokenize a string and skip delimiters if enclosed within double quotes (like a param)
// Note: when double quotes are used, must be first after delimiters.
/////////////////////////////////////////////////////////////////////////////////////////
void
tokenize(std::string const& str, string_vector_t & tokens, std::string const& delimit)
{
std::string::size_type ipos = 0;
int count=0;
while(ipos!=std::string::npos and (ipos=str.find_first_not_of(delimit, ipos)) != std::string::npos and ++count < 50) {
// std::cout << "first not delimit is str[ipos] is '" << str[ipos] << "' at " << ipos << std::endl;
if(str[ipos] == '\"') {
// got a quoted param
std::string::size_type jpos = 0;
tokens.push_back(parse_param(str.substr(ipos), jpos));
ipos += jpos+1;
if(ipos < str.size()) {
// std::cout << "- after parse param str[ipos] is '" << str[ipos] << "' at " << ipos << std::endl;
} else {
ipos = std::string::npos;
// std::cout << "- took last token as param!\n";
}
} else {
std::string::size_type jpos = str.find_first_of(delimit, ipos);
if(jpos == std::string::npos) {
tokens.push_back(str.substr(ipos));
ipos = std::string::npos;
// std::cout << "- took last token!\n";
} else {
tokens.push_back(str.substr(ipos, jpos - ipos));
ipos = jpos;
// std::cout << "- after taking token, str[ipos] is '" << str[ipos] << "' at " << ipos << std::endl;
}
}
}
if(count == 50) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, while tokenizing (infinite loop!)");
};
/////////////////////////////////////////////////////////////////////////////////////////
// split_at
//
/////////////////////////////////////////////////////////////////////////////////////////
void
split_at(std::string const& str, std::string & s1, std::string & s2, std::string const& d)
{
std::string::size_type ipos = str.find(d);
if(ipos==0 or ipos==std::string::npos) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, invalid syntax");
s1 = str.substr(0, ipos);
s2 = str.substr(ipos+d.size());
};
/////////////////////////////////////////////////////////////////////////////////////////
// next_token
//
// utility function - split next token from the rest
// the double-quotes are not consumed - token will extend untill the end quote
// and token returned with the quotes.
/////////////////////////////////////////////////////////////////////////////////////////
std::string
next_token(std::string str, std::string & rest, std::string const& delimit)
{
std::string::size_type ipos = str.find_first_not_of(delimit, 0);
if(ipos == std::string::npos) throw rdf::rdf_exception(rdf::parsing_error, "ERROR, invalid syntax - cannot get next token");
if(str[ipos] == '\"') {
// got a quoted param as the token
return parse_param(str, rest, false);
}
std::string::size_type jpos = str.find_first_of(delimit, ipos);
if(jpos == std::string::npos) {
rest = "";
return str.substr(ipos);
} else {
rest = str.substr(jpos);
return str.substr(ipos, jpos - ipos);
}
};
}; /* parser namespace */
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.