use std::hash::Hash;
use std::marker::PhantomData;
use solverforge_core::score::Score;
use solverforge_core::{ConstraintRef, ImpactType};
use super::collection_extract::CollectionExtract;
use super::collector::UniCollector;
use super::complemented_stream::ComplementedConstraintStream;
use super::filter::UniFilter;
use crate::constraint::grouped::GroupedUniConstraint;
pub struct GroupedConstraintStream<S, A, K, E, Fi, KF, C, Sc>
where
Sc: Score,
{
extractor: E,
filter: Fi,
key_fn: KF,
collector: C,
_phantom: PhantomData<(fn() -> S, fn() -> A, fn() -> K, fn() -> Sc)>,
}
impl<S, A, K, E, Fi, KF, C, Sc> GroupedConstraintStream<S, A, K, E, Fi, KF, C, Sc>
where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
K: Clone + Eq + Hash + Send + Sync + 'static,
E: CollectionExtract<S, Item = A>,
Fi: UniFilter<S, A>,
KF: Fn(&A) -> K + Send + Sync,
C: UniCollector<A> + Send + Sync + 'static,
C::Accumulator: Send + Sync,
C::Result: Clone + Send + Sync,
Sc: Score + 'static,
{
pub(crate) fn new(extractor: E, filter: Fi, key_fn: KF, collector: C) -> Self {
Self {
extractor,
filter,
key_fn,
collector,
_phantom: PhantomData,
}
}
pub fn penalize_with<W>(
self,
weight_fn: W,
) -> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, W, Sc>
where
W: Fn(&C::Result) -> Sc + Send + Sync,
{
GroupedConstraintBuilder {
extractor: self.extractor,
filter: self.filter,
key_fn: self.key_fn,
collector: self.collector,
impact_type: ImpactType::Penalty,
weight_fn,
is_hard: false,
expected_descriptor: None,
_phantom: PhantomData,
}
}
pub fn penalize_hard_with<W>(
self,
weight_fn: W,
) -> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, W, Sc>
where
W: Fn(&C::Result) -> Sc + Send + Sync,
{
GroupedConstraintBuilder {
extractor: self.extractor,
filter: self.filter,
key_fn: self.key_fn,
collector: self.collector,
impact_type: ImpactType::Penalty,
weight_fn,
is_hard: true,
expected_descriptor: None,
_phantom: PhantomData,
}
}
pub fn reward_with<W>(
self,
weight_fn: W,
) -> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, W, Sc>
where
W: Fn(&C::Result) -> Sc + Send + Sync,
{
GroupedConstraintBuilder {
extractor: self.extractor,
filter: self.filter,
key_fn: self.key_fn,
collector: self.collector,
impact_type: ImpactType::Reward,
weight_fn,
is_hard: false,
expected_descriptor: None,
_phantom: PhantomData,
}
}
pub fn reward_hard_with<W>(
self,
weight_fn: W,
) -> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, W, Sc>
where
W: Fn(&C::Result) -> Sc + Send + Sync,
{
GroupedConstraintBuilder {
extractor: self.extractor,
filter: self.filter,
key_fn: self.key_fn,
collector: self.collector,
impact_type: ImpactType::Reward,
weight_fn,
is_hard: true,
expected_descriptor: None,
_phantom: PhantomData,
}
}
pub fn penalize_hard(
self,
) -> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, impl Fn(&C::Result) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
let w = Sc::one_hard();
self.penalize_hard_with(move |_: &C::Result| w)
}
pub fn penalize_soft(
self,
) -> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, impl Fn(&C::Result) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
let w = Sc::one_soft();
self.penalize_with(move |_: &C::Result| w)
}
pub fn reward_hard(
self,
) -> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, impl Fn(&C::Result) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
let w = Sc::one_hard();
self.reward_hard_with(move |_: &C::Result| w)
}
pub fn reward_soft(
self,
) -> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, impl Fn(&C::Result) -> Sc + Send + Sync, Sc>
where
Sc: Copy,
{
let w = Sc::one_soft();
self.reward_with(move |_: &C::Result| w)
}
pub fn complement<B, EB, KB, D>(
self,
extractor_b: EB,
key_b: KB,
default_fn: D,
) -> ComplementedConstraintStream<
S,
A,
B,
K,
E,
EB,
impl Fn(&A) -> Option<K> + Send + Sync,
KB,
C,
D,
Sc,
>
where
B: Clone + Send + Sync + 'static,
EB: CollectionExtract<S, Item = B>,
KB: Fn(&B) -> K + Send + Sync,
D: Fn(&B) -> C::Result + Send + Sync,
{
let key_fn = self.key_fn;
let wrapped_key_fn = move |a: &A| Some((key_fn)(a));
ComplementedConstraintStream::new(
self.extractor,
extractor_b,
wrapped_key_fn,
key_b,
self.collector,
default_fn,
)
}
pub fn complement_with_key<B, EB, KA2, KB, D>(
self,
extractor_b: EB,
key_a: KA2,
key_b: KB,
default_fn: D,
) -> ComplementedConstraintStream<S, A, B, K, E, EB, KA2, KB, C, D, Sc>
where
B: Clone + Send + Sync + 'static,
EB: CollectionExtract<S, Item = B>,
KA2: Fn(&A) -> Option<K> + Send + Sync,
KB: Fn(&B) -> K + Send + Sync,
D: Fn(&B) -> C::Result + Send + Sync,
{
ComplementedConstraintStream::new(
self.extractor,
extractor_b,
key_a,
key_b,
self.collector,
default_fn,
)
}
}
impl<S, A, K, E, Fi, KF, C, Sc: Score> std::fmt::Debug
for GroupedConstraintStream<S, A, K, E, Fi, KF, C, Sc>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GroupedConstraintStream").finish()
}
}
pub struct GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, W, Sc>
where
Sc: Score,
{
extractor: E,
filter: Fi,
key_fn: KF,
collector: C,
impact_type: ImpactType,
weight_fn: W,
is_hard: bool,
expected_descriptor: Option<usize>,
_phantom: PhantomData<(fn() -> S, fn() -> A, fn() -> K, fn() -> Sc)>,
}
impl<S, A, K, E, Fi, KF, C, W, Sc> GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, W, Sc>
where
S: Send + Sync + 'static,
A: Clone + Send + Sync + 'static,
K: Clone + Eq + Hash + Send + Sync + 'static,
E: CollectionExtract<S, Item = A>,
Fi: UniFilter<S, A>,
KF: Fn(&A) -> K + Send + Sync,
C: UniCollector<A> + Send + Sync + 'static,
C::Accumulator: Send + Sync,
C::Result: Clone + Send + Sync,
W: Fn(&C::Result) -> Sc + Send + Sync,
Sc: Score + 'static,
{
pub fn named(self, name: &str) -> GroupedUniConstraint<S, A, K, E, Fi, KF, C, W, Sc> {
let mut constraint = GroupedUniConstraint::new(
ConstraintRef::new("", name),
self.impact_type,
self.extractor,
self.filter,
self.key_fn,
self.collector,
self.weight_fn,
self.is_hard,
);
if let Some(d) = self.expected_descriptor {
constraint = constraint.with_descriptor(d);
}
constraint
}
pub fn for_descriptor(mut self, descriptor_index: usize) -> Self {
self.expected_descriptor = Some(descriptor_index);
self
}
}
impl<S, A, K, E, Fi, KF, C, W, Sc: Score> std::fmt::Debug
for GroupedConstraintBuilder<S, A, K, E, Fi, KF, C, W, Sc>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GroupedConstraintBuilder")
.field("impact_type", &self.impact_type)
.finish()
}
}