Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/app/models/node.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,31 @@ export class Node {
return undefined;
}

getPreviousTrainrunSection(trainrunSection: TrainrunSection): TrainrunSection {
let transition = this.getTransitions().find((trans: Transition) => {
const t = this.getPortOfTrainrunSection(trainrunSection.getId());
if (t === undefined) {
return false;
}
return trans.getPortId2() === t.getId();
});
if (transition !== undefined) {
return this.getPort(transition.getPortId1()).getTrainrunSection();
}
transition = this.getTransitions().find((trans: Transition) => {
const t = this.getPortOfTrainrunSection(trainrunSection.getId());
if (t === undefined) {
return false;
}
return trans.getPortId1() === t.getId();
});
if (transition !== undefined) {
return this.getPort(transition.getPortId2()).getTrainrunSection();
}

return undefined;
}

isEndNode(trainrunSection: TrainrunSection): boolean {
const port = this.getPortOfTrainrunSection(trainrunSection.getId());
if (port === undefined) {
Expand Down
27 changes: 26 additions & 1 deletion src/app/services/data/trainrun.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {DataService} from "./data.service";
import {Node} from "../../models/node.model";
import {TrainrunSection} from "../../models/trainrunsection.model";
import {GeneralViewFunctions} from "../../view/util/generalViewFunctions";
import {NonStopTrainrunIterator, TrainrunIterator} from "../util/trainrun.iterator";
import {
BackwardNonStopTrainrunIterator,
BackwardTrainrunIterator,
NonStopTrainrunIterator,
TrainrunIterator,
} from "../util/trainrun.iterator";
import {LogService} from "../../logger/log.service";
import {LabelService} from "./label.service";
import {FilterService} from "../ui/filter.service";
Expand Down Expand Up @@ -691,6 +696,18 @@ export class TrainrunService {
};
}

getFirstNonStopTrainrunSection(trainrunSection: TrainrunSection): TrainrunSection {
// starts at the target node, goes backwards to find the first section that is not a non-stop section
const iterator = this.getBackwardNonStopIterator(
trainrunSection.getTargetNode(),
trainrunSection,
);
while (iterator.hasNext()) {
iterator.next();
}
return iterator.current().trainrunSection;
}

sumTravelTimeUpToLastNonStopNode(node: Node, trainrunSection: TrainrunSection): number {
let summedTravelTime = 0;
const iterator = this.getNonStopIterator(node, trainrunSection);
Expand Down Expand Up @@ -760,6 +777,14 @@ export class TrainrunService {
return new NonStopTrainrunIterator(this.logService, node, trainrunSection);
}

public getBackwardIterator(node: Node, trainrunSection: TrainrunSection) {
return new BackwardTrainrunIterator(this.logService, node, trainrunSection);
}

public getBackwardNonStopIterator(node: Node, trainrunSection: TrainrunSection) {
return new BackwardNonStopTrainrunIterator(this.logService, node, trainrunSection);
}

// For each trainrun, get iterator from the smallest consecutiveTime.
public getRootIterators(): Map<number, TrainrunIterator> {
const trainrunSections = this.trainrunSectionService.getTrainrunSections();
Expand Down
55 changes: 53 additions & 2 deletions src/app/services/util/trainrun.iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export class TrainrunIterator {
protected currentElement: TrainrunSectionNodePair = null;
protected pointerElement: TrainrunSectionNodePair = null;

private visitedNodes: TrainrunSectionNodePair[] = [];
protected visitedNodes: TrainrunSectionNodePair[] = [];

constructor(
private logService: LogService,
protected logService: LogService,
private startNode: Node,
private startTrainrunSection: TrainrunSection,
) {
Expand Down Expand Up @@ -73,6 +73,45 @@ export class TrainrunIterator {
}
}

export class BackwardTrainrunIterator extends TrainrunIterator {
public next(): TrainrunSectionNodePair {
const currentElement = Object.assign({}, this.pointerElement);
const trainrunSection = this.pointerElement.node.getPreviousTrainrunSection(
this.pointerElement.trainrunSection,
);

if (trainrunSection === undefined) {
this.pointerElement = new TrainrunSectionNodePair(undefined, undefined);
this.currentElement = currentElement;
return this.currentElement;
}

const node = this.pointerElement.node.getOppositeNode(trainrunSection);
this.pointerElement = new TrainrunSectionNodePair(node, trainrunSection);

if (
this.visitedNodes.find(
(element) =>
element.node.getId() === node.getId() &&
element.trainrunSection.getId() === trainrunSection.getId(),
) !== undefined
) {
// The trainrun has a loop -> early break the avoid unfinitiy iterating
this.currentElement = Object.assign({}, this.pointerElement);
this.pointerElement = new TrainrunSectionNodePair(undefined, undefined);
// log the issue
this.logService.error(
$localize`:@@app.services.util.trainrun-iteration.error.infinity-loop:Iterator has detected an infinity loop. The iteration terminated early!`,
new Error().stack,
);
return this.currentElement;
}
this.visitedNodes.push(this.currentElement);
this.currentElement = currentElement;
return this.currentElement;
}
}

export class NonStopTrainrunIterator extends TrainrunIterator {
public next(): TrainrunSectionNodePair {
if (!this.pointerElement.node.isNonStop(this.pointerElement.trainrunSection)) {
Expand All @@ -84,3 +123,15 @@ export class NonStopTrainrunIterator extends TrainrunIterator {
return super.next();
}
}

export class BackwardNonStopTrainrunIterator extends BackwardTrainrunIterator {
public next(): TrainrunSectionNodePair {
if (!this.pointerElement.node.isNonStop(this.pointerElement.trainrunSection)) {
// The trainrun has a stop and break the backward iteration
this.currentElement = Object.assign({}, this.pointerElement);
this.pointerElement = new TrainrunSectionNodePair(undefined, undefined);
return this.currentElement;
}
return super.next();
}
}