Skip to content
Snippets Groups Projects
Commit 9c84ecdd authored by Blinningjr's avatar Blinningjr
Browse files

Seperated the code so it is easier to read

parent 5a771c36
Branches
No related tags found
No related merge requests found
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use super::common::*;
/*
* Write `contents` to the file `file`.
*/
pub fn write_to_file(contents: String, file: PathBuf) -> std::io::Result<()> {
let mut file = File::create(file)?;
file.write_all(contents.as_bytes())?;
Ok(())
}
/*
* Load the tasks from a json file `file`.
*/
pub fn load_tasks(file: PathBuf) -> Tasks {
let mut serialized = String::new();
read_from_file(&mut serialized, file);
return serde_json::from_str(&serialized).unwrap();
}
/*
* Read string from file `file` and store it in `contents`.
*/
pub fn read_from_file(contents: &mut String, file: PathBuf) -> std::io::Result<()> {
let mut file = File::open(file)?;
file.read_to_string(contents)?;
Ok(())
}
use super::common::*;
use super::srp_analyser::*;
/*
* Creates a readable representation of the analysis and the given information.
*/
pub fn format_analysis(analysis: &Vec<AnalysedTask>, tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) -> String {
let overview = format_overview(tasks, ip, tr);
let mut body = String::new();
for at in analysis {
body = format!("{}\n{}", body, format_analysed_task(at));
}
body = format!("{}\n", body);
for t in tasks {
body = format!("{}\n{}", body, format_task(t));
}
return format!("{}\n{}", overview, body);
}
/*
* Creates the overview section of the readable representation.
*/
pub fn format_overview(tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) -> String {
// Formats the names of all the tasks.
fn format_task_names(tasks: &Tasks) -> String {
let mut task_names = "Tasks:".to_string();
for t in tasks {
task_names = format!("{} {}", task_names, t.id);
}
return task_names;
}
// Formats the Id and priority of tasks.
fn format_id_prio(ip: &IdPrio) -> String {
// let id_prio = format!("Id Priority: {:?}", ip);
let mut id_prio = "Priority:".to_string();
for (key, value) in ip {
id_prio = format!("{} P({}) = {}", id_prio, key, value);
}
return id_prio;
}
// Formats the tasks resources.
fn format_task_res(tr: &TaskResources) -> String {
// let task_res = format!("Task Resources: {:?}", tr);
let mut task_res = "Task Resources:".to_string();
for (key, value) in tr {
let mut values_st = "(".to_string();
for v in value {
values_st = format!("{} {}", values_st, v);
}
task_res = format!("{} {}:{} )", task_res, key, values_st);
}
return task_res;
}
let tot_util = format!("Total Util: {}", total_load_factor(tasks));
let task_names = format_task_names(tasks);
let id_prio = format_id_prio(ip);
let task_res = format_task_res(tr);
return format!("Overview\n\t{}\n\t{}\n\t{}\n\t{}", tot_util, task_names, id_prio, task_res);
}
/*
* Creates a readable representation of a analysed task.
*/
pub fn format_analysed_task(task: &AnalysedTask) -> String {
let header = format!("Analysis of {}", task.task.id);
let res_t = format!("Response Time: {}", task.response_time);
let wcet = format!("wcet: {}", task.wcet);
let block_t = format!("Block Time: {}", task.block_time);
let pre_t = format!("Preemtion Time: {}", task.preemtion_time);
return format!("{}\n\t{}\n\t{}\n\t{}\n\t{}", header, res_t, wcet, block_t, pre_t);
}
/*
* Creates a readable representation of a task.
*/
pub fn format_task(task: &Task) -> String {
let id = format!("Task {}", task.id);
let prio = format!("Priority: {}", task.prio);
let dl = format!("Deadline: {}", task.deadline);
let inter_a = format!("Inter Arrival: {}", task.inter_arrival);
let trace = format_trace(&task.trace);
return format!("{}\n\t{}\n\t{}\n\t{}\n\t{}", id, prio, dl, inter_a, trace);
}
/*
* Creates a readable representation of a trace.
*/
pub fn format_trace(trace: &Trace) -> String {
return format!("{:?}", trace);
}
mod common; mod common;
mod srp_analyser; mod srp_analyser;
mod formater;
mod file_handler;
use common::*; use common::*;
use srp_analyser::*; use srp_analyser::*;
use formater::*;
use file_handler::*;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
use std::fs::File;
use std::io::prelude::*;
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt(name = "srp_analysis", about = "Preforms srp analysis.")] #[structopt(name = "srp_analysis", about = "Preforms srp analysis.")]
...@@ -33,7 +34,6 @@ struct Opt { ...@@ -33,7 +34,6 @@ struct Opt {
fn main() { fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
//println!("{:?}", opt); //println!("{:?}", opt);
...@@ -59,117 +59,12 @@ fn main() { ...@@ -59,117 +59,12 @@ fn main() {
}, },
None => println!("{}", output), None => println!("{}", output),
}; };
// match opt.output {
// Some(file) => {
// write_to_file(serde_json::to_string(&analysis).unwrap(), file);
// }, // TODO: Implement so that the analysis can be saved to txt.
// None => {
// println!("{}", format_analysis(&analysis, &tasks, &ip, &tr));
// },
// };
}
fn format_analysis(analysis: &Vec<AnalysedTask>, tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) -> String {
let overview = format_overview(tasks, ip, tr);
let mut body = String::new();
for at in analysis {
body = format!("{}\n{}", body, format_analysed_task(at));
}
body = format!("{}\n", body);
for t in tasks {
body = format!("{}\n{}", body, format_task(t));
}
return format!("{}\n{}", overview, body);
}
fn format_overview(tasks: &Tasks, ip: &IdPrio, tr: &TaskResources) -> String {
fn format_task_names(tasks: &Tasks) -> String {
let mut task_names = "Tasks:".to_string();
for t in tasks {
task_names = format!("{} {}", task_names, t.id);
}
return task_names;
}
fn format_id_prio(ip: &IdPrio) -> String {
// let id_prio = format!("Id Priority: {:?}", ip);
let mut id_prio = "Priority:".to_string();
for (key, value) in ip {
id_prio = format!("{} P({}) = {}", id_prio, key, value);
}
return id_prio;
}
fn format_task_res(tr: &TaskResources) -> String {
// let task_res = format!("Task Resources: {:?}", tr);
let mut task_res = "Task Resources:".to_string();
for (key, value) in tr {
let mut values_st = "(".to_string();
for v in value {
values_st = format!("{} {}", values_st, v);
}
task_res = format!("{} {}:{} )", task_res, key, values_st);
}
return task_res;
}
let tot_util = format!("Total Util: {}", total_load_factor(tasks));
let task_names = format_task_names(tasks);
let id_prio = format_id_prio(ip);
let task_res = format_task_res(tr);
return format!("Overview\n\t{}\n\t{}\n\t{}\n\t{}", tot_util, task_names, id_prio, task_res);
}
fn format_analysed_task(task: &AnalysedTask) -> String {
let header = format!("Analysis of {}", task.task.id);
let res_t = format!("Response Time: {}", task.response_time);
let wcet = format!("wcet: {}", task.wcet);
let block_t = format!("Block Time: {}", task.block_time);
let pre_t = format!("Preemtion Time: {}", task.preemtion_time);
return format!("{}\n\t{}\n\t{}\n\t{}\n\t{}", header, res_t, wcet, block_t, pre_t);
}
fn format_task(task: &Task) -> String {
let id = format!("Task {}", task.id);
let prio = format!("Priority: {}", task.prio);
let dl = format!("Deadline: {}", task.deadline);
let inter_a = format!("Inter Arrival: {}", task.inter_arrival);
let trace = format_trace(&task.trace);
return format!("{}\n\t{}\n\t{}\n\t{}\n\t{}", id, prio, dl, inter_a, trace);
}
fn format_trace(trace: &Trace) -> String {
return format!("{:?}", trace);
}
fn write_to_file(contents: String, file: PathBuf) -> std::io::Result<()> {
let mut file = File::create(file)?;
file.write_all(contents.as_bytes())?;
Ok(())
}
fn load_tasks(file: PathBuf) -> Tasks {
let mut serialized = String::new();
read_from_file(&mut serialized, file);
return serde_json::from_str(&serialized).unwrap();
}
fn read_from_file(contents: &mut String, filename: PathBuf) -> std::io::Result<()> {
let mut file = File::open(filename)?;
file.read_to_string(contents)?;
Ok(())
} }
/*
* Creates example tasks.
*/
fn create_tasks() -> Tasks { let t1 = Task { fn create_tasks() -> Tasks { let t1 = Task {
id: "T1".to_string(), id: "T1".to_string(),
prio: 1, prio: 1,
......
...@@ -195,4 +195,3 @@ pub fn analyse(tasks: &Tasks, ip: &IdPrio, tr: &TaskResources, approx: bool) -> ...@@ -195,4 +195,3 @@ pub fn analyse(tasks: &Tasks, ip: &IdPrio, tr: &TaskResources, approx: bool) ->
return analysis; return analysis;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment