1use std::marker::PhantomData;
12
13use solverforge_core::score::Score;
14use solverforge_core::{ConstraintRef, ImpactType};
15
16use super::relational::operator::ExistenceNode;
17use super::relational::Leaf;
18use super::weighting_support::ConstraintWeight;
19use crate::constraint::relational::OperatorTerminal;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum ExistenceMode {
24 Exists,
26 NotExists,
28}
29
30pub struct ExistsConstraintStream<S, A, K, L, R, LK, RK, Sc>
34where
35 Sc: Score,
36{
37 pub(super) mode: ExistenceMode,
38 pub(super) left: L,
39 pub(super) right: R,
40 pub(super) left_key: LK,
41 pub(super) right_keys: RK,
42 pub(super) _phantom: PhantomData<(fn() -> S, fn() -> A, fn() -> K, fn() -> Sc)>,
43}
44
45impl<S, A, K, L, R, LK, RK, Sc> ExistsConstraintStream<S, A, K, L, R, LK, RK, Sc>
46where
47 Sc: Score,
48{
49 pub(super) fn new(
50 mode: ExistenceMode,
51 left: L,
52 right: R,
53 left_key: LK,
54 right_keys: RK,
55 ) -> Self {
56 Self {
57 mode,
58 left,
59 right,
60 left_key,
61 right_keys,
62 _phantom: PhantomData,
63 }
64 }
65}
66
67impl<S, A, K, L, R, LK, RK, Sc> ExistsConstraintStream<S, A, K, L, R, LK, RK, Sc>
68where
69 S: Send + Sync + 'static,
70 A: Clone + Send + Sync + 'static,
71 K: Eq + std::hash::Hash + Clone + Send + Sync + 'static,
72 L: super::relational::operator::Operator<S>,
73 for<'a> LK: super::relational::view_plan::ViewKey<L::View<'a>, Key = K>,
74 R: super::relational::operator::Operator<S>,
75 for<'a> RK: crate::stream::relational::operator::RightKeySet<R::View<'a>, Key = K>,
76 Sc: Score + 'static,
77{
78 fn into_weighted_builder<W>(
79 self,
80 impact_type: ImpactType,
81 weight: W,
82 is_hard: bool,
83 ) -> ExistsConstraintBuilder<S, A, K, L, R, LK, RK, W, Sc>
84 where
85 W: for<'a> Fn(&S, &Leaf<'a, A>) -> Sc,
86 {
87 ExistsConstraintBuilder {
88 mode: self.mode,
89 left: self.left,
90 right: self.right,
91 left_key: self.left_key,
92 right_keys: self.right_keys,
93 impact_type,
94 weight,
95 is_hard,
96 _phantom: PhantomData,
97 }
98 }
99
100 pub fn penalize<W>(
106 self,
107 weight: W,
108 ) -> ExistsConstraintBuilder<
109 S,
110 A,
111 K,
112 L,
113 R,
114 LK,
115 RK,
116 impl for<'a> Fn(&S, &Leaf<'a, A>) -> Sc + Send + Sync,
117 Sc,
118 >
119 where
120 W: for<'w> ConstraintWeight<(&'w A,), Sc> + Send + Sync,
121 {
122 let is_hard = weight.is_hard();
123 self.into_weighted_builder(
124 ImpactType::Penalty,
125 move |_s: &S, row: &Leaf<'_, A>| weight.score((row.entity,)),
126 is_hard,
127 )
128 }
129
130 pub fn reward<W>(
136 self,
137 weight: W,
138 ) -> ExistsConstraintBuilder<
139 S,
140 A,
141 K,
142 L,
143 R,
144 LK,
145 RK,
146 impl for<'a> Fn(&S, &Leaf<'a, A>) -> Sc + Send + Sync,
147 Sc,
148 >
149 where
150 W: for<'w> ConstraintWeight<(&'w A,), Sc> + Send + Sync,
151 {
152 let is_hard = weight.is_hard();
153 self.into_weighted_builder(
154 ImpactType::Reward,
155 move |_s: &S, row: &Leaf<'_, A>| weight.score((row.entity,)),
156 is_hard,
157 )
158 }
159}
160
161pub struct ExistsConstraintBuilder<S, A, K, L, R, LK, RK, W, Sc>
163where
164 Sc: Score,
165{
166 mode: ExistenceMode,
167 left: L,
168 right: R,
169 left_key: LK,
170 right_keys: RK,
171 impact_type: ImpactType,
172 weight: W,
173 is_hard: bool,
174 _phantom: PhantomData<(fn() -> S, fn() -> A, fn() -> K, fn() -> Sc)>,
175}
176
177impl<S, A, K, L, R, LK, RK, W, Sc> ExistsConstraintBuilder<S, A, K, L, R, LK, RK, W, Sc>
178where
179 S: Send + Sync + 'static,
180 A: 'static,
181 K: Eq + std::hash::Hash + Clone + Send + Sync + 'static,
182 L: super::relational::operator::Operator<S>,
183 for<'a> LK: super::relational::view_plan::ViewKey<L::View<'a>, Key = K>,
184 R: super::relational::operator::Operator<S>,
185 for<'a> RK: crate::stream::relational::operator::RightKeySet<R::View<'a>, Key = K>,
186 W: for<'a> Fn(&S, &Leaf<'a, A>) -> Sc + Send + Sync,
187 Sc: Score + 'static,
188{
189 pub fn named(
192 self,
193 name: &str,
194 ) -> OperatorTerminal<S, ExistenceNode<S, L, R, K, LK, RK>, W, Sc> {
195 let exists = matches!(self.mode, ExistenceMode::Exists);
196 let node = ExistenceNode::new(
197 self.left,
198 self.right,
199 self.left_key,
200 self.right_keys,
201 exists,
202 );
203 OperatorTerminal::new(
204 ConstraintRef::new("", name),
205 self.impact_type,
206 node,
207 self.weight,
208 self.is_hard,
209 )
210 }
211}
212
213impl<S, A, K, L, R, LK, RK, Sc: Score> std::fmt::Debug
214 for ExistsConstraintStream<S, A, K, L, R, LK, RK, Sc>
215{
216 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
217 f.debug_struct("ExistsConstraintStream")
218 .field("mode", &self.mode)
219 .finish()
220 }
221}
222
223impl<S, A, K, L, R, LK, RK, W, Sc: Score> std::fmt::Debug
224 for ExistsConstraintBuilder<S, A, K, L, R, LK, RK, W, Sc>
225{
226 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
227 f.debug_struct("ExistsConstraintBuilder")
228 .field("mode", &self.mode)
229 .field("impact_type", &self.impact_type)
230 .finish()
231 }
232}