solverforge_scoring/stream/factory.rs
1//! Constraint factory for creating monomorphized constraint streams.
2//!
3//! The factory is the entry point for the fluent constraint API.
4
5use std::marker::PhantomData;
6
7use solverforge_core::score::Score;
8
9use super::collection_extract::CollectionExtract;
10use super::filter::TrueFilter;
11use super::UniConstraintStream;
12
13/// Factory for creating constraint streams.
14///
15/// `ConstraintFactory` is parameterized by the solution type `S` and score type `Sc`.
16/// It serves as the entry point for defining constraints using the fluent API.
17///
18/// # Example
19///
20/// ```
21/// use solverforge_core::score::SoftScore;
22/// use solverforge_scoring::api::constraint_set::IncrementalConstraint;
23/// use solverforge_scoring::stream::ConstraintFactory;
24///
25/// #[derive(Clone)]
26/// struct Solution {
27/// values: Vec<Option<i32>>,
28/// }
29/// # use solverforge_scoring::stream::{source, ChangeSource, CollectionExtract};
30/// # impl Solution {
31/// # fn values() -> impl CollectionExtract<Solution, Item = Option<i32>> {
32/// # fn values(s: &Solution) -> &[Option<i32>] {
33/// # s.values.as_slice()
34/// # }
35/// # source(values as fn(&Solution) -> &[Option<i32>], ChangeSource::Descriptor(0))
36/// # }
37/// # }
38///
39/// let factory = ConstraintFactory::<Solution, SoftScore>::new();
40///
41/// let constraint = factory
42/// .for_each(Solution::values())
43/// .filter(|v: &Option<i32>| v.is_none())
44/// .penalize(SoftScore::of(1))
45/// .named("Unassigned");
46///
47/// let solution = Solution {
48/// values: vec![Some(1), None, None],
49/// };
50/// assert_eq!(constraint.evaluate(&solution), SoftScore::of(-2));
51/// ```
52pub struct ConstraintFactory<S, Sc: Score> {
53 _phantom: PhantomData<(fn() -> S, fn() -> Sc)>,
54}
55
56impl<S, Sc> ConstraintFactory<S, Sc>
57where
58 S: Send + Sync + 'static,
59 Sc: Score + 'static,
60{
61 /// Creates a new constraint factory.
62 pub fn new() -> Self {
63 Self {
64 _phantom: PhantomData,
65 }
66 }
67
68 /// Creates a zero-erasure uni-constraint stream over a collection source.
69 ///
70 /// For macro-generated models, pass the inherent solution source method:
71 /// `ConstraintFactory::new().for_each(Schedule::shifts())`.
72 /// Low-level callers can still pass any `CollectionExtract<S, Item = A>`.
73 /// The extractor type is preserved as a concrete generic for full zero-erasure.
74 pub fn for_each<A, E>(self, extractor: E) -> UniConstraintStream<S, A, E, TrueFilter, Sc>
75 where
76 A: Clone + Send + Sync + 'static,
77 E: CollectionExtract<S, Item = A>,
78 {
79 UniConstraintStream::new(extractor)
80 }
81}
82
83impl<S, Sc> Default for ConstraintFactory<S, Sc>
84where
85 S: Send + Sync + 'static,
86 Sc: Score + 'static,
87{
88 fn default() -> Self {
89 Self::new()
90 }
91}
92
93impl<S, Sc: Score> Clone for ConstraintFactory<S, Sc> {
94 fn clone(&self) -> Self {
95 Self {
96 _phantom: PhantomData,
97 }
98 }
99}
100
101impl<S, Sc: Score> std::fmt::Debug for ConstraintFactory<S, Sc> {
102 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103 f.debug_struct("ConstraintFactory").finish()
104 }
105}