sim/
events.rs

1use serde::{Deserialize, Serialize};
2
3use geom::Duration;
4use map_model::{
5    BuildingID, IntersectionID, LaneID, Map, Path, PathRequest, TransitRouteID, TransitStopID,
6    Traversable, TurnID,
7};
8use synthpop::TripMode;
9
10use crate::{AgentID, CarID, ParkingSpot, PedestrianID, PersonID, Problem, TripID};
11
12/// As a simulation runs, different systems emit Events. This cleanly separates the internal
13/// mechanics of the simulation from consumers that just want to know what's happening.
14///
15/// An Event always occurs at a particular time, plumbed separately to consumers.
16///
17/// Many of these were created for a test framework that's been abandoned. They could be removed or
18/// have their API adjusted, but it's not urgent; publishing an event that's not used by Analytics
19/// has no performance impact.
20#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
21pub enum Event {
22    CarReachedParkingSpot(CarID, ParkingSpot),
23    CarLeftParkingSpot(CarID, ParkingSpot),
24
25    BusArrivedAtStop(CarID, TransitRouteID, TransitStopID),
26    BusDepartedFromStop(CarID, TransitRouteID, TransitStopID),
27    /// How long waiting at the stop?
28    PassengerBoardsTransit(PersonID, CarID, TransitRouteID, TransitStopID, Duration),
29    PassengerAlightsTransit(PersonID, CarID, TransitRouteID, TransitStopID),
30
31    PersonEntersBuilding(PersonID, BuildingID),
32    PersonLeavesBuilding(PersonID, BuildingID),
33    /// None if cancelled
34    PersonLeavesMap(PersonID, Option<AgentID>, IntersectionID),
35    PersonEntersMap(PersonID, AgentID, IntersectionID),
36
37    PedReachedParkingSpot(PedestrianID, ParkingSpot),
38
39    BikeStoppedAtSidewalk(CarID, LaneID),
40
41    ProblemEncountered(TripID, Problem),
42
43    /// If the agent is a transit vehicle, then include a count of how many passengers are on
44    /// board.
45    AgentEntersTraversable(AgentID, Option<TripID>, Traversable, Option<usize>),
46    /// TripID, TurnID (Where the delay was encountered), Time spent waiting at that turn
47    IntersectionDelayMeasured(TripID, TurnID, AgentID, Duration),
48
49    TripFinished {
50        trip: TripID,
51        mode: TripMode,
52        total_time: Duration,
53        blocked_time: Duration,
54    },
55    TripCancelled(TripID, TripMode),
56    TripPhaseStarting(TripID, PersonID, Option<PathRequest>, TripPhaseType),
57
58    /// Just use for parking replanning. Not happy about copying the full path in here, but the way
59    /// to plumb info into Analytics is Event.
60    PathAmended(Path),
61
62    Alert(AlertLocation, String),
63}
64
65#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
66pub enum AlertLocation {
67    Nil,
68    Intersection(IntersectionID),
69    Person(PersonID),
70    Building(BuildingID),
71}
72
73#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
74pub enum TripPhaseType {
75    Driving,
76    Walking,
77    Biking,
78    Parking,
79    WaitingForBus(TransitRouteID, TransitStopID),
80    /// What stop did they board at?
81    RidingBus(TransitRouteID, TransitStopID, CarID),
82    Cancelled,
83    Finished,
84    DelayedStart,
85}
86
87impl TripPhaseType {
88    pub fn describe(self, map: &Map) -> String {
89        match self {
90            TripPhaseType::Driving => "Driving".to_string(),
91            TripPhaseType::Walking => "Walking".to_string(),
92            TripPhaseType::Biking => "Biking".to_string(),
93            TripPhaseType::Parking => "Parking".to_string(),
94            TripPhaseType::WaitingForBus(r, _) => {
95                format!("Waiting for transit route {}", map.get_tr(r).long_name)
96            }
97            TripPhaseType::RidingBus(r, _, _) => {
98                format!("Riding route {}", map.get_tr(r).long_name)
99            }
100            TripPhaseType::Cancelled => "Trip was cancelled due to some bug".to_string(),
101            TripPhaseType::Finished => "Trip finished".to_string(),
102            TripPhaseType::DelayedStart => "Delayed by a previous trip taking too long".to_string(),
103        }
104    }
105}