Skip to content

Commit 99e968b

Browse files
committed
Add cli command (not yet implemented)
1 parent fe49756 commit 99e968b

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/cli.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! The command line interface for the simulation.
2+
use crate::graph::save_commodity_graphs_for_model;
23
use crate::input::load_model;
34
use crate::log;
45
use crate::output::{create_output_directory, get_output_dir};
@@ -62,6 +63,13 @@ enum Commands {
6263
/// The path to the model directory.
6364
model_dir: PathBuf,
6465
},
66+
/// Build and output commodity flow graphs for a model.
67+
BuildCommodityGraphs {
68+
/// The path to the model directory.
69+
model_dir: PathBuf,
70+
/// Output path
71+
output_dir: PathBuf,
72+
},
6573
/// Manage settings file.
6674
Settings {
6775
/// The subcommands for managing the settings file.
@@ -77,6 +85,10 @@ impl Commands {
7785
Self::Run { model_dir, opts } => handle_run_command(&model_dir, &opts, None),
7886
Self::Example { subcommand } => subcommand.execute(),
7987
Self::Validate { model_dir } => handle_validate_command(&model_dir, None),
88+
Self::BuildCommodityGraphs {
89+
model_dir,
90+
output_dir,
91+
} => handle_build_commodity_graphs_command(&model_dir, &output_dir, None),
8092
Self::Settings { subcommand } => subcommand.execute(),
8193
}
8294
}
@@ -178,3 +190,30 @@ pub fn handle_validate_command(model_path: &Path, settings: Option<Settings>) ->
178190

179191
Ok(())
180192
}
193+
194+
/// Handle the `build-commodity-graphs` command.
195+
pub fn handle_build_commodity_graphs_command(
196+
model_path: &Path,
197+
output_path: &Path,
198+
settings: Option<Settings>,
199+
) -> Result<()> {
200+
// Load program settings, if not provided
201+
let settings = if let Some(settings) = settings {
202+
settings
203+
} else {
204+
Settings::load().context("Failed to load settings.")?
205+
};
206+
207+
// Initialise program logger (we won't save log files when running the validate command)
208+
log::init(&settings.log_level, None).context("Failed to initialise logging.")?;
209+
210+
// Load/validate the model
211+
let (model, _) = load_model(model_path).context("Failed to load model.")?;
212+
info!("Loaded model from {}", model_path.display());
213+
214+
// Save commodity flow graphs to file
215+
save_commodity_graphs_for_model(&model.commodity_graphs, output_path)?;
216+
info!("Commodity flow graphs saved to file");
217+
218+
Ok(())
219+
}

src/graph.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use petgraph::algo::toposort;
1212
use petgraph::graph::Graph;
1313
use std::collections::HashMap;
1414
use std::fmt::Display;
15+
use std::path::Path;
1516
use strum::IntoEnumIterator;
1617

1718
/// A graph of commodity flows for a given region and year
@@ -400,6 +401,19 @@ pub fn validate_commodity_graphs_for_model(
400401
Ok(commodity_order)
401402
}
402403

404+
/// Saves commodity graphs to file
405+
///
406+
/// The graphs are saved as DOT files to the specified output path
407+
pub fn save_commodity_graphs_for_model(
408+
commodity_graphs: &HashMap<(RegionID, u32), CommoditiesGraph>,
409+
_output_path: &Path,
410+
) -> Result<()> {
411+
for ((_region_id, _year), _graph) in commodity_graphs {
412+
todo!()
413+
}
414+
Ok(())
415+
}
416+
403417
#[cfg(test)]
404418
mod tests {
405419
use super::*;

src/input.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ pub fn load_model<P: AsRef<Path>>(model_dir: P) -> Result<(Model, AssetPool)> {
233233
processes,
234234
time_slice_info,
235235
regions,
236+
commodity_graphs,
236237
commodity_order,
237238
};
238239
Ok((model, AssetPool::new(assets)))

src/model.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! The model represents the static input data provided by the user.
22
use crate::agent::AgentMap;
33
use crate::commodity::{CommodityID, CommodityMap};
4+
use crate::graph::CommoditiesGraph;
45
use crate::process::ProcessMap;
56
use crate::region::{RegionID, RegionMap};
67
use crate::time_slice::TimeSliceInfo;
@@ -26,6 +27,8 @@ pub struct Model {
2627
pub time_slice_info: TimeSliceInfo,
2728
/// Regions for the simulation
2829
pub regions: RegionMap,
30+
/// Commodity flow graphs for each region and year
31+
pub commodity_graphs: HashMap<(RegionID, u32), CommoditiesGraph>,
2932
/// Commodity ordering for each region and year
3033
pub commodity_order: HashMap<(RegionID, u32), Vec<CommodityID>>,
3134
}

0 commit comments

Comments
 (0)