-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainprogram.cc
1803 lines (1537 loc) · 59.1 KB
/
mainprogram.cc
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 <string>
using std::string;
using std::getline;
#include <iostream>
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
using std::flush;
using std::noskipws;
#include <iomanip>
using std::setfill;
using std::setw;
#include <istream>
using std::istream;
#include <ostream>
using std::ostream;
#include <fstream>
using std::ifstream;
#include <sstream>
using std::istringstream;
using std::ostringstream;
using std::stringstream;
#include <iomanip>
using std::setw;
#include <tuple>
using std::tuple;
using std::make_tuple;
using std::get;
using std::tie;
#include <regex>
using std::regex_match;
using std::regex_search;
using std::smatch;
using std::regex;
using std::sregex_token_iterator;
#include <algorithm>
using std::find_if;
using std::find;
using std::binary_search;
using std::max_element;
using std::max;
using std::min;
using std::shuffle;
using std::sort;
#include <random>
using std::minstd_rand;
using std::uniform_int_distribution;
#include <chrono>
#include <functional>
using std::function;
using std::equal_to;
#include <vector>
using std::vector;
#include <set>
using std::set;
#include <array>
using std::array;
#include <bitset>
using std::bitset;
#include <iterator>
using std::next;
#include <ctime>
using std::time;
#include <memory>
using std::move;
#include <utility>
using std::pair;
using std::make_pair;
#include <cmath>
using std::abs;
#include <cstdlib>
using std::div;
#include <algorithm>
using std::transform;
#include <iterator>
using std::back_inserter;
#include <cstddef>
#include <cassert>
#include "mainprogram.hh"
#include "datastructures.hh"
#ifdef GRAPHICAL_GUI
#include "mainwindow.hh"
#endif
string const MainProgram::PROMPT = "> ";
void MainProgram::test_get_functions(AffiliationID id)
{
ds_.get_affiliation_name(id);
ds_.get_affiliation_coord(id);
}
MainProgram::CmdResult MainProgram::cmd_add_affiliation(ostream& /*output*/, MatchIter begin, MatchIter end)
{
AffiliationID id = *begin++;
string name = *begin++;
string xstr = *begin++;
string ystr = *begin++;
assert( begin == end && "Impossible number of parameters!");
int x = convert_string_to<int>(xstr);
int y = convert_string_to<int>(ystr);
bool success = ds_.add_affiliation(id, name, {x, y});
view_dirty = true;
return {ResultType::IDLIST, CmdResultIDs{{}, {success ? id : NO_AFFILIATION}}};
}
MainProgram::CmdResult MainProgram::cmd_affiliation_info(std::ostream& /*output*/, MatchIter begin, MatchIter end)
{
AffiliationID id = *begin++;
assert( begin == end && "Impossible number of parameters!");
return {ResultType::IDLIST, CmdResultIDs{{}, {id}}};
}
void MainProgram::test_affiliation_info()
{
if (random_affiliations_added_ > 0) // Don't do anything if there's no affiliations
{
auto id = random_affiliation();
test_get_functions(id);
}
}
MainProgram::CmdResult MainProgram::cmd_change_affiliation_coord(std::ostream& /*output*/, MainProgram::MatchIter begin, MainProgram::MatchIter end)
{
AffiliationID id = *begin++;
string xstr = *begin++;
string ystr = *begin++;
assert( begin == end && "Impossible number of parameters!");
int x = convert_string_to<int>(xstr);
int y = convert_string_to<int>(ystr);
bool success = ds_.change_affiliation_coord(id, {x,y});
view_dirty = true;
return {ResultType::IDLIST, CmdResultIDs{{}, {success ? id : NO_AFFILIATION}}};
}
MainProgram::CmdResult MainProgram::cmd_get_publications_after(std::ostream &output, MatchIter begin, MatchIter end)
{
AffiliationID affiliationid = *begin++;
Year time = convert_string_to<Year>(*begin++);
assert( begin == end && "Impossible number of parameters!");
auto publications = ds_.get_publications_after(affiliationid, time);
if (publications.size() == 1 && publications.front() == std::make_pair(NO_YEAR, NO_PUBLICATION))
{
output << "No such publications found (NO_YEAR, NO_PUBLICATION returned)" << endl;
return {};
}
if (!publications.empty())
{
output << "Publications from affiliation ";
print_affiliation_brief(affiliationid, output, false);
output << " after year " << setw(4) << setfill('0') << time << ":" << endl;
for (auto& [deptime, publicationid] : publications)
{
output << " " << publicationid << " at " << setw(4) << setfill('0') << deptime << endl;
}
}
else
{
output << "No publications from affiliation ";
print_affiliation_brief(affiliationid, output, false);
output << " after year " << time << endl;
}
return {};
}
void MainProgram::test_get_publications_after()
{
if (random_affiliations_added_ > 0) // Don't do anything if there's no affiliations
{
auto id = random_affiliation();
ds_.get_publications_after(id, get_random_year());
}
}
void MainProgram::test_change_affiliation_coord()
{
if (random_affiliations_added_ > 0) // Don't do anything if there's no affiliations
{
auto id = random_affiliation();
ds_.change_affiliation_coord(id, get_random_coords());
}
}
MainProgram::CmdResult MainProgram::cmd_add_reference(std::ostream& output, MainProgram::MatchIter begin, MainProgram::MatchIter end)
{
// TODO check order of parameters!!
PublicationID id = convert_string_to<PublicationID>(*begin++);
PublicationID parentid = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
bool ok = ds_.add_reference(id, parentid);
if (ok)
{
try
{
auto referencename = ds_.get_publication_name(id);
auto parentname = ds_.get_publication_name(parentid);
output << "Added '" << referencename << "' as a reference of '" << parentname << "'" << endl;
}
catch (NotImplemented&)
{
output << "Added a reference to a publication." << endl;
}
return {ResultType::IDLIST, CmdResultIDs{{id, parentid}, {}}};
}
else
{
output << "Adding a reference failed!" << endl;
return {};
}
}
MainProgram::CmdResult MainProgram::cmd_add_affiliation_to_publication(std::ostream &output, MatchIter begin, MatchIter end)
{
AffiliationID affiliationid = *begin++;
PublicationID publicationid = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
bool ok = ds_.add_affiliation_to_publication(affiliationid, publicationid);
if (ok)
{
try
{
auto affiliationname = ds_.get_affiliation_name(affiliationid);
auto publicationname = ds_.get_publication_name(publicationid);
output << "Added '" << affiliationname << "' as an affiliation to publication '" << publicationname << "'" << endl;
}
catch (NotImplemented&)
{
output << "Added a affiliation to publication." << endl;
}
return {ResultType::IDLIST, CmdResultIDs{{publicationid}, {affiliationid}}};
}
else
{
output << "Adding a affiliation to publication failed!" << endl;
return {};
}
}
MainProgram::CmdResult MainProgram::cmd_get_affiliations_closest_to(std::ostream &output, MatchIter begin, MatchIter end)
{
string xstr = *begin++;
string ystr = *begin++;
assert( begin == end && "Impossible number of parameters!");
int x = convert_string_to<int>(xstr);
int y = convert_string_to<int>(ystr);
auto affiliations = ds_.get_affiliations_closest_to({x,y});
if (affiliations.empty())
{
output << "No affiliations!" << endl;
}
return {ResultType::IDLIST, CmdResultIDs{{}, affiliations}};
}
MainProgram::CmdResult MainProgram::cmd_get_closest_common_parent(std::ostream &output, MatchIter begin, MatchIter end)
{
PublicationID publicationid1 = convert_string_to<PublicationID>(*begin++);
PublicationID publicationid2 = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
auto publicationid = ds_.get_closest_common_parent(publicationid1, publicationid2);
if (publicationid == NO_PUBLICATION)
{
output << "No common referring publication found." << endl;
}
return {ResultType::IDLIST, CmdResultIDs{{publicationid1, publicationid2, publicationid}, {}}};
}
MainProgram::CmdResult MainProgram::cmd_remove_publication(std::ostream &output, MatchIter begin, MatchIter end)
{
PublicationID pubid = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
auto pubname = ds_.get_publication_name(pubid);
bool success = ds_.remove_publication(pubid);
if (success)
{
output << pubname << " removed." << endl;
view_dirty = true;
return {};
}
else
{
return {ResultType::IDLIST, CmdResultIDs{{NO_PUBLICATION}, {}}};
}
}
MainProgram::CmdResult MainProgram::cmd_get_parent(std::ostream& output, MatchIter begin, MatchIter end)
{
PublicationID pubid = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
auto refid = ds_.get_parent(pubid);
if (refid != NO_PUBLICATION) {
return {ResultType::IDLIST, CmdResultIDs{{refid},{}}};
}
output << "No references or publication doesn't exist." << std::endl;
return {};
}
MainProgram::CmdResult MainProgram::cmd_get_referenced_by_chain(std::ostream &output, MatchIter begin, MatchIter end)
{
PublicationID pubid = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
auto reference_chain = ds_.get_referenced_by_chain(pubid);
if (reference_chain.empty()) { output << "Publication is not cited anywhere." << std::endl; }
return {ResultType::IDLIST, CmdResultIDs{reference_chain, {}}};
}
MainProgram::CmdResult MainProgram::cmd_get_direct_references(std::ostream &output, MatchIter begin, MatchIter end)
{
PublicationID pubid = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
auto references = ds_.get_direct_references(pubid);
if (references.empty()) { output << "Publication has no direct references." << std::endl; }
std::sort(references.begin(),references.end());
return {ResultType::IDLIST, CmdResultIDs{references, {}}};
}
MainProgram::CmdResult MainProgram::cmd_get_publications(std::ostream& output, MainProgram::MatchIter begin, MainProgram::MatchIter end)
{
AffiliationID id = *begin++;
assert( begin == end && "Impossible number of parameters!");
auto result = ds_.get_publications(id);
if (result.empty()) { output << "Affiliation has no publications." << std::endl; }
std::sort(result.begin(),result.end());
return {ResultType::IDLIST, CmdResultIDs{result, {id}}};
}
void MainProgram::test_get_publications()
{
if (random_affiliations_added_ > 0) // Don't do anything if there's no affiliations
{
auto id = random_affiliation();
ds_.get_publications(id);
}
}
void MainProgram::test_get_all_references()
{
if (random_publications_added_ > 0) // Don't do anything if there's no publications
{
auto id = random_root_publication();
ds_.get_all_references(id);
}
}
MainProgram::CmdResult MainProgram::cmd_remove_affiliation(ostream& output, MatchIter begin, MatchIter end)
{
string id = *begin++;
assert( begin == end && "Impossible number of parameters!");
auto name = ds_.get_affiliation_name(id);
bool success = ds_.remove_affiliation(id);
if (success)
{
output << name << " removed." << endl;
view_dirty = true;
return {};
}
else
{
return {ResultType::IDLIST, CmdResultIDs{{}, {NO_AFFILIATION}}};
}
}
void MainProgram::test_remove_affiliation()
{
// Choose random number to remove
if (random_affiliations_added_ > 0) // Don't remove if there's nothing to remove
{
auto affiliationid = random_affiliation(); // there is a risk of getting the same id to remove multiple times -> usually takes less time than with existing affiliation
ds_.remove_affiliation(affiliationid);
}
}
void MainProgram::add_random_affiliations_publications(unsigned int size, Coord min, Coord max, const std::vector<Coord> &coordinates)
{
if(coordinates.size()!=size){
for (unsigned int i = 0; i < size; ++i)
{
auto name = n_to_name(random_affiliations_added_);
AffiliationID id = n_to_affiliationid(random_affiliations_added_);
ds_.add_affiliation(id, name, get_random_coords(min, max));
++random_affiliations_added_;
}
} else {
for (unsigned int i = 0; i < size; ++i)
{
auto name = n_to_name(random_affiliations_added_);
AffiliationID id = n_to_affiliationid(random_affiliations_added_);
ds_.add_affiliation(id, name, coordinates.at(i));
++random_affiliations_added_;
}
}
for (unsigned int i = 0; i< size; ++i) {
auto publicationid = n_to_publicationid(random_publications_added_);
vector<AffiliationID> affiliations;
for (int j=0; j<4; ++j)
{
affiliations.push_back(random_affiliation());
}
ds_.add_publication(publicationid, convert_to_string(publicationid), get_random_year(), std::move(affiliations));
// Add area as subarea so that we get a binary tree
if (random_publications_added_ > 0)
{
auto parentid = n_to_publicationid(random_publications_added_ / 2);
ds_.add_reference(publicationid, parentid);
}
++random_publications_added_;
}
}
MainProgram::CmdResult MainProgram::cmd_random_affiliations(ostream& output, MatchIter begin, MatchIter end)
{
string sizestr = *begin++;
string minxstr = *begin++;
string minystr = *begin++;
string maxxstr = *begin++;
string maxystr = *begin++;
assert( begin == end && "Impossible number of parameters!");
unsigned int size = convert_string_to<unsigned int>(sizestr);
Coord min = RANDOM_MIN_COORD;
Coord max = RANDOM_MAX_COORD;
std::unordered_set<Coord,CoordHash> exclude_list;
auto affiliations = ds_.get_all_affiliations();
for(const auto& affid : affiliations){
exclude_list.insert(ds_.get_affiliation_coord(affid));
}
if (!minxstr.empty() && !minystr.empty() && !maxxstr.empty() && !maxystr.empty())
{
min.x = convert_string_to<unsigned int>(minxstr);
min.y = convert_string_to<unsigned int>(minystr);
max.x = convert_string_to<unsigned int>(maxxstr);
max.y = convert_string_to<unsigned int>(maxystr);
}
else
{
if (!exclude_list.empty())
{
// Find out the min and max coordinates current affiliations reside within -> generates more data within the same region
min = {std::numeric_limits<int>::max(), std::numeric_limits<int>::max()};
max = {std::numeric_limits<int>::min(), std::numeric_limits<int>::min()};
for (auto const& coord : exclude_list)
{
auto x = coord.x;
auto y = coord.y;
if (x < min.x) { min.x = x; }
if (y < min.y) { min.y = y; }
if (x > max.x) { max.x = x; }
if (y > max.y) { max.y = y; }
}
}
}
std::vector<Coord> unique_new_coords;
try {
unique_new_coords = get_unique_coords(size,exclude_list,min,max);
} catch (...) {
output << "Impossible to create such number of unique coordinates within perimeters" <<endl;
return{};
}
add_random_affiliations_publications(size, min, max, unique_new_coords);
output << "Added: " << size << " affiliations and publications." << endl;
view_dirty = true;
return {};
}
void MainProgram::test_random_affiliations()
{
add_random_affiliations_publications(1);
}
void MainProgram::test_remove_publication()
{
if (random_publications_added_ > 0){
auto publicationid = random_publication(); // same as with remove_affiliation, could result in already removed id
ds_.remove_publication(publicationid);
}
}
void MainProgram::test_get_parent()
{
if (random_publications_added_ > 0){
auto publicationid = random_publication();
ds_.get_parent(publicationid);
}
}
void MainProgram::test_get_referenced_by_chain()
{
if (random_publications_added_ > 0){
auto publicationid = random_leaf_publication();
ds_.get_referenced_by_chain(publicationid);
}
}
void MainProgram::test_get_direct_references()
{
if (random_publications_added_ > 0) {
auto publicationid = random_publication();
ds_.get_direct_references(publicationid);
}
}
void MainProgram::test_get_affiliations()
{
if (random_publications_added_ > 0) {
auto publicationid = random_publication();
ds_.get_affiliations(publicationid);
}
}
void MainProgram::test_get_affiliation_count()
{
ds_.get_affiliation_count();
}
void MainProgram::test_get_all_publications()
{
ds_.all_publications();
}
void MainProgram::test_add_affiliation_to_publication()
{
if (random_publications_added_ > 0 || random_affiliations_added_ > 0) {
auto publicationid = random_publication();
auto affiliationid = random_affiliation();
ds_.add_affiliation_to_publication(affiliationid, publicationid);
}
}
Coord MainProgram::get_random_coords(const Coord min, const Coord max)
{
int x = random<int>(min.x, max.x);
int y = random<int>(min.y, max.y);
return {x, y};
}
Year MainProgram::get_random_year(const Year min, const Year max)
{
return random<int>(min, max);
}
std::vector<Coord> MainProgram::get_unique_coords(const unsigned int n, const std::unordered_set<Coord, CoordHash> &exclude_list, const Coord min, const Coord max)
{
const unsigned int max_unique_coords=abs(max.x-min.x)*abs(max.y-min.y)-exclude_list.size();
if(n>max_unique_coords){
throw NotImplemented("Impossible to create such number of unique coordinates within perimeters");
}
//assert(n<max_unique_coords&&"Impossible to create such number of unique coordinates within perimeters");
if(n>max_unique_coords/2){
// let's not use the lottery in this case, let's list the coordinates not in use
std::vector<Coord> retvec;
retvec.reserve(max_unique_coords);
for(auto x = min.x; x<max.x;++x){
for(auto y=min.y;y<max.y;++y){
Coord newCoord ={x,y};
if(exclude_list.find(newCoord)==exclude_list.end()){
retvec.push_back(newCoord);
}
}
}
// and get the required number of random_coordinates
std::shuffle(retvec.begin(),retvec.end(),rand_engine_);
retvec.erase(std::next(retvec.begin(),n),retvec.end());
return retvec;
} else {
std::unordered_set<Coord,CoordHash> coords;
coords.reserve(n);
while(coords.size()<n){
Coord newCoord = get_random_coords(min, max);
if(exclude_list.find(newCoord)==exclude_list.end()){
coords.insert(newCoord);
}
}
std::vector<Coord> retvec(coords.begin(),coords.end());
return retvec;
}
}
MainProgram::CmdResult MainProgram::cmd_get_affiliation_count(ostream& output, MatchIter begin, MatchIter end)
{
assert( begin == end && "Impossible number of parameters!");
output << "Number of affiliations: " << ds_.get_affiliation_count() << endl;
return {};
}
MainProgram::CmdResult MainProgram::cmd_get_all_affiliations(ostream& output, MatchIter begin, MatchIter end)
{
assert( begin == end && "Impossible number of parameters!");
auto affiliations = ds_.get_all_affiliations();
if (affiliations.empty())
{
output << "No affiliations!" << endl;
}
std::sort(affiliations.begin(), affiliations.end());
return {ResultType::IDLIST, CmdResultIDs{{}, affiliations}};
}
MainProgram::CmdResult MainProgram::cmd_add_publication(std::ostream& /*output*/, MatchIter begin, MatchIter end)
{
PublicationID id = convert_string_to<PublicationID>(*begin++);
string name = *begin++;
Year year = convert_string_to<Year>(*begin++);
string affilsstr = *begin++;
assert( begin == end && "Impossible number of parameters!");
vector<AffiliationID> affiliations;
smatch affil;
auto sbeg = affilsstr.cbegin();
auto send = affilsstr.cend();
for ( ; regex_search(sbeg, send, affil, affil_regex_); sbeg = affil.suffix().first)
{
affiliations.push_back(affil[1]);
}
bool success = ds_.add_publication(id, name, year, affiliations);
view_dirty = true;
return {ResultType::IDLIST, CmdResultIDs{{success ? id : NO_PUBLICATION}, {}}};
}
MainProgram::CmdResult MainProgram::cmd_get_all_publications(std::ostream &output, MatchIter begin, MatchIter end)
{
assert( begin == end && "Impossible number of parameters!");
auto publications = ds_.all_publications();
if (publications.empty())
{
output << "No publications!" << endl;
}
std::sort(publications.begin(), publications.end());
return {ResultType::IDLIST, CmdResultIDs{publications, {}}};
}
MainProgram::CmdResult MainProgram::cmd_publication_info(std::ostream& /*output*/, MatchIter begin, MatchIter end)
{
PublicationID id = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
return {ResultType::IDLIST, CmdResultIDs{{id}, {}}};
}
MainProgram::CmdResult MainProgram::cmd_get_all_references(std::ostream &output, MatchIter begin, MatchIter end)
{
PublicationID publicationid = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
auto references = ds_.get_all_references(publicationid);
if (references.empty())
{
output << "No (direct) references!" << endl;
}
std::sort(references.begin(), references.end());
references.insert(references.begin(), publicationid); // Add parameter as the first publication
return {ResultType::IDLIST, CmdResultIDs{references, {}}};
}
Distance MainProgram::calc_distance(Coord c1, Coord c2)
{
if (c1 == NO_COORD || c2 == NO_COORD) { return NO_DISTANCE; }
long long int deltax = c1.x - c2.x;
long long int deltay = c1.y - c2.y;
return static_cast<Distance>(std::sqrt(deltax*deltax + deltay*deltay));
}
MainProgram::CmdResult MainProgram::cmd_clear_all(ostream& output, MatchIter begin, MatchIter end)
{
assert(begin == end && "Invalid number of parameters");
ds_.clear_all();
init_primes();
output << "Cleared all affiliations and publications" << endl;
view_dirty = true;
return {};
}
string MainProgram::print_affiliation(AffiliationID id, ostream& output, bool nl)
{
try
{
if (id != NO_AFFILIATION)
{
auto name = ds_.get_affiliation_name(id);
auto xy = ds_.get_affiliation_coord(id);
if (!name.empty())
{
output << name << ": ";
}
else
{
output << "*: ";
}
output << "pos=";
print_coord(xy, output, false);
output << ", id=" << id;
if (nl) { output << endl; }
ostringstream retstream;
retstream << id;
return retstream.str();
}
else
{
output << "--NO_AFFILIATION--";
if (nl) { output << endl; }
return "";
}
}
catch (NotImplemented const& e)
{
output << endl << "NotImplemented while printing affiliation : " << e.what() << endl;
std::cerr << endl << "NotImplemented while printing affiliation : " << e.what() << endl;
return "";
}
}
string MainProgram::print_affiliation_brief(AffiliationID id, std::ostream &output, bool nl)
{
try
{
if (id != NO_AFFILIATION)
{
auto name = ds_.get_affiliation_name(id);
if (!name.empty())
{
output << name << " ";
}
else
{
output << "* ";
}
output << "(" << id << ")";
if (nl) { output << endl; }
ostringstream retstream;
retstream << id;
return retstream.str();
}
else
{
output << "--NO_AFFILIATION--";
if (nl) { output << endl; }
return "";
}
}
catch (NotImplemented const& e)
{
output << endl << "NotImplemented while printing affiliation : " << e.what() << endl;
std::cerr << endl << "NotImplemented while printing affiliation : " << e.what() << endl;
return "";
}
}
string MainProgram::print_publication(PublicationID id, std::ostream &output, bool nl)
{
try
{
if (id != NO_PUBLICATION)
{
auto name = ds_.get_publication_name(id);
auto year = ds_.get_publication_year(id);
if (!name.empty())
{
output << name << ": ";
}
else
{
output << "*: ";
}
output << "year=";
if (year == NO_YEAR) {
output << "--NO_YEAR--";
} else {
output << std::to_string(year);
}
output << ", id=" << id;
if (nl) { output << endl; }
ostringstream retstream;
retstream << id;
return retstream.str();
}
else
{
output << "--NO_PUBLICATION--";
if (nl) { output << endl; }
return "";
}
}
catch (NotImplemented const& e)
{
output << endl << "NotImplemented while printing publication : " << e.what() << endl;
std::cerr << endl << "NotImplemented while printing publication : " << e.what() << endl;
return "";
}
}
MainProgram::CmdResult MainProgram::cmd_find_affiliation_with_coord(ostream& /* output */, MatchIter begin, MatchIter end)
{
string xstr = *begin++;
string ystr = *begin++;
assert( begin == end && "Impossible number of parameters!");
int x = convert_string_to<int>(xstr);
int y = convert_string_to<int>(ystr);
auto result = ds_.find_affiliation_with_coord({x,y});
return {ResultType::IDLIST, CmdResultIDs{{}, {result}}};
}
MainProgram::CmdResult MainProgram::cmd_get_affiliations(std::ostream &output, MatchIter begin, MatchIter end)
{
auto pubid = convert_string_to<PublicationID>(*begin++);
assert( begin == end && "Impossible number of parameters!");
auto affiliations = ds_.get_affiliations(pubid);
std::sort(affiliations.begin(),affiliations.end());
if (affiliations.empty()) { output << "Publication has no affiliations." << std::endl; }
return {ResultType::IDLIST, CmdResultIDs{{pubid},affiliations}};
}
AffiliationID MainProgram::random_affiliation()
{
return n_to_affiliationid(random<decltype(random_affiliations_added_)>(0, random_affiliations_added_));
}
PublicationID MainProgram::random_publication()
{
return n_to_publicationid(random<decltype(random_publications_added_)>(0, random_publications_added_));
}
PublicationID MainProgram::random_root_publication()
{
unsigned long end = ROOT_BIAS_MULTIPLIER * random_publications_added_;
if (end == 0 ) {
return 0;
}
return n_to_publicationid(random<decltype(random_publications_added_)>(0, end));
}
PublicationID MainProgram::random_leaf_publication()
{
unsigned long start = LEAF_BIAS_MULTIPLIER * random_publications_added_;
if (start == random_publications_added_) {
start = 0;
}
return n_to_publicationid(random<decltype(random_publications_added_)>(start, random_publications_added_));
}
void MainProgram::test_find_affiliation_with_coord()
{
ds_.find_affiliation_with_coord(get_random_coords());
}
void MainProgram::test_publication_info()
{
if (random_publications_added_ > 0) // Don't do anything if there's no publications
{
auto id = random_publication();
ds_.get_publication_name(id);
ds_.get_publication_year(id);
}
}
void MainProgram::test_affiliations_closest_to()
{
ds_.get_affiliations_closest_to(get_random_coords());
}
void MainProgram::test_get_closest_common_parent()
{
if (random_publications_added_ > 0) // Don't do anything if there's no publications
{
auto id1 = random_leaf_publication();
auto id2 = random_leaf_publication();
ds_.get_closest_common_parent(id1, id2);
}
}
MainProgram::CmdResult MainProgram::cmd_randseed(std::ostream& output, MatchIter begin, MatchIter end)
{
string seedstr = *begin++;
assert(begin == end && "Invalid number of parameters");
unsigned long int seed = convert_string_to<unsigned long int>(seedstr);
rand_engine_.seed(seed);
init_primes();
output << "Random seed set to " << seed << endl;
return {};
}
MainProgram::CmdResult MainProgram::cmd_read(std::ostream& output, MatchIter begin, MatchIter end)
{
string filename = *begin++;
string silentstr = *begin++;
assert( begin == end && "Impossible number of parameters!");
bool silent = !silentstr.empty();
ostream* new_output = &output;
ostringstream dummystr; // Given as output if "silent" is specified, the output is discarded
if (silent)
{
new_output = &dummystr;
}
ifstream input(filename);
if (input)
{
output << "** Commands from '" << filename << "'" << endl;
command_parser(input, *new_output, PromptStyle::NORMAL);
if (silent) { output << "...(output discarded in silent mode)..." << endl; }
output << "** End of commands from '" << filename << "'" << endl;