Skip to content

Commit 0a58a79

Browse files
committed
fix(service, test): checkmissingtransitionsafterdeletion test
-transition are not check anymore when the user add and delete stop from the trainrunsection tab component
1 parent b1299af commit 0a58a79

File tree

4 files changed

+33
-36
lines changed

4 files changed

+33
-36
lines changed

src/app/services/data/trainrunsection.service.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,15 @@ describe("TrainrunSectionService", () => {
140140

141141
trainrunSectionService.deleteTrainrunSection(
142142
trainrunSectionService.getTrainrunSections()[1].getId(),
143+
true,
144+
true,
143145
);
144146

145147
expect(nodeA.getTransitions().length).toBe(1);
146148
expect(nodeB.getTransitions().length).toBe(0);
147149
expect(nodeC.getTransitions().length).toBe(0);
148150
expect(nodeD.getTransitions().length).toBe(1);
149151
expect(nodeE.getTransitions().length).toBe(1);
150-
151152
const transA_AB2 = nodeA.getTransition(tsAB.getId());
152153
const transA_EA2 = nodeA.getTransition(tsEA.getId());
153154
expect(transA_AB2.getId()).toBe(transA_EA2.getId());

src/app/services/data/trainrunsection.service.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,13 @@ export class TrainrunSectionService implements OnDestroy {
681681
}
682682
}
683683

684-
deleteTrainrunSection(trainrunSectionId: number, enforceUpdate = true) {
684+
deleteTrainrunSection(
685+
trainrunSectionId: number,
686+
enforceUpdate = true,
687+
checkAllTransitions = false,
688+
) {
685689
const trainrunSection = this.getTrainrunSectionFromId(trainrunSectionId);
690+
const trainrun = trainrunSection.getTrainrun();
686691
const sourceNodeId = trainrunSection.getSourceNodeId();
687692
const targetNodeId = trainrunSection.getTargetNodeId();
688693
this.deleteTrainrunSectionAndCleanupNodes(trainrunSection, false);
@@ -702,6 +707,12 @@ export class TrainrunSectionService implements OnDestroy {
702707
false,
703708
);
704709
});
710+
if (
711+
checkAllTransitions &&
712+
this.getAllTrainrunSectionsForTrainrun(trainrun.getId()).length > 0
713+
) {
714+
this.checkMissingTransitionsAfterDeletion(trainrun);
715+
}
705716
if (enforceUpdate) {
706717
this.nodeService.transitionsUpdated();
707718
this.nodeService.connectionsUpdated();
@@ -1160,40 +1171,25 @@ export class TrainrunSectionService implements OnDestroy {
11601171

11611172
checkMissingTransitionsAfterDeletion(trainrun: Trainrun) {
11621173
const trainrunSections = this.getAllTrainrunSectionsForTrainrun(trainrun.getId());
1174+
1175+
const nodesSeen = new Map<number, Node>();
11631176
trainrunSections.forEach((ts) => {
1164-
const sourceNode = ts.getSourceNode();
1165-
const targetNode = ts.getTargetNode();
1166-
1167-
const srcPortsWithoutTransitions = sourceNode
1168-
.getPorts()
1169-
.filter(
1170-
(port) =>
1171-
port.getTrainrunSection().getTrainrunId() === trainrun.getId() &&
1172-
sourceNode.getTransitionFromPortId(port.getId()) === undefined,
1173-
);
1174-
const trgPortsWithoutTransitions = targetNode
1175-
.getPorts()
1176-
.filter(
1177-
(port) =>
1178-
port.getTrainrunSection().getTrainrunId() === trainrun.getId() &&
1179-
targetNode.getTransitionFromPortId(port.getId()) === undefined,
1180-
);
1177+
nodesSeen.set(ts.getSourceNode().getId(), ts.getSourceNode());
1178+
nodesSeen.set(ts.getTargetNode().getId(), ts.getTargetNode());
1179+
});
11811180

1182-
if (srcPortsWithoutTransitions.length > 1) {
1183-
this.nodeService.addTransitionToNodes(
1184-
sourceNode.getId(),
1185-
targetNode.getId(),
1186-
srcPortsWithoutTransitions[0].getTrainrunSection(),
1187-
);
1188-
}
1181+
nodesSeen.forEach((node) => {
1182+
const freePorts = node.getFreePortsForTrainrun(trainrun.getId());
1183+
if (freePorts.length !== 2) return;
11891184

1190-
if (trgPortsWithoutTransitions.length > 1) {
1191-
this.nodeService.addTransitionToNodes(
1192-
sourceNode.getId(),
1193-
targetNode.getId(),
1194-
trgPortsWithoutTransitions[0].getTrainrunSection(),
1195-
);
1196-
}
1185+
const ts1 = freePorts[0].getTrainrunSection();
1186+
const ts2 = freePorts[1].getTrainrunSection();
1187+
1188+
// Skip natural terminal nodes of circular paths (e.g. A in A-B-C-A):
1189+
// if the trainrun still forms a cycle, no transition should be created.
1190+
if (this.trainrunService.isStartEqualsEndNode(ts1.getId())) return;
1191+
1192+
this.nodeService.addTransitionToNodeForTrainrunSections(node.getId(), ts1, ts2);
11971193
});
11981194
}
11991195

src/app/view/editor-main-view/data-views/editor.keyEvents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ export class EditorKeyEvents {
543543
if (ts.getSourceNode().selected() && ts.getTargetNode().selected()) {
544544
newTrainrunSectionToModify.push(ts);
545545
} else {
546-
this.trainrunSectionService.deleteTrainrunSection(ts.getId(), false);
546+
this.trainrunSectionService.deleteTrainrunSection(ts.getId(), false, true);
547547
}
548548
});
549549
});
@@ -751,7 +751,7 @@ export class EditorKeyEvents {
751751
});
752752
visibleTrainrunSections = visibleTrainrunSections.filter((v, i, a) => a.indexOf(v) === i);
753753
visibleTrainrunSections.forEach((trainrunSectionId: number) => {
754-
this.trainrunSectionService.deleteTrainrunSection(trainrunSectionId, false);
754+
this.trainrunSectionService.deleteTrainrunSection(trainrunSectionId, false, true);
755755
});
756756

757757
let selectedNodeDeleted = false;

src/app/view/editor-main-view/editor-main-view.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class EditorMainViewComponent implements AfterViewInit, OnDestroy {
217217
},
218218
);
219219
this.editorView.bindDeleteTrainrunSection((trainrunSection: TrainrunSection) =>
220-
this.trainrunSectionService.deleteTrainrunSection(trainrunSection.getId()),
220+
this.trainrunSectionService.deleteTrainrunSection(trainrunSection.getId(), true, true),
221221
);
222222
this.editorView.bindSetTrainrunSectionAsSelected((trainrunSection: TrainrunSection) => {
223223
this.trainrunService.setTrainrunAsSelected(trainrunSection.getTrainrun().getId());

0 commit comments

Comments
 (0)