Skip to content

Commit 0628291

Browse files
committed
Added doc comments.
1 parent 435fd57 commit 0628291

File tree

10 files changed

+73
-12
lines changed

10 files changed

+73
-12
lines changed

example_apps/transit/src/agents/agency.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,24 @@ use self::statistics::Statistics;
3737

3838
mod statistics;
3939

40+
/// Agency representing a transit agency (and its current state).
4041
#[derive(AgentLaneModel)]
4142
#[projections]
4243
#[agent(transient, convention = "camel")]
4344
pub struct AgencyAgent {
45+
// Vehicles currently associated with the agency (keys are the IDs used by the service.).
4446
vehicles: MapLane<String, Vehicle>,
47+
// Total count of vehicles associated with the agency.
4548
count: ValueLane<usize>,
49+
// Average current speed of vehicles associated with the agency.
4650
speed: ValueLane<f64>,
51+
// Update the vehicles associated with the agency.
4752
add_vehicles: CommandLane<Vec<VehicleResponse>>,
53+
// Exposes the agency metadata.
4854
info: DemandLane<Agency>,
55+
// Routes in the agency (keys are the IDs used by the service).
4956
routes: MapLane<String, Route>,
57+
// Smallest geographical bounding box containing all vehicles associated with the agency.
5058
bounding_box: ValueLane<BoundingBox>,
5159
}
5260

@@ -62,12 +70,16 @@ impl AgencyLifecycle {
6270
#[on_start]
6371
fn init(&self, context: HandlerContext<AgencyAgent>) -> impl EventHandler<AgencyAgent> + '_ {
6472
let this = self.clone();
73+
74+
//Fetch the titles of the routes for the agency and start polling for updates to its vehicles.
6575
let get_routes_and_poll = async move {
6676
let on_routes = this.clone().load_routes(context).await;
6777
let start_polling = this.start_polling(context);
6878
on_routes.followed_by(start_polling)
6979
};
7080
let state_uri = self.agency.state_uri();
81+
82+
//Associate this agency with the state that contains it.
7183
let add_to_state = context.send_command(
7284
None,
7385
state_uri,
@@ -111,6 +123,7 @@ impl AgencyLifecycle {
111123
vehicles: &[VehicleResponse],
112124
) -> impl EventHandler<AgencyAgent> + '_ {
113125
let responses = vehicles.to_vec();
126+
//Compute statistics for the new list of vehicles and update the state of all of the lanes.
114127
let stats = vehicles
115128
.iter()
116129
.fold(Statistics::default(), |s, v| s.update(v));

example_apps/transit/src/agents/country.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@ use crate::model::{agency::Agency, counts::Count};
2929

3030
use super::join_value_logging_lifecycle;
3131

32+
/// An agent with country level aggregate information.
3233
#[derive(AgentLaneModel)]
3334
#[projections]
3435
#[agent(transient, convention = "camel")]
3536
pub struct CountryAgent {
37+
// Count of the number of vehicles within the country.
3638
count: ValueLane<Count>,
39+
// Agencies in the country (keyed by service ID).
3740
agencies: MapLane<String, Agency>,
41+
// The states within the country.
3842
states: MapLane<String, ()>,
43+
// Counts of the number of vehicles within each state in the country.
3944
state_count: JoinValueLane<String, Count>,
45+
// Average speed of all vehicles in the country.
4046
speed: ValueLane<f64>,
47+
// Average speed of vehicles in each state in the country.
4148
state_speed: JoinValueLane<String, f64>,
49+
// Add a new agency to the country.
4250
add_agency: CommandLane<Agency>,
4351
}
4452

example_apps/transit/src/agents/state.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ use crate::model::{agency::Agency, counts::Count};
2626

2727
use super::join_value_logging_lifecycle;
2828

29+
/// An agent with state level aggregate information.
2930
#[derive(AgentLaneModel)]
3031
#[projections]
3132
#[agent(transient, convention = "camel")]
3233
pub struct StateAgent {
34+
// Count of the number of vehicles within the state.
3335
count: ValueLane<Count>,
36+
// Counts of the number of vehicles within each agency in the state.
3437
agency_count: JoinValueLane<Agency, usize>,
38+
// Average speed of vehicles within the state.
3539
speed: ValueLane<f64>,
40+
// Average speeds of vehicles within each agency in the state.
3641
agency_speed: JoinValueLane<Agency, f64>,
42+
// Add an agency to the state.
3743
add_agency: CommandLane<Agency>,
3844
}
3945

example_apps/transit/src/agents/vehicle.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ use tracing::{debug, info};
2828

2929
use crate::model::vehicle::Vehicle;
3030

31+
/// A agent representing the current state of a vehicle.
3132
#[derive(AgentLaneModel)]
3233
#[projections]
3334
#[agent(transient, convention = "camel")]
3435
pub struct VehicleAgent {
36+
// Description of the vehicle.
3537
vehicle: ValueLane<Option<Vehicle>>,
38+
// Speed history of the vehicle (keyed by arbitrary epoch milliseconds).
3639
speeds: MapLane<u64, u32>,
40+
// Acceleration history of the vehicle (keyed by arbitrary epoch milliseconds).
3741
accelerations: MapLane<u64, u32>,
42+
// Set the descriptor of the vehicle.
3843
add_vehicle: CommandLane<Vehicle>,
3944
}
4045

example_apps/transit/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub fn create_plane(
7878
Ok(builder)
7979
}
8080

81+
/// Starts the agents for each supported agency and then waits for the server to stop.
8182
pub async fn start_agencies_and_wait(
8283
agency_uris: Vec<RouteUri>,
8384
handle: ServerHandle,

example_apps/transit/src/ui.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ impl UiConfig {
3434
}
3535
}
3636

37+
/// Trivial web server to present the app UI.
38+
///
39+
/// #Arguments
40+
/// * `port` - The port that the swim server is listening on.
3741
pub fn ui_server_router(port: u16) -> Router {
3842
Router::new()
3943
.route("/index.html", get(index_html))

example_apps/transit/transit-model/src/model/agency.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use super::{
2323
vehicle::{Vehicle, VehicleResponse},
2424
};
2525

26+
/// Representation of a transport agency. An agency is contained within a state, within a country. It will have
27+
/// some number of vehicles associated with it on some number of routes.
2628
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Form, Hash)]
2729
#[form(tag = "agency")]
2830
pub struct Agency {
@@ -38,7 +40,7 @@ fn enc(s: &str) -> PercentEncode<'_> {
3840
}
3941

4042
impl Agency {
41-
43+
/// The Swim node URI of the agent that will represent this agency.
4244
pub fn uri(&self) -> String {
4345
format!(
4446
"/agency/{}/{}/{}",
@@ -48,14 +50,18 @@ impl Agency {
4850
)
4951
}
5052

53+
/// The Swim node URI of the country that contains this agency.
5154
pub fn country_uri(&self) -> String {
5255
format!("/country/{}", enc(&self.country))
5356
}
5457

58+
/// The Swim node URI of the state that contains this agency.
5559
pub fn state_uri(&self) -> String {
5660
format!("/state/{}/{}", enc(&self.country), enc(&self.state))
5761
}
5862

63+
/// Create a new vehicle entity from the response returned by the web service (incorporating information
64+
/// from the route of the vehicle.)
5965
pub fn create_vehicle(&self, route: &Route, response: VehicleResponse) -> Vehicle {
6066
let VehicleResponse {
6167
id,

example_apps/transit/transit-model/src/model/bounding_box.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::fmt::Display;
1616

1717
use swim::form::Form;
1818

19+
/// A "square" geographical region described by intervals of latitude and longitude.
1920
#[derive(Clone, Copy, Debug, Default, PartialEq, Form)]
2021
#[form(tag = "bounds", fields_convention = "camel")]
2122
pub struct BoundingBox {

example_apps/transit/transit-model/src/model/route.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,8 @@ use quick_xml::DeError;
1818
use serde::{Deserialize, Serialize};
1919
use swim::form::Form;
2020

21-
#[derive(Deserialize, Serialize)]
22-
#[serde(rename = "body")]
23-
struct Body {
24-
#[serde(rename = "@copyright")]
25-
copyright: String,
26-
#[serde(default)]
27-
route: Vec<Route>,
28-
}
29-
21+
/// A transit agency has some number of routes upon which are some number of vehicles. This type
22+
/// defines the mapping from the ID of a route to its descriptive title.
3023
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Form)]
3124
#[form(tag = "route")]
3225
pub struct Route {
@@ -36,6 +29,16 @@ pub struct Route {
3629
pub title: String,
3730
}
3831

32+
#[derive(Deserialize, Serialize)]
33+
#[serde(rename = "body")]
34+
struct Body {
35+
#[serde(rename = "@copyright")]
36+
copyright: String,
37+
#[serde(default)]
38+
route: Vec<Route>,
39+
}
40+
41+
/// Parse the routes for an agency from the XML returned by the corresponding service endpoint.
3942
pub fn load_xml_routes<R: BufRead>(read: R) -> Result<Vec<Route>, DeError> {
4043
quick_xml::de::from_reader::<R, Body>(read).map(|body| body.route)
4144
}
@@ -70,5 +73,4 @@ mod tests {
7073
let result = load_xml_routes(ROUTES_EXAMPLE).expect("Loading routes failed.");
7174
assert_eq!(result, expected);
7275
}
73-
7476
}

example_apps/transit/transit-model/src/model/vehicle.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,37 @@ struct Body {
4545
last_time: Option<LastTime>,
4646
}
4747

48+
/// The current state of a vehicle as reported by the web service.
4849
#[derive(Debug, Clone, PartialEq, Form)]
4950
#[form(tag = "vehicle", fields_convention = "camel")]
5051
pub struct Vehicle {
52+
/// Unique service identifier for the vehicle.
5153
pub id: String,
54+
/// The ID of the agency to which the vehicle belongs.
5255
pub agency: String,
56+
/// The Swim node URI of the agent representing the vehicle.
5357
pub uri: String,
58+
/// The service identifier of the route which the vehicle is on.
5459
pub route_tag: String,
60+
/// ID defining the direction of the vehicle on its route.
5561
pub dir_id: String,
62+
/// Last reported latitude of the vehicle.
5663
pub latitude: f64,
64+
/// Last reported longitude of the vehicle.
5765
pub longitude: f64,
66+
/// Last reported speed of the vehicle.
5867
pub speed: u32,
68+
/// Number of seconds since the vehicle reported, relative to the timestamp of the response.
5969
pub secs_since_report: u32,
70+
/// Last reported heading of the vehicle.
6071
pub heading: Heading,
72+
/// Whether the future state of the vehicle can be predicted.
6173
pub predictable: bool,
74+
/// Descriptive title of the route which the vehicle is on.
6275
pub route_title: String,
6376
}
6477

78+
/// Representation of the type that is returned by the vehicles endpoint of the web servive.
6579
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Form)]
6680
pub struct VehicleResponse {
6781
#[serde(rename = "@id")]
@@ -91,6 +105,7 @@ pub struct LastTime {
91105
pub time: u64,
92106
}
93107

108+
/// Parse the XML returned by the vehicles endpoint of the web service.
94109
pub fn load_xml_vehicles<R: BufRead>(
95110
read: R,
96111
) -> Result<(Vec<VehicleResponse>, Option<u64>), DeError> {
@@ -101,6 +116,7 @@ pub fn load_xml_vehicles<R: BufRead>(
101116
)
102117
}
103118

119+
/// Enumeration of cardinal and ordinal headings.
104120
#[derive(Debug, Clone, Copy, PartialEq, Eq, Tag)]
105121
pub enum Heading {
106122
N,
@@ -290,5 +306,4 @@ mod tests {
290306
assert_eq!(vehicles, expected);
291307
assert_eq!(time, Some(TIMESTAMP));
292308
}
293-
294309
}

0 commit comments

Comments
 (0)