-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
editoast: explanation of why a simulation is not feasible refactor editoast #9434
Closed
hamz2a
wants to merge
6
commits into
dev
from
hai/editoast-explanation-of-why-a-simulation-is-not-feasible-refactor
+466
−415
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
252aa8d
editoast: return conflicts list when STDCM request fails
hamz2a 32700a0
editoast: move simulation_run_time to SimulationResponse
hamz2a 6cd21b4
editoast: refactor STDCMRequestPayload methods for encapsulation
hamz2a 06565d3
editoast: refactor path not found handling
hamz2a 257f22c
editoast: add STDCMCoreResponse
hamz2a 0f81762
editoast: move map_to_core_work_schedule to WorkSchedule
hamz2a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use std::sync::Arc; | ||
|
||
use chrono::DateTime; | ||
use chrono::Utc; | ||
|
||
use crate::core::conflict_detection::ConflictDetectionRequest; | ||
use crate::core::conflict_detection::WorkSchedulesRequest; | ||
use crate::core::simulation::SimulationResponse; | ||
use crate::core::AsCoreRequest; | ||
use crate::core::CoreClient; | ||
use crate::error::Result; | ||
use crate::models::train_schedule::TrainSchedule; | ||
use crate::models::work_schedules::WorkSchedule; | ||
use crate::views::path::pathfinding::PathfindingResult; | ||
use crate::views::timetable::stdcm::STDCMResponse; | ||
|
||
use super::stdcm::build_train_requirements; | ||
|
||
pub struct PathNotFoundHandler { | ||
pub core_client: Arc<CoreClient>, | ||
pub infra_id: i64, | ||
pub infra_version: String, | ||
pub train_schedules: Vec<TrainSchedule>, | ||
pub simulations: Vec<(SimulationResponse, PathfindingResult)>, | ||
pub work_schedules: Vec<WorkSchedule>, | ||
pub virtual_train_schedule: TrainSchedule, | ||
pub virtual_train_sim_result: SimulationResponse, | ||
pub virtual_train_pathfinding_result: PathfindingResult, | ||
pub earliest_departure_time: DateTime<Utc>, | ||
pub latest_simulation_end: DateTime<Utc>, | ||
} | ||
|
||
impl PathNotFoundHandler { | ||
pub async fn handle(self) -> Result<STDCMResponse> { | ||
let virtual_train_id = self.virtual_train_schedule.id; | ||
|
||
// Combine the original train schedules with the virtual train schedule. | ||
let train_schedules = [self.train_schedules, vec![self.virtual_train_schedule]].concat(); | ||
|
||
// Combine the original simulations with the virtual train's simulation results. | ||
let simulations = [ | ||
self.simulations, | ||
vec![( | ||
self.virtual_train_sim_result, | ||
self.virtual_train_pathfinding_result.clone(), | ||
)], | ||
] | ||
.concat(); | ||
|
||
// Build train requirements based on the combined train schedules and simulations | ||
// This prepares the data structure required for conflict detection. | ||
let trains_requirements = build_train_requirements( | ||
train_schedules, | ||
simulations, | ||
self.earliest_departure_time, | ||
self.latest_simulation_end, | ||
); | ||
|
||
// Filter the provided work schedules to find those that conflict with the given parameters | ||
// This identifies any work schedules that may overlap with the earliest departure time and maximum run time. | ||
let conflict_work_schedules = make_work_schedules_request( | ||
&self.work_schedules, | ||
self.earliest_departure_time, | ||
self.latest_simulation_end, | ||
); | ||
|
||
// Prepare the conflict detection request. | ||
let conflict_detection_request = ConflictDetectionRequest { | ||
infra: self.infra_id, | ||
expected_version: self.infra_version, | ||
trains_requirements, | ||
work_schedules: conflict_work_schedules, | ||
}; | ||
|
||
// Send the conflict detection request and await the response. | ||
let conflict_detection_response = | ||
conflict_detection_request.fetch(&self.core_client).await?; | ||
|
||
// Filter the conflicts to find those specifically related to the virtual train. | ||
let conflicts: Vec<_> = conflict_detection_response | ||
.conflicts | ||
.into_iter() | ||
.filter(|conflict| conflict.train_ids.contains(&virtual_train_id)) | ||
.map(|mut conflict| { | ||
conflict.train_ids.retain(|id| id != &virtual_train_id); | ||
conflict | ||
}) | ||
.collect(); | ||
|
||
// Return the conflicts found along with the pathfinding result for the virtual train. | ||
Ok(STDCMResponse::Conflicts { | ||
pathfinding_result: self.virtual_train_pathfinding_result, | ||
conflicts, | ||
}) | ||
} | ||
} | ||
|
||
fn make_work_schedules_request( | ||
work_schedules: &[WorkSchedule], | ||
start_time: DateTime<Utc>, | ||
latest_simulation_end: DateTime<Utc>, | ||
) -> Option<WorkSchedulesRequest> { | ||
if work_schedules.is_empty() { | ||
return None; | ||
} | ||
let search_window_duration = (latest_simulation_end - start_time).num_milliseconds() as u64; | ||
|
||
let work_schedule_requirements = work_schedules | ||
.iter() | ||
.map(|ws| (ws.id, ws.map_to_core_work_schedule(start_time))) | ||
.filter(|(_, ws)| ws.end_time > 0 && ws.start_time < search_window_duration) | ||
.collect(); | ||
|
||
Some(WorkSchedulesRequest { | ||
start_time, | ||
work_schedule_requirements, | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that I understand the renaming. This
struct
is defined inside a module namecore
so we already know it's relative to core's logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed it to avoid conflicts and
as
, since we have anotherSTDCMResponse
inviews
, and both are used in the same file.