Skip to content

Commit

Permalink
Sorting channel list, removing hard coded model directory
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohnson5 committed Dec 11, 2024
1 parent c2a8e5b commit 05fd5f1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
16 changes: 15 additions & 1 deletion blast_cli/src/blast_cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Standard libraries
use std::env;
use std::path::PathBuf;

// Blast libraries
use blast_core::Blast;

Expand All @@ -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<Model> = Vec::new();
Expand Down
37 changes: 14 additions & 23 deletions blast_core/src/blast_model_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, BlastModel>
}

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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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<String, BlastModel> {
fn parse_models(dir: String) -> HashMap<String, BlastModel> {
// 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(&current_dir.as_path(), 0, &mut model_map);
check_for_model(&Path::new(&dir), 0, &mut model_map);

model_map
}
Expand Down
4 changes: 2 additions & 2 deletions blast_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 16 additions & 6 deletions blast_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 05fd5f1

Please sign in to comment.