diff --git a/planner/src/bin/weekly.rs b/planner/src/bin/weekly.rs index 22289d4..b5b6e92 100644 --- a/planner/src/bin/weekly.rs +++ b/planner/src/bin/weekly.rs @@ -5,7 +5,11 @@ extern crate planner; use self::cookbook::*; use self::cookbook::models::Recipe; -use self::planner::solver::{Domain, Problem, Variables}; +use self::planner::solver::{Domain, Problem, Constraint, Variables}; + +/// We want a mapping of the week meals (matin, midi, soir) +/// associated with filters for Domain, depending on recipes RecipeCategory +/// It may also contains an initial value for each variable fn ingredients_contains<'a>(assign: &Variables<'a,Recipe>) -> bool { assign.get("Lundi_midi").unwrap().unwrap().ingredients.contains("Patates") @@ -16,18 +20,15 @@ fn get_planning_all_results() -> String { let conn = establish_connection(); let possible_values = recipes::load_all(&conn); let domain = Domain::new(possible_values); - let mut problem = Problem::build() .add_variable("Lundi_midi".to_string(), &domain, None) .add_variable("Mardi_midi".to_string(), &domain, None) .add_constraint(ingredients_contains) .finish(); - let results = problem.solve_all(); format!("{:#?}\nTotal = {}", &results, results.len()) } fn main() { - println!("{}", get_planning_all_results()); } diff --git a/planner/src/solver.rs b/planner/src/solver.rs index c041869..d70206e 100644 --- a/planner/src/solver.rs +++ b/planner/src/solver.rs @@ -17,13 +17,32 @@ enum Assignment<'a, V> { /// The domain of values that can be assigned to variables #[derive(Clone)] pub struct Domain { - values: Vec + pub values: Vec } impl Domain { pub fn new(values: Vec) -> Domain { Domain { values } } + + /// Returns a new domain with the given filter applied to inner values + /// + /// # Examples + /// + /// ``` + /// # extern crate planner; + /// # use planner::solver::Domain; + /// let domain = Domain::new(vec![1,2,3]); + /// fn even(i: &i32) -> bool { i % 2 == 0 }; + /// assert_eq!(&domain.filter(even).values, &vec![2]); + /// ``` + pub fn filter(&self, f: fn(&V) -> bool) -> Domain + where V: std::clone::Clone + { + Domain { + values: self.values.iter().cloned().filter(f).collect(), + } + } } impl fmt::Debug for Domain { @@ -32,7 +51,7 @@ impl fmt::Debug for Domain { } } -type Constraint<'a,V> = fn(&Variables<'a,V>) -> bool; +pub type Constraint<'a,V> = fn(&Variables<'a,V>) -> bool; pub struct Problem<'a, V> { /// The initial assignements map