Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 129 additions & 57 deletions rust_qsim/src/simulation/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,64 @@ impl Config {
}
}

pub fn proto_files(&self) -> ProtoFiles {
if let Some(proto_files) = self.module::<ProtoFiles>("protofiles") {
proto_files
pub fn network(&self) -> Network {
if let Some(network) = self.module::<Network>("network") {
network
} else {
panic!("Protofiles were not set.")
panic!("Network was not set.")
}
}

pub fn set_proto_files(&mut self, proto_files: ProtoFiles) {
pub fn set_network(&mut self, network: Network) {
self.modules
.lock()
.unwrap()
.insert("protofiles".to_string(), Box::new(proto_files));
.insert("network".to_string(), Box::new(network));
}

pub fn population(&self) -> Population {
if let Some(population) = self.module::<Population>("population") {
population
} else {
panic!("Population was not set.")
}
}

pub fn set_population(&mut self, population: Population) {
self.modules
.lock()
.unwrap()
.insert("population".to_string(), Box::new(population));
}

pub fn vehicles(&self) -> Vehicles {
if let Some(vehicles) = self.module::<Vehicles>("vehicles") {
vehicles
} else {
panic!("Vehicles was not set.")
}
}

pub fn set_vehicles(&mut self, vehicles: Vehicles) {
self.modules
.lock()
.unwrap()
.insert("vehicles".to_string(), Box::new(vehicles));
}

pub fn ids(&self) -> Ids {
if let Some(ids) = self.module::<Ids>("ids") {
ids
} else {
panic!("Ids was not set.")
}
}

pub fn set_ids(&mut self, ids: Ids) {
self.modules
.lock()
.unwrap()
.insert("ids".to_string(), Box::new(ids));
}

pub fn partitioning(&self) -> Partitioning {
Expand Down Expand Up @@ -306,35 +351,47 @@ pub fn write_config(config: &Config, output_path: PathBuf) {
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ProtoFiles {
pub network: PathBuf,
pub population: PathBuf,
pub vehicles: PathBuf,
pub ids: PathBuf,
pub struct Network {
pub path: PathBuf,
}

register_override!("protofiles.network", |config, value| {
let mut proto_files = config.proto_files();
proto_files.network = PathBuf::from(value);
config.set_proto_files(proto_files);
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Population {
pub path: PathBuf,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Vehicles {
pub path: PathBuf,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Ids {
pub path: PathBuf,
}

register_override!("network.path", |config, value| {
let mut network = config.network();
network.path = PathBuf::from(value);
config.set_network(network);
});

register_override!("protofiles.population", |config, value| {
let mut proto_files = config.proto_files();
proto_files.population = PathBuf::from(value);
config.set_proto_files(proto_files);
register_override!("population.path", |config, value| {
let mut population = config.population();
population.path = PathBuf::from(value);
config.set_population(population);
});

register_override!("protofiles.vehicles", |config, value| {
let mut proto_files = config.proto_files();
proto_files.vehicles = PathBuf::from(value);
config.set_proto_files(proto_files);
register_override!("vehicles.path", |config, value| {
let mut vehicles = config.vehicles();
vehicles.path = PathBuf::from(value);
config.set_vehicles(vehicles);
});

register_override!("protofiles.ids", |config, value| {
let mut proto_files = config.proto_files();
proto_files.ids = PathBuf::from(value);
config.set_proto_files(proto_files);
register_override!("ids.path", |config, value| {
let mut ids = config.ids();
ids.path = PathBuf::from(value);
config.set_ids(ids);
});

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -485,7 +542,28 @@ pub trait ConfigModule: Debug + Send + DynClone {
}

#[typetag::serde]
impl ConfigModule for ProtoFiles {
impl ConfigModule for Network {
fn as_any(&self) -> &dyn Any {
self
}
}

#[typetag::serde]
impl ConfigModule for Population {
fn as_any(&self) -> &dyn Any {
self
}
}

#[typetag::serde]
impl ConfigModule for Vehicles {
fn as_any(&self) -> &dyn Any {
self
}
}

#[typetag::serde]
impl ConfigModule for Ids {
fn as_any(&self) -> &dyn Any {
self
}
Expand Down Expand Up @@ -725,13 +803,13 @@ mod tests {
use crate::simulation::config::Output;
use crate::simulation::config::PathBuf;
use crate::simulation::config::Profiling;
use crate::simulation::config::ProtoFiles;
use crate::simulation::config::WriteEvents;
use crate::simulation::config::{
parse_key_val, CommandLineArgs, ComputationalSetup, Config, Drt, DrtProcessType,
DrtService, EdgeWeight, MetisOptions, PartitionMethod, Partitioning, Simulation,
VertexWeight,
};
use crate::simulation::config::{Ids, Network, Population, Vehicles};
use crate::simulation::config::{Logging, RoutingMode};
use std::io::Write;
use tempfile::NamedTempFile;
Expand Down Expand Up @@ -935,39 +1013,33 @@ mod tests {
}

#[test]
fn test_override_protofiles_population() {
fn test_override_population_path() {
let yaml = r#"
modules:
protofiles:
type: ProtoFiles
network: net
population: pop
vehicles: veh
ids: ids
population:
type: Population
path: pop
output:
type: Output
output_dir: out
"#;

let file = write_temp_config(yaml);

let args = CommandLineArgs {
config_path: file.path().to_str().unwrap().to_string(),
overrides: vec![("protofiles.population".to_string(), "new_pop".to_string())],
overrides: vec![("population.path".to_string(), "new_pop".to_string())],
};

let config = Config::from(args);
assert_eq!(config.proto_files().population.to_str().unwrap(), "new_pop");
assert_eq!(config.proto_files().network.to_str().unwrap(), "net");

assert_eq!(config.population().path.to_str().unwrap(), "new_pop");
}

#[test]
fn test_override_output_dir() {
let yaml = r#"
modules:
protofiles:
type: ProtoFiles
network: net
population: pop
vehicles: veh
ids: ids
output:
type: Output
output_dir: out
Expand Down Expand Up @@ -1004,29 +1076,29 @@ modules:

#[test]
fn test_parse_key_val_valid() {
let input = "protofiles.population=some_path";
let input = "population.path=some_path";
let parsed = parse_key_val(input);
assert_eq!(
parsed,
Ok(("protofiles.population".to_string(), "some_path".to_string()))
Ok(("population.path".to_string(), "some_path".to_string()))
);
}

#[test]
fn test_parse_key_val_invalid() {
let input = "protofiles.population_some_path";
let input = "population.path_some_path";
let parsed = parse_key_val(input);
assert!(parsed.is_err());
}

fn base_config() -> Config {
let mut config = Config::default();
config.set_proto_files(ProtoFiles {
network: "net".into(),
population: "pop".into(),
vehicles: "veh".into(),
ids: "ids".into(),
});

config.set_network(Network { path: "net".into() });
config.set_population(Population { path: "pop".into() });
config.set_vehicles(Vehicles { path: "veh".into() });
config.set_ids(Ids { path: "ids".into() });

config.set_output(Output {
output_dir: "out".into(),
profiling: Profiling::None,
Expand All @@ -1044,10 +1116,10 @@ modules:
}

#[test]
fn override_protofiles_network() {
fn override_network_path() {
let mut config = base_config();
config.apply_overrides(&[("protofiles.network".to_string(), "new_net".to_string())]);
assert_eq!(config.proto_files().network, PathBuf::from("new_net"));
config.apply_overrides(&[("network.path".to_string(), "new_net".to_string())]);
assert_eq!(config.network().path, PathBuf::from("new_net"));
}

#[test]
Expand Down
13 changes: 11 additions & 2 deletions rust_qsim/src/simulation/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ pub fn try_join(mut handles: IntMap<u32, JoinHandle<()>>, adapters: Vec<AdapterH
}
for i in finished {
let handle = handles.remove(&i).unwrap();
let name = handle.thread().name().unwrap().to_string();
let name = handle
.thread()
.name()
.unwrap_or("unnamed_thread")
.to_string();
handle
.join()
.unwrap_or_else(|_| panic!("Error in adapter thread {:?}", name));
Expand All @@ -216,7 +220,12 @@ pub fn try_join(mut handles: IntMap<u32, JoinHandle<()>>, adapters: Vec<AdapterH
// When all simulation threads are finished, we shutdown the adapters.
for a in adapters {
a.shutdown_sender.send(true).unwrap();
let name = a.handle.thread().name().unwrap().to_string();
let name = a
.handle
.thread()
.name()
.unwrap_or("unnamed_thread")
.to_string();
a.handle
.join()
.unwrap_or_else(|_| panic!("Error in adapter thread {:?}", name));
Expand Down
14 changes: 4 additions & 10 deletions rust_qsim/src/simulation/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ pub struct GlobalScenario {

impl GlobalScenario {
pub fn load(config: Arc<Config>) -> Self {
id::load_from_file(&io::resolve_path(
config.context(),
&config.proto_files().ids,
));
id::load_from_file(&io::resolve_path(config.context(), &config.ids().path));

// mandatory content to create a scenario
let network = Self::create_network(&config);
Expand All @@ -39,7 +36,7 @@ impl GlobalScenario {
}

fn create_network(config: &Config) -> Network {
let net_in_path = io::resolve_path(config.context(), &config.proto_files().network);
let net_in_path = io::resolve_path(config.context(), &config.network().path);
let num_parts = config.partitioning().num_parts;
let network =
Network::from_file_path(&net_in_path, num_parts, config.partitioning().method);
Expand All @@ -54,15 +51,12 @@ impl GlobalScenario {
}

fn create_garage(config: &Config) -> Garage {
Garage::from_file(&io::resolve_path(
config.context(),
&config.proto_files().vehicles,
))
Garage::from_file(&io::resolve_path(config.context(), &config.vehicles().path))
}

fn create_population(config: &Config, garage: &mut Garage) -> Population {
Population::from_file(
&io::resolve_path(config.context(), &config.proto_files().population),
&io::resolve_path(config.context(), &config.population().path),
garage,
)
}
Expand Down
18 changes: 12 additions & 6 deletions rust_qsim/tests/resources/3-links-url/3-links-config-1.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
modules:
protofiles:
type: ProtoFiles
network: ./test_output/simulation/execute_3_links_single_part_from_url/3-links-network.binpb
population: ./test_output/simulation/execute_3_links_single_part_from_url/1-agent-full-leg.binpb
vehicles: ./test_output/simulation/execute_3_links_single_part_from_url/vehicles.binpb
ids: ./test_output/simulation/execute_3_links_single_part_from_url/ids.binpb
network:
type: Network
path: ./test_output/simulation/execute_3_links_single_part_from_url/3-links-network.binpb
population:
type: Population
path: ./test_output/simulation/execute_3_links_single_part_from_url/1-agent-full-leg.binpb
vehicles:
type: Vehicles
path: ./test_output/simulation/execute_3_links_single_part_from_url/vehicles.binpb
ids:
type: Ids
path: ./test_output/simulation/execute_3_links_single_part_from_url/ids.binpb
partitioning:
type: Partitioning
num_parts: 1
Expand Down
18 changes: 12 additions & 6 deletions rust_qsim/tests/resources/3-links/3-links-config-1.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
modules:
protofiles:
type: ProtoFiles
network: ./test_output/simulation/execute_3_links_single_part/3-links-network.binpb
population: ./test_output/simulation/execute_3_links_single_part/1-agent-full-leg.binpb
vehicles: ./test_output/simulation/execute_3_links_single_part/vehicles.binpb
ids: ./test_output/simulation/execute_3_links_single_part/ids.binpb
network:
type: Network
path: ./test_output/simulation/execute_3_links_single_part/3-links-network.binpb
population:
type: Population
path: ./test_output/simulation/execute_3_links_single_part/1-agent-full-leg.binpb
vehicles:
type: Vehicles
path: ./test_output/simulation/execute_3_links_single_part/vehicles.binpb
ids:
type: Ids
path: ./test_output/simulation/execute_3_links_single_part/ids.binpb
partitioning:
type: Partitioning
num_parts: 1
Expand Down
Loading