Skip to content

Commit

Permalink
core: change interface for pathfinding errors on rolling-stock constr…
Browse files Browse the repository at this point in the history
…aints

TODO:
* build real response
* tests
  • Loading branch information
bougue-pe committed Jul 5, 2024
1 parent 839f7ef commit 0a2d394
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import fr.sncf.osrd.api.api_v2.TrackRange
import fr.sncf.osrd.conflicts.TravelledPath
import fr.sncf.osrd.graph.Pathfinding.Range
import fr.sncf.osrd.reporting.exceptions.OSRDError
import fr.sncf.osrd.sim_infra.api.Path
import fr.sncf.osrd.utils.json.UnitAdapterFactory
Expand Down Expand Up @@ -39,41 +41,21 @@ class NotFoundInRoutes(

class NotFoundInTracks : PathfindingBlockResponse

open class IncompatibleConstraint(
val blocks: List<String>,
val routes: List<String>,
@Json(name = "track_section_ranges") val trackSectionRanges: List<TrackRange>,
val length: Length<Path>,
@Json(name = "incompatible_ranges")
val incompatibleRanges: List<List<Offset<Path>>>, // List of pairs
class IncompatibleConstraintsPathResponse(
@Json(name = "relaxed_constraints_path") val relaxedConstraintsPath: PathfindingBlockSuccess,
val constraints: IncompatibleConstraints
) : PathfindingBlockResponse

class IncompatibleElectrification(
blocks: List<String>,
routes: List<String>,
@Json(name = "track_section_ranges") trackSectionRanges: List<TrackRange>,
length: Length<Path>,
@Json(name = "incompatible_ranges")
incompatibleRanges: List<List<Offset<Path>>> // List of pairs
) : IncompatibleConstraint(blocks, routes, trackSectionRanges, length, incompatibleRanges)

class IncompatibleLoadingGauge(
blocks: List<String>,
routes: List<String>,
@Json(name = "track_section_ranges") trackSectionRanges: List<TrackRange>,
length: Length<Path>,
@Json(name = "incompatible_ranges")
incompatibleRanges: List<List<Offset<Path>>> // List of pairs
) : IncompatibleConstraint(blocks, routes, trackSectionRanges, length, incompatibleRanges)
class IncompatibleConstraints(
@Json(name = "incompatible_electrification_ranges")
val incompatibleElectrificationRanges: List<RangeValue<String>>,
@Json(name = "incompatible_gauge_ranges")
val incompatibleGaugeRanges: List<Range<TravelledPath>>,
@Json(name = "incompatible_signaling_system_ranges")
val incompatibleSignalingSystemRanges: List<RangeValue<String>>
)

class IncompatibleSignalingSystem(
blocks: List<String>,
routes: List<String>,
@Json(name = "track_section_ranges") trackSectionRanges: List<TrackRange>,
length: Length<Path>,
@Json(name = "incompatible_ranges")
incompatibleRanges: List<List<Offset<Path>>> // List of pairs
) : IncompatibleConstraint(blocks, routes, trackSectionRanges, length, incompatibleRanges)
class RangeValue<T>(val range: Range<TravelledPath>, val value: T)

class PathfindingFailed(
@Json(name = "core_error") val coreError: OSRDError,
Expand All @@ -87,9 +69,7 @@ val polymorphicPathfindingResponseAdapter: PolymorphicJsonAdapterFactory<Pathfin
.withSubtype(NotFoundInBlocks::class.java, "not_found_in_blocks")
.withSubtype(NotFoundInRoutes::class.java, "not_found_in_routes")
.withSubtype(NotFoundInTracks::class.java, "not_found_in_tracks")
.withSubtype(IncompatibleElectrification::class.java, "incompatible_electrification")
.withSubtype(IncompatibleLoadingGauge::class.java, "incompatible_loading_gauge")
.withSubtype(IncompatibleSignalingSystem::class.java, "incompatible_signaling_system")
.withSubtype(IncompatibleConstraintsPathResponse::class.java, "incompatible_constraints")
.withSubtype(NotEnoughPathItems::class.java, "not_enough_path_items")
.withSubtype(PathfindingFailed::class.java, "pathfinding_failed")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import fr.sncf.osrd.utils.indexing.*
import fr.sncf.osrd.utils.units.Length
import fr.sncf.osrd.utils.units.Offset
import fr.sncf.osrd.utils.units.meters
import java.util.*
import org.takes.Request
import org.takes.Response
import org.takes.Take
Expand All @@ -26,6 +25,7 @@ import org.takes.rs.RsJson
import org.takes.rs.RsText
import org.takes.rs.RsWithBody
import org.takes.rs.RsWithStatus
import java.util.*

/**
* Exception used to wrap the response when we can't find a path. We do want to interrupt the
Expand Down Expand Up @@ -155,37 +155,53 @@ private fun computePaths(
.runPathfinding(waypoints)
if (res == null) {
// This way of handling it is suboptimal, but it should be reworked soon
val relaxedPathResponse =
runPathfindingPostProcessing(infra, possiblePathWithoutErrorNoConstraints)

when (currentConstraint::class.java) {
ElectrificationConstraints::class.java -> {
throw NoPathFoundException(
IncompatibleElectrification(
listOf(),
listOf(),
listOf(),
Length(0.meters),
listOf(),
IncompatibleConstraintsPathResponse(
relaxedPathResponse,
IncompatibleConstraints(
listOf(
RangeValue(
Pathfinding.Range(Offset.zero(), Offset.zero()),
"elec"
)
),
listOf(),
listOf()
)
)
)
}
LoadingGaugeConstraints::class.java -> {
throw NoPathFoundException(
IncompatibleLoadingGauge(
listOf(),
listOf(),
listOf(),
Length(0.meters),
listOf(),
IncompatibleConstraintsPathResponse(
relaxedPathResponse,
IncompatibleConstraints(
listOf(),
listOf(Pathfinding.Range(Offset.zero(), Offset.zero())),
listOf()
)
)
)
}
SignalingSystemConstraints::class.java -> {
throw NoPathFoundException(
IncompatibleSignalingSystem(
listOf(),
listOf(),
listOf(),
Length(0.meters),
listOf(),
IncompatibleConstraintsPathResponse(
relaxedPathResponse,
IncompatibleConstraints(
listOf(),
listOf(),
listOf(
RangeValue(
Pathfinding.Range(Offset.zero(), Offset.zero()),
"signal"
)
)
)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import fr.sncf.osrd.utils.units.sumDistances
fun runPathfindingPostProcessing(
infra: FullInfra,
rawPath: PathfindingResultId<Block>
): PathfindingBlockResponse {
): PathfindingBlockSuccess {
// We reuse some of the old function of pathfindingResultConverter,
// there will be some cleanup to be made when the old version is removed
val oldRoutePath = makeRoutePath(infra.blockInfra, infra.rawInfra, rawPath.ranges)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package fr.sncf.osrd.pathfinding
import fr.sncf.osrd.api.ApiTest
import fr.sncf.osrd.api.api_v2.TrackLocation
import fr.sncf.osrd.api.api_v2.TrackRange
import fr.sncf.osrd.api.api_v2.pathfinding.*
import fr.sncf.osrd.api.api_v2.pathfinding.IncompatibleConstraintsPathResponse
import fr.sncf.osrd.api.api_v2.pathfinding.PathfindingBlockRequest
import fr.sncf.osrd.api.api_v2.pathfinding.PathfindingBlockSuccess
import fr.sncf.osrd.api.api_v2.pathfinding.PathfindingBlocksEndpointV2
import fr.sncf.osrd.api.api_v2.pathfinding.pathfindingRequestAdapter
import fr.sncf.osrd.api.api_v2.pathfinding.pathfindingResponseAdapter
import fr.sncf.osrd.railjson.schema.common.graph.EdgeDirection
import fr.sncf.osrd.railjson.schema.rollingstock.RJSLoadingGaugeType
import fr.sncf.osrd.utils.takes.TakesUtils
Expand Down Expand Up @@ -96,6 +101,7 @@ class PathfindingV2Test : ApiTest() {
.act(RqFake("POST", "/v2/pathfinding/blocks", requestBody))
val response = TakesUtils.readBodyResponse(rawResponse)
val parsed =
(pathfindingResponseAdapter.fromJson(response) as? IncompatibleElectrification)!!
(pathfindingResponseAdapter.fromJson(response)
as? IncompatibleConstraintsPathResponse)!!
}
}

0 comments on commit 0a2d394

Please sign in to comment.