use crate::behaviour::{BehaviourType};
use crate::agents_types::AgentType;
use crate::grid::cell::CellID;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TripType {
Undefined,
Constant,
Random,
}
impl fmt::Display for TripType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let trip_type_str = match self {
TripType::Undefined => "undefined",
TripType::Constant => "constant",
TripType::Random => "random",
};
write!(f, "{}", trip_type_str)
}
}
pub type TripID = i64;
#[derive(Debug, Clone)]
pub struct Trip {
pub id: TripID,
pub transit_cells: Vec<CellID>,
pub initial_speed: i32,
pub probability: f64,
pub from_node: CellID,
pub to_node: CellID,
pub allowed_agent_type: AgentType,
pub allowed_behaviour_type: BehaviourType,
pub trip_type: TripType,
pub time: i32,
pub start_time: i32,
pub end_time: i32,
pub relax_time: i32,
pub vehicle_tail_size: usize,
pub speed_limit: i32,
}
pub struct TripBuilder {
trip: Trip,
}
impl Trip {
pub fn new(from_node: CellID, to_node: CellID, trip_type: TripType) -> TripBuilder {
TripBuilder {
trip: Trip {
id: 0,
transit_cells: Vec::new(),
initial_speed: 0,
probability: 0.5,
from_node,
to_node,
allowed_agent_type: AgentType::Undefined,
allowed_behaviour_type: BehaviourType::Undefined,
trip_type,
time: -1,
start_time: 0,
end_time: i32::MAX,
relax_time: -1,
vehicle_tail_size: 0,
speed_limit: -1,
},
}
}
}
impl TripBuilder {
pub fn with_id(mut self, id: TripID) -> Self {
self.trip.id = id;
self
}
pub fn with_transits_cells(mut self, cells: Vec<CellID>, t: i32) -> Self {
self.trip.transit_cells = cells;
self.trip.relax_time = t;
self
}
pub fn with_initial_speed(mut self, speed: i32) -> Self {
self.trip.initial_speed = speed;
self
}
pub fn with_allowed_agent_type(mut self, agent_type: AgentType) -> Self {
self.trip.allowed_agent_type = agent_type;
self
}
pub fn with_allowed_behaviour_type(mut self, behaviour_type: BehaviourType) -> Self {
self.trip.allowed_behaviour_type = behaviour_type;
self
}
pub fn with_time(mut self, time: i32) -> Self {
self.trip.time = time;
self
}
pub fn with_probability(mut self, probability: f64) -> Self {
self.trip.probability = probability;
self
}
pub fn with_start_time(mut self, start_time: i32) -> Self {
self.trip.start_time = start_time;
self
}
pub fn with_end_time(mut self, end_time: i32) -> Self {
self.trip.end_time = end_time;
self
}
pub fn with_vehicle_tail_size(mut self, len: usize) -> Self {
self.trip.vehicle_tail_size = len;
self
}
pub fn with_speed_limit(mut self, limit: i32) -> Self {
self.trip.speed_limit = limit;
self
}
pub fn build(mut self) -> Trip {
if self.trip.vehicle_tail_size == 0 {
self.trip.vehicle_tail_size = self.trip.allowed_agent_type.tail_size_default();
}
self.trip
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tail_size_auto_resolution() {
let car_trip = Trip::new(1, 10, TripType::Constant)
.with_allowed_agent_type(AgentType::Car)
.build();
assert_eq!(car_trip.vehicle_tail_size, 0);
let bus_trip = Trip::new(1, 10, TripType::Constant)
.with_allowed_agent_type(AgentType::Bus)
.build();
assert_eq!(bus_trip.vehicle_tail_size, 1);
let truck_trip = Trip::new(1, 10, TripType::Constant)
.with_allowed_agent_type(AgentType::Truck)
.build();
assert_eq!(truck_trip.vehicle_tail_size, 1);
let large_bus_trip = Trip::new(1, 10, TripType::Constant)
.with_allowed_agent_type(AgentType::LargeBus)
.build();
assert_eq!(large_bus_trip.vehicle_tail_size, 2);
}
#[test]
fn test_tail_size_explicit_override() {
let trip = Trip::new(1, 10, TripType::Constant)
.with_allowed_agent_type(AgentType::Car)
.with_vehicle_tail_size(5)
.build();
assert_eq!(trip.vehicle_tail_size, 5);
}
#[test]
fn test_speed_limit_default() {
let trip = Trip::new(1, 10, TripType::Constant).build();
assert_eq!(trip.speed_limit, -1);
}
#[test]
fn test_speed_limit_explicit() {
let trip = Trip::new(1, 10, TripType::Constant)
.with_speed_limit(5)
.build();
assert_eq!(trip.speed_limit, 5);
}
#[test]
fn test_speed_limit_with_behaviour_first() {
let trip = Trip::new(1, 10, TripType::Constant)
.with_allowed_behaviour_type(BehaviourType::Aggressive)
.with_speed_limit(3)
.build();
assert_eq!(trip.speed_limit, 3);
}
#[test]
fn test_speed_limit_before_behaviour() {
let trip = Trip::new(1, 10, TripType::Constant)
.with_speed_limit(3)
.with_allowed_behaviour_type(BehaviourType::Aggressive)
.build();
assert_eq!(trip.speed_limit, 3);
}
}