-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp.cpp
3206 lines (3117 loc) · 191 KB
/
sp.cpp
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
#include "logger.h"
#include "sp.h"
#include "hi_constants.h"
#include <iterator>
#include <algorithm>
#include <functional>
#include "lexer.h"
#include <numeric>
/*PUBLIC*/
interpreter::interpreter(const unsigned char toa,const std::string& timestamp,const preprocessor *pp){
nr_of_nodes_=0;
toa_=toa;
timestamp_=timestamp;
pp_=pp;
db *sqlite=NULL;
sqlite=db_factory::get_instance();
query_result *query_result=NULL;
query_result=sqlite->exec_sql("SELECT * FROM SETTINGS WHERE KEY = 'ref_symbol';");
if(query_result!=NULL){
ref_symbol=*query_result->field_value_at_row_position(0,"value");
delete query_result;
}
query_result=sqlite->exec_sql("SELECT * FROM SETTINGS WHERE KEY = 'ref_stem';");
if(query_result!=NULL){
ref_stem=*query_result->field_value_at_row_position(0,"value");
delete query_result;
}
query_result=sqlite->exec_sql("SELECT * FROM SETTINGS WHERE KEY = 'ref_tag';");
if(query_result!=NULL){
ref_tag=*query_result->field_value_at_row_position(0,"value");
delete query_result;
}
}
interpreter::~interpreter(){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"destructing interpreter");
destroy_node_infos();
}
int interpreter::set_node_info(const std::string& symbol, const lexicon& word){
node_info nodeinfo;
db *sqlite=NULL;
unsigned int rule_step_failed=0;
nodeinfo.node_id=++nr_of_nodes_;
nodeinfo.symbol=symbol;
if(word.lexeme.empty()==false){
if(word.gcat.empty()==true){
throw std::runtime_error("No way that I set an empty gcat for a node.");
}
nodeinfo.expression=word;
}
nodeinfo.left_child=0;
nodeinfo.right_child=0;
if(nodeinfo.expression.morphalytics!=NULL) nodeinfo.expression.morphalytics->set_node_id(nodeinfo.node_id);
node_infos.push_back(nodeinfo);
return nodeinfo.node_id;
}
int interpreter::set_node_info(const std::string& symbol, const node_info& node){
lexicon word;
node_info nodeinfo;
db *sqlite=NULL;
unsigned int rule_step_failed=0;
word.gcat=symbol;
nodeinfo.node_id=++nr_of_nodes_;
if(word.gcat.empty()==true){
throw std::runtime_error("No way that I set an empty gcat for a node.");
}
nodeinfo.symbol=symbol;
if(word.lexeme.empty()==false) nodeinfo.expression=word;
nodeinfo.left_child=0;
nodeinfo.right_child=node.node_id;
if(nodeinfo.expression.morphalytics!=NULL) nodeinfo.expression.morphalytics->set_node_id(nodeinfo.node_id);
node_infos.push_back(nodeinfo);
if(nodeinfo.right_child!=0){//This means that no checks can be set up for nonterminal->terminal symbol rules
rule_step_failed=check_prerequisite_symbols(nodeinfo,get_node_info(nodeinfo.right_child));
if(rule_step_failed!=0){
throw missing_prerequisite_symbol(nodeinfo.symbol,get_node_info(nodeinfo.right_child).symbol);
}
}
return nodeinfo.node_id;
}
int interpreter::set_node_info_left(const std::string& symbol, const node_info& node){
lexicon word;
node_info nodeinfo;
db *sqlite=NULL;
unsigned int rule_step_failed=0;
word.gcat=symbol;
nodeinfo.node_id=++nr_of_nodes_;
if(word.gcat.empty()==true){
throw std::runtime_error("No way that I set an empty gcat for a node.");
}
nodeinfo.symbol=symbol;
if(word.lexeme.empty()==false) nodeinfo.expression=word;
nodeinfo.left_child=node.node_id;
nodeinfo.right_child=0;
if(nodeinfo.expression.morphalytics!=NULL) nodeinfo.expression.morphalytics->set_node_id(nodeinfo.node_id);
node_infos.push_back(nodeinfo);
if(nodeinfo.right_child!=0){//This means that no checks can be set up for nonterminal->terminal symbol rules
rule_step_failed=check_prerequisite_symbols(nodeinfo,get_node_info(nodeinfo.right_child));
if(rule_step_failed!=0){
throw missing_prerequisite_symbol(nodeinfo.symbol,get_node_info(nodeinfo.right_child).symbol);
}
}
return nodeinfo.node_id;
}
unsigned int interpreter::check_prerequisite_symbols(const node_info& parent_node, const node_info& child_node){
bool valid_combination=false;
db *sqlite=NULL;
query_result *rule_to_rule_map=NULL;
std::vector<unsigned int> main_nodes_found_by_symbol,main_subtree_nodes_found_by_symbol;
const std::pair<const unsigned int,field> *rule_entry=NULL,*rule_entry_failure_copy=NULL;
unsigned int current_step=0,successor=0,failover=0,rule_step_failed=0;
std::map<unsigned int,bool> empty_ref_node_id_parents;
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"checking prerequisite symbols for parent symbol:"+parent_node.symbol+", child symbol:"+child_node.symbol);
sqlite=db_factory::get_instance();
rule_to_rule_map=sqlite->exec_sql("SELECT * FROM RULE_TO_RULE_MAP WHERE PARENT_SYMBOL = '"+parent_node.symbol+"' AND HEAD_ROOT_SYMBOL = '"+child_node.symbol+"' AND (NON_HEAD_ROOT_SYMBOL = '' OR NON_HEAD_ROOT_SYMBOL IS NULL) ORDER BY STEP;");
if(rule_to_rule_map!=NULL){
rule_entry=rule_to_rule_map->first_value_for_field_name_found("step","1");
while(rule_entry!=NULL){
rule_entry_failure_copy=rule_entry;
current_step=std::atoi(rule_to_rule_map->field_value_at_row_position(rule_entry->first,"step")->c_str());
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"step:"+std::to_string(current_step));
failover=std::atoi(rule_to_rule_map->field_value_at_row_position(rule_entry->first,"failover")->c_str());
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"failover:"+std::to_string(failover));
successor=std::atoi(rule_to_rule_map->field_value_at_row_position(rule_entry->first,"successor")->c_str());
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"successor:"+std::to_string(successor));
main_nodes_found_by_symbol.clear();
main_subtree_nodes_found_by_symbol.clear();
if(*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"parent_symbol")==parent_node.symbol
&&*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"head_root_symbol")==child_node.symbol){
if(rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_node_symbol")->empty()==false){
if(rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_lookup_subtree_symbol")->empty()==true)
get_nodes_by_symbol(child_node,*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_node_symbol"),*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_node_lexeme"),empty_ref_node_id_parents,main_nodes_found_by_symbol);
else{
get_nodes_by_symbol(child_node,*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_lookup_subtree_symbol"),std::string(),empty_ref_node_id_parents,main_subtree_nodes_found_by_symbol);
for(auto&& main_subtree_node:main_subtree_nodes_found_by_symbol){
get_nodes_by_symbol(get_node_info(main_subtree_node),*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_node_symbol"),*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_node_lexeme"),empty_ref_node_id_parents,main_nodes_found_by_symbol);
}
}
}
else{
throw std::runtime_error("Main node symbol is empty for parent symbol "+parent_node.symbol+", head root symbol "+child_node.symbol+" and empty or NULL non-head root symbol in RULE_TO_RULE_MAP db table.");
}
if(rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_node_symbol")->empty()==false
&&main_nodes_found_by_symbol.empty()==false){
if(successor==0||successor<current_step){
valid_combination=false;
break;
}
else if(successor==current_step){
valid_combination=true;
break;
}
else{
valid_combination=true;
}
}
else if(rule_to_rule_map->field_value_at_row_position(rule_entry->first,"main_node_symbol")->empty()==false
&&main_nodes_found_by_symbol.empty()==true){
if(failover==0||failover<current_step){
valid_combination=false;
break;
}
else if(failover==current_step){
valid_combination=true;
break;
}
else{
valid_combination=false;
}
}
else{
throw std::runtime_error("Main node symbol is empty for parent symbol "+parent_node.symbol+", head root symbol "+child_node.symbol+" and empty or NULL non-head root symbol in RULE_TO_RULE_MAP db table.");
}
if(valid_combination==true){
if(successor>current_step){
rule_entry=rule_to_rule_map->value_for_field_name_found_after_row_position(rule_entry->first,"step",*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"successor"));
}
else if(successor!=0&&successor<=current_step){
throw std::runtime_error("You have a successor branch loop for parent symbol "+parent_node.symbol+", head root symbol "+child_node.symbol+" and empty/NULL non-head root symbol at step "+std::to_string(current_step)+". Sorry, no loops are allowed in the rule to rule map.");
}
else{
rule_entry=NULL;
}
}
else{
if(failover>current_step){
rule_entry=rule_to_rule_map->value_for_field_name_found_after_row_position(rule_entry->first,"step",*rule_to_rule_map->field_value_at_row_position(rule_entry->first,"failover"));
}
else if(failover!=0&&failover<current_step){
throw std::runtime_error("You have a failover branch loop for parent symbol "+parent_node.symbol+", head root symbol "+child_node.symbol+" and empty/NULL non-head root symbol at step "+std::to_string(current_step)+". Sorry, no loops are allowed in the rule to rule map.");
}
else{
rule_entry=NULL;
if(failover==current_step&&rule_to_rule_map->nr_of_result_rows()==current_step) valid_combination=true;
}
}
}
else{
throw std::runtime_error("No entry found for parent symbol "+parent_node.symbol+", head root symbol "+child_node.symbol+" and empty or NULL non-head root symbol in RULE_TO_RULE_MAP db table.");
}
}
if(valid_combination==true){
rule_step_failed=0;
}
else{
rule_step_failed=std::atoi(rule_entry_failure_copy->second.field_value.c_str());
}
delete rule_to_rule_map;
}
return rule_step_failed;
}
const node_info& interpreter::get_node_info(unsigned int node_id){
//TODO: consider returning a copy instead of reference even though it's a performance penalty
//but references become invalid if the node_infos vector grows and gets reallocated
if(node_id>0) return node_infos.at(node_id-1);//Make use of out_of_range exception
}
node_info& interpreter::get_private_node_info(unsigned int node_id){
if(node_id>0) return node_infos.at(node_id-1);//Make use of out_of_range exception
}
int interpreter::combine_nodes(const std::string& symbol, const node_info& left_node, const node_info& right_node){
node_info nodeinfo, new_phrase_head_root, new_phrase_non_head_root;
db *sqlite=NULL;
query_result *functors=NULL, *rule_to_rule_map=NULL,*main_symbol_entry=NULL,*main_symbol_root_entry=NULL,*dependent_symbol_entry=NULL,*dependent_symbol_root_entry=NULL;
unsigned int rule_step_failed=0;
const std::pair<const unsigned int,field> *rule_entry=NULL;
std::vector<unsigned int> head_leafs, non_head_leafs;
std::string head_leaf_words, non_head_leaf_words;
nodeinfo.node_id=0;
//TODO: Head first - head last determination
if(true){//If language is head first
new_phrase_head_root=left_node;
new_phrase_non_head_root=right_node;
}
else if(false){//If language is head last
new_phrase_head_root=right_node;
new_phrase_non_head_root=left_node;
}
else ;//TODO: What if none of them -like a programming language?
if(new_phrase_head_root.node_id!=0&&new_phrase_non_head_root.node_id!=0){
/*TODO:valamit kezdeni kell azzal ha a left_node->symbol='QPro' ill. ha az object_node-nak van gyereke*/
if(toa_&HI_SEMANTICS){
sqlite=db_factory::get_instance();
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"Looking for symbols for parent:"+symbol+", head root:"+new_phrase_head_root.symbol+", non-head root:"+new_phrase_non_head_root.symbol);
rule_to_rule_map=sqlite->exec_sql("SELECT * FROM RULE_TO_RULE_MAP WHERE PARENT_SYMBOL = '"+symbol+"' AND HEAD_ROOT_SYMBOL = '"+new_phrase_head_root.symbol+"' AND NON_HEAD_ROOT_SYMBOL = '"+new_phrase_non_head_root.symbol+"';");
if(rule_to_rule_map!=NULL){
/* TODO: Instead of the current validation, the head node needs to be validated against
* all child nodes of the right_node having a non-empty expression. This would ensure that
* all constituents are checked against each other and not only the new head of the phrase and
* the object of the phrase. E.g. 'list big small files!' is contradictory but now only
* big<->files, small<->files and list<->files are validated and not small<->files, big<->small,
* big<->files, list<->files.*/
rule_step_failed=is_valid_combination(symbol,new_phrase_head_root,new_phrase_non_head_root);
if(rule_step_failed!=0){
//TODO:depending of head first or head last left-to-right (lr) or right-to-left (rl) method needs to be called to collect leafs
get_leafs_of_node_lr(new_phrase_head_root,head_leafs);
get_leafs_of_node_lr(new_phrase_non_head_root,non_head_leafs);
for(auto&& i:head_leafs){
if(get_node_info(i).expression.morphalytics!=NULL&&get_node_info(i).expression.morphalytics->is_mocked()==false&&get_node_info(i).expression.word.empty()==false)
head_leaf_words+=get_node_info(i).expression.morphalytics->word()+" ";
}
if(head_leaf_words.empty()==false) head_leaf_words.pop_back();
for(auto&& i:non_head_leafs){
if(get_node_info(i).expression.morphalytics!=NULL&&get_node_info(i).expression.morphalytics->is_mocked()==false&&get_node_info(i).expression.word.empty()==false)
non_head_leaf_words+=get_node_info(i).expression.morphalytics->word()+" ";
}
if(non_head_leaf_words.empty()==false) non_head_leaf_words.pop_back();
throw invalid_combination(head_leaf_words,non_head_leaf_words);
}
}
}
nodeinfo.symbol=symbol;
nodeinfo.node_id=++nr_of_nodes_;
//TODO: Head first - head last determination
if(true){//If language is head first
nodeinfo.left_child=new_phrase_head_root.node_id;
nodeinfo.right_child=new_phrase_non_head_root.node_id;
}
else if(false){//If language is head last
nodeinfo.left_child=new_phrase_non_head_root.node_id;
nodeinfo.right_child=new_phrase_head_root.node_id;
}
else ;//TODO: What if none of them -like a programming language?
node_infos.push_back(nodeinfo);
validated_nodes.push_back(nodeinfo.node_id);
}
return nodeinfo.node_id;
}
std::multimap<std::pair<std::string,std::string>,std::pair<unsigned int,std::string> >* interpreter::is_valid_expression(node_info& main_node, node_info& dependent_node){
std::multimap<std::pair<std::string,std::string>,std::pair<unsigned int,std::string> > *functors=NULL;
if(main_node.expression.lexeme.empty()==false){//testing for leaves being leaves indeed
if(dependent_node.node_id!=0){
functors=functors_found_for_dependencies(dependent_node,main_node);
if(functors!=NULL){
if(functors->count(functors->begin()->first)==functors->size()){//check if one or more than one functor was found
if(main_node.expression.lexicon_entry==true&&functors->begin()->first.first!=main_node.expression.lexeme){
throw std::runtime_error("Functor "+functors->begin()->first.first+" and main node lexeme "+main_node.expression.lexeme+" don't match.");
}
}
else if(functors->count(functors->begin()->first)<functors->size()){//More than one functor is acceptable as an intermediate result
}
else{
throw std::runtime_error("More functors found for "+functors->begin()->first.first+" with key "+functors->begin()->first.second+" than the number of functors found altogether.");
}
}
}
}
return functors;
}
std::multimap<std::pair<std::string,std::string>,std::pair<unsigned int,std::string> >* interpreter::functors_found_for_dependencies(const node_info& dependent_node, node_info& main_node){
std::multimap<std::pair<std::string,std::string>,std::pair<unsigned int,std::string> > *functors_found=NULL;
std::set<std::tuple<std::string,std::string,unsigned int,std::string> > functors_deps_found_unique;
std::string d_key_list,functor;
query_result *functors=NULL;
std::vector<std::pair<unsigned int,std::string> > dependency_stack;
db *sqlite=NULL;
field field;
if(main_node.expression.lexicon_entry==true){
functor=main_node.expression.lexeme;
}
else{
functor=main_node.expression.gcat;
}
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"functor to be found for dependency "+dependent_node.expression.lexeme+":"+functor);
functors_found=new std::multimap<std::pair<std::string,std::string>,std::pair<unsigned int,std::string> >();
if(dependent_node.expression.gcat=="CON"){
find_functors_for_dependency("CON","",main_node.expression.dependencies,*functors_found,dependency_stack);
}
else if(dependent_node.expression.lexicon_entry==false){
find_functors_for_dependency(dependent_node.expression.gcat,"",main_node.expression.dependencies,*functors_found,dependency_stack);
}
else{
find_functors_for_dependency(dependent_node.expression.lexeme,"",main_node.expression.dependencies,*functors_found,dependency_stack);
}
if(functors_found->empty()==true){
return NULL;
}
//generate d_key_list string which lists the d_keys of functors_found like: ('1', '2', ..., '3')
d_key_list="(";
for(std::multimap<std::pair<std::string,std::string>,std::pair<unsigned int,std::string> >::const_iterator j=functors_found->begin();
j!=functors_found->end();++j){
if(j->first.first==functor&&d_key_list.find("'"+j->first.second+"'")==std::string::npos){
d_key_list+="'"+j->first.second+"',";
}
}
if(d_key_list.back()==',') d_key_list.resize(d_key_list.length()-1);//remove last ','
d_key_list+=")";
sqlite=db_factory::get_instance();
functors=sqlite->exec_sql("SELECT * FROM FUNCTORS WHERE FUNCTOR = '"+functor+"' AND D_KEY IN "+d_key_list+";");
if(functors==NULL){
throw std::runtime_error("Functor "+functor+" not found in FUNCTOR db table.");
}
for(auto i=functors_found->begin();i!=functors_found->end();){
if(i->first.first==functor&&functors_deps_found_unique.find(std::make_tuple(i->first.first,i->first.second,i->second.first,i->second.second))==functors_deps_found_unique.end()){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"dependency "+i->second.second+" with node id "+std::to_string(dependent_node.node_id)
+" and depolex row id "+std::to_string(i->second.first)
+" for functor "+i->first.first+" with d_key "+i->first.second
+" with node id "+std::to_string(main_node.node_id)+" found");
functors_deps_found_unique.insert(std::make_tuple(i->first.first,i->first.second,i->second.first,i->second.second));
++i;
}
else{
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"dependency "+i->second.second+" with node id "+std::to_string(dependent_node.node_id)
+" and depolex row id "+std::to_string(i->second.first)
+" for functor "+i->first.first+" with d_key "+i->first.second
+" with node id "+std::to_string(main_node.node_id)+" erased");
i=functors_found->erase(i);
}
}
return functors_found;
}
void interpreter::find_functors_for_dependency(const std::string& dependency, const std::string& d_key, const query_result* dependencies,
std::multimap<std::pair<std::string,std::string>,std::pair<unsigned int,std::string> >& functors_found,
std::vector<std::pair<unsigned int,std::string> >& dependency_stack){
const std::pair<const unsigned int,field> *depolex_entry=NULL;
std::string lexeme,lexeme_d_key;
std::vector<std::pair<unsigned int,std::string> >::const_iterator j;
if(dependencies!=NULL){
depolex_entry=dependencies->first_value_for_field_name_found("semantic_dependency",dependency);
if(d_key.empty()==false){
while(depolex_entry!=NULL&&*dependencies->field_value_at_row_position(depolex_entry->first,"semantic_dependency")==dependency
&&*dependencies->field_value_at_row_position(depolex_entry->first,"ref_d_key")!=d_key){
depolex_entry=dependencies->value_for_field_name_found_after_row_position(depolex_entry->first,"semantic_dependency",dependency);
}
}
while(depolex_entry!=NULL){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"looking for functor with dependency "
+*dependencies->field_value_at_row_position(depolex_entry->first,"semantic_dependency")+" with d_key "
+*dependencies->field_value_at_row_position(depolex_entry->first,"d_key"));
lexeme=*dependencies->field_value_at_row_position(depolex_entry->first,"lexeme");
lexeme_d_key=*dependencies->field_value_at_row_position(depolex_entry->first,"d_key");
for(j=dependency_stack.begin();j!=dependency_stack.end();++j){
if(j->first==depolex_entry->first&&j->second==depolex_entry->second.field_value) break;
}
if(j==dependency_stack.end()){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"push to dependency stack the row index:"+std::to_string(depolex_entry->first)+" and semantic dependency:"+depolex_entry->second.field_value);
dependency_stack.push_back(std::make_pair(depolex_entry->first,depolex_entry->second.field_value));
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting in functors_found an entry with row id "+std::to_string(dependency_stack.begin()->first)+", functor="+lexeme+" d_key="+lexeme_d_key);
functors_found.insert(std::make_pair(std::make_pair(lexeme,lexeme_d_key),*dependency_stack.begin()));
find_functors_for_dependency(lexeme,lexeme_d_key,dependencies,functors_found,dependency_stack);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"pop from dependency stack the row index:"+std::to_string(depolex_entry->first)+" and semantic dependency:"+depolex_entry->second.field_value);
dependency_stack.pop_back();
}
depolex_entry=dependencies->value_for_field_name_found_after_row_position(depolex_entry->first,"semantic_dependency",dependency);
if(d_key.empty()==false){
while(depolex_entry!=NULL&&*dependencies->field_value_at_row_position(depolex_entry->first,"semantic_dependency")==dependency
&&*dependencies->field_value_at_row_position(depolex_entry->first,"ref_d_key")!=d_key){
depolex_entry=dependencies->value_for_field_name_found_after_row_position(depolex_entry->first,"semantic_dependency",dependency);
}
}
}
}
}
void interpreter::get_nodes_by_symbol(const node_info& root_node, const std::string symbol, const std::string lexeme, std::map<unsigned int,bool>& lookup_nodes_reached, std::vector<unsigned int>& nodes_found){
//root_node: root node of the subtree in which the node should be found by the symbol
//symbol: symbol of the node by which the node should be found
//nodes_found: node ids of the nodes found
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"get nodes by symbol: root_node.symbol:"+root_node.symbol+", symbol:"+symbol+", lexeme:"+lexeme);
if(symbol.empty()==false){
auto lookup_node_reached=lookup_nodes_reached.find(root_node.node_id);
bool is_lookup_node_reached=false;
if(lookup_node_reached!=lookup_nodes_reached.end()){
lookup_node_reached->second=true;
is_lookup_node_reached=true;
}
else{
for(lookup_node_reached=lookup_nodes_reached.begin();lookup_node_reached!=lookup_nodes_reached.end();++lookup_node_reached){
if(lookup_node_reached->second==true){
is_lookup_node_reached=true;
lookup_node_reached=lookup_nodes_reached.end();
break;
}
}
}
if(root_node.ref_to_context==true&&root_node.ref_node_ids.empty()==false){
for(unsigned int i:root_node.ref_node_ids){
node_info ref_node=get_node_info(i);
if(lookup_nodes_reached.empty()==true||is_lookup_node_reached==true){
if(ref_node.symbol==symbol&&lexeme.empty()==true
||lexeme.empty()==true&&ref_node.expression.morphalytics!=NULL&&ref_node.expression.morphalytics->gcat()==symbol
||lexeme.empty()==true&&ref_node.expression.morphalytics!=NULL&&ref_node.expression.morphalytics->is_mocked()==false&&ref_node.expression.morphalytics->has_feature(symbol)==true
||lexeme.empty()==false&&ref_node.expression.morphalytics!=NULL&&ref_node.expression.morphalytics->gcat()==symbol&&ref_node.expression.lexeme==lexeme){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"node_found_by_symbol '"+symbol+"':"+std::to_string(ref_node.node_id));
nodes_found.push_back(ref_node.node_id);
}
}
if(ref_node.left_child!=0)get_nodes_by_symbol(get_node_info(ref_node.left_child),symbol,lexeme,lookup_nodes_reached,nodes_found);
if(ref_node.right_child!=0)get_nodes_by_symbol(get_node_info(ref_node.right_child),symbol,lexeme,lookup_nodes_reached,nodes_found);
}
}
else{
if(lookup_nodes_reached.empty()==true||is_lookup_node_reached==true){
if(root_node.symbol==symbol&&lexeme.empty()==true
||lexeme.empty()==true&&root_node.expression.morphalytics!=NULL&&root_node.expression.morphalytics->gcat()==symbol
||lexeme.empty()==true&&root_node.expression.morphalytics!=NULL&&root_node.expression.morphalytics->is_mocked()==false&&root_node.expression.morphalytics->has_feature(symbol)==true
||lexeme.empty()==false&&root_node.expression.morphalytics!=NULL&&root_node.expression.morphalytics->gcat()==symbol&&root_node.expression.lexeme==lexeme){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"node_found_by_symbol '"+symbol+"':"+std::to_string(root_node.node_id));
nodes_found.push_back(root_node.node_id);
}
}
if(root_node.left_child!=0)get_nodes_by_symbol(get_node_info(root_node.left_child),symbol,lexeme,lookup_nodes_reached,nodes_found);
if(root_node.right_child!=0)get_nodes_by_symbol(get_node_info(root_node.right_child),symbol,lexeme,lookup_nodes_reached,nodes_found);
}
if(lookup_node_reached!=lookup_nodes_reached.end()){
lookup_node_reached->second=false;
}
}
else{
throw std::runtime_error("Do you want me to look for empty symbols in parser tree? Stop.");
}
}
void interpreter::find_dependencies_for_node(const unsigned int node_id, t_map_of_node_ids_and_d_keys_to_nr_of_deps& dependencies_found,
std::multimap<std::pair<std::string,unsigned int>,std::tuple<std::string,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int> >& optional_dependencies_checked){
std::pair<unsigned int,unsigned int> dependency_for_d_key_found;
std::string d_key="0";
unsigned int d_counter=0;
const std::pair<const unsigned int,field> *depolex_entry=NULL;
std::vector<p_m1_node_id_m2_d_key> node_d_key_route;
p_m1_node_id_m2_d_key parent_node;
//TODO: shall dependencies_found_via_optional_paths be a map instead of multimap?
std::multimap<unsigned int,std::tuple<unsigned int,unsigned int,unsigned int,unsigned int> > dependencies_found_via_optional_paths;
std::set<unsigned int> unique_optional_dependency_paths;
const node_info& node=get_node_info(node_id);
if(node.expression.dependencies!=NULL){
depolex_entry=node.expression.dependencies->first_value_for_field_name_found("lexeme",node.expression.lexeme);
while(depolex_entry!=NULL&&node.expression.lexeme==*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"lexeme")
&&d_key!=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key")){
d_key=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key");
d_counter=std::atoi(node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_counter")->c_str());
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"checking top level entry for functor "+node.expression.lexeme+" d_key "+d_key);
if(node_dependency_traversal_stack.empty()==false){
parent_node=node_dependency_traversal_stack.top();
auto traversal_node=node_dependency_traversal_stack_tree.find(std::make_pair(node_dependency_traversal_stack.size(),parent_node));
if(traversal_node!=node_dependency_traversal_stack_tree.end()&&node.node_links.find(parent_node.first)!=node.node_links.end()){
traversal_node->second.push_back(std::make_pair(node_id,std::atoi(d_key.c_str())));
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"push_back to vector of node_id "+std::to_string(parent_node.first)+" with d_key "+std::to_string(parent_node.second)+" the node_id "+std::to_string(node_id)+" with d_key "+d_key);
}
if(dependencies_found.empty()==false){
auto traversal_node_dependencies=node_dependency_traversals.find(parent_node);
if(traversal_node_dependencies==node_dependency_traversals.end()){
node_dependency_traversals.insert(std::make_pair(parent_node,dependencies_found));
}
else{
traversal_node_dependencies->second.clear();
traversal_node_dependencies->second.insert(dependencies_found.begin(),dependencies_found.end());
}
}
if(optional_dependencies_checked.empty()==false){
auto traversal_node_odependencies=node_odependency_traversals.find(parent_node);
if(traversal_node_odependencies==node_odependency_traversals.end()){
node_odependency_traversals.insert(std::make_pair(parent_node,optional_dependencies_checked));
}
else{
traversal_node_odependencies->second.clear();
traversal_node_odependencies->second.insert(optional_dependencies_checked.begin(),optional_dependencies_checked.end());
}
}
}
dependencies_found.clear();
optional_dependencies_checked.clear();
node_dependency_traversal_stack.push(std::make_pair(node_id,std::atoi(d_key.c_str())));
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"pushed to stack node_id "+std::to_string(node_id)+" with d_key "+d_key);
auto traversal_node=node_dependency_traversal_stack_tree.find(std::make_pair(node_dependency_traversal_stack.size(),std::make_pair(node_id,std::atoi(d_key.c_str()))));
if(traversal_node==node_dependency_traversal_stack_tree.end()){
node_d_key_route.clear();
node_dependency_traversal_stack_tree.insert(std::make_pair(std::make_pair(node_dependency_traversal_stack.size(),std::make_pair(node_id,std::atoi(d_key.c_str()))),node_d_key_route));
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting in traversal tree at level "+std::to_string(node_dependency_traversal_stack.size())+": node_id "+std::to_string(node_id)+" with d_key "+d_key+" the node_d_key_route");
}
else{
throw std::runtime_error("What shall I do in this case? A previously processed node with its functor/d_key gets processed again. There may be a conflict in the rule_to_rule_map table.");
}
find_dependencies_for_functor(std::to_string(node.node_id),d_key,d_counter,node.node_id,d_key,dependencies_found,optional_dependencies_checked,dependencies_found_via_optional_paths);
unique_optional_dependency_paths.clear();
for(auto&& i:dependencies_found_via_optional_paths){
//if there are more than one optional dependency paths leading to the same real dependency,
//select the first one and collect them in a set
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"checking uniqueness of optional dependency with path nr "+std::to_string(i.first)
+" and node id "+std::to_string(std::get<0>(i.second))
+" d_key "+std::to_string(std::get<1>(i.second))
+" dependent node id "+std::to_string(std::get<2>(i.second))
+" odep level "+std::to_string(std::get<3>(i.second)));
bool duplicate_found=false;
for(auto&& j:unique_optional_dependency_paths){
auto optional_dependency_path=dependencies_found_via_optional_paths.find(j);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"against optional dependency with path nr "+std::to_string(j)
+" and node id "+std::to_string(std::get<0>(optional_dependency_path->second))
+" d_key "+std::to_string(std::get<1>(optional_dependency_path->second))
+" dependent node id "+std::to_string(std::get<2>(optional_dependency_path->second))
+" odep level "+std::to_string(std::get<3>(optional_dependency_path->second)));
if(i.first!=j&&std::get<0>(i.second)==std::get<0>(optional_dependency_path->second)
&&std::get<1>(i.second)==std::get<1>(optional_dependency_path->second)
&&std::get<2>(i.second)==std::get<2>(optional_dependency_path->second)){
duplicate_found=true;
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"duplicate found");
}
}
if(duplicate_found==false){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting optional dependency in unique optional dependency paths");
unique_optional_dependency_paths.insert(i.first);
}
}
for(std::map<std::pair<std::string,unsigned int>,std::tuple<std::string,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int> >::iterator j=optional_dependencies_checked.begin();j!=optional_dependencies_checked.end();){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"looking for optional dependency in unique odep paths with path nr "+std::to_string(std::get<3>(j->second))
+" and functor "+j->first.first
+" d_key "+std::to_string(j->first.second)
+" d_counter "+std::to_string(std::get<5>(j->second))
+" parent node id "+std::get<0>(j->second)
+" parent d_key "+std::to_string(std::get<1>(j->second))
+" parent d_counter "+std::to_string(std::get<2>(j->second))
+" odep level "+std::to_string(std::get<4>(j->second)));
auto unique_optional_dependency_path=unique_optional_dependency_paths.find(std::get<3>(j->second));
if(unique_optional_dependency_path==unique_optional_dependency_paths.end()){
//if there are more than one optional dependency paths leading to the same real dependency,
//keep only the first ones selected into unique_optional_dependency_paths, delete all others
j=optional_dependencies_checked.erase(j);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"deleting optional dependency: no unique optional dependency path found");
}
else if(std::get<4>(j->second)>std::get<3>(dependencies_found_via_optional_paths.find(*unique_optional_dependency_path)->second)){
//Cut off optional dependencies checked out that stem from a real dependency but lead to no other real dependency
//TODO: if there are more than one entries with the same path nr in dependencies_found_via_optional_paths
//find the entry with the greatest odep level and carry out the comparison with that.
//That will mean that the optional dependency being iterated over was inserted in its container AFTER
//finding the last real dependency that was registered in dependencies_found_via_optional_paths.
j=optional_dependencies_checked.erase(j);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"deleting optional dependency: odep level in optional_dependencies_checked is greater for this functor than in dependencies_found_via_optional_paths");
}
else{
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"optional dependency path is unique: leave it as it is");
++j;
}
}
parent_node=node_dependency_traversal_stack.top();
if(dependencies_found.empty()==false){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting dependencies found into dependency traversals of parent node id "+std::to_string(parent_node.first)+" with d_key "+std::to_string(parent_node.second));
auto traversal_node_dependencies=node_dependency_traversals.find(parent_node);
if(traversal_node_dependencies==node_dependency_traversals.end()){
node_dependency_traversals.insert(std::make_pair(parent_node,dependencies_found));
}
else{
traversal_node_dependencies->second.clear();
traversal_node_dependencies->second.insert(dependencies_found.begin(),dependencies_found.end());
}
}
else{
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"there are no dependencies found to insert into dependency traversals");
}
dependencies_found.clear();
if(optional_dependencies_checked.empty()==false){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting optional dependencies checked into optional dependency traversals of parent node id "+std::to_string(parent_node.first)+" with d_key "+std::to_string(parent_node.second));
auto traversal_node_odependencies=node_odependency_traversals.find(parent_node);
if(traversal_node_odependencies==node_odependency_traversals.end()){
node_odependency_traversals.insert(std::make_pair(parent_node,optional_dependencies_checked));
}
else{
traversal_node_odependencies->second.clear();
traversal_node_odependencies->second.insert(optional_dependencies_checked.begin(),optional_dependencies_checked.end());
}
}
else{
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"there are no optional dependencies checked to insert into optional dependency traversals");
}
optional_dependencies_checked.clear();
node_dependency_traversal_stack.pop();
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"popped dependency traversal stack");
if(node_dependency_traversal_stack.empty()==false){//restore dependencies_found and optional_dependencies_checked using node_dependency_traversal_stack.top()
parent_node=node_dependency_traversal_stack.top();
auto traversal_node_dependencies=node_dependency_traversals.find(parent_node);
auto traversal_node_odependencies=node_odependency_traversals.find(parent_node);
dependencies_found.clear();
if(traversal_node_dependencies!=node_dependency_traversals.end()){
dependencies_found.insert(traversal_node_dependencies->second.begin(),traversal_node_dependencies->second.end());
}
optional_dependencies_checked.clear();
if(traversal_node_odependencies!=node_odependency_traversals.end()){
optional_dependencies_checked.insert(traversal_node_odependencies->second.begin(),traversal_node_odependencies->second.end());
}
}
while(depolex_entry!=NULL&&*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key")==d_key){
depolex_entry=node.expression.dependencies->value_for_field_name_found_after_row_position(depolex_entry->first,"lexeme",node.expression.lexeme);
}
}
}
}
const std::pair<const unsigned int,field>* interpreter::followup_dependency(const unsigned int previous_dependency_row_index,const std::string& lexeme,const std::string& d_key,const bool previous_dependency_found,const query_result& dependencies){
std::string next_counter;
const std::pair<const unsigned int,field> *depolex_entry=NULL;
if(previous_dependency_found==true){
next_counter=*dependencies.field_value_at_row_position(previous_dependency_row_index,"d_successor");
}
else{
next_counter=*dependencies.field_value_at_row_position(previous_dependency_row_index,"d_failover");
}
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"next counter: "+next_counter);
if(std::atoi(next_counter.c_str())>0&&std::atoi(next_counter.c_str())>std::atoi(dependencies.field_value_at_row_position(previous_dependency_row_index,"d_counter")->c_str())){
depolex_entry=dependencies.value_for_field_name_found_after_row_position(previous_dependency_row_index,"lexeme",lexeme);
while(depolex_entry!=NULL&&*dependencies.field_value_at_row_position(depolex_entry->first,"d_key")==d_key
&&*dependencies.field_value_at_row_position(depolex_entry->first,"d_counter")!=next_counter){
depolex_entry=dependencies.value_for_field_name_found_after_row_position(depolex_entry->first,"lexeme",lexeme);
}
}
return depolex_entry;
}
void interpreter::find_dependencies_for_functor(const std::string& parent_node_id, const std::string& parent_d_key, const unsigned int parent_d_counter,
const unsigned int node_id, const std::string& d_key,
t_map_of_node_ids_and_d_keys_to_nr_of_deps& dependencies_found,
std::multimap<std::pair<std::string,unsigned int>,std::tuple<std::string,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int> >& optional_dependencies_checked,
std::multimap<unsigned int,std::tuple<unsigned int,unsigned int,unsigned int,unsigned int> >& dependencies_found_via_optional_paths){
const std::pair<const unsigned int,field> *depolex_entry=NULL;
std::string semantic_dependency,ref_d_key,d_counter="0",manner,d_failover,d_successor;
std::multimap<unsigned int,unsigned int>::const_iterator dependency_matrix_entry;
std::map<unsigned int,unsigned int>::const_iterator j;
t_map_of_node_ids_and_d_keys_to_nr_of_deps::iterator dependencies_found_entry;
std::set<unsigned int> d_counter_validated_dependencies,d_key_validated_dependencies;
bool dependency_found_for_functor=false,node_being_processed=false;
std::stack<p_m1_node_id_m2_d_key> traversal_stack;
const node_info& node=get_node_info(node_id);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"checking dependency for functor "+node.expression.lexeme+" d_key "+d_key);
if(node.expression.gcat=="CON"){
depolex_entry=node.expression.dependencies->first_value_for_field_name_found("lexeme","CON");
// Just copied here as a reminder once CON is allowed to have more than one d_key
// while(depolex_entry!=NULL&&*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key")!=d_key){
// depolex_entry=node.expression.dependencies->value_for_field_name_found_after_row_position(depolex_entry->first,"lexeme","CON");
// }
}
else if(node.expression.lexicon_entry==false){
depolex_entry=node.expression.dependencies->first_value_for_field_name_found("lexeme",node.expression.gcat);
while(depolex_entry!=NULL&&*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key")!=d_key){
depolex_entry=node.expression.dependencies->value_for_field_name_found_after_row_position(depolex_entry->first,"lexeme",node.expression.gcat);
}
}
else{
depolex_entry=node.expression.dependencies->first_value_for_field_name_found("lexeme",node.expression.lexeme);
while(depolex_entry!=NULL&&*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key")!=d_key){
depolex_entry=node.expression.dependencies->value_for_field_name_found_after_row_position(depolex_entry->first,"lexeme",node.expression.lexeme);
}
}
if(depolex_entry==NULL){
//throw exception as for each functor there must be one entry with at least NULL dependency
throw std::runtime_error("Exiting, no dependency entry found for functor "+node.expression.lexeme+" in DEPOLEX db table.");
}
while(depolex_entry!=NULL&&*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key")==d_key){
d_counter=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_counter");
semantic_dependency=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"semantic_dependency");
ref_d_key=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"ref_d_key");
manner=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"manner");
d_failover=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_failover");
d_successor=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_successor");
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"checking dependency entry "+semantic_dependency+" ref_d_key "+ref_d_key+" for functor "+node.expression.lexeme+" d_key "+d_key+" d_counter "+d_counter);
if(semantic_dependency.empty()==false&&ref_d_key.empty()==false&&(manner.empty()==true||manner=="0"||manner=="1"||manner=="2")){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"looking up depolex entry with row id "+std::to_string(depolex_entry->first)+" in dep.val.matrix");
d_counter_validated_dependencies.clear();
//Insert the corresponding entry into dependencies_found to indicate that the node id is already being checked
dependencies_found.insert(std::make_pair(std::make_pair(node_id,std::atoi(d_key.c_str())),std::make_tuple(parent_node_id,0,0,std::atoi(parent_d_key.c_str()),parent_d_counter)));
for(dependency_matrix_entry=node.dependency_validation_matrix.lower_bound(depolex_entry->first);
dependency_matrix_entry!=node.dependency_validation_matrix.upper_bound(depolex_entry->first);
++dependency_matrix_entry){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"dvm entry row index:"+std::to_string(dependency_matrix_entry->first)+", dep.node id:"+std::to_string(dependency_matrix_entry->second));
//If the row_id of the depolex_entry is found among the row_ids stored in the dep.vld.matrix
//then the field values match as well since both row_ids refer to the corresponding
//field in the node.expression.dependencies attribute.
if(d_key_validated_dependencies.find(dependency_matrix_entry->second)==d_key_validated_dependencies.end()){
d_counter_validated_dependencies.insert(dependency_matrix_entry->second);
d_key_validated_dependencies.insert(dependency_matrix_entry->second);
find_dependencies_for_functor(std::to_string(node_id),d_key,std::atoi(d_counter.c_str()),get_node_info(dependency_matrix_entry->second).node_id,ref_d_key,dependencies_found,optional_dependencies_checked,dependencies_found_via_optional_paths);
}
}
if((manner.empty()==true||manner=="0")&&d_counter_validated_dependencies.size()==1
||manner=="1"&&d_counter_validated_dependencies.size()>=1
||manner=="2"&&d_counter_validated_dependencies.size()>1){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"dependency "+semantic_dependency+" checked for functor "+node.expression.lexeme);
dependency_found_for_functor=true;
}
else if((manner.empty()==true||manner=="0")&&d_counter_validated_dependencies.size()==0
||manner=="1"&&d_counter_validated_dependencies.size()<1
||manner=="2"&&d_counter_validated_dependencies.size()<=1){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"dependency check for "+semantic_dependency+" returned FALSE for functor "+node.expression.lexeme+" with d_key "+d_key);
if(std::atoi(d_failover.c_str())>=std::atoi(d_counter.c_str())){
find_dependencies_for_functor(std::to_string(node_id),d_key,std::atoi(d_counter.c_str()),node_id,d_key,semantic_dependency,ref_d_key,dependencies_found,optional_dependencies_checked,dependencies_found_via_optional_paths);
}
dependency_found_for_functor=false;
}
else{
throw dependency_counter_manner_validation_failed(node.expression.lexeme);
}
dependencies_found_entry=dependencies_found.find(std::make_pair(node_id,std::atoi(d_key.c_str())));
if(dependency_found_for_functor==true){
if(dependencies_found_entry!=dependencies_found.end()){
std::get<2>(dependencies_found_entry->second)+=d_counter_validated_dependencies.size();
if(std::atoi(d_successor.c_str())==0||std::atoi(d_successor.c_str())>std::atoi(d_counter.c_str())){
std::get<1>(dependencies_found_entry->second)+=d_counter_validated_dependencies.size();
}
else{
//Don't increase counter for found dependencies: this indicates a deliberate error on the success branch
}
}
else{
dependencies_found.insert(std::make_pair(std::make_pair(node_id,std::atoi(d_key.c_str())),std::make_tuple(parent_node_id,d_counter_validated_dependencies.size(),d_counter_validated_dependencies.size(),std::atoi(parent_d_key.c_str()),parent_d_counter)));
}
}
else{
if(dependencies_found_entry!=dependencies_found.end()){
//No manner condition was fulfilled, don't increase the number of dependencies found
//but increase the number of dependencies to be found
if(d_failover.empty()==true||d_failover=="0"||std::atoi(d_failover.c_str())<std::atoi(d_counter.c_str())){
if(manner.empty()==true||manner=="0"||manner=="1"){
std::get<2>(dependencies_found_entry->second)+=1;
}
else if(manner=="2"){
std::get<2>(dependencies_found_entry->second)+=2;
}
}
}
else{
dependencies_found.insert(std::make_pair(std::make_pair(node_id,std::atoi(d_key.c_str())),std::make_tuple(parent_node_id,0,0,std::atoi(parent_d_key.c_str()),parent_d_counter)));
}
}
}
else if(semantic_dependency.empty()==true&&ref_d_key.empty()==true){
//A leaf in the dependency tree is found so the semantic dependency is empty. As such, there's no
//functor-dependency pair to be checked in the dep.vld.matrix so let's go on with the next dependency.
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"leaf dependency "+semantic_dependency+" checked for functor "+node.expression.lexeme+" and has no further dependency");
dependencies_found_entry=dependencies_found.find(std::make_pair(node_id,std::atoi(d_key.c_str())));
if(dependencies_found_entry!=dependencies_found.end()){
//Don't increase the number of dependencies (++dependencies_found_entry->second;), NULL dependency is considered as found but not counted
//dependency_found_for_functor=true;//TODO: consider if setting this flag is ok here or should it be set to true anyway for leaves?
}
else{
dependencies_found.insert(std::make_pair(std::make_pair(node_id,std::atoi(d_key.c_str())),std::make_tuple(parent_node_id,0,0,std::atoi(parent_d_key.c_str()),parent_d_counter)));
//dependency_found_for_functor=false;//TODO: consider if setting this flag is ok here or should it be set to true anyway for leaves?
}
dependency_found_for_functor=true;
}
else{
throw std::runtime_error("Inconsistent setup of manner, semantic_dependency and ref_d_key for lexeme "+node.expression.lexeme+", d_key "+d_key+" and d_counter "+d_counter+" in DEPOLEX db table.");
}
if(node.expression.gcat=="CON"||node.expression.lexicon_entry==false){
depolex_entry=followup_dependency(depolex_entry->first,node.expression.gcat,d_key,dependency_found_for_functor,*node.expression.dependencies);
}
else{
depolex_entry=followup_dependency(depolex_entry->first,node.expression.lexeme,d_key,dependency_found_for_functor,*node.expression.dependencies);
}
}
//Commented out the code below as it was meant to support graphs with more than one head. A minimal example for that
//is when A and B nodes are head (main) nodes and C is a dependency of both but A and B are not related to each other at all.
//The course of development did not follow that idea and now it poses a problem for nodes with a functor having more than one
//d_key. Let's extend the previous example to a diamond shape where A is the root, B and C are dependencies of A but
//B is a dependency of A's functor with d_key 1 and C is a dependency of A's functor with d_key 2. D is a dependency of B and C.
//When this function processes the dependencies of D (if any), in the end it'll check the node links of D and finds either B or C
//depending on which direction is processed first. So find_dependencies_for_node() will be called for the node D, registering B or C
//as it's main node, connecting the nodes in a way like A->B->D->C (or A->C->D->B) which is wrong since it should either be A->B->D
//or A->C->D.
/*for(j=node.node_links.begin();j!=node.node_links.end();++j){
node_being_processed=false;
for(dependencies_found_entry=dependencies_found.begin();dependencies_found_entry!=dependencies_found.end();++dependencies_found_entry){
if(dependencies_found_entry->first.first==j->first){
node_being_processed=true;
break;
}
}
if(node_being_processed==false){
traversal_stack=node_dependency_traversal_stack;
while(traversal_stack.empty()==false){
auto stack_top_node=traversal_stack.top();
auto stack_top_node_dependencies=node_dependency_traversals.find(stack_top_node);
if(stack_top_node_dependencies!=node_dependency_traversals.end()){
for(auto dependency_entry=stack_top_node_dependencies->second.begin();dependency_entry!=stack_top_node_dependencies->second.end();++dependency_entry){
if(dependency_entry->first.first==j->first){
node_being_processed=true;
break;
}
}
if(node_being_processed==true) break;
}
traversal_stack.pop();
}
}
if(node_being_processed==false){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"finding dependencies for node with node id "+std::to_string(j->first));
find_dependencies_for_node(j->first,dependencies_found,optional_dependencies_checked);
}
}*/
}
void interpreter::find_dependencies_for_functor(const std::string& parent_node_id, const std::string& parent_d_key,const unsigned int parent_d_counter,
const unsigned int node_id, const std::string& node_d_key, const std::string& functor, const std::string& d_key,
t_map_of_node_ids_and_d_keys_to_nr_of_deps& dependencies_found,
std::multimap<std::pair<std::string,unsigned int>,std::tuple<std::string,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int> >& optional_dependencies_checked,
std::multimap<unsigned int,std::tuple<unsigned int,unsigned int,unsigned int,unsigned int> >& dependencies_found_via_optional_paths){
const std::pair<const unsigned int,field> *depolex_entry=NULL;
std::string semantic_dependency,ref_d_key,d_counter="0",manner,d_failover,d_successor;
std::multimap<unsigned int,unsigned int>::const_iterator dependency_matrix_entry;
std::map<unsigned int,unsigned int>::const_iterator j;
t_map_of_node_ids_and_d_keys_to_nr_of_deps::iterator dependencies_found_entry;
std::set<unsigned int> d_counter_validated_dependencies,d_key_validated_dependencies,checked_d_counters;
bool dependency_found_for_functor=false;
unsigned int nr_of_skipped_opa_rules=0,nr_of_rules=0,path_nr=0,odep_level=0;
const node_info& node=get_node_info(node_id);
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"checking dependency for optional functor "+functor+" d_key "+d_key);
depolex_entry=node.expression.dependencies->first_value_for_field_name_found("lexeme",functor);
if(depolex_entry==NULL){
//throw exception as for each functor there must be one entry with at least NULL dependency
throw std::runtime_error("Exiting, no dependency entry found for functor "+functor+" in DEPOLEX db table.");
}
while(depolex_entry!=NULL&&*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key")!=d_key){
depolex_entry=node.expression.dependencies->value_for_field_name_found_after_row_position(depolex_entry->first,"lexeme",functor);
}
while(depolex_entry!=NULL&&*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_key")==d_key){
++nr_of_rules;
if(std::atoi(node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"optional_parent_allowed")->c_str())==true){
d_counter=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_counter");
semantic_dependency=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"semantic_dependency");
ref_d_key=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"ref_d_key");
manner=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"manner");
d_failover=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_failover");
d_successor=*node.expression.dependencies->field_value_at_row_position(depolex_entry->first,"d_successor");
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"checking dependency entry "+semantic_dependency+" ref_d_key "+ref_d_key+" for functor "+functor+" d_key "+d_key+" d_counter "+d_counter);
if(semantic_dependency.empty()==false&&ref_d_key.empty()==false&&(manner.empty()==true||manner=="0"||manner=="1"||manner=="2")){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"looking up depolex entry with row id "+std::to_string(depolex_entry->first)+" in dep.val.matrix");
d_counter_validated_dependencies.clear();
//Insert the corresponding entry into optional_dependencies_checked to indicate that the node id is already being checked
if(dependencies_found_via_optional_paths.empty()==true) path_nr=1;
else path_nr=dependencies_found_via_optional_paths.rbegin()->first+1;
odep_level=optional_dependencies_checked.size()+1;
optional_dependencies_checked.insert(std::make_pair(std::make_pair(functor,std::atoi(d_key.c_str())),std::make_tuple(parent_node_id,std::atoi(parent_d_key.c_str()),parent_d_counter,path_nr,odep_level,std::atoi(d_counter.c_str()))));
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"optional dependency level:"+std::to_string(odep_level)+", path_nr "+std::to_string(path_nr));
for(dependency_matrix_entry=node.dependency_validation_matrix.lower_bound(depolex_entry->first);
dependency_matrix_entry!=node.dependency_validation_matrix.upper_bound(depolex_entry->first);
++dependency_matrix_entry){
//If the row_id of the depolex_entry is found among the row_ids stored in the dep.vld.matrix
//then the field values match as well since both row_ids refer to the corresponding
//field in the node.expression.dependencies attribute.
if(d_key_validated_dependencies.find(dependency_matrix_entry->second)==d_key_validated_dependencies.end()){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"inserting dependency node id "+std::to_string(dependency_matrix_entry->second)+" of dependency "+get_node_info(dependency_matrix_entry->second).expression.lexeme+" d_counter_validated_dependencies");
d_counter_validated_dependencies.insert(dependency_matrix_entry->second);
d_key_validated_dependencies.insert(dependency_matrix_entry->second);
find_dependencies_for_functor(functor,d_key,std::atoi(d_counter.c_str()),get_node_info(dependency_matrix_entry->second).node_id,ref_d_key,dependencies_found,optional_dependencies_checked,dependencies_found_via_optional_paths);
}
}
if((manner.empty()==true||manner=="0")&&d_counter_validated_dependencies.size()==1
||manner=="1"&&d_counter_validated_dependencies.size()>=1
||manner=="2"&&d_counter_validated_dependencies.size()>1){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"dependency "+semantic_dependency+" checked for functor "+functor);
dependency_found_for_functor=true;
}
else if((manner.empty()==true||manner=="0")&&d_counter_validated_dependencies.size()==0
||manner=="1"&&d_counter_validated_dependencies.size()<1
||manner=="2"&&d_counter_validated_dependencies.size()<=1){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"dependency check for "+semantic_dependency+" returned FALSE for functor "+functor+" with d_key "+d_key);
if((std::atoi(d_failover.c_str())>=std::atoi(d_counter.c_str()))){
find_dependencies_for_functor(functor,d_key,std::atoi(d_counter.c_str()),node_id,node_d_key,semantic_dependency,ref_d_key,dependencies_found,optional_dependencies_checked,dependencies_found_via_optional_paths);
}
dependency_found_for_functor=false;
}
else{
throw dependency_counter_manner_validation_failed(functor);
}
dependencies_found_entry=dependencies_found.find(std::make_pair(node_id,std::atoi(node_d_key.c_str())));
if(dependency_found_for_functor==true){
unsigned int nr_of_deps_to_add=0;
if(dependencies_found_entry!=dependencies_found.end()){
//shall path_nr be recalculated here or the previously calculated value should be used?
if(dependencies_found_via_optional_paths.empty()==true) path_nr=1;
else path_nr=dependencies_found_via_optional_paths.rbegin()->first+1;
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"path nr "+std::to_string(path_nr)+" when dependency found");
for(auto&& i:d_counter_validated_dependencies){
bool dependency_already_found_for_node_id=false;
for(auto&& j:dependencies_found_via_optional_paths){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"checking if dependency "+semantic_dependency+", ref_d_key "+ref_d_key+" with node_id "+std::to_string(i)+" for functor "+get_node_info(node_id).expression.lexeme+" node_id "+std::to_string(node_id)+", d_key "+node_d_key+" is registered");
if(std::get<0>(j.second)==node_id
&&std::get<1>(j.second)==std::atoi(node_d_key.c_str())
&&std::get<2>(j.second)==i){
dependency_already_found_for_node_id=true;
break;
}
}
if(dependency_already_found_for_node_id==false){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"register dependency "+semantic_dependency+", ref_d_key "+ref_d_key+" with node_id "+std::to_string(i)+" for functor "+get_node_info(node_id).expression.lexeme+" node_id "+std::to_string(node_id)+", d_key "+node_d_key
+" with optional functor "+functor+" and optional dependency level:"+std::to_string(odep_level)+" on path nr "+std::to_string(path_nr));
dependencies_found_via_optional_paths.insert(std::make_pair(path_nr,std::make_tuple(node_id,std::atoi(node_d_key.c_str()),i,odep_level)));
++nr_of_deps_to_add;
}
}
if(nr_of_deps_to_add>0){
std::get<2>(dependencies_found_entry->second)+=nr_of_deps_to_add;
if(std::atoi(d_successor.c_str())==0||std::atoi(d_successor.c_str())>std::atoi(d_counter.c_str())){
std::get<1>(dependencies_found_entry->second)+=nr_of_deps_to_add;
}
else{
//Don't increase counter for found dependencies: this indicates a deliberate error on the success branch
}
}
}
else{
//shall path_nr be recalculated here or the previously calculated value should be used?
if(dependencies_found_via_optional_paths.empty()==true) path_nr=1;
else path_nr=dependencies_found_via_optional_paths.rbegin()->first+1;
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"path nr "+std::to_string(path_nr)+" when dependency not found");
for(auto&& i:d_counter_validated_dependencies){
bool dependency_already_found_for_node_id=false;
for(auto&& j:dependencies_found_via_optional_paths){
if(std::get<0>(j.second)==node_id
&&std::get<1>(j.second)==std::atoi(node_d_key.c_str())
&&std::get<2>(j.second)==i){
dependency_already_found_for_node_id=true;
break;
}
}
if(dependency_already_found_for_node_id==false){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"register dependency "+semantic_dependency+" for functor "+get_node_info(node_id).expression.lexeme+" node_id "+std::to_string(node_id)+" with optional functor "+functor
+"optional dependency level:"+std::to_string(odep_level));
dependencies_found_via_optional_paths.insert(std::make_pair(path_nr,std::make_tuple(node_id,std::atoi(node_d_key.c_str()),i,odep_level)));
++nr_of_deps_to_add;
}