Compare commits
2 Commits
874a9f86f8
...
253045c742
| Author | SHA1 | Date | |
|---|---|---|---|
| 253045c742 | |||
| 5e92e30f51 |
Binary file not shown.
@@ -10,7 +10,6 @@ fn main() {
|
||||
|
||||
let conn = establish_connection();
|
||||
let result = recipes
|
||||
.limit(5)
|
||||
.load::<Recipe>(&conn)
|
||||
.expect("Error loading recipes");
|
||||
|
||||
|
||||
@@ -21,6 +21,18 @@ pub fn establish_connection() -> SqliteConnection {
|
||||
.expect(&format!("Error connecting to {}", db_url))
|
||||
}
|
||||
|
||||
pub mod recipes {
|
||||
use crate::models::{Recipe, NewRecipe};
|
||||
use super::{SqliteConnection, schema};
|
||||
use super::diesel::prelude::*;
|
||||
|
||||
pub fn load_all(conn: &SqliteConnection) -> Vec<Recipe> {
|
||||
use self::schema::recipes::dsl::*;
|
||||
recipes.load::<Recipe>(conn)
|
||||
.expect("Error loading recipe's list")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub mod ingredients {
|
||||
use crate::models::{Ingredient, NewIngredient};
|
||||
|
||||
@@ -2,7 +2,7 @@ use super::schema::recipes;
|
||||
use super::schema::ingredients;
|
||||
use super::diesel::prelude::*;
|
||||
|
||||
#[derive(Queryable)]
|
||||
#[derive(Debug, Clone, Queryable)]
|
||||
pub struct Recipe {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
|
||||
1
planner/.env
Normal file
1
planner/.env
Normal file
@@ -0,0 +1 @@
|
||||
DATABASE_URL=../cookbook/db.sqlite3
|
||||
@@ -1,2 +1,34 @@
|
||||
//! The weekly menu planner
|
||||
//!
|
||||
extern crate cookbook;
|
||||
extern crate planner;
|
||||
|
||||
use self::cookbook::*;
|
||||
use self::cookbook::models::Recipe;
|
||||
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")
|
||||
&& assign.get("Mardi_midi").unwrap().unwrap().ingredients.contains("Patates")
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -17,13 +17,32 @@ enum Assignment<'a, V> {
|
||||
/// The domain of values that can be assigned to variables
|
||||
#[derive(Clone)]
|
||||
pub struct Domain<V> {
|
||||
values: Vec<V>
|
||||
pub values: Vec<V>
|
||||
}
|
||||
|
||||
impl<V> Domain<V> {
|
||||
pub fn new(values: Vec<V>) -> Domain<V> {
|
||||
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<V>
|
||||
where V: std::clone::Clone
|
||||
{
|
||||
Domain {
|
||||
values: self.values.iter().cloned().filter(f).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
||||
@@ -32,7 +51,7 @@ impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user