Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error when you try to change a global property. Fixes #71. #74

Merged
merged 4 commits into from
Dec 2, 2024
Merged
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
4 changes: 3 additions & 1 deletion examples/births-deaths/infection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ mod test {

let mut context = Context::new();

context.set_global_property_value(Parameters, p_values.clone());
context
.set_global_property_value(Parameters, p_values.clone())
.unwrap();
context.init_random(p_values.seed);
init(&mut context);

Expand Down
8 changes: 6 additions & 2 deletions examples/births-deaths/parameters_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ define_global_property!(Foi, HashMap<AgeGroupRisk, f64>);

pub fn init_parameters(context: &mut Context, file_path: &Path) -> Result<(), IxaError> {
let parameters_json = context.load_parameters_from_json::<ParametersValues>(file_path)?;
context.set_global_property_value(Parameters, parameters_json.clone());
context
.set_global_property_value(Parameters, parameters_json.clone())
.unwrap();

let foi_map = parameters_json
.foi_groups
Expand All @@ -41,7 +43,9 @@ pub fn init_parameters(context: &mut Context, file_path: &Path) -> Result<(), Ix
.map(|x| (x.group_name, x.foi))
.collect::<HashMap<AgeGroupRisk, f64>>();

context.set_global_property_value(Foi, foi_map.clone());
context
.set_global_property_value(Foi, foi_map.clone())
.unwrap();

Ok(())
}
8 changes: 6 additions & 2 deletions examples/births-deaths/population_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ mod test {
};

let mut context = Context::new();
context.set_global_property_value(Parameters, p_values.clone());
context
.set_global_property_value(Parameters, p_values.clone())
.unwrap();
context.init_random(p_values.seed);

schedule_birth(&mut context);
Expand All @@ -300,7 +302,9 @@ mod test {
};

let mut context = Context::new();
context.set_global_property_value(Parameters, p_values.clone());
context
.set_global_property_value(Parameters, p_values.clone())
.unwrap();
context.init_random(p_values.seed);
let _person = context.create_new_person(0);
schedule_death(&mut context);
Expand Down
4 changes: 3 additions & 1 deletion examples/parameter-loading/infection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ mod test {
};
let mut context = Context::new();

context.set_global_property_value(Parameters, p_values.clone());
context
.set_global_property_value(Parameters, p_values.clone())
.unwrap();
context.init_random(42);
init(&mut context);

Expand Down
2 changes: 1 addition & 1 deletion examples/parameter-loading/parameters_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ define_global_property!(Parameters, ParametersValues);

pub fn init_parameters(context: &mut Context, file_path: &Path) -> Result<(), IxaError> {
let parameters_json = context.load_parameters_from_json::<ParametersValues>(file_path)?;
context.set_global_property_value(Parameters, parameters_json);
context.set_global_property_value(Parameters, parameters_json)?;
Ok(())
}
4 changes: 3 additions & 1 deletion examples/parameter-loading/transmission_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ mod test {
};

let mut context = Context::new();
context.set_global_property_value(Parameters, p_values);
context
.set_global_property_value(Parameters, p_values)
.unwrap();
context.init_random(123);
let pid = context.add_person(()).unwrap();
attempt_infection(&mut context);
Expand Down
8 changes: 6 additions & 2 deletions examples/time-varying-infection/exposure_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ mod test {
output_file: ".".to_string(),
};
let mut context = Context::new();
context.set_global_property_value(Parameters, p_values);
context
.set_global_property_value(Parameters, p_values)
.unwrap();
let parameters = context
.get_global_property_value(Parameters)
.unwrap()
Expand Down Expand Up @@ -132,7 +134,9 @@ mod test {
output_file: ".".to_string(),
};
let mut context = Context::new();
context.set_global_property_value(Parameters, p_values);
context
.set_global_property_value(Parameters, p_values)
.unwrap();
let parameters = context
.get_global_property_value(Parameters)
.unwrap()
Expand Down
12 changes: 9 additions & 3 deletions examples/time-varying-infection/infection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ mod test {
};
let mut context = Context::new();

context.set_global_property_value(Parameters, p_values);
context
.set_global_property_value(Parameters, p_values)
.unwrap();
let parameters = context
.get_global_property_value(Parameters)
.unwrap()
Expand Down Expand Up @@ -161,7 +163,9 @@ mod test {
output_dir: ".".to_string(),
output_file: ".".to_string(),
};
context.set_global_property_value(Parameters, parameters.clone());
context
.set_global_property_value(Parameters, parameters.clone())
.unwrap();
context.init_random(parameters.seed);
for _ in 0..parameters.population {
let person_id = context.add_person(()).unwrap();
Expand Down Expand Up @@ -211,7 +215,9 @@ mod test {
let mut sum = 0.0;
for seed in 0..n_iter {
let mut context = Context::new();
context.set_global_property_value(Parameters, parameters.clone());
context
.set_global_property_value(Parameters, parameters.clone())
.unwrap();
context.init_random(seed);
init(&mut context);
let person_id = context
Expand Down
2 changes: 1 addition & 1 deletion examples/time-varying-infection/parameters_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ define_global_property!(Parameters, ParametersValues);

pub fn init_parameters(context: &mut Context, file_path: &Path) -> Result<(), IxaError> {
let parameters_json = context.load_parameters_from_json::<ParametersValues>(file_path)?;
context.set_global_property_value(Parameters, parameters_json);
context.set_global_property_value(Parameters, parameters_json)?;
Ok(())
}
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::io;

#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
/// A wrapper around other error types, as well
/// as our specific error type.
/// Provides `IxaError` and maps to other errors to
/// convert to an `IxaError`
pub enum IxaError {
IoError(io::Error),
JsonError(serde_json::Error),
Expand Down
57 changes: 43 additions & 14 deletions src/global_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::error::IxaError;
use serde::de::DeserializeOwned;
use std::any::{Any, TypeId};
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::{hash_map::Entry, HashMap};
use std::fmt::Debug;
use std::fs;
use std::io::BufReader;
Expand Down Expand Up @@ -40,7 +40,7 @@ where
if context.get_global_property_value(T::new()).is_some() {
return Err(IxaError::IxaError(format!("Duplicate property {name}")));
}
context.set_global_property_value(T::new(), val);
let _ = context.set_global_property_value(T::new(), val);
Ok(())
},
),
Expand Down Expand Up @@ -122,11 +122,14 @@ crate::context::define_data_plugin!(

pub trait ContextGlobalPropertiesExt {
/// Set the value of a global property of type T
///
/// # Errors
/// Will return an error if an attempt is made to change a value.
fn set_global_property_value<T: GlobalProperty + 'static>(
&mut self,
property: T,
value: T::Value,
);
) -> Result<(), IxaError>;

/// Return value of global property T
fn get_global_property_value<T: GlobalProperty + 'static>(
Expand Down Expand Up @@ -171,11 +174,14 @@ impl GlobalPropertiesDataContainer {
&mut self,
_property: &T,
value: T::Value,
) {
let _data_container = self
.global_property_container
.entry(TypeId::of::<T>())
.or_insert_with(|| Box::new(value));
) -> Result<(), IxaError> {
match self.global_property_container.entry(TypeId::of::<T>()) {
Entry::Vacant(entry) => {
entry.insert(Box::new(value));
Ok(())
}
Entry::Occupied(_) => Err(IxaError::from("Entry already exists")),
}
}

#[must_use]
Expand All @@ -194,9 +200,9 @@ impl ContextGlobalPropertiesExt for Context {
&mut self,
property: T,
value: T::Value,
) {
) -> Result<(), IxaError> {
let data_container = self.get_data_container_mut(GlobalPropertiesPlugin);
data_container.set_global_property_value(&property, value);
data_container.set_global_property_value(&property, value)
}

#[allow(unused_variables)]
Expand Down Expand Up @@ -252,16 +258,37 @@ mod test {
}

define_global_property!(DiseaseParams, ParamType);
//Since global properties aren't mutable right now, only
// check that they are properly set

#[test]
fn set_get_global_property() {
let params: ParamType = ParamType {
days: 10,
diseases: 2,
};
let params2: ParamType = ParamType {
days: 11,
diseases: 3,
};

let mut context = Context::new();
context.set_global_property_value(DiseaseParams, params.clone());

// Set and check the stored value.
context
.set_global_property_value(DiseaseParams, params.clone())
.unwrap();
let global_params = context
.get_global_property_value(DiseaseParams)
.unwrap()
.clone();
assert_eq!(global_params.days, params.days);
assert_eq!(global_params.diseases, params.diseases);

// Setting again should fail because global properties are immutable.
assert!(context
.set_global_property_value(DiseaseParams, params2.clone())
.is_err());

// Check that the value is unchanged.
let global_params = context
.get_global_property_value(DiseaseParams)
.unwrap()
Expand Down Expand Up @@ -298,7 +325,9 @@ mod test {
.load_parameters_from_json::<ParamType>(&file_path)
.unwrap();

context.set_global_property_value(Parameters, params_json);
context
.set_global_property_value(Parameters, params_json)
.unwrap();

let params_read = context
.get_global_property_value(Parameters)
Expand Down