Skip to content

Commit

Permalink
editoast: example using model(uom_unit) for a Length
Browse files Browse the repository at this point in the history
Signed-off-by: Tristram Gräbener <[email protected]>
  • Loading branch information
Tristramg committed Dec 5, 2024
1 parent 590a971 commit 38c7da7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
8 changes: 4 additions & 4 deletions editoast/src/models/rolling_stock_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use power_restrictions::PowerRestriction;
use serde::Deserialize;
use serde::Serialize;
use uom::si::acceleration::meter_per_second_squared;
use uom::si::length::meter;
use uom::si::velocity::meter_per_second;

use uom::si::f64::{Acceleration, Length, Velocity};
Expand Down Expand Up @@ -48,7 +47,8 @@ pub struct RollingStockModel {
#[model(json)]
#[schema(required)]
pub metadata: Option<RollingStockMetadata>,
pub length: f64,
#[model(uom_unit = "uom::si::length::meter")]
pub length: Length,
pub max_speed: f64,
pub startup_time: f64,
pub startup_acceleration: f64,
Expand Down Expand Up @@ -153,7 +153,7 @@ impl From<RollingStockModel> for RollingStock {
name: rolling_stock_model.name,
effort_curves: rolling_stock_model.effort_curves,
base_power_class: rolling_stock_model.base_power_class,
length: Length::new::<meter>(rolling_stock_model.length),
length: rolling_stock_model.length,
max_speed: Velocity::new::<meter_per_second>(rolling_stock_model.max_speed),
startup_time: rolling_stock_model.startup_time,
startup_acceleration: Acceleration::new::<meter_per_second_squared>(
Expand Down Expand Up @@ -185,7 +185,7 @@ impl From<RollingStock> for RollingStockModelChangeset {
.name(rolling_stock.name)
.effort_curves(rolling_stock.effort_curves)
.base_power_class(rolling_stock.base_power_class)
.length(rolling_stock.length.get::<meter>())
.length(rolling_stock.length)
.max_speed(rolling_stock.max_speed.get::<meter_per_second>())
.startup_time(rolling_stock.startup_time)
.startup_acceleration(
Expand Down
3 changes: 2 additions & 1 deletion editoast/src/views/path/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use serde::Deserialize;
use serde::Serialize;
use tracing::debug;
use tracing::info;
use uom::si::length::meter;
use utoipa::ToSchema;

use crate::core::pathfinding::PathfindingCoreResult;
Expand Down Expand Up @@ -391,7 +392,7 @@ pub async fn pathfinding_from_train_batch(
rolling_stock_supported_electrifications: rolling_stock.supported_electrification(),
rolling_stock_supported_signaling_systems: rolling_stock.supported_signaling_systems.0,
rolling_stock_maximum_speed: OrderedFloat(rolling_stock.max_speed),
rolling_stock_length: OrderedFloat(rolling_stock.length),
rolling_stock_length: OrderedFloat(rolling_stock.length.get::<meter>()),
path_items: train_schedule
.path
.clone()
Expand Down
5 changes: 2 additions & 3 deletions editoast/src/views/rolling_stock/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use serde::Deserialize;
use serde::Serialize;
use uom::si::acceleration::meter_per_second_squared;
use uom::si::f64::{Acceleration, Length, Velocity};
use uom::si::length::meter;
use uom::si::velocity::meter_per_second;
use utoipa::ToSchema;
use validator::Validate;
Expand Down Expand Up @@ -65,7 +64,7 @@ impl From<RollingStockForm> for Changeset<RollingStockModel> {
.name(rolling_stock.name)
.effort_curves(rolling_stock.effort_curves)
.base_power_class(rolling_stock.base_power_class)
.length(rolling_stock.length.get::<meter>())
.length(rolling_stock.length)
.max_speed(rolling_stock.max_speed.get::<meter_per_second>())
.startup_time(rolling_stock.startup_time)
.startup_acceleration(
Expand Down Expand Up @@ -109,7 +108,7 @@ impl From<RollingStockModel> for RollingStockForm {
name: value.name,
effort_curves: value.effort_curves,
base_power_class: value.base_power_class,
length: Length::new::<meter>(value.length),
length: value.length,
max_speed: Velocity::new::<meter_per_second>(value.max_speed),
startup_time: value.startup_time,
startup_acceleration: Acceleration::new::<meter_per_second_squared>(
Expand Down
3 changes: 1 addition & 2 deletions editoast/src/views/rolling_stock/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use serde::Serialize;
use std::collections::HashMap;
use uom::si::acceleration::meter_per_second_squared;
use uom::si::f64::{Acceleration, Length, Velocity};
use uom::si::length::meter;
use uom::si::velocity::meter_per_second;
use utoipa::ToSchema;

Expand Down Expand Up @@ -267,7 +266,7 @@ impl From<RollingStockModel> for LightRollingStock {
locked,
effort_curves: effort_curves.into(),
base_power_class,
length: Length::new::<meter>(length),
length,
max_speed: Velocity::new::<meter_per_second>(max_speed),
startup_time,
startup_acceleration: Acceleration::new::<meter_per_second_squared>(
Expand Down
9 changes: 6 additions & 3 deletions editoast/src/views/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ mod tests {
use serde_json::json;
use std::str::FromStr;
use uom::si::acceleration::meter_per_second_squared;
use uom::si::length::meter;
use uom::si::length::millimeter;
use uom::si::f64::{Acceleration, Length, Velocity};
use uom::si::length::{meter, millimeter};
use uom::si::velocity::meter_per_second;
use uuid::Uuid;

Expand Down Expand Up @@ -541,7 +541,10 @@ mod tests {
let physics_consist: PhysicsConsist = simulation_parameters.into();

assert_eq!(physics_consist.mass, 123_u64);
assert_eq!(physics_consist.length, Length::new::<millimeter>(455000.0)); // It should be converted in mm
assert_eq!(
physics_consist.length,
uom::si::f64::Length::new::<millimeter>(455000.0)
); // It should be converted in mm
assert_eq!(
physics_consist.max_speed,
Velocity::new::<meter_per_second>(10_f64)
Expand Down
3 changes: 2 additions & 1 deletion editoast/src/views/train_schedule/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::hash::Hash;
use std::hash::Hasher;
use std::sync::Arc;
use tracing::info;
use uom::si::length::millimeter;
use utoipa::ToSchema;

use super::TrainScheduleError;
Expand Down Expand Up @@ -320,7 +321,7 @@ async fn project_path(
id,
ProjectPathTrainResult {
departure_time: train.start_time,
rolling_stock_length: (length * 1000.) as u64,
rolling_stock_length: length.get::<millimeter>() as u64,
cached,
},
);
Expand Down

0 comments on commit 38c7da7

Please sign in to comment.