pub struct IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>where
Sc: Score,{ /* private fields */ }Expand description
Zero-erasure stream for building if_exists/if_not_exists constraints.
Created by UniConstraintStream::if_exists() or if_not_exists().
Filters A entities based on whether a matching B exists.
§Type Parameters
S- Solution typeA- Primary entity type (scored)B- Secondary entity type (checked for existence)K- Join key typeEA- Extractor for A entitiesEB- Extractor for B entities (returns Vec for filtering)KA- Key extractor for AKB- Key extractor for BFA- Filter on A entitiesSc- Score type
§Example
use solverforge_scoring::stream::ConstraintFactory;
use solverforge_scoring::stream::joiner::equal_bi;
use solverforge_scoring::api::constraint_set::IncrementalConstraint;
use solverforge_core::score::SimpleScore;
#[derive(Clone)]
struct Shift { id: usize, employee_idx: Option<usize> }
#[derive(Clone)]
struct Employee { id: usize, on_vacation: bool }
#[derive(Clone)]
struct Schedule { shifts: Vec<Shift>, employees: Vec<Employee> }
// Penalize shifts assigned to employees who are on vacation
let constraint = ConstraintFactory::<Schedule, SimpleScore>::new()
.for_each(|s: &Schedule| s.shifts.as_slice())
.filter(|shift: &Shift| shift.employee_idx.is_some())
.if_exists_filtered(
|s: &Schedule| s.employees.iter().filter(|e| e.on_vacation).cloned().collect(),
equal_bi(
|shift: &Shift| shift.employee_idx,
|emp: &Employee| Some(emp.id),
),
)
.penalize(SimpleScore::of(1))
.as_constraint("Vacation conflict");
let schedule = Schedule {
shifts: vec![
Shift { id: 0, employee_idx: Some(0) },
Shift { id: 1, employee_idx: Some(1) },
Shift { id: 2, employee_idx: None },
],
employees: vec![
Employee { id: 0, on_vacation: true },
Employee { id: 1, on_vacation: false },
],
};
// Shift 0 is assigned to employee 0 who is on vacation
assert_eq!(constraint.evaluate(&schedule), SimpleScore::of(-1));Implementations§
Source§impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
B: Clone + Send + Sync + 'static,
K: Eq + Hash + Clone + Send + Sync,
EA: Fn(&S) -> &[A] + Send + Sync,
EB: Fn(&S) -> Vec<B> + Send + Sync,
KA: Fn(&A) -> K + Send + Sync,
KB: Fn(&B) -> K + Send + Sync,
FA: UniFilter<A>,
Sc: Score + 'static,
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
B: Clone + Send + Sync + 'static,
K: Eq + Hash + Clone + Send + Sync,
EA: Fn(&S) -> &[A] + Send + Sync,
EB: Fn(&S) -> Vec<B> + Send + Sync,
KA: Fn(&A) -> K + Send + Sync,
KB: Fn(&B) -> K + Send + Sync,
FA: UniFilter<A>,
Sc: Score + 'static,
Sourcepub fn penalize(
self,
weight: Sc,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>where
Sc: Clone,
pub fn penalize(
self,
weight: Sc,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>where
Sc: Clone,
Penalizes each matching entity with a fixed weight.
Sourcepub fn penalize_with<W>(
self,
weight_fn: W,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
pub fn penalize_with<W>( self, weight_fn: W, ) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
Penalizes each matching entity with a dynamic weight.
Sourcepub fn penalize_hard_with<W>(
self,
weight_fn: W,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
pub fn penalize_hard_with<W>( self, weight_fn: W, ) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
Penalizes each matching entity with a dynamic weight, explicitly marked as hard.
Sourcepub fn reward(
self,
weight: Sc,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>where
Sc: Clone,
pub fn reward(
self,
weight: Sc,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, impl Fn(&A) -> Sc + Send + Sync, Sc>where
Sc: Clone,
Rewards each matching entity with a fixed weight.
Sourcepub fn reward_with<W>(
self,
weight_fn: W,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
pub fn reward_with<W>( self, weight_fn: W, ) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
Rewards each matching entity with a dynamic weight.
Sourcepub fn reward_hard_with<W>(
self,
weight_fn: W,
) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
pub fn reward_hard_with<W>( self, weight_fn: W, ) -> IfExistsBuilder<S, A, B, K, EA, EB, KA, KB, FA, W, Sc>
Rewards each matching entity with a dynamic weight, explicitly marked as hard.
Trait Implementations§
Auto Trait Implementations§
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> Freeze for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> RefUnwindSafe for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>where
EA: RefUnwindSafe,
EB: RefUnwindSafe,
KA: RefUnwindSafe,
KB: RefUnwindSafe,
FA: RefUnwindSafe,
S: RefUnwindSafe,
A: RefUnwindSafe,
B: RefUnwindSafe,
K: RefUnwindSafe,
Sc: RefUnwindSafe,
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> Send for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> Sync for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> Unpin for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>
impl<S, A, B, K, EA, EB, KA, KB, FA, Sc> UnwindSafe for IfExistsStream<S, A, B, K, EA, EB, KA, KB, FA, Sc>where
EA: UnwindSafe,
EB: UnwindSafe,
KA: UnwindSafe,
KB: UnwindSafe,
FA: UnwindSafe,
S: UnwindSafe,
A: UnwindSafe,
B: UnwindSafe,
K: UnwindSafe,
Sc: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more