@@ -1472,19 +1472,16 @@ export class TrainrunSectionService implements OnDestroy {
14721472
14731473 /**
14741474 * Groups consecutive TrainrunSections that have collapsed nodes between them
1475- * into chains with start and end nodes. Each chain starts and ends with a non-collapsed node.
1475+ * into chains. Each chain starts and ends with a non-collapsed node.
1476+ * Start and end nodes can be accessed via: sections[0].getSourceNode() and sections[sections.length - 1].getTargetNode()
14761477 * @param trainrunSections List of TrainrunSections to group
1477- * @returns Array of section groups with start/end nodes
1478+ * @returns Array of section chains
14781479 */
14791480 groupTrainrunSectionsIntoChains ( trainrunSections : TrainrunSection [ ] ) : Array < {
14801481 sections : TrainrunSection [ ] ;
1481- startNode : Node ;
1482- endNode : Node ;
14831482 } > {
14841483 const groups : Array < {
14851484 sections : TrainrunSection [ ] ;
1486- startNode : Node ;
1487- endNode : Node ;
14881485 } > = [ ] ;
14891486 const visitedSections = new Set < number > ( ) ;
14901487
@@ -1493,46 +1490,33 @@ export class TrainrunSectionService implements OnDestroy {
14931490 return ;
14941491 }
14951492
1496- // Start a new group from this section
1493+ // Build chain using TrainrunIterator to leverage existing graph traversal
14971494 const chain : TrainrunSection [ ] = [ ] ;
1498- let currentSection = section ;
1499-
1500- // Add the first section
1501- chain . push ( currentSection ) ;
1502- visitedSections . add ( currentSection . getId ( ) ) ;
1503-
1504- // Look for consecutive sections with collapsed intermediate nodes
1505- // eslint-disable-next-line no-constant-condition
1506- while ( true ) {
1507- const targetNode = this . nodeService . getNodeFromId ( currentSection . getTargetNodeId ( ) ) ;
1508- if ( ! targetNode || ! targetNode . getIsCollapsed ( ) ) {
1509- break ; // End of chain if target node is not collapsed
1495+ const startNode = section . getSourceNode ( ) ;
1496+ const iterator = this . trainrunService . getIterator ( startNode , section ) ;
1497+
1498+ // Traverse the trainrun and collect sections with collapsed intermediate nodes
1499+ while ( iterator . hasNext ( ) ) {
1500+ const pair = iterator . next ( ) ;
1501+
1502+ if ( visitedSections . has ( pair . trainrunSection . getId ( ) ) ) {
1503+ break ; // Already processed this section
15101504 }
15111505
1512- // Find next section starting from this collapsed node
1513- const nextSection = trainrunSections . find (
1514- ( ts ) =>
1515- ts . getSourceNodeId ( ) === currentSection . getTargetNodeId ( ) &&
1516- ts . getTrainrunId ( ) === currentSection . getTrainrunId ( ) &&
1517- ! visitedSections . has ( ts . getId ( ) ) ,
1518- ) ;
1519- if ( ! nextSection ) {
1520- break ; // No further section found, end of chain
1506+ chain . push ( pair . trainrunSection ) ;
1507+ visitedSections . add ( pair . trainrunSection . getId ( ) ) ;
1508+
1509+ // Stop if we reach a non-collapsed node (end of collapsed chain)
1510+ if ( ! pair . node . getIsCollapsed ( ) ) {
1511+ break ;
15211512 }
1522- chain . push ( nextSection ) ;
1523- visitedSections . add ( nextSection . getId ( ) ) ;
1524- currentSection = nextSection ;
15251513 }
15261514
1527- // Create group with start and end nodes
1528- const startNode = this . nodeService . getNodeFromId ( chain [ 0 ] . getSourceNodeId ( ) ) ;
1529- const endNode = this . nodeService . getNodeFromId ( chain [ chain . length - 1 ] . getTargetNodeId ( ) ) ;
1530-
1531- groups . push ( {
1532- sections : chain ,
1533- startNode : startNode ,
1534- endNode : endNode ,
1535- } ) ;
1515+ if ( chain . length > 0 ) {
1516+ groups . push ( {
1517+ sections : chain ,
1518+ } ) ;
1519+ }
15361520 } ) ;
15371521
15381522 return groups ;
0 commit comments