Skip to content

Commit c2b8718

Browse files
committed
core: stdcm: simplify stdcm edge builder
Turns out, starting the path with a node instead of an edge can make things much easier than before
1 parent 615f035 commit c2b8718

File tree

2 files changed

+22
-97
lines changed

2 files changed

+22
-97
lines changed

core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/BacktrackingManager.kt

+1-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class BacktrackingManager(private val graph: STDCMGraph) {
3939
// Create the new edge
4040
val newNode = newPreviousEdge.getEdgeEnd(graph)
4141
return STDCMEdgeBuilder.fromNode(graph, newNode, edge.infraExplorer)
42-
.setStartOffset(edge.envelopeStartOffset)
4342
.setEnvelope(envelope)
4443
.findEdgeSameNextOccupancy(edge.timeNextOccupancy)
4544
}
@@ -78,13 +77,8 @@ class BacktrackingManager(private val graph: STDCMGraph) {
7877
graph
7978
)
8079
val prevNode = old.previousNode
81-
return STDCMEdgeBuilder(old.infraExplorer, graph, prevNode)
82-
.setStartTime(old.timeStart - old.addedDelay)
83-
.setStartOffset(old.envelopeStartOffset)
84-
.setPrevMaximumAddedDelay(old.maximumAddedDelayAfter + old.addedDelay)
85-
.setPrevAddedDelay(old.totalDepartureTimeShift - old.addedDelay)
80+
return STDCMEdgeBuilder.fromNode(graph, prevNode, old.infraExplorer)
8681
.setEnvelope(newEnvelope)
87-
.setWaypointIndex(old.waypointIndex)
8882
.findEdgeSameNextOccupancy(old.timeNextOccupancy)
8983
}
9084
}

core/src/main/kotlin/fr/sncf/osrd/stdcm/graph/STDCMEdgeBuilder.kt

+21-90
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import fr.sncf.osrd.utils.units.meters
1515
import kotlin.math.min
1616

1717
/** This class handles the creation of new edges, handling the many optional parameters. */
18+
@ConsistentCopyVisibility
1819
data class STDCMEdgeBuilder
1920
internal constructor(
2021
/** Instance used to explore the infra, contains the underlying edge */
@@ -23,26 +24,8 @@ internal constructor(
2324
private val graph: STDCMGraph,
2425
/** Previous node, used to compute the final path */
2526
private var prevNode: STDCMNode,
26-
/** Start time of the edge */
27-
private var startTime: Double = 0.0,
28-
29-
/** Start speed, ignored if envelope is specified */
30-
private var startSpeed: Double = 0.0,
31-
3227
/** Start offset on the given block */
3328
private var startOffset: Offset<Block> = Offset(0.meters),
34-
35-
/**
36-
* Maximum delay we can add on any of the previous edges by shifting the departure time, without
37-
* causing a conflict
38-
*/
39-
private var prevMaximumAddedDelay: Double = 0.0,
40-
41-
/**
42-
* Sum of all the delay that has been added in the previous edges by shifting the departure time
43-
*/
44-
private var prevAddedDelay: Double = 0.0,
45-
4629
/** Envelope to use on the edge, if unspecified we try to go at maximum allowed speed */
4730
private var envelope: Envelope? = null,
4831

@@ -51,46 +34,7 @@ internal constructor(
5134
* for the resource generator caches. This is the instance that must be used for next edges
5235
*/
5336
private var explorerWithNewEnvelope: InfraExplorerWithEnvelope? = null,
54-
55-
/** Index of the last waypoint passed by the train */
56-
private var waypointIndex: Int = 0
5737
) {
58-
// region SETTERS
59-
/** Sets the start time of the edge */
60-
fun setStartTime(startTime: Double): STDCMEdgeBuilder {
61-
this.startTime = startTime
62-
return this
63-
}
64-
65-
/** Sets the start speed, ignored if the envelope has been specified */
66-
fun setStartSpeed(startSpeed: Double): STDCMEdgeBuilder {
67-
this.startSpeed = startSpeed
68-
return this
69-
}
70-
71-
/** Start offset on the given block */
72-
fun setStartOffset(startOffset: Offset<Block>): STDCMEdgeBuilder {
73-
this.startOffset = startOffset
74-
return this
75-
}
76-
77-
/**
78-
* Sets the maximum delay we can add on any of the previous edges by shifting the departure time
79-
*/
80-
fun setPrevMaximumAddedDelay(prevMaximumAddedDelay: Double): STDCMEdgeBuilder {
81-
this.prevMaximumAddedDelay = prevMaximumAddedDelay
82-
return this
83-
}
84-
85-
/**
86-
* Sets the sum of all the delay that has been added in the previous edges by shifting the
87-
* departure time
88-
*/
89-
fun setPrevAddedDelay(prevAddedDelay: Double): STDCMEdgeBuilder {
90-
this.prevAddedDelay = prevAddedDelay
91-
return this
92-
}
93-
9438
/**
9539
* Sets the envelope to use on the edge, if unspecified we try to go at maximum allowed speed
9640
*/
@@ -99,16 +43,6 @@ internal constructor(
9943
return this
10044
}
10145

102-
/**
103-
* Sets the waypoint index on the new edge (i.e. the index of the last waypoint passed by the
104-
* train)
105-
*/
106-
fun setWaypointIndex(waypointIndex: Int): STDCMEdgeBuilder {
107-
this.waypointIndex = waypointIndex
108-
return this
109-
}
110-
// endregion SETTERS
111-
// region BUILDERS
11246
/**
11347
* Creates all edges that can be accessed on the given block, using all the parameters
11448
* specified.
@@ -143,12 +77,10 @@ internal constructor(
14377
val delay =
14478
getDelaysPerOpening()
14579
.stream()
146-
.filter { x: Double -> startTime + x <= timeNextOccupancy }
80+
.filter { x: Double -> prevNode.time + x <= timeNextOccupancy }
14781
.max { obj: Double, anotherDouble: Double? -> obj.compareTo(anotherDouble!!) }
14882
return delay.map { delayNeeded: Double -> makeSingleEdge(delayNeeded) }.orElse(null)
14983
}
150-
// endregion BUILDERS
151-
// region UTILITIES
15284
/** Returns the envelope to be used for the new edges */
15385
private fun getEnvelope(): Envelope? {
15486
if (envelope == null)
@@ -162,13 +94,13 @@ internal constructor(
16294
infraExplorer,
16395
BlockSimulationParameters(
16496
infraExplorer.getCurrentBlock(),
165-
startSpeed,
97+
prevNode.speed,
16698
startOffset,
16799
getStopOnBlock(
168100
graph,
169101
infraExplorer.getCurrentBlock(),
170102
startOffset,
171-
waypointIndex
103+
prevNode.waypointIndex
172104
)
173105
)
174106
)
@@ -213,7 +145,7 @@ internal constructor(
213145
private fun getDelaysPerOpening(): Set<Double> {
214146
return graph.delayManager.minimumDelaysPerOpening(
215147
getExplorerWithNewEnvelope()!!,
216-
startTime,
148+
prevNode.time,
217149
envelope!!,
218150
startOffset,
219151
)
@@ -222,33 +154,37 @@ internal constructor(
222154
/** Returns the stop duration at the end of the edge being built, or null if there's no stop */
223155
private fun getEndStopDuration(): Double? {
224156
val endAtStop =
225-
getStopOnBlock(graph, infraExplorer.getCurrentBlock(), startOffset, waypointIndex) !=
226-
null
157+
getStopOnBlock(
158+
graph,
159+
infraExplorer.getCurrentBlock(),
160+
startOffset,
161+
prevNode.waypointIndex
162+
) != null
227163
if (!endAtStop) return null
228-
return graph.getFirstStopAfterIndex(waypointIndex)!!.duration!!
164+
return graph.getFirstStopAfterIndex(prevNode.waypointIndex)!!.duration!!
229165
}
230166

231167
/** Creates a single STDCM edge, adding the given amount of delay */
232168
private fun makeSingleEdge(delayNeeded: Double): STDCMEdge? {
233169
if (java.lang.Double.isInfinite(delayNeeded)) return null
234-
val actualStartTime = startTime + delayNeeded
170+
val actualStartTime = prevNode.time + delayNeeded
235171

236172
var maximumDelay = 0.0
237173
var departureTimeShift = delayNeeded
238-
if (delayNeeded > prevMaximumAddedDelay) {
174+
if (delayNeeded > prevNode.maximumAddedDelay) {
239175
// We can't just shift the departure time, we need an engineering allowance
240176
// It's not computed yet, we just check that it's possible
241177
if (!graph.allowanceManager.checkEngineeringAllowance(prevNode, actualStartTime))
242178
return null
243179
// We still need to adapt the delay values
244-
departureTimeShift = prevMaximumAddedDelay
180+
departureTimeShift = prevNode.maximumAddedDelay
245181
} else {
246182
maximumDelay =
247183
min(
248-
prevMaximumAddedDelay - delayNeeded,
184+
prevNode.maximumAddedDelay - delayNeeded,
249185
graph.delayManager.findMaximumAddedDelay(
250186
getExplorerWithNewEnvelope()!!,
251-
startTime + delayNeeded,
187+
prevNode.time + delayNeeded,
252188
startOffset,
253189
envelope!!,
254190
)
@@ -266,14 +202,14 @@ internal constructor(
266202
departureTimeShift,
267203
graph.delayManager.findNextOccupancy(
268204
getExplorerWithNewEnvelope()!!,
269-
startTime + delayNeeded,
205+
prevNode.time + delayNeeded,
270206
startOffset,
271207
envelope!!,
272208
),
273-
prevAddedDelay + departureTimeShift,
209+
prevNode.totalPrevAddedDelay + departureTimeShift,
274210
prevNode,
275211
startOffset,
276-
waypointIndex,
212+
prevNode.waypointIndex,
277213
endAtStop,
278214
envelope!!.beginSpeed,
279215
envelope!!.endSpeed,
@@ -293,7 +229,7 @@ internal constructor(
293229
return true
294230
node = prevEdge.previousNode
295231
}
296-
} // endregion UTILITIES
232+
}
297233

298234
companion object {
299235
fun fromNode(
@@ -305,12 +241,7 @@ internal constructor(
305241
if (node.locationOnEdge != null) {
306242
builder.startOffset = node.locationOnEdge
307243
}
308-
builder.startTime = node.time
309-
builder.startSpeed = node.speed
310-
builder.prevMaximumAddedDelay = node.maximumAddedDelay
311-
builder.prevAddedDelay = node.totalPrevAddedDelay
312244
builder.prevNode = node
313-
builder.waypointIndex = node.waypointIndex
314245
return builder
315246
}
316247
}

0 commit comments

Comments
 (0)