-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy patheditor-tools-view.component.ts
676 lines (611 loc) · 28.7 KB
/
editor-tools-view.component.ts
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
import {parse, ParseResult} from "papaparse";
import {Component, ElementRef, ViewChild} from "@angular/core";
import * as svg from "save-svg-as-png";
import {DataService} from "../../services/data/data.service";
import {TrainrunService} from "../../services/data/trainrun.service";
import {NodeService} from "../../services/data/node.service";
import {FilterService} from "../../services/ui/filter.service";
import {TrainrunSectionService} from "../../services/data/trainrunsection.service";
import {UiInteractionService} from "../../services/ui/ui.interaction.service";
import {StammdatenService} from "../../services/data/stammdaten.service";
import {LogService} from "../../logger/log.service";
import {VersionControlService} from "../../services/data/version-control.service";
import {
HaltezeitFachCategories,
NetzgrafikDto,
NodeDto,
TrainrunCategoryHaltezeit,
TrainrunSectionDto,
} from "../../data-structures/business.data.structures";
import {downloadBlob} from "../util/download-utils";
import {map} from "rxjs/operators";
import {LabelService} from "../../services/data/label.service";
import {NetzgrafikColoringService} from "../../services/data/netzgrafikColoring.service";
import {ViewportCullService} from "../../services/ui/viewport.cull.service";
import {LevelOfDetailService} from "../../services/ui/level.of.detail.service";
import {
buildEdges,
computeNeighbors,
computeShortestPaths,
topoSort
} from "../util/origin-destination-graph";
import {TrainrunsectionValidator} from "../../services/util/trainrunsection.validator";
@Component({
selector: "sbb-editor-tools-view-component",
templateUrl: "./editor-tools-view.component.html",
styleUrls: ["./editor-tools-view.component.scss"],
})
export class EditorToolsViewComponent {
@ViewChild("stammdatenFileInput", {static: false})
stammdatenFileInput: ElementRef;
@ViewChild("netgrafikJsonFileInput", {static: false})
netgrafikJsonFileInput: ElementRef;
public isDeletable$ = this.versionControlService.variant$.pipe(
map((v) => v?.isDeletable),
);
public isWritable$ = this.versionControlService.variant$.pipe(
map((v) => v?.isWritable),
);
constructor(
private dataService: DataService,
private trainrunService: TrainrunService,
private nodeService: NodeService,
public filterService: FilterService,
private trainrunSectionService: TrainrunSectionService,
private uiInteractionService: UiInteractionService,
private stammdatenService: StammdatenService,
private labelService: LabelService,
private logger: LogService,
private versionControlService: VersionControlService,
private netzgrafikColoringService: NetzgrafikColoringService,
private viewportCullService: ViewportCullService,
private levelOfDetailService: LevelOfDetailService,
) {
}
onLoadButton() {
this.netgrafikJsonFileInput.nativeElement.click();
}
onLoad(param) {
const file = param.target.files[0];
const reader = new FileReader();
reader.onload = () => {
let netzgrafikDto: any;
try {
netzgrafikDto = JSON.parse(reader.result.toString());
} catch (err: any) {
const msg = $localize`:@@app.view.editor-side-view.editor-tools-view-component.import-netzgrafik-error:JSON error`;
this.logger.error(msg);
return;
}
if (netzgrafikDto === undefined) {
const msg = $localize`:@@app.view.editor-side-view.editor-tools-view-component.import-netzgrafik-error:JSON error`;
this.logger.error(msg);
return;
}
if (
"nodes" in netzgrafikDto &&
"trainrunSections" in netzgrafikDto &&
"trainruns" in netzgrafikDto &&
"resources" in netzgrafikDto &&
"metadata" in netzgrafikDto
) {
this.processNetzgrafikJSON(netzgrafikDto);
return;
}
const msg = $localize`:@@app.view.editor-side-view.editor-tools-view-component.import-netzgrafik-error:JSON error`;
this.logger.error(msg);
};
reader.readAsText(file);
// set the event target value to null in order to be able to load the same file multiple times after one another
param.target.value = null;
}
onSave() {
const data: NetzgrafikDto = this.dataService.getNetzgrafikDto();
const blob = new Blob([JSON.stringify(data)], {type: "application/json"});
downloadBlob(blob, $localize`:@@app.view.editor-side-view.editor-tools-view-component.netzgrafikFile:netzgrafik` + ".json");
}
onExportNetzgrafikSVG() {
// option 2: save svg as svg
// https://www.npmjs.com/package/save-svg-as-png
this.levelOfDetailService.disableLevelOfDetailRendering();
this.viewportCullService.onViewportChangeUpdateRendering(false);
const containerInfo = this.getContainertoExport();
svg
.svgAsDataUri(
containerInfo.documentToExport,
containerInfo.exportParameter,
)
.then((uri) => {
const a = document.createElement("a");
document.body.appendChild(a);
a.href = uri;
a.download = $localize`:@@app.view.editor-side-view.editor-tools-view-component.netzgrafikFile:netzgrafik` + ".svg";
a.click();
URL.revokeObjectURL(a.href);
a.remove();
containerInfo.documentToExport.setAttribute(
"style",
containerInfo.documentSavedStyle,
);
this.levelOfDetailService.enableLevelOfDetailRendering();
});
}
onPrintNetzgrafik() {
this.uiInteractionService.closeFilter();
this.uiInteractionService.print();
}
onExportNetzgrafikPNG() {
// option 1: save svg as png
// https://www.npmjs.com/package/save-svg-as-png
this.levelOfDetailService.disableLevelOfDetailRendering();
this.viewportCullService.onViewportChangeUpdateRendering(false);
const containerInfo = this.getContainertoExport();
svg.saveSvgAsPng(
containerInfo.documentToExport,
$localize`:@@app.view.editor-side-view.editor-tools-view-component.netzgrafikFile:netzgrafik` + ".png",
containerInfo.exportParameter,
);
//containerInfo.documentToExport.setAttribute('style', containerInfo.documentSavedStyle);
this.levelOfDetailService.enableLevelOfDetailRendering();
}
onLoadStammdatenButton() {
this.stammdatenFileInput.nativeElement.click();
}
onLoadStammdaten(param) {
const file = param.target.files[0];
const reader = new FileReader();
reader.onload = () => {
const finalResult: ParseResult = parse(reader.result.toString(), {
header: true,
});
this.stammdatenService.setStammdaten(finalResult.data);
};
reader.readAsText(file);
// set the event target value to null in order to be able to load the same file multiple times after one another
param.target.value = null;
}
onExportStammdaten() {
const filename = $localize`:@@app.view.editor-side-view.editor-tools-view-component.baseDataFile:baseData` + ".csv";
const csvData = this.convertToStammdatenCSV();
this.onExport(filename, csvData);
}
onExportZuglauf() {
const filename = $localize`:@@app.view.editor-side-view.editor-tools-view-component.trainrunFile:trainrun` + ".csv";
const csvData = this.convertToZuglaufCSV();
this.onExport(filename, csvData);
}
onExportOriginDestination() {
const filename = $localize`:@@app.view.editor-side-view.editor-tools-view-component.originDestinationFile:originDestination` + ".csv";
const csvData = this.convertToOriginDestinationCSV();
this.onExport(filename, csvData);
}
onExport(filename: string, csvData: string) {
const blob = new Blob([csvData], {
type: "text/csv",
});
const url = window.URL.createObjectURL(blob);
const nav = window.navigator as any;
if (nav.msSaveOrOpenBlob) {
nav.msSaveBlob(blob, filename);
} else {
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
window.URL.revokeObjectURL(url);
}
getVariantIsWritable() {
return this.versionControlService.getVariantIsWritable();
}
private buildCSVString(headers: string[], rows: string[][]): string {
const separator = ";";
const contentData: string[] = [];
contentData.push(headers.join(separator));
rows.forEach((row) => {
contentData.push(row.join(separator));
});
return contentData.join("\n");
}
private convertToStammdatenCSV(): string {
const comma = ",";
const headers: string[] = [];
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.bp:BP`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.station:Station`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.category:category`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.region:Region`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeIPV:passengerConnectionTimeIPV`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeA:Passenger_connection_time_A`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeB:Passenger_connection_time_B`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeC:Passenger_connection_time_C`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeD:Passenger_connection_time_D`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.ZAZ:ZAZ`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.transferTime:Transfer_time`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.labels:Labels`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.X:X`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.Y:Y`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.create:Create`);
const rows: string[][] = [];
this.nodeService.getNodes().forEach((nodeElement) => {
const trainrunCategoryHaltezeit: TrainrunCategoryHaltezeit =
nodeElement.getTrainrunCategoryHaltezeit();
const stammdaten = this.stammdatenService.getBPStammdaten(
nodeElement.getBetriebspunktName(),
);
const zaz = stammdaten !== null ? stammdaten.getZAZ() : 0;
const erstellen = stammdaten !== null ? stammdaten.getErstellen() : "JA";
const kategorien = stammdaten !== null ? stammdaten.getKategorien() : [];
const regions = stammdaten !== null ? stammdaten.getRegions() : [];
const row: string[] = [];
row.push(nodeElement.getBetriebspunktName());
row.push(nodeElement.getFullName());
row.push(kategorien.map((kat) => "" + kat).join(comma));
row.push(regions.map((reg) => "" + reg).join(comma));
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.IPV].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.IPV].haltezeit -
zaz),
);
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.A].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.A].haltezeit -
zaz),
);
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.B].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.B].haltezeit -
zaz),
);
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.C].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.C].haltezeit -
zaz),
);
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.D].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.D].haltezeit -
zaz),
);
row.push("" + zaz);
row.push("" + nodeElement.getConnectionTime());
row.push(
nodeElement
.getLabelIds()
.map((labelID) => {
const labelOfInterest = this.labelService.getLabelFromId(labelID);
if (labelOfInterest !== undefined) {
return labelOfInterest.getLabel();
}
return "";
})
.join(comma),
);
row.push("" + nodeElement.getPositionX());
row.push("" + nodeElement.getPositionY());
row.push(erstellen);
rows.push(row);
});
return this.buildCSVString(headers, rows);
}
private getContainertoExport() {
let htmlElementToExport = document.getElementById(
"main-streckengrafik-container",
);
let param = {};
console.log("Try -1- (main-streckengrafik-container): ", htmlElementToExport !== null);
if (htmlElementToExport === null) {
htmlElementToExport = document.getElementById("graphContainer");
console.log("Try -2- (graphContainer): ", htmlElementToExport !== null);
const boundingBox = this.nodeService.getNetzgrafikBoundingBox();
param = {
encoderOptions: 1.0,
scale: 2.0,
left: boundingBox.minCoordX - 32,
top: boundingBox.minCoordY - 32,
width: boundingBox.maxCoordX - boundingBox.minCoordX + 64,
height: boundingBox.maxCoordY - boundingBox.minCoordY + 64,
backgroundColor:
this.uiInteractionService.getActiveTheme().backgroundColor,
};
} else {
param = {
encoderOptions: 1.0,
scale: 1.0,
left: htmlElementToExport.offsetWidth / 3,
top: 80,
width: htmlElementToExport.offsetWidth,
height: htmlElementToExport.offsetHeight,
backgroundColor:
this.uiInteractionService.getActiveTheme().backgroundColor,
};
}
const oldStyle = htmlElementToExport.getAttribute("style");
const htmlsTagCollection = document.getElementsByTagName("html");
if (htmlsTagCollection.length > 0) {
const htmlRoot = htmlsTagCollection[0];
htmlElementToExport.setAttribute("style", htmlRoot.getAttribute("style"));
const styles = this.netzgrafikColoringService.generateGlobalStyles(
this.dataService.getTrainrunCategories(),
this.trainrunSectionService.getTrainrunSections(),
);
styles.forEach((s) => {
const docStyles = htmlRoot.ownerDocument.styleSheets;
for (let i = 0; i < s.cssRules.length; i++) {
htmlRoot.ownerDocument.styleSheets[docStyles.length - 1].insertRule(
s.cssRules[i].cssText,
);
}
});
}
return {
documentToExport: htmlElementToExport,
exportParameter: param,
documentSavedStyle: oldStyle,
};
}
private convertToZuglaufCSV(): string {
const comma = ",";
const headers: string[] = [];
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.trainCategory:Train category`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.trainName:Train name`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.startStation:Start station`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.destinationStation:Destination station`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.trafficPeriod:Traffic period`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.frequence:Frequence`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.departureMinuteAtStart:Minute of departure at start node`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.travelTimeStartDestination:Travel time start-destination`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.arrivalMinuteAtDestination:Arrival minute at destination node`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.turnaroundTimeDestination:Turnaround time at destination station`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.departureMinuteDeparture:Departure minute at destination node`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.travelTimeDestinationStart:Travel time destination-start`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.arrivalMinuteAtStart:Arrival minute at start node`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.turnaroundTimeStart:Turnaround time at start station`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.turnaroundTime:Turnaround time`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.labels:Labels`);
const rows: string[][] = [];
this.trainrunService
.getTrainruns()
.filter((trainrun) => this.filterService.filterTrainrun(trainrun))
.forEach((trainrun) => {
let startBetriebspunktName = "";
let endBetriebspunktName = "";
// Retrieve start -> end with:
// start {startNode, startTrainrunSection}
// end {iterator.current.node, iterator.current.trainrunSection}
const startNode = this.trainrunService.getStartNodeWithTrainrunId(trainrun.getId());
const startTrainrunSection = startNode.getStartTrainrunSection(trainrun.getId());
const iterator = this.trainrunService.getIterator(startNode, startTrainrunSection);
while (iterator.hasNext()) {
iterator.next();
}
startBetriebspunktName = startNode.getBetriebspunktName();
endBetriebspunktName = iterator.current().node.getBetriebspunktName();
const departureTimeAtStart = startTrainrunSection.getSourceNodeId() === startNode.getId() ?
startTrainrunSection.getSourceDepartureConsecutiveTime() :
startTrainrunSection.getTargetDepartureConsecutiveTime();
const arrivalTimeAtEnd = iterator.current().trainrunSection.getSourceNodeId() === iterator.current().node.getId() ?
iterator.current().trainrunSection.getSourceArrivalConsecutiveTime() :
iterator.current().trainrunSection.getTargetArrivalConsecutiveTime();
const travelTime = arrivalTimeAtEnd - departureTimeAtStart;
const startNodeDeparture = startTrainrunSection.getSourceNodeId() === startNode.getId() ?
startTrainrunSection.getSourceDeparture() :
startTrainrunSection.getTargetDeparture();
const endNodeArrival = iterator.current().trainrunSection.getSourceNodeId() === iterator.current().node.getId() ?
iterator.current().trainrunSection.getSourceArrival() :
iterator.current().trainrunSection.getTargetArrival();
const endNodeDeparture = iterator.current().trainrunSection.getSourceNodeId() === iterator.current().node.getId() ?
iterator.current().trainrunSection.getSourceDeparture() :
iterator.current().trainrunSection.getTargetDeparture();
const startNodeArrival = startTrainrunSection.getSourceNodeId() === startNode.getId() ?
startTrainrunSection.getSourceArrival() :
startTrainrunSection.getTargetArrival();
let waitingTimeOnStartStation = startNodeDeparture - startNodeArrival;
let waitingTimeOnEndStation = endNodeDeparture - endNodeArrival;
if (trainrun.getFrequency() > 60) {
// special case - if the freq is bigger than 60min (1h) - then just mirror
waitingTimeOnStartStation = 2.0 * (trainrun.getFrequency() / 2.0 - startNodeArrival);
waitingTimeOnEndStation = 2.0 * (trainrun.getFrequency() / 2.0 - endNodeArrival);
} else {
// find next freq (departing)
while (waitingTimeOnStartStation < 0) {
waitingTimeOnStartStation += trainrun.getFrequency();
}
while (waitingTimeOnEndStation < 0) {
waitingTimeOnEndStation += trainrun.getFrequency();
}
}
if (trainrun.getFrequency() < 60) {
waitingTimeOnEndStation =
waitingTimeOnEndStation % trainrun.getFrequency();
waitingTimeOnStartStation =
waitingTimeOnStartStation % trainrun.getFrequency();
}
const timeOfCirculation =
travelTime +
waitingTimeOnEndStation +
travelTime +
waitingTimeOnStartStation;
const row: string[] = [];
row.push(trainrun.getTrainrunCategory().shortName.trim());
row.push(trainrun.getTitle().trim());
row.push(startBetriebspunktName.trim());
row.push(endBetriebspunktName.trim());
row.push("Verkehrt: " + trainrun.getTrainrunTimeCategory().shortName.trim());
row.push("" + trainrun.getTrainrunFrequency().shortName.trim());
row.push("" + startNodeDeparture);
row.push("" + travelTime);
row.push("" + endNodeArrival);
row.push("" + waitingTimeOnEndStation);
row.push("" + endNodeDeparture);
row.push("" + travelTime);
row.push("" + startNodeArrival);
row.push("" + waitingTimeOnStartStation);
row.push("" + timeOfCirculation);
row.push(
trainrun
.getLabelIds()
.map((labelID) => {
const label = this.labelService.getLabelFromId(labelID);
if (label) {
return label.getLabel().trim();
}
return "";
}
)
.join(comma),
);
rows.push(row);
});
return this.buildCSVString(headers, rows);
}
// TODO: this may be incorrect for trainruns going through the same node several times.
private convertToOriginDestinationCSV(): string {
// Duration of the schedule to consider (in minutes).
// TODO: ideally this would be 24 hours, but performance is a concern.
// One idea to optimize would be to consider the minimum time window before the schedule repeats (LCM).
// Draft here: https://colab.research.google.com/drive/1Z1r2uU2pgffWxCbG_wt2zoLStZKzWleE#scrollTo=F6vOevK6znee
const timeLimit = 16 * 60;
const headers: string[] = [];
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.origin:Origin`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.destination:Destination`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.travelTime:Travel time`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.transfers:Transfers`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.totalCost:Total cost`);
const metadata = this.dataService.getNetzgrafikDto().metadata;
// The cost to add for each connection.
const connectionPenalty = metadata.analyticsSettings.originDestinationSettings.connectionPenalty;
const nodes = this.nodeService.getNodes();
const selectedNodes = this.nodeService.getSelectedNodes();
const odNodes = selectedNodes.length > 0 ? selectedNodes : this.nodeService.getVisibleNodes();
const trainruns = this.trainrunService.getVisibleTrainruns();
const [edges, tsSuccessor] = buildEdges(nodes, odNodes, trainruns, connectionPenalty, this.trainrunService, timeLimit);
const neighbors = computeNeighbors(edges);
const vertices = topoSort(neighbors);
// In theory we could parallelize the pathfindings, but the overhead might be too big.
const res = new Map<string, [number, number]>();
odNodes.forEach((origin) => {
computeShortestPaths(origin.getId(), neighbors, vertices, tsSuccessor).forEach((value, key) => {
res.set([origin.getId(), key].join(","), value);
});
});
const rows = [];
odNodes.sort((a, b) => a.getBetriebspunktName().localeCompare(b.getBetriebspunktName()));
odNodes.forEach((origin) => {
odNodes.forEach((destination) => {
if (origin.getId() === destination.getId()) {
return;
}
const costs = res.get([origin.getId(), destination.getId()].join(","));
if (costs === undefined) {
// Keep empty if no path is found.
rows.push([origin.getBetriebspunktName(), destination.getBetriebspunktName(), "", "", ""]);
return;
}
const [totalCost, connections] = costs;
// Check if the reverse path has the same cost.
if (destination.getId() < origin.getId()) {
const reverseCosts = res.get([destination.getId(), origin.getId()].join(","));
if (reverseCosts === undefined || reverseCosts[0] !== totalCost) {
console.log("Reverse path not found or different cost: ", origin.getId(), destination.getId());
}
}
const row = [origin.getBetriebspunktName(), destination.getBetriebspunktName(),
(totalCost - connections * connectionPenalty).toString(),
connections.toString(), totalCost.toString()];
rows.push(row);
});
});
return this.buildCSVString(headers, rows);
}
private detectNetzgrafikJSON3rdParty(netzgrafikDto: NetzgrafikDto): boolean {
return netzgrafikDto.nodes.find((n: NodeDto) =>
n.ports === undefined) !== undefined
||
netzgrafikDto.nodes.filter((n: NodeDto) =>
n.ports?.length === 0).length === netzgrafikDto.nodes.length
||
netzgrafikDto.trainrunSections.find((ts: TrainrunSectionDto) =>
ts.path === undefined ||
ts.path?.path === undefined ||
ts.path?.path?.length === 0
) !== undefined;
}
private processNetzgrafikJSON3rdParty(netzgrafikDto: NetzgrafikDto) {
// --------------------------------------------------------------------------------
// 3rd party generated JSON detected
// --------------------------------------------------------------------------------
console.log("Import: Automatic Port Alignment Detection - 3rd Party Data Import.");
const msg = $localize`:@@app.view.editor-side-view.editor-tools-view-component.import-netzgrafik-as-json-info-3rd-party:3rd party import`;
this.logger.info(msg);
// --------------------------------------------------------------------------------
// (Step 1) Import only nodes
const netzgrafikOnlyNodeDto: NetzgrafikDto = Object.assign({}, netzgrafikDto);
netzgrafikOnlyNodeDto.trainruns = [];
netzgrafikOnlyNodeDto.trainrunSections = [];
this.dataService.loadNetzgrafikDto(netzgrafikOnlyNodeDto);
// (Step 2) Import nodes and trainrunSectiosn by trainrun inseration (copy => create)
this.dataService.insertCopyNetzgrafikDto(netzgrafikDto, false);
// step(3) Check whether a transitions object was given when not
// departureTime - arrivatelTime == 0 => non-stop
this.nodeService.getNodes().forEach((n) => {
n.getTransitions().forEach((trans) => {
const p1 = n.getPort(trans.getPortId1());
const p2 = n.getPort(trans.getPortId2());
let arrivalTime = p1.getTrainrunSection().getTargetArrival();
if (p1.getTrainrunSection().getSourceNodeId() === n.getId()) {
arrivalTime = p1.getTrainrunSection().getSourceArrival();
}
let departureTime = p2.getTrainrunSection().getTargetDeparture();
if (p2.getTrainrunSection().getSourceNodeId() === n.getId()) {
departureTime = p2.getTrainrunSection().getSourceDeparture();
}
trans.setIsNonStopTransit(arrivalTime - departureTime === 0);
});
});
// step(4) Recalc/propagate consecutive times
this.trainrunService.propagateInitialConsecutiveTimes();
// step(5) Validate all trainrun sections
this.trainrunSectionService.getTrainrunSections().forEach((ts) => {
TrainrunsectionValidator.validateOneSection(ts);
TrainrunsectionValidator.validateTravelTime(ts);
});
}
private processNetzgrafikJSON(netzgrafikDto: NetzgrafikDto) {
// prepare JSON import
this.uiInteractionService.showNetzgrafik();
this.uiInteractionService.closeNodeStammdaten();
this.uiInteractionService.closePerlenkette();
this.nodeService.unselectAllNodes();
// import data
if (
netzgrafikDto.trainrunSections.length === 0
||
!this.detectNetzgrafikJSON3rdParty(netzgrafikDto)
) {
// -----------------------------------------------
// Default: Netzgrafik-Editor exported JSON
// -----------------------------------------------
this.dataService.loadNetzgrafikDto(netzgrafikDto);
// -----------------------------------------------
} else {
// -----------------------------------------------
// 3rd Party: Netzgrafik-Editor exported JSON
// -----------------------------------------------
this.processNetzgrafikJSON3rdParty(netzgrafikDto);
}
// recompute viewport
this.uiInteractionService.viewportCenteringOnNodesBoundingBox();
}
}