Skip to content

Commit

Permalink
core: directly import infrastructure from RailJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
bougue-pe authored and multun committed Apr 18, 2024
1 parent 40c175f commit c1da19e
Show file tree
Hide file tree
Showing 53 changed files with 3,206 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,46 @@ private fun CollectionItemType.generateArray(context: GeneratorContext, currentF
set(index.toInt(), value)
}
/**
* Array must be sorted.
* If the array contains the value, return its index.
* Otherwise returns -(insertion offset) - 1, where insertion offset is
* where to insert the new value to keep the array sorted.
*/
fun binarySearch(
value: $type,
comparator: ${simpleName}Comparator${paramsUse},
fromIndex: Int,
toIndex: Int,
): Int {
var low = fromIndex
var high = toIndex - 1
while (low <= high) {
val mid = (low + high) ushr 1
val midVal = data[mid]
val cmp = comparator.compare(${storageType.fromPrimitive("midVal")}, value)
if (cmp < 0)
low = mid + 1
else if (cmp > 0)
high = mid - 1
else
return mid
}
return -(low + 1)
}
/**
* Array must be sorted.
* If the array contains the value, return its index.
* Otherwise returns -(insertion offset) - 1, where insertion offset is
* where to insert the new value to keep the array sorted.
*/
fun binarySearch(value: $type, comparator: ${simpleName}Comparator${paramsUse}): Int {
return binarySearch(value, comparator, 0, data.size)
}
/** Creates an iterator over the elements of the array. */
operator fun iterator(): Iterator<$type> {
return object : Iterator<$type> {
Expand Down Expand Up @@ -145,6 +185,33 @@ private fun CollectionItemType.generateArray(context: GeneratorContext, currentF
fun copyOf(size: Int): ${simpleName}Array${paramsUse} {
return ${simpleName}Array${paramsUse}(data.copyOf(size))
}
fun binarySearch(
value: $type,
comparator: ${simpleName}Comparator${paramsUse},
fromIndex: Int,
toIndex: Int,
): Int {
var low = fromIndex
var high = toIndex - 1
while (low <= high) {
val mid = (low + high) ushr 1
val midVal = data[mid]
val cmp = comparator.compare(${storageType.fromPrimitive("midVal")}, value)
if (cmp < 0)
low = mid + 1
else if (cmp > 0)
high = mid - 1
else
return mid
}
return -(low + 1)
}
fun binarySearch(value: $type, comparator: ${simpleName}Comparator${paramsUse}): Int {
return binarySearch(value, comparator, 0, data.size)
}
/** Creates an iterator over the elements of the array. */
operator fun iterator(): Iterator<$type> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ private fun CollectionItemType.generateInterfaces(context: GeneratorContext, cur
val size: Int
}
fun interface ${simpleName}Comparator${paramsDecl} {
fun compare(a: $type, b: $type): Int
}
/** GENERATED CODE */
@Suppress("INAPPLICABLE_JVM_NAME")
interface ${simpleName}List${paramsDecl} : ${simpleName}Collection${paramsUse} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fr.sncf.osrd.signaling
import fr.sncf.osrd.signaling.impl.MockSigSystemManager
import fr.sncf.osrd.signaling.impl.SignalingSimulatorImpl
import fr.sncf.osrd.sim_infra.api.*
import fr.sncf.osrd.sim_infra.impl.RawInfraBuilder
import fr.sncf.osrd.sim_infra.impl.blockInfraBuilder
import fr.sncf.osrd.sim_infra.impl.rawInfraBuilder
import fr.sncf.osrd.utils.indexing.StaticIdx
import fr.sncf.osrd.utils.indexing.StaticIdxList
import fr.sncf.osrd.utils.indexing.mutableStaticIdxArrayListOf
Expand All @@ -27,7 +27,7 @@ class BlockBuilderTest {
// <-- reverse normal -->

// region build the test infrastructure
val builder = RawInfraBuilder()
val builder = rawInfraBuilder()
// region switches
val switch =
builder.movableElement("S", delay = 10L.milliseconds) {
Expand Down Expand Up @@ -60,13 +60,14 @@ class BlockBuilderTest {
// endregion

// region signals
// TODO: add actual track sections
val parameters = RawSignalParameters(mapOf(Pair("jaune_cli", "false")), mapOf())
val signalX =
builder.physicalSignal("X", 300.meters) {
builder.physicalSignal("X", 300.meters, StaticIdx(42u), Offset(42.meters)) {
logicalSignal("BAL", listOf("BAL"), mapOf(Pair("Nf", "true")), parameters)
}
val signalV =
builder.physicalSignal("Y", 300.meters) {
builder.physicalSignal("Y", 300.meters, StaticIdx(42u), Offset(42.meters)) {
logicalSignal("BAL", listOf("BAL"), mapOf(Pair("Nf", "true")), parameters)
}
// endregion
Expand Down
1 change: 1 addition & 0 deletions core/kt-osrd-sim-infra/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
api project(":osrd-railjson")
api project(":osrd-reporting")
implementation libs.kotlin.stdlib
implementation libs.kotlin.logging
testImplementation libs.kotlin.test

// Use JUnit Jupiter API for testing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface LocationInfra : TrackNetworkInfra, TrackInfra, TrackProperties {

fun getPreviousZone(dirDet: DirDetectorId): ZoneId?

fun getDetectorName(det: DetectorId): String?
fun getDetectorName(det: DetectorId): String
}

fun LocationInfra.isBufferStop(detector: StaticIdx<Detector>): Boolean {
Expand Down Expand Up @@ -87,7 +87,7 @@ interface RoutingInfra : ReservationInfra {

fun getRoutePath(route: RouteId): StaticIdxList<ZonePath>

fun getRouteName(route: RouteId): String?
fun getRouteName(route: RouteId): String

fun getRouteLength(route: RouteId): Length<Route>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package fr.sncf.osrd.sim_infra.api

import fr.sncf.osrd.railjson.schema.rollingstock.RJSLoadingGaugeType
import fr.sncf.osrd.utils.indexing.StaticIdx
import fr.sncf.osrd.utils.indexing.StaticIdxSortedSet
import fr.sncf.osrd.utils.indexing.mutableStaticIdxArraySetOf

sealed interface LoadingGaugeType

typealias LoadingGaugeTypeId = StaticIdx<LoadingGaugeType>

interface LoadingGaugeConstraint {
data class LoadingGaugeConstraint(val blockedTypes: StaticIdxSortedSet<LoadingGaugeType>) {
/** Returns true if a train of the given type is compatible */
fun isCompatibleWith(trainType: LoadingGaugeTypeId): Boolean
fun isCompatibleWith(trainType: LoadingGaugeTypeId): Boolean {
return !blockedTypes.contains(trainType)
}
}

fun fromAllowedSet(allowedTypes: Set<RJSLoadingGaugeType>): LoadingGaugeConstraint {
val blockedTypes = mutableStaticIdxArraySetOf<LoadingGaugeType>()
for (gaugeType in RJSLoadingGaugeType.entries) {
if (!allowedTypes.contains(gaugeType)) {
blockedTypes.add(StaticIdx(gaugeType.ordinal.toUInt()))
}
}
return LoadingGaugeConstraint(blockedTypes)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fr.sncf.osrd.sim_infra.api

import fr.sncf.osrd.geom.LineString
import fr.sncf.osrd.sim_infra.impl.ChunkPath
import fr.sncf.osrd.sim_infra.impl.NeutralSection
import fr.sncf.osrd.sim_infra.impl.PathPropertiesImpl
import fr.sncf.osrd.sim_infra.impl.buildChunkPath
import fr.sncf.osrd.utils.DistanceRangeMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fr.sncf.osrd.utils.indexing.StaticIdx
import fr.sncf.osrd.utils.indexing.StaticIdxList
import fr.sncf.osrd.utils.indexing.StaticIdxSpace
import fr.sncf.osrd.utils.units.Distance
import fr.sncf.osrd.utils.units.Offset
import fr.sncf.osrd.utils.units.OffsetList

/** A fixed size signaling block */
Expand Down Expand Up @@ -46,6 +47,11 @@ interface RawSignalingInfra : RoutingInfra {

fun getPhysicalSignal(signal: LogicalSignalId): PhysicalSignalId

fun getPhysicalSignalTrack(signal: PhysicalSignalId): TrackSectionId

/** This offset is undirected */
fun getPhysicalSignalTrackOffset(signal: PhysicalSignalId): Offset<TrackSection>

fun getPhysicalSignalName(signal: PhysicalSignalId): String?

fun getSignalSightDistance(signal: PhysicalSignalId): Distance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ typealias DirTrackChunkId = DirStaticIdx<TrackChunk>

typealias OptDirTrackChunkId = OptDirStaticIdx<TrackChunk>

fun <T> StaticIdx<T>.withDirection(dir: Direction): DirStaticIdx<T> {
return DirStaticIdx(this, dir)
}

val <T> StaticIdx<T>.increasing
get() = DirStaticIdx(this, Direction.INCREASING)
val <T> StaticIdx<T>.decreasing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package fr.sncf.osrd.sim_infra.api

import fr.sncf.osrd.geom.LineString
import fr.sncf.osrd.sim_infra.impl.NeutralSection
import fr.sncf.osrd.utils.DistanceRangeMap
import fr.sncf.osrd.utils.indexing.StaticIdx
import fr.sncf.osrd.utils.indexing.StaticIdxList
import fr.sncf.osrd.utils.units.Length
import fr.sncf.osrd.utils.units.Offset
import fr.sncf.osrd.utils.units.Speed

data class NeutralSection(
val lowerPantograph: Boolean,
val isAnnouncement: Boolean,
)

/**
* An operational point is a special location (such as a station). It has an ID and a set of
* locations (parts). In the current internal infra representation, we only consider the operational
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ fun makeBlock(rawInfra: RawInfra, blockInfra: BlockInfra, id: StaticIdx<Block>):
return BlockViewer(
blockInfra.getBlockPath(id).map { path -> makeZonePath(rawInfra, path) },
id,
makeDirViewer(entry, rawInfra.getDetectorName(entry.value)!!),
makeDirViewer(exit, rawInfra.getDetectorName(exit.value)!!),
makeDirViewer(entry, rawInfra.getDetectorName(entry.value)),
makeDirViewer(exit, rawInfra.getDetectorName(exit.value)),
blockInfra.getBlockLength(id),
)
}
Expand Down
Loading

0 comments on commit c1da19e

Please sign in to comment.