@@ -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
0 commit comments