| 5054 | } |
| 5055 | |
| 5056 | public *traverse( |
| 5057 | source: GraphSource<any>, |
| 5058 | input: Iterable<TraversalPath<any, any, any>>, |
| 5059 | _context?: QueryContext, |
| 5060 | ): IterableIterator<TraversalPath<any, any, any>> { |
| 5061 | const { |
| 5062 | targetId, |
| 5063 | targetCondition, |
| 5064 | direction = "out", |
| 5065 | edgeLabels = [], |
| 5066 | maxDepth = 100, |
| 5067 | weightProperty, |
| 5068 | stepLabels, |
| 5069 | } = this.config; |
| 5070 | |
| 5071 | if (targetId === undefined && targetCondition === undefined) { |
| 5072 | // No target specified - yield nothing |
| 5073 | // This allows the step to be created first and configured later via the fluent API |
| 5074 | return; |
| 5075 | } |
| 5076 | |
| 5077 | for (const path of input) { |
| 5078 | this.traversed++; |
| 5079 | const startVertex = path.value; |
| 5080 | if (!(startVertex instanceof Vertex)) { |
| 5081 | continue; |
| 5082 | } |
| 5083 | |
| 5084 | // Use appropriate algorithm based on whether weights are used |
| 5085 | const result = weightProperty |
| 5086 | ? this.dijkstra( |
| 5087 | source, |
| 5088 | startVertex, |
| 5089 | targetId, |
| 5090 | targetCondition, |
| 5091 | direction, |
| 5092 | edgeLabels, |
| 5093 | maxDepth, |
| 5094 | weightProperty, |
| 5095 | ) |
| 5096 | : this.bfs(source, startVertex, targetId, targetCondition, direction, edgeLabels, maxDepth); |
| 5097 | |
| 5098 | if (result) { |
| 5099 | // Build a TraversalPath from the result |
| 5100 | let currentPath: TraversalPath<any, any, any> = path; |
| 5101 | |
| 5102 | // Add edges and vertices alternately to the path |
| 5103 | for (let i = 0; i < result.edges.length; i++) { |
| 5104 | currentPath = currentPath.with(result.edges[i]!, []); |
| 5105 | // Add vertex with step labels only if it's the target |
| 5106 | const isTarget = i === result.edges.length - 1; |
| 5107 | currentPath = currentPath.with( |
| 5108 | result.vertices[i + 1]!, |
| 5109 | isTarget && stepLabels ? stepLabels : [], |
| 5110 | ); |
| 5111 | } |
| 5112 | |
| 5113 | this.emitted++; |