Compare commits
2 Commits
3e477945ea
...
04e8c554cc
| Author | SHA1 | Date | |
|---|---|---|---|
| 04e8c554cc | |||
| bb965413a8 |
@@ -14,12 +14,14 @@ enum Assignment<'a, V, K> {
|
|||||||
Clear(K)
|
Clear(K)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Collection of references to values owned by a domain.
|
||||||
pub type DomainValues<'a, V> = Vec<&'a V>;
|
pub type DomainValues<'a, V> = Vec<&'a V>;
|
||||||
/// The domain of values that can be assigned to variables
|
|
||||||
|
/// The domain of values that can be assigned to a variable.
|
||||||
|
/// The values are owned by the instance.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Domain<V> {
|
pub struct Domain<V> {
|
||||||
pub values: Vec<V>
|
values: Vec<V>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V> Domain<V> {
|
impl<V> Domain<V> {
|
||||||
@@ -27,11 +29,21 @@ impl<V> Domain<V> {
|
|||||||
Domain { values }
|
Domain { values }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns all values of a Domain instance
|
/// Returns references to all values of this instance
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # extern crate planner;
|
||||||
|
/// # use planner::solver::Domain;
|
||||||
|
/// let domain = Domain::new(vec!["a", "b", "c"]);
|
||||||
|
/// assert_eq!(domain.all(), vec![&"a", &"b", &"c"]);
|
||||||
|
/// ```
|
||||||
pub fn all(&self) -> DomainValues<V> {
|
pub fn all(&self) -> DomainValues<V> {
|
||||||
self.values.iter().collect()
|
self.values.iter().collect()
|
||||||
}
|
}
|
||||||
/// Returns a Filter filter applied to inner values
|
/// Returns a collection of references to a filtered
|
||||||
|
/// subset of this domain.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@@ -45,13 +57,39 @@ impl<V> Domain<V> {
|
|||||||
/// assert_eq!(domain.filter(even), vec![&2]);
|
/// assert_eq!(domain.filter(even), vec![&2]);
|
||||||
/// assert_eq!(domain.filter(|i: &i32| i % 2 == 1), vec![&1,&3]);
|
/// assert_eq!(domain.filter(|i: &i32| i % 2 == 1), vec![&1,&3]);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn filter(&self, filter_func: fn(&V) -> bool) -> DomainValues<V> {
|
pub fn filter<F>(&self, filter_func: F) -> DomainValues<V>
|
||||||
|
where F: Fn(&V) -> bool
|
||||||
|
{
|
||||||
self.values
|
self.values
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|v: &&V| filter_func(*v))
|
.filter(|v: &&V| filter_func(*v))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wrapper for `find`, returns a optionnal reference
|
||||||
|
/// to the first found value of this domain.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # extern crate planner;
|
||||||
|
/// # use planner::solver::Domain;
|
||||||
|
/// let domain = Domain::new(vec![1,2,3]);
|
||||||
|
/// fn even(i: &i32) -> bool {
|
||||||
|
/// *i == 2
|
||||||
|
/// };
|
||||||
|
/// assert_eq!(domain.find(even), Some(&2));
|
||||||
|
/// assert_eq!(domain.find(|i: &i32| i % 2 == 1), Some(&1));
|
||||||
|
/// assert_eq!(domain.find(|i| *i == 4), None);
|
||||||
|
/// ```
|
||||||
|
pub fn find<F>(&self, getter_func: F) -> Option<&V>
|
||||||
|
where F: Fn(&V) -> bool
|
||||||
|
{
|
||||||
|
self.values
|
||||||
|
.iter()
|
||||||
|
.find(|v: &&V| getter_func(*v))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
||||||
@@ -63,6 +101,12 @@ impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
|||||||
|
|
||||||
pub type Constraint<'a,V, K> = fn(&Variables<'a,V, K>) -> bool;
|
pub type Constraint<'a,V, K> = fn(&Variables<'a,V, K>) -> bool;
|
||||||
|
|
||||||
|
|
||||||
|
/// Could be more efficient to just use fixed array of options as variables,
|
||||||
|
/// using a helper to bind Keys to Index in this array.
|
||||||
|
/// Domains could be a similar array of DomainValues.
|
||||||
|
/// It makes sense with an array where indexing is O(1)
|
||||||
|
|
||||||
pub struct Problem<'a, V, K> {
|
pub struct Problem<'a, V, K> {
|
||||||
/// The initial assignements map
|
/// The initial assignements map
|
||||||
variables: Variables<'a, V, K>,
|
variables: Variables<'a, V, K>,
|
||||||
@@ -72,7 +116,7 @@ pub struct Problem<'a, V, K> {
|
|||||||
constraints: Vec<Constraint<'a,V,K>>,
|
constraints: Vec<Constraint<'a,V,K>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a,V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
impl<'a, V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
||||||
|
|
||||||
pub fn build() -> ProblemBuilder<'a,V, K> {
|
pub fn build() -> ProblemBuilder<'a,V, K> {
|
||||||
ProblemBuilder::new()
|
ProblemBuilder::new()
|
||||||
@@ -88,22 +132,37 @@ impl<'a,V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
|||||||
/// a Clear to ensure the variable is unset before when leaving the branch.
|
/// a Clear to ensure the variable is unset before when leaving the branch.
|
||||||
fn _push_updates(&self) -> Option<Vec<Assignment<'a,V, K>>> {
|
fn _push_updates(&self) -> Option<Vec<Assignment<'a,V, K>>> {
|
||||||
// TODO: should be able to inject a choosing strategy
|
// TODO: should be able to inject a choosing strategy
|
||||||
if let Some((key,_)) = self.variables.iter().find(|(_, val)| val.is_none()) {
|
if let Some(key) = self._next_assign() {
|
||||||
let domain_values = self.domains.get(key).expect("No domain for variable !");
|
let domain_values = self.domains
|
||||||
|
.get(key)
|
||||||
|
.expect("No domain for variable !");
|
||||||
|
assert!(!domain_values.is_empty());
|
||||||
// Push a clear assignment first, just before going up the stack.
|
// Push a clear assignment first, just before going up the stack.
|
||||||
let mut updates = vec![Assignment::Clear(key.clone())];
|
let mut updates = vec![Assignment::Clear(key.clone())];
|
||||||
|
|
||||||
if domain_values.is_empty() { panic!("No value in domain !"); }
|
|
||||||
// TODO: should be able to filter domain values (inference, pertinence)
|
// TODO: should be able to filter domain values (inference, pertinence)
|
||||||
for value in domain_values.into_iter() {
|
domain_values.iter().for_each(|value| {
|
||||||
updates.push(Assignment::Update(key.clone(), *value));
|
updates.push(
|
||||||
}
|
Assignment::Update(
|
||||||
|
key.clone(),
|
||||||
|
*value
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
Some(updates)
|
Some(updates)
|
||||||
} else { // End of assignements
|
} else { // End of assignements
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn _next_assign(&self) -> Option<&K> {
|
||||||
|
self.variables
|
||||||
|
.iter()
|
||||||
|
.find_map(|(key, val)| {
|
||||||
|
if val.is_none() { Some(key) }
|
||||||
|
else { None }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Checks that the current assignments doesn't violate any constraint
|
/// Checks that the current assignments doesn't violate any constraint
|
||||||
fn _is_valid(&self) -> bool {
|
fn _is_valid(&self) -> bool {
|
||||||
for validator in self.constraints.iter() {
|
for validator in self.constraints.iter() {
|
||||||
|
|||||||
@@ -151,7 +151,8 @@ mod api {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
let ini = if let Some(id) = initial_id {
|
let ini = if let Some(id) = initial_id {
|
||||||
let new_ini = domain.values.iter().find(|r| r.id == id);
|
// Not working because we're capturing `id`
|
||||||
|
let new_ini = domain.find(|r| {r.id == id});
|
||||||
println!("Overrided {:?}", new_ini);
|
println!("Overrided {:?}", new_ini);
|
||||||
new_ini
|
new_ini
|
||||||
} else {
|
} else {
|
||||||
@@ -184,7 +185,7 @@ fn main() -> Result<(), Error> {
|
|||||||
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["http://localhost:8080"]);
|
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["http://localhost:8080"]);
|
||||||
assert!(failed_origins.is_empty());
|
assert!(failed_origins.is_empty());
|
||||||
|
|
||||||
// You can also deserialize this
|
// You can also deserialize this
|
||||||
let cors = rocket_cors::CorsOptions {
|
let cors = rocket_cors::CorsOptions {
|
||||||
allowed_origins: allowed_origins,
|
allowed_origins: allowed_origins,
|
||||||
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
||||||
|
|||||||
Reference in New Issue
Block a user