work on planner templates in progress

This commit is contained in:
2019-02-07 21:58:38 +01:00
parent f220c6c960
commit 29b2f076bf
10 changed files with 209 additions and 108 deletions

View File

@@ -7,6 +7,7 @@ use self::cookbook::*;
use self::cookbook::recipes::Recipe;
use self::planner::solver::{Variables, Domain, Problem};
type DomainValues<'a, V> = Vec<&'a V>;
/// We want a mapping of the week meals (matin, midi, soir)
/// Breakfast => RecipeCategory::Breakfast
/// Lunch => RecipeCategory::MainCourse
@@ -14,52 +15,87 @@ use self::planner::solver::{Variables, Domain, Problem};
mod template {
//! Exports the [`Template`] struct.
use super::{Domain, DomainValues, Variables, Recipe};
type Day = String;
const DAYS: &[&str] = &["Lundi", "Mardi", "Mercredi"];
const DAYS: &[&str] = &[
"Lundi", "Mardi", "Mercredi",
"Jeudi", "Vendredi", "Samedi",
"Dimanche"];
/// An enum to discriminate every meals
#[allow(dead_code)]
enum Meals {
pub enum Meals {
Breakfast(Day),
Lunch(Day),
Dinner(Day)
}
impl From<Meals> for String {
fn from(item: Meals) -> String {
match item {
Meals::Breakfast(d) => format!("{}_Breakfast", d),
Meals::Lunch(d) => format!("{}_Lunch", d),
Meals::Dinner(d) => format!("{}_Dinner", d),
}
}
}
/// A fixed template of meals
///
/// TODO: mask -> disabled choosen meals
/// initials -> fixed values
pub struct Template;
impl Template {
fn empty() {
fn keys() -> Vec<Meals> {
let mut keys = Vec::new();
for day in DAYS {
for meal in &[Meals::Breakfast, Meals::Lunch, Meals::Dinner] {
keys.push(meal(day.to_string()))
}
}
keys
}
/// Build a [`Template`] from a variables assignment map,
/// usually a solution returned by solver
pub(super) fn from_variables<'a, V>(_vars: Variables<'a,V>) -> Template{
Template
}
/// Builds a vector of variables, to be used with
/// [`ProblemBuilder`].
pub(super) fn generate_variables(domain: &Domain<Recipe>)
-> Vec<(String, DomainValues<Recipe>, Option<&Recipe>)>
{
use cookbook::recipes::RecipeCategory;
let mut vars = Vec::new();
for key in Self::keys().into_iter() {
//TODO: Use key variants to set filters on domain
//TODO: Initial values
let _filter: fn(&&Recipe) -> bool = match key {
Meals::Breakfast(_) => |r: &&Recipe| {
r.category == RecipeCategory::Breakfast // Breakfast
},
Meals::Lunch(_) => |r: &&Recipe| {
r.category as i16 == 2i16 // MainCourse
},
Meals::Dinner(_) => |r: &&Recipe| {
r.category as i16 == 1i16 // Starter
}
};
vars.push((key.into(), domain.filter(_filter), None));
}
vars
}
}
}
impl Into<String> for Meals {
fn into(self) -> String {
match self {
Meals::Breakfast(d) => format!("{}_Breakfast", d),
Meals::Lunch(d) => format!("{}_Lunch", d),
Meals::Dinner(d) => format!("{}_Dinner", d),
}
}
}
/// It may also contains an initial value for each variable
fn generate_variables<V>(domain: &Domain<V>) -> Vec<(String, &Domain<V>, Option<&V>)> {
let mut vars = Vec::new();
for day in DAYS {
vars.push((Meals::Lunch(day.to_string()).into(), domain, None));
vars.push((Meals::Dinner(day.to_string()).into(), domain, None));
}
vars
}
fn ingredients_contains<'a>(assign: &Variables<'a,Recipe>) -> bool {
let id = 0;
assign.get("Lundi_Lunch").unwrap().unwrap().ingredients.contains(&id)
&& !assign.get("Mardi_Lunch").unwrap().unwrap().ingredients.contains(&id)
fn ingredients_contains<'a>(_assign: &Variables<'a,Recipe>) -> bool {
true
}
@@ -80,7 +116,7 @@ fn get_planning_all_results() -> String {
let possible_values = recipes::load_all(&conn);
let domain = Domain::new(possible_values);
let mut problem = Problem::build();
for (var, dom, ini) in generate_variables(&domain) {
for (var, dom, ini) in template::Template::generate_variables(&domain) {
problem = problem.add_variable(var, dom, ini);
}
let mut problem = problem