diff --git a/blast_cli/src/blast_cli.rs b/blast_cli/src/blast_cli.rs index efbe6d6..f1d0f12 100644 --- a/blast_cli/src/blast_cli.rs +++ b/blast_cli/src/blast_cli.rs @@ -1,3 +1,7 @@ +// Standard libraries +use std::env; +use std::path::PathBuf; + // Blast libraries use blast_core::Blast; @@ -18,8 +22,18 @@ pub struct BlastCli { impl BlastCli { pub fn new() -> Self { + // Get the model directory + let mut current_dir = match env::current_dir() { + Ok(d) => d, + Err(_) => { + PathBuf::new() + } + }; + current_dir.push("../blast_models/"); + let model_dir = current_dir.to_string_lossy().into_owned(); + // Create the blast core object - let blast = Blast::new(); + let blast = Blast::new(model_dir); // Get a list of the available models let mut model_list: Vec = Vec::new(); diff --git a/blast_core/src/blast_model_manager.rs b/blast_core/src/blast_model_manager.rs index e20ba57..6180d57 100644 --- a/blast_core/src/blast_model_manager.rs +++ b/blast_core/src/blast_model_manager.rs @@ -49,14 +49,16 @@ struct BlastModel { /// The BlastModelManager struct is the public interface that allows models to be controlled #[derive(Clone)] pub struct BlastModelManager { + model_dir: String, models: HashMap } impl BlastModelManager { /// Create a new BlastModelManager by searching the models directory and parsing all model.json files that are found - pub fn new() -> Self { + pub fn new(model_dir: String) -> Self { let blast_model_manager = BlastModelManager { - models: parse_models(), + model_dir: model_dir.clone(), + models: parse_models(model_dir), }; blast_model_manager @@ -115,18 +117,7 @@ impl BlastModelManager { } }; - // Get the current working directory - let mut current_dir = match env::current_dir() { - Ok(d) => d, - Err(e) => { - return Err(format!("Failed to get the current directory: {:?}", e)); - } - }; - - // Get the full path to the model executable - current_dir.push("../blast_models/".to_owned()+&model.config.name+"/"+&model.config.start); - let model_exe = current_dir.to_string_lossy().into_owned(); - + let model_exe = self.model_dir.to_owned()+"/"+&model.config.name+"/"+&model.config.start; let home = env::var("HOME").expect("HOME environment variable not set"); let folder_path = PathBuf::from(home).join(BLAST_MODEL_LOG_DIR); @@ -449,6 +440,13 @@ impl BlastModelManager { } } + // Sort the Vec of Strings by the first character interpreted as an integer + chans.sort_by(|a, b| { + let first_char_a = String::from(a.split(":").next().unwrap()).parse().unwrap_or(0); + let first_char_b = String::from(b.split(":").next().unwrap()).parse().unwrap_or(0); + first_char_a.cmp(&first_char_b) + }); + chans } @@ -661,19 +659,12 @@ fn get_model_from_node(node_id: String) -> String { } /// Check for model.json files and create BlastModel objects for all known models -fn parse_models() -> HashMap { +fn parse_models(dir: String) -> HashMap { // Create a new map of all the models that are found and then get the models directory. let mut model_map = HashMap::new(); - let mut current_dir = match env::current_dir() { - Ok(d) => d, - Err(_) => { - return model_map; - } - }; - current_dir.push("../blast_models/"); // Search for model.json files in the models directory - check_for_model(¤t_dir.as_path(), 0, &mut model_map); + check_for_model(&Path::new(&dir), 0, &mut model_map); model_map } diff --git a/blast_core/src/lib.rs b/blast_core/src/lib.rs index d1d26d9..afa2a9d 100644 --- a/blast_core/src/lib.rs +++ b/blast_core/src/lib.rs @@ -75,10 +75,10 @@ pub struct BlastStats { impl Blast { /// Create a new Blast object with a new BlastModelManager. - pub fn new() -> Self { + pub fn new(model_dir: String) -> Self { // Create the blast object let blast = Blast { - blast_model_manager: BlastModelManager::new(), + blast_model_manager: BlastModelManager::new(model_dir), blast_event_manager: BlastEventManager::new(), blast_simln_manager: BlastSimLnManager::new(), network: None, diff --git a/blast_example/src/main.rs b/blast_example/src/main.rs index 95afb41..40f06d9 100644 --- a/blast_example/src/main.rs +++ b/blast_example/src/main.rs @@ -27,15 +27,25 @@ async fn main() { .init() .unwrap(); + // Get the model directory + let mut current_dir = match env::current_dir() { + Ok(d) => d, + Err(e) => { + return println!("Failed to get the current directory: {:?}", e); + } + }; + current_dir.push("../blast_models/"); + let model_dir = current_dir.to_string_lossy().into_owned(); + let args: Vec = env::args().collect(); if args.len() > 1 { - load_simulation(args[1].clone()).await; + load_simulation(args[1].clone(), model_dir).await; } else { - new_simulation().await; + new_simulation(model_dir).await; } } -async fn new_simulation() { +async fn new_simulation(model_dir: String) { println!("BLAST starting up..."); // Set up a Ctrl+C signal handler @@ -46,7 +56,7 @@ async fn new_simulation() { }).expect("Error setting Ctrl-C handler"); // Create the blast core object - let mut blast = Blast::new(); + let mut blast = Blast::new(model_dir); // Control Flow: // create_network -- starts models and nodes OR load network @@ -511,7 +521,7 @@ async fn new_simulation() { println!("BLAST shutting down..."); } -async fn load_simulation(name: String) { +async fn load_simulation(name: String, model_dir: String) { println!("BLAST starting up..."); // Set up a Ctrl+C signal handler @@ -522,7 +532,7 @@ async fn load_simulation(name: String) { }).expect("Error setting Ctrl-C handler"); // Create the blast core object - let mut blast = Blast::new(); + let mut blast = Blast::new(model_dir); // Load a previously saved simulation