fixing sim serialization after TurnID became a tuple struct

This commit is contained in:
Dustin Carlino 2018-07-29 16:06:26 -07:00
parent cce85994f6
commit af6b7c08ac
3 changed files with 58 additions and 2 deletions

View File

@ -7,6 +7,7 @@ use geom::{Angle, Pt2D};
use intersections::{IntersectionPolicy, StopSign, TrafficSignal};
use map_model::{LaneID, LaneType, Map, TurnID};
use multimap::MultiMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std;
use std::collections::{BTreeMap, HashSet, VecDeque};
use std::f64;
@ -214,10 +215,33 @@ pub struct DrivingSimState {
// Using BTreeMap instead of HashMap so iteration is deterministic.
pub(crate) cars: BTreeMap<CarID, Car>,
lanes: Vec<SimQueue>,
// See https://github.com/serde-rs/json/issues/402.
#[serde(serialize_with = "serialize_turn_map")]
#[serde(deserialize_with = "deserialize_turn_map")]
turns: BTreeMap<TurnID, SimQueue>,
intersections: Vec<IntersectionPolicy>,
}
fn serialize_turn_map<S: Serializer>(
map: &BTreeMap<TurnID, SimQueue>,
s: S,
) -> Result<S::Ok, S::Error> {
map.iter()
.map(|(a, b)| (a.clone(), b.clone()))
.collect::<Vec<(_, _)>>()
.serialize(s)
}
fn deserialize_turn_map<'de, D: Deserializer<'de>>(
d: D,
) -> Result<BTreeMap<TurnID, SimQueue>, D::Error> {
let vec = <Vec<(TurnID, SimQueue)>>::deserialize(d)?;
let mut map = BTreeMap::new();
for (k, v) in vec {
map.insert(k, v);
}
Ok(map)
}
impl DrivingSimState {
pub fn new(map: &Map) -> DrivingSimState {
let mut intersections: Vec<IntersectionPolicy> = Vec::new();

View File

@ -3,6 +3,7 @@ use dimensioned::si;
use draw_ped::DrawPedestrian;
use map_model::{Lane, LaneID, Map, Turn, TurnID};
use multimap::MultiMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std;
use std::collections::VecDeque;
use {On, PedestrianID};
@ -105,11 +106,34 @@ impl Pedestrian {
pub struct WalkingSimState {
// Trying a different style than driving for storing things
peds_per_sidewalk: MultiMap<LaneID, Pedestrian>,
#[serde(serialize_with = "serialize_turn_map")]
#[serde(deserialize_with = "deserialize_turn_map")]
peds_per_turn: MultiMap<TurnID, Pedestrian>,
id_counter: usize,
}
fn serialize_turn_map<S: Serializer>(
map: &MultiMap<TurnID, Pedestrian>,
s: S,
) -> Result<S::Ok, S::Error> {
// TODO maybe need to sort by TurnID to have deterministic output
map.iter()
.map(|(a, b)| (a.clone(), b.clone()))
.collect::<Vec<(_, _)>>()
.serialize(s)
}
fn deserialize_turn_map<'de, D: Deserializer<'de>>(
d: D,
) -> Result<MultiMap<TurnID, Pedestrian>, D::Error> {
let vec = <Vec<(TurnID, Pedestrian)>>::deserialize(d)?;
let mut map = MultiMap::new();
for (k, v) in vec {
map.insert(k, v);
}
Ok(map)
}
impl WalkingSimState {
pub fn new() -> WalkingSimState {
WalkingSimState {

View File

@ -3,6 +3,16 @@ extern crate control;
extern crate map_model;
extern crate sim;
#[test]
fn serialization() {
// This assumes this map has been built
let input = "../data/small.abst";
let map = map_model::Map::new(input, &map_model::Edits::new()).expect("Couldn't load map");
let sim = sim::Sim::new(&map, Some(42));
abstutil::write_json("/tmp/sim_state.json", &sim).unwrap();
}
#[test]
fn from_scratch() {
// This assumes this map has been built
@ -11,8 +21,6 @@ fn from_scratch() {
let spawn_count = 1000;
println!("Creating two simulations");
// TODO bundle all of the layers of the map together in some super-struct, so this
// initialization and plumbing is easier
let map = map_model::Map::new(input, &map_model::Edits::new()).expect("Couldn't load map");
let control_map = control::ControlMap::new(&map);