Added command line argument parsing.

This commit is contained in:
Jacob VanDomelen 2019-05-24 19:23:50 -07:00
parent 5abe93a0dc
commit e6570cf4b7
8 changed files with 103 additions and 62 deletions

View file

@ -5,4 +5,5 @@ authors = ["Jacob VanDomelen <Jacob.Vandome15@gmail.com>"]
edition = "2018"
[dependencies]
uuid = { version = "0.7", features = ["serde", "v4"] }
uuid = { version = "0.7", features = ["serde", "v4"] }
clap = { version = "~2.27.0", features = ["yaml"] }

9
gemla/cli.yml Normal file
View file

@ -0,0 +1,9 @@
name: GEMLA
version: "0.1"
autor: Jacob VanDomelen <jacob.vandome15@gmail.com>
about: Uses a genetic algorithm to generate a machine learning algorithm.
args:
- DIRECTORY:
help: Sets the input/output directory for the program.
required: true
index: 1

View file

@ -3,29 +3,38 @@ mod state;
use uuid::Uuid;
// pub struct Bracket {
// tree: tree::Tree,
// directory: String
// }
fn build_tree(h: u32) -> Option<Box<tree::Tree>> {
let mut result: Option<Box<tree::Tree>> = None;
let mut result: Option<Box<tree::Tree>> = None;
if h != 0 {
result = Some(tree::concat_trees(Uuid::new_v4(), build_tree(h - 1), build_tree(h - 1)));
match &result {
Some(r) => (*r).run_simulation(),
_ => ()
}
}
if h != 0 {
result = Some(Box::new(tree::combine_trees(Uuid::new_v4(), build_tree(h - 1), build_tree(h - 1))));
match &result {
Some(r) => (*r).run_simulation(),
_ => ()
}
}
result
result
}
pub fn run_bracket() {
let mut height = 1;
let mut tree: tree::Tree = *build_tree(height).expect("Error getting result from build_tree.");
let mut height = 1;
let mut tree: tree::Tree = *build_tree(height).expect("Error getting result from build_tree.");
loop {
println!("=========================================");
println!("Running bracket...");
height += 1;
tree = *tree::concat_trees(Uuid::new_v4(), Some(Box::new(tree)), build_tree(height));
tree.run_simulation();
}
loop {
println!("=========================================");
println!("Running bracket...");
height += 1;
tree = tree::combine_trees(Uuid::new_v4(), Some(Box::new(tree)), build_tree(height));
tree.run_simulation();
if height == 3 {
break;
}
}
}

View file

@ -1,18 +1,18 @@
use uuid::Uuid;
use std::fmt;
// use uuid::Uuid;
// use std::fmt;
pub struct State {
id: Uuid
}
// pub struct State {
// id: Uuid
// }
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.id)
}
}
// impl fmt::Display for State {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// write!(f, "{}", self.id)
// }
// }
pub fn create(id: &Uuid) -> State {
State {
id: id.clone()
}
}
// pub fn create(id: &Uuid) -> State {
// State {
// id: id.clone()
// }
// }

View file

@ -2,43 +2,43 @@ use std::fmt;
use uuid::Uuid;
pub struct Tree {
id: Uuid,
left: Option<Box<Tree>>,
right: Option<Box<Tree>>
id: Uuid,
left: Option<Box<Tree>>,
right: Option<Box<Tree>>
}
pub fn concat_trees(id: Uuid, l: Option<Box<Tree>>, r: Option<Box<Tree>>) -> Box<Tree> {
Box::new(Tree {
id: id,
left: l,
right: r
})
pub fn combine_trees(id: Uuid, l: Option<Box<Tree>>, r: Option<Box<Tree>>) -> Tree {
Tree {
id: id,
left: l,
right: r
}
}
impl fmt::Display for Tree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let node_str = |t: &Option<Box<Tree>>| -> String {
match t {
Some(n) => format!("{}", *n),
_ => String::from("_")
}
};
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let node_str = |t: &Option<Box<Tree>>| -> String {
match t {
Some(n) => format!("{}", *n),
_ => String::from("_")
}
};
write!(f, "({} :{}|{})", self.id, node_str(&self.left), node_str(&self.right))
}
write!(f, "({} :{}|{})", self.id, node_str(&self.left), node_str(&self.right))
}
}
fn fmt_node(t: &Option<Box<Tree>>) -> String {
match t {
Some(n) => format!("{}", (*n).id),
_ => String::from("_")
}
match t {
Some(n) => format!("{}", (*n).id),
_ => String::from("_")
}
}
impl Tree {
pub fn run_simulation(&self) {
println!("================================");
println!("Running simulation for node: {}", self.id);
println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right));
}
pub fn run_simulation(&self) {
println!("================================");
println!("Running simulation for node: {}", self.id);
println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right));
}
}

View file

@ -0,0 +1 @@
pub const DIRECTORY: &str = "DIRECTORY";

View file

@ -0,0 +1 @@
pub mod args;

View file

@ -1,5 +1,25 @@
mod bracket;
mod constants;
#[macro_use]
extern crate clap;
use clap::App;
use std::fs::metadata;
fn main() {
bracket::run_bracket();
let yaml = load_yaml!("../cli.yml");
let matches = App::from_yaml(yaml).get_matches();
let directory = matches.value_of(constants::args::DIRECTORY).unwrap();
let metadata = metadata(directory);
match &metadata {
Ok(m) if m.is_dir() == true => {
println!("{} is a valid directory!", directory);
println!("Building tree for {}.", directory);
bracket::run_bracket();
},
Ok(_) => println!("{} is not a valid directory!", directory),
_ => println!("{} does not exist!", directory)
}
}