This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParserXML.h
785 lines (708 loc) · 26 KB
/
ParserXML.h
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
#ifndef TRAFFICSIMULATION2022_PARSERXML_H
#define TRAFFICSIMULATION2022_PARSERXML_H
// custom headers
#include "TrafficSimulation.h"
#include "TrafficLight.h"
#include "Road.h"
#include "Vehicle.h"
#include "VehicleGenerator.h"
#include "CrossRoad.h"
#include "BusStop.h"
#include "tinyxml/tinyxml.h"
#include "Standard_Values.h"
#include "DesignByContract.h"
// build in libs
#include <iostream>
#include <string>
#include <stdexcept>
#include <typeinfo>
#include <sstream>
using namespace std;
int convertStrToInt(string input){
int tempi = 0;
stringstream temps(input);
temps >> tempi;
return tempi;
}
bool parseBusStop(TiXmlElement* &root, TrafficSimulation &trafficSimulation){
REQUIRE(trafficSimulation.properlyInitialized(), "TrafficSimulation was not initialized when calling parseBusStop");
string filename;
filename = TESTLOG;
filename += BUSLOG;
fstream log;
log.open(filename.c_str(), ios::out| ios::trunc);
// create new object
BusStop* busStop = new BusStop();
// temp values
string tempn;
int tempi;
bool failed = true;
bool exists = false;
// parsing
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) {
string elemname = elem->Value();
if(elem->NoChildren()){
busStop->setRoad(trafficSimulation.getDummyRoad());
busStop->setPosition(0);
busStop->setWaitTime(0);
failed = false;
log.close();
log.open(filename.c_str(), ios::out | ios::trunc);
log << "One of the elements had no value assigned\n";
log.close();
return false;
}
tempn = elem->GetText();
if (elemname == BAANL){
for (unsigned int i = 0; i < trafficSimulation.getRoads().size(); i++){
if (trafficSimulation.getRoads()[i]->getRoadName() == tempn){
busStop->setRoad(trafficSimulation.getRoads()[i]);
trafficSimulation.getRoads()[i]->addBusStop(busStop);
log << "road found\n";
exists = true;
}
}
if (!exists){
log << "road not found\n";
failed = false;
}
} else if (elemname == WACHTTIJD) {
tempi = convertStrToInt(tempn);
if (tempi < 1){
failed = false;
log << "Cycle time was lower than 1\n";
}
else {
busStop->setWaitTime(tempi);
busStop->setCooldown(tempi);
log << "Cycle time was higher than 0\n";
}
} else if (elemname == POSITIE) {
tempi = convertStrToInt(tempn);
if (tempi < 1){
failed = false;
log << "Position was lower than 1\n";
}
else {
busStop->setPosition(tempi);
log << "Position was higher than 0\n";
}
}
else{
cout << "Not recognized parameter" << endl;
log << "Not reconizable parameter\n";
failed = false;
}
}
if (failed){
trafficSimulation.addBusStop(busStop);
}
log.close();
return failed;
}
bool parseCrossRoad(TiXmlElement* &root, TrafficSimulation &trafficSimulation){
REQUIRE(trafficSimulation.properlyInitialized(), "TrafficSimulation was not initialized when calling parseCrossRoad");
string filename;
filename = TESTLOG;
filename += CROSSROADLOG;
fstream log;
log.open(filename.c_str(), ios::out| ios::trunc);
// create new object
CrossRoad* crossRoad = new CrossRoad();
// temp values
string tempn;
int tempi;
bool failed = true;
// parsing
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) {
string elemname = elem->Value();
if(elem->NoChildren()){
delete crossRoad;
log.open(filename.c_str(), ios::out | ios::trunc);
log << "One of the elements had no value assigned\n";
log.close();
return false;
}
tempn = elem->GetText();
if (elemname == BAANL){
TiXmlAttribute* atr = elem->FirstAttribute();
string pos;
if (atr != NULL){
log << "Position was assigned\n";
pos = atr->Value();
}
else{
pos = "0";
log << "Position was not assigned\n";
failed = false;
}
tempi = convertStrToInt(pos);
if (tempi > 1){
log << "Position value is higher than 1\n";
}
else {
log << "Position was lower than 1\n";
failed = false;
}
bool roadFound = false;
for (unsigned int i = 0; i < trafficSimulation.getRoads().size(); ++i) {
if (trafficSimulation.getRoads()[i]->getRoadName() == tempn){
crossRoad->addRoad(trafficSimulation.getRoads()[i], tempi);
trafficSimulation.getRoads()[i]->addCrossRoad(crossRoad);
roadFound = true;
break;
}
}
if (roadFound){
log << "Road is found\n";
}
else {
log << "Road not found\n";
failed = false;
}
}
}
bool end = false;
if (crossRoad->getRoads().size() > 1 and failed){
trafficSimulation.addCrossRoad(crossRoad);
end = true;
}
log.close();
return end;
}
/**
* REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling parseRoad");
* REQUIRE(*typeid(root).name() == 'P' , "parseRoad was called with invalid parameter");
* REQUIRE(!elem->NoChildren(), "One of the parameters was empty");
* REQUIRE(tempi >= 0, "Road length is not valid");
* @param root A pointer to a TiXmlElement
* @return True if parsing was successful
*/
bool parseRoad(TiXmlElement* &root, TrafficSimulation &trafficSimulation){
REQUIRE(trafficSimulation.properlyInitialized(), "TrafficSimulation was not initialized when calling parseRoad");
REQUIRE(*typeid(root).name() == 'P' , "parseRoad was called with invalid parameter");
string filename;
filename = TESTLOG;
filename += ROADLOG;
fstream log;
log.open(filename.c_str(), ios::out| ios::trunc);
// create road object
Road *newRoad = new Road();
// temp values
string tempn;
int tempi;
// Parsing
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) {
string elemName = elem->Value();
if(elem->NoChildren()){
log.close();
log.open(filename.c_str(), ios::out | ios::trunc);
log << "One of the elements had no value assigned\n";
log.close();
return false;
}
tempn = elem->GetText();
if(elemName == NAAM){
newRoad->setRoadName(tempn);
}
else if(elemName == LENGTE){
tempi = convertStrToInt(tempn);
if(tempi <= 0){
newRoad->setLength(0);
log << "Road length was assigned lower than 1\n";
}
else{
newRoad->setLength(tempi);
log << "Road length was assigned higher than 1\n";
}
}
}
// Check if road already exists + add road
if(newRoad->getLength() > 0 and !newRoad->getRoadName().empty()){
if(!trafficSimulation.addRoad(newRoad)){
log << "Road was not unique\n";
log.close();
return false;
}
else{
log << "Road is unique\n";
log.close();
return true;
}
}
log.close();
return false;
}
bool setTypeParser(const string &tempn, Vehicle* &vehicle){
Vehicle *newVehicle;
Road* tempRoad;
if (tempn == AUTO){
// Create new vehicle
newVehicle = new Vehicle(vehicle->getSpeed() , vehicle->getVehiclePosition() , T_AUTO);
// Remove the vehicle from the road
tempRoad = vehicle->getRoad();
if (tempRoad != NULL){
tempRoad->removeVehicle(vehicle);
}
// Update parameters
newVehicle->setStatus(vehicle->getStatus());
newVehicle->setAcceleration(vehicle->getAcceleration());
// Update pointer and InitCheck
*vehicle = *newVehicle;
vehicle->updateInitCheck();
// Add vehicle back on the road
if (tempRoad != NULL){
tempRoad->addVehicle(vehicle);
}
}
else if ( tempn == BUS ){
// Create new vehicle
newVehicle = new Vehicle(vehicle->getSpeed() , vehicle->getVehiclePosition() , T_BUS);
// Remove the vehicle from the road
tempRoad = vehicle->getRoad();
if (tempRoad != NULL){
tempRoad->removeVehicle(vehicle);
}
// Update parameters
newVehicle->setStatus(vehicle->getStatus());
newVehicle->setAcceleration(vehicle->getAcceleration());
// Update pointer and InitCheck
*vehicle = *newVehicle;
vehicle->updateInitCheck();
// Add vehicle back on the road
if (tempRoad != NULL){
tempRoad->addVehicle(vehicle);
}
}
else if ( tempn == BRANDWEERWAGEN ){
// Create new vehicle
newVehicle = new Vehicle(vehicle->getSpeed() , vehicle->getVehiclePosition() , T_FIRETRUCK);
// Remove the vehicle from the road
tempRoad = vehicle->getRoad();
if (tempRoad != NULL){
tempRoad->removeVehicle(vehicle);
}
// Update parameters
newVehicle->setStatus(vehicle->getStatus());
newVehicle->setAcceleration(vehicle->getAcceleration());
// Update pointer and InitCheck
*vehicle = *newVehicle;
vehicle->updateInitCheck();
// Add vehicle back on the road
if (tempRoad != NULL){
tempRoad->addVehicle(vehicle);
}
}
else if ( tempn == ZIEKENWAGEN ){
// Create new vehicle
newVehicle = new Vehicle(vehicle->getSpeed() , vehicle->getVehiclePosition() , T_AMBULANCE);
// Remove the vehicle from the road
tempRoad = vehicle->getRoad();
if (tempRoad != NULL){
tempRoad->removeVehicle(vehicle);
}
// Update parameters
newVehicle->setStatus(vehicle->getStatus());
newVehicle->setAcceleration(vehicle->getAcceleration());
// Update pointer and InitCheck
*vehicle = *newVehicle;
vehicle->updateInitCheck();
// Add vehicle back on the road
if (tempRoad != NULL){
tempRoad->addVehicle(vehicle);
}
}
else if ( tempn == POLITIECOMBI ){
// Create new vehicle
newVehicle = new Vehicle(vehicle->getSpeed() , vehicle->getVehiclePosition() , T_POLICE);
// Remove the vehicle from the road
tempRoad = vehicle->getRoad();
if (tempRoad != NULL){
tempRoad->removeVehicle(vehicle);
}
// Update parameters
newVehicle->setStatus(vehicle->getStatus());
newVehicle->setAcceleration(vehicle->getAcceleration());
// Update pointer and InitCheck
*vehicle = *newVehicle;
vehicle->updateInitCheck();
// Add vehicle back on the road
if (tempRoad != NULL){
tempRoad->addVehicle(vehicle);
}
}
else{
return false;
}
vehicle->updateInitCheck();
// vehicle->setStandardValues();
delete newVehicle;
return true;
}
/**
* REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling parseVehicle");
* REQUIRE(*typeid(root).name() == 'P' , "parseVehicle was called with invalid parameter");
* REQUIRE(!elem->NoChildren(), "One of the parameters was empty");
* REQUIRE(tempi > 0, "Position is not valid");
* @param root A pointer to a TiXmlElement
* @return True if parsing was successful
*/
bool parseVehicle(TiXmlElement* &root, TrafficSimulation &trafficSimulation){
REQUIRE(trafficSimulation.properlyInitialized(), "TrafficSimulation was not initialized when calling parseVehicle");
REQUIRE(*typeid(root).name() == 'P' , "parseVehicle was called with invalid parameter");
string filename;
filename = TESTLOG;
filename += VEHICLELOG;
fstream log;
log.open(filename.c_str(), ios::out| ios::trunc);
Vehicle* vehicle = new Vehicle();
string tempn;
int tempi;
bool failed = true;
bool roadExist = false;
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) {
string elemName = elem->Value();
// if elem is empty, end parsing and return false
if(elem->NoChildren()){
// vehicle->setType(T_NULL);
vehicle->setPosition(0);
if (roadExist){
vehicle->getRoad()->removeVehicle(vehicle);
}
vector<Vehicle*> tempVec = trafficSimulation.getVehicles();
if (!trafficSimulation.getVehicles().empty() and trafficSimulation.getVehicles()[trafficSimulation.getVehicles().size()-1] == vehicle){
tempVec.pop_back();
trafficSimulation.setVehicles(tempVec);
}
vehicle->setRoad(trafficSimulation.getDummyRoad());
log.close();
log.open(filename.c_str(), ios::out | ios::trunc);
log << "One of the elements had no value assigned\n";
log.close();
return false;
}
tempn = elem->GetText();
if (elemName == TYPE){
if(!setTypeParser(tempn, vehicle)){
// vehicle->setType(T_NULL);
log << "Type not found\n";
failed = false;
}
else {
log << "Type was found\n";
}
}
else if(elemName == BAANL){
for (unsigned int i = 0; i < trafficSimulation.getRoads().size(); ++i) {
if (tempn == trafficSimulation.getRoads()[i]->getRoadName()) {
vehicle->setRoad(trafficSimulation.getRoads()[i]);
trafficSimulation.getRoads()[i]->addVehicle(vehicle);
trafficSimulation.addVehicle(vehicle);
roadExist = true;
break;
}
}
if (roadExist){
log << "Road was found\n";
}
else {
log << "Road was not found\n";
failed = false;
}
}
else if(elemName == POSITIE){
tempi = convertStrToInt(tempn);
if (tempi < 0){
vehicle->setPosition(0);
log << "Position was assigned lower than 0\n";
failed = false;
}
else if (roadExist and (unsigned) tempi > vehicle->getRoad()->getLength()){
vehicle->setPosition(vehicle->getRoad()->getLength());
log << "Position was assigned higher than road length\n";
failed = false;
}
else {
vehicle->setPosition(tempi);
log << "Position was assigned higher than 0\n";
}
}
}
if (!failed){
// vehicle->setType(T_NULL);
vehicle->setPosition(0);
if (roadExist){
vehicle->getRoad()->removeVehicle(vehicle);
}
vector<Vehicle*> tempVec = trafficSimulation.getVehicles();
if (!trafficSimulation.getVehicles().empty() and trafficSimulation.getVehicles()[trafficSimulation.getVehicles().size()-1] == vehicle){
tempVec.pop_back();
trafficSimulation.setVehicles(tempVec);
}
vehicle->setRoad(trafficSimulation.getDummyRoad());
}
log.close();
return failed;
}
/**
* REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling parseTrafficLight");
* REQUIRE(*typeid(root).name() == 'P' , "parseTrafficLight was called with invali parameter");
* REQUIRE(!elem->NoChildren(), "One of the parameters was empty");
* REQUIRE(tempi >= 0, "Cycle is not valid");
* REQUIRE(tempi > 0, "Position is not valid");
* @param root A pointer to a TiXmlElement
* @return True if parsing was successful
*/
bool parseTrafficLight(TiXmlElement* &root, TrafficSimulation &trafficSimulation){
REQUIRE(trafficSimulation.properlyInitialized(), "TrafficSimulation was not initialized when calling parseTrafficLight");
REQUIRE(*typeid(root).name() == 'P' , "parseTrafficLight was called with invali parameter");
string filename;
filename = TESTLOG;
filename += TRAFFICLIGHTLOG;
fstream log;
log.open(filename.c_str(), ios::out| ios::trunc);
// Create object
TrafficLight* trafficLight = new TrafficLight();
// Create temp values
string tempn;
int tempi;
bool failed = true;
bool roadExist = false;
// Parsing
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) {
string elemName = elem->Value();
// if elem is empty, end parsing and return false
if(elem->NoChildren()){
trafficLight->setPosition(0);
trafficLight->setRoad(trafficSimulation.getDummyRoad());
trafficLight->setCyclus(0);
log.close();
log.open(filename.c_str(), ios::out | ios::trunc);
log << "One of the elements had no value assigned\n";
log.close();
return false;
}
tempn = elem->GetText();
if(elemName == BAANL){
for (unsigned int i = 0; i < trafficSimulation.getRoads().size(); ++i) {
if (tempn == trafficSimulation.getRoads()[i]->getRoadName()) {
trafficLight->setRoad(trafficSimulation.getRoads()[i]);
roadExist = true;
break;
}
}
if (roadExist){
log << "Road found\n";
}
else {
log << "Road not found\n";
failed = false;
}
}
else if(elemName == POSITIE){
tempi = convertStrToInt(elem->GetText());
if ((roadExist) && (tempi < 0 || tempi > int(trafficLight->getRoad()->getLength()))){
trafficLight->setPosition(0);
log << "Position was assigned higher than road length or lower than 0\n";
failed = false;
}
else {
log << "Position is valid\n";
trafficLight->setPosition(tempi);
}
}
else if(elemName == CYCLUS){
tempi = convertStrToInt(elem->GetText());
if (tempi <= 0){
trafficLight->setCyclus(0);
log << "Cycle time was assigned lower than 1\n";
failed = false;
}
else {
log << "Cycle time is valid\n";
trafficLight->setCyclus(tempi);
}
}
}
// add traffic light to road
if (failed){
for (unsigned int i = 0; i < trafficSimulation.getRoads().size(); ++i) {
if (trafficSimulation.getRoads()[i] == trafficLight->getRoad()){
trafficSimulation.getRoads()[i]->addLight(trafficLight);
trafficSimulation.addTrafficLight(trafficLight);
break;
}
}
}
log.close();
return failed;
}
/**
* REQUIRE(this->properlyInitialized(), "TrafficSimulation wasn't properly initialized when calling parseVehicleGenerator");
* REQUIRE(*typeid(root).name() == 'P' , "parseVehicleGenerator was called with invalid parameter");
* REQUIRE(!elem->NoChildren(), "One of the parameters was empty");
* REQUIRE(tempf > 0, "Frequency is not valid");
* @param root A pointer to a TiXmlElement
* @return True if parsing was successful
*/
bool parseVehicleGenerator(TiXmlElement* &root, TrafficSimulation &trafficSimulation){
REQUIRE(trafficSimulation.properlyInitialized(), "TrafficSimulation was not initialized when calling parseVehicleGenerator");
REQUIRE(*typeid(root).name() == 'P' , "parseVehicleGenerator was called with invalid parameter");
string filename;
filename = TESTLOG;
filename += VEHICLEGENERATORLOG;
fstream log;
log.open(filename.c_str(), ios::out| ios::trunc);
VehicleGenerator* vehicleGenerator = new VehicleGenerator();
string tempn;
int tempf;
bool failed = true;
bool exist = false;
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) {
string elemName = elem->Value();
// if elem is empty, end parsing and return false
if(elem->NoChildren()){
vehicleGenerator->setType(T_NULL);
vehicleGenerator->setRoad(trafficSimulation.getDummyRoad());
vehicleGenerator->setFrequentie(0);
vehicleGenerator->setCooldown(0);
log.close();
log.open(filename.c_str(), ios::out | ios::trunc);
log << "One of the elements had no value assigned\n";
log.close();
return false;
}
tempn = elem->GetText();
if (elemName == BAANL) {
for (unsigned int i = 0; i < trafficSimulation.getRoads().size(); ++i) {
if (tempn == trafficSimulation.getRoads()[i]->getRoadName()) {
vehicleGenerator->setRoad(trafficSimulation.getRoads()[i]);
exist = true;
/*if (!trafficSimulation.addVehicleGenerator(vehicleGenerator)){
vehicleGenerator->setRoad(NULL);
return false;
}*/
break;
}
}
if (exist){
log << "Road was found\n";
}
else {
log << "Road was nit found\n";
failed = false;
}
}
else if (elemName == FREQUENTIE) {
tempf = convertStrToInt(elem->GetText());
if (tempf <= 0){
vehicleGenerator->setFrequentie(0);
log << "Frequency was assigned lower than 1\n";
failed = false;
}
else {
vehicleGenerator->setFrequentie(tempf);
// Set cooldown : 0 for instant generation upon simulation start
vehicleGenerator->setCooldown(tempf);
log << "Frequency was assigned higher than 1\n";
}
}
if (tempn == AUTO){
vehicleGenerator->setType(T_AUTO);
}
else if ( tempn == BUS ){
vehicleGenerator->setType(T_BUS);
}
else if ( tempn == BRANDWEERWAGEN ){
vehicleGenerator->setType(T_FIRETRUCK);
}
else if ( tempn == ZIEKENWAGEN ){
vehicleGenerator->setType(T_AMBULANCE);
}
else if ( tempn == POLITIECOMBI ){
vehicleGenerator->setType(T_POLICE);
}
else{
// Set default type
vehicleGenerator->setType(T_AUTO);
}
}
if (failed){
trafficSimulation.addVehicleGenerator(vehicleGenerator);
}
log.close();
return failed;
}
void parseTrafficSimulationX(TrafficSimulation &trafficSimulation){
TiXmlDocument doc;
// File readable detection with error message
try{
if(!doc.LoadFile(trafficSimulation.getFilename().c_str())) {
cerr << doc.ErrorDesc() << endl;
throw 1;
}
}
catch(int error){
if(error == 1){
cout << "File not readable" << endl;
}
}
// Check if file has elements to parse
TiXmlElement* root = doc.FirstChildElement();
if(root == NULL) {
cerr << "Failed to load file: No root element." << endl;
doc.Clear();
}
// log file
ofstream logOutput;
// write in a file
logOutput.open("TestLog/newLog.txt");
// Parsing of data
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) {
string elemName = elem->Value();
if (elemName == BAANU) {
if (!parseRoad(elem, trafficSimulation)) {
//cout << "Error: Could not make road" << endl;
logOutput << false << '\n';
} else {
logOutput << true << '\n';
}
} else if (elemName == VERKEERSLICHT) {
if (!parseTrafficLight(elem, trafficSimulation)) {
//cout << "Error: Could not make traffic light" << endl;
logOutput << false << '\n';
} else {
logOutput << true << '\n';
}
} else if (elemName == VOERTUIG) {
if (!parseVehicle(elem, trafficSimulation)) {
//cout << "Error: Could not make vehicle" << endl;
logOutput << false << '\n';
} else {
logOutput << true << '\n';
}
} else if (elemName == VOERTUIGGENERATOR) {
if (!parseVehicleGenerator(elem, trafficSimulation)) {
//cout << "Error: Could not make vehicle generator" << endl;
logOutput << false << '\n';
} else {
logOutput << true << '\n';
}
} else if (elemName == KRUISPUNT) {
if (!parseCrossRoad(elem, trafficSimulation)){
//cout << "Error: Could not make crossroad" << endl;
logOutput << false << '\n';
} else {
logOutput << true << '\n';
}
} else if (elemName == BUSHALTE) {
if (!parseBusStop(elem, trafficSimulation)) {
//cout << "Error: Could not make bus stop" << endl;
logOutput << false << '\n';
} else {
logOutput << true << '\n';
}
}
}
}
#endif //TRAFFICSIMULATION2022_PARSERXML_H