-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from luleyleo/cleanup-modules
Cleanup project structure
- Loading branch information
Showing
42 changed files
with
303 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::{ | ||
framework::{components::AnyComponent, state::State}, | ||
problems::Problem, | ||
}; | ||
use serde::Serialize; | ||
|
||
pub trait Condition<P>: AnyComponent { | ||
#[allow(unused_variables)] | ||
fn initialize(&self, problem: &P, state: &mut State) {} | ||
fn evaluate(&self, problem: &P, state: &mut State) -> bool; | ||
} | ||
erased_serde::serialize_trait_object!(<P: Problem> Condition<P>); | ||
|
||
#[derive(Serialize)] | ||
#[serde(bound = "")] | ||
pub struct And<P: Problem>(Vec<Box<dyn Condition<P>>>); | ||
impl<P: Problem + 'static> And<P> { | ||
pub fn new(conditions: Vec<Box<dyn Condition<P>>>) -> Box<dyn Condition<P>> { | ||
Box::new(Self(conditions)) | ||
} | ||
} | ||
impl<P: Problem + 'static> Condition<P> for And<P> { | ||
fn initialize(&self, problem: &P, state: &mut State) { | ||
for condition in self.0.iter() { | ||
condition.initialize(problem, state); | ||
} | ||
} | ||
|
||
fn evaluate(&self, problem: &P, state: &mut State) -> bool { | ||
self.0 | ||
.iter() | ||
.all(|condition| condition.evaluate(problem, state)) | ||
} | ||
} | ||
impl<P: Problem + 'static> std::ops::BitAnd for Box<dyn Condition<P>> { | ||
type Output = Box<dyn Condition<P>>; | ||
|
||
fn bitand(self, rhs: Self) -> Self::Output { | ||
And::new(vec![self, rhs]) | ||
} | ||
} | ||
|
||
#[derive(Serialize)] | ||
#[serde(bound = "")] | ||
pub struct Or<P: Problem>(Vec<Box<dyn Condition<P>>>); | ||
impl<P: Problem + 'static> Or<P> { | ||
pub fn new(conditions: Vec<Box<dyn Condition<P>>>) -> Box<dyn Condition<P>> { | ||
Box::new(Self(conditions)) | ||
} | ||
} | ||
impl<P: Problem + 'static> Condition<P> for Or<P> { | ||
fn initialize(&self, problem: &P, state: &mut State) { | ||
for condition in self.0.iter() { | ||
condition.initialize(problem, state); | ||
} | ||
} | ||
|
||
fn evaluate(&self, problem: &P, state: &mut State) -> bool { | ||
self.0 | ||
.iter() | ||
.any(|condition| condition.evaluate(problem, state)) | ||
} | ||
} | ||
impl<P: Problem + 'static> std::ops::BitOr for Box<dyn Condition<P>> { | ||
type Output = Box<dyn Condition<P>>; | ||
|
||
fn bitor(self, rhs: Self) -> Self::Output { | ||
Or::new(vec![self, rhs]) | ||
} | ||
} |
Oops, something went wrong.