Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions editoast/core_task/src/envs/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ where
}| {
if let Some(tx) = ready_trains_tx.as_ref() {
let trains = runner
.input_train_set(&input)
.train_set(&input)
.expect("input provided by the Runner should be valid");
tx.unbounded_send(trains).ok();
}
Expand Down Expand Up @@ -112,7 +112,7 @@ where
data: path,
}| {
let trains = runner
.input_train_set(&input)
.train_set(&input)
.expect("input provided by the Runner should be valid");
Correlated::new(trains, path)
},
Expand Down Expand Up @@ -176,23 +176,23 @@ pub struct PathfindingConsist {
#[cfg_attr(test, derive(Clone))]
pub struct PathfindingConstraints {
/// An ordered list of waypoints the resulting path must pass through
pub path_items: Vec<PathWaypointAlternatives>,
pub path_items: Vec<PathItemAlternatives>,
}

/// A set of [TrackOffset]
///
/// The resulting path can cross any of these.
#[derive(Debug, Hash, PartialEq, Eq)]
#[cfg_attr(test, derive(Clone))]
pub struct PathWaypointAlternatives(Vec<TrackOffset>);
pub struct PathItemAlternatives(Vec<TrackOffset>);

impl FromIterator<TrackOffset> for PathWaypointAlternatives {
impl FromIterator<TrackOffset> for PathItemAlternatives {
fn from_iter<T: IntoIterator<Item = TrackOffset>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}

impl IntoIterator for PathWaypointAlternatives {
impl IntoIterator for PathItemAlternatives {
type Item = TrackOffset;
type IntoIter = <Vec<TrackOffset> as IntoIterator>::IntoIter;

Expand Down Expand Up @@ -230,7 +230,7 @@ impl<Train> PathfindingEnvInputs<Train>
where
Train: TrainKey + 'static,
{
fn iter(&self) -> impl Iterator<Item = Correlated<Train, Input>> {
fn iter(&self) -> impl Iterator<Item = Correlated<Train, PathfindingKey>> {
self.consists.keys().map(|train| {
let pf_key = self
.train_input(train)
Expand All @@ -239,10 +239,10 @@ where
})
}

pub(in crate::envs) fn train_input(&self, train: &Train) -> Option<Input> {
pub(in crate::envs) fn train_input(&self, train: &Train) -> Option<PathfindingKey> {
let consist = self.consists.get(train)?;
let constraints = self.constraints.get(train)?;
Some(Input(consist.clone(), constraints.clone()))
Some(PathfindingKey(consist.clone(), constraints.clone()))
}
}

Expand All @@ -258,11 +258,11 @@ where
{
core_env: CoreEnv,
pub(in crate::envs) pathfinding_inputs: Arc<PathfindingEnvInputs<Train>>,
input_index: DashMap<Input, TrainSet<Train>>,
rev: DashMap<PathfindingKey, TrainSet<Train>>,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(in crate::envs) struct Input(
pub(in crate::envs) struct PathfindingKey(
pub(in crate::envs) Arc<PathfindingConsist>,
pub(in crate::envs) Arc<PathfindingConstraints>,
);
Expand All @@ -272,42 +272,42 @@ where
Train: TrainKey + 'static,
{
pub(in crate::envs) fn new(PathfindingEnv { core_env, inputs }: PathfindingEnv<Train>) -> Self {
let input_index = DashMap::<Input, TrainSet<Train>>::new();
let rev = DashMap::<PathfindingKey, TrainSet<Train>>::new();
for Correlated {
correlation_key: train,
data: input,
} in inputs.iter()
{
input_index.entry(input).or_default().insert(train);
rev.entry(input).or_default().insert(train);
}
Self {
core_env: core_env.clone(),
pathfinding_inputs: Arc::new(inputs),
input_index,
rev,
}
}

pub(in crate::envs) fn input_train_set(&self, input: &Input) -> Option<TrainSet<Train>> {
self.input_index.get(input).as_deref().cloned()
pub(in crate::envs) fn train_set(&self, input: &PathfindingKey) -> Option<TrainSet<Train>> {
self.rev.get(input).as_deref().cloned()
}

fn iter_inputs(&self) -> impl Iterator<Item = Input> {
self.input_index.iter().map(|set| set.key().clone())
fn iter_keys(&self) -> impl Iterator<Item = PathfindingKey> {
self.rev.iter().map(|set| set.key().clone())
}

pub(in crate::envs) fn stream(
self: Arc<Self>,
vkconn: Arc<Mutex<cache::Connection>>,
) -> impl stream::Stream<
Item = Correlated<
Input,
PathfindingKey,
Result<core_client::pathfinding::PathfindingCoreResult, core_client::Error>,
>,
> {
use crate::TaskStreamExt as _;

let requests = self
.iter_inputs()
.iter_keys()
.map(|input| {
let request = build_request(&self.core_env, &input);
Correlated::new(input, request)
Expand All @@ -334,7 +334,7 @@ where
{
inputs: Arc<PathfindingEnvInputs<Train>>,
// optionally deduplicate similar outputs of different inputs
paths: Arc<DashMap<Input, PendingPathfindingResult>>,
paths: Arc<DashMap<PathfindingKey, PendingPathfindingResult>>,
}

/// Ready when the pathfinding result is available and stored in [PathfindingRun], pending if the task is not yet completed
Expand All @@ -348,7 +348,7 @@ where
fn new(runner: &Runner<Train>) -> Self {
let paths = DashMap::from_iter(
runner
.iter_inputs()
.iter_keys()
.zip(std::iter::repeat_with(|| Poll::Pending)),
);
Self {
Expand All @@ -375,15 +375,15 @@ where

fn build_request(
core_env: &CoreEnv,
Input(consist, constraints): &Input,
PathfindingKey(consist, constraints): &PathfindingKey,
) -> core_client::pathfinding::PathfindingRequest {
core_client::pathfinding::PathfindingRequest {
infra: core_env.infra_id as i64,
expected_version: core_env.infra_version,
path_items: constraints
.path_items
.iter()
.map(|PathWaypointAlternatives(track_alternatives)| {
.map(|PathItemAlternatives(track_alternatives)| {
track_alternatives.clone().into_iter().collect()
})
.collect(),
Expand Down Expand Up @@ -442,12 +442,12 @@ pub(crate) mod test_data {
pub(crate) fn constraints(id: usize) -> PathfindingConstraints {
PathfindingConstraints {
path_items: vec![
PathWaypointAlternatives::from_iter([TrackOffset::new("id", id as u64)]),
PathWaypointAlternatives::from_iter([
PathItemAlternatives::from_iter([TrackOffset::new("id", id as u64)]),
PathItemAlternatives::from_iter([
TrackOffset::new("tr1", 100),
TrackOffset::new("tr1bis", 100),
]),
PathWaypointAlternatives::from_iter([TrackOffset::new("tr2", 200)]),
PathItemAlternatives::from_iter([TrackOffset::new("tr2", 200)]),
],
}
}
Expand Down
16 changes: 8 additions & 8 deletions editoast/core_task/src/envs/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ where
pub(in crate::envs) struct SimulationKey(
Arc<SimulationConsist>,
Arc<SimulationTrainParameters>,
pathfinding::Input,
pathfinding::PathfindingKey,
);

impl<Train> Runner<Train>
Expand Down Expand Up @@ -178,10 +178,10 @@ where
/// do not necessarily share the same simulation key (simulation inputs differ).
fn simulation_keys_of_pathfinding_key(
&self,
pathfinding_key: &pathfinding::Input,
pathfinding_key: &pathfinding::PathfindingKey,
) -> impl Iterator<Item = SimulationKey> {
self.pathfinding_runner
.input_train_set(pathfinding_key)
.train_set(pathfinding_key)
.into_iter()
.flatten()
.filter_map(|train| {
Expand Down Expand Up @@ -213,7 +213,7 @@ where
correlation_key: pf_key,
data: Ok(core_client::pathfinding::PathfindingCoreResult::Success(path)),
} => {
let trains = runner.pathfinding_runner.input_train_set(&pf_key).expect("input provided by the runner should be valid");
let trains = runner.pathfinding_runner.train_set(&pf_key).expect("input provided by the runner should be valid");
(trains, path)
}
Correlated {
Expand Down Expand Up @@ -429,12 +429,12 @@ pub(crate) mod test_data {
),
);
builder.push_waypoint(
pathfinding::PathWaypointAlternatives::from_iter([TrackOffset::new("id", id as u64)]),
pathfinding::PathItemAlternatives::from_iter([TrackOffset::new("id", id as u64)]),
NonBlankString::from("start"),
SimulationWaypoint::PathItem,
);
builder.push_waypoint(
pathfinding::PathWaypointAlternatives::from_iter([TrackOffset::new("a", 42)]),
pathfinding::PathItemAlternatives::from_iter([TrackOffset::new("a", 42)]),
NonBlankString::from("a"),
SimulationWaypoint::ScheduleItem {
arrival_at: Some(1200),
Expand All @@ -443,7 +443,7 @@ pub(crate) mod test_data {
},
);
builder.push_waypoint(
pathfinding::PathWaypointAlternatives::from_iter([
pathfinding::PathItemAlternatives::from_iter([
TrackOffset::new("b", 43),
TrackOffset::new("bis", 34),
]),
Expand All @@ -455,7 +455,7 @@ pub(crate) mod test_data {
},
);
builder.push_waypoint(
pathfinding::PathWaypointAlternatives::from_iter([TrackOffset::new("finish", 44)]),
pathfinding::PathItemAlternatives::from_iter([TrackOffset::new("finish", 44)]),
NonBlankString::from("finish"),
SimulationWaypoint::ScheduleItem {
arrival_at: Some(2400),
Expand Down
Loading
Loading