1use std::hash::Hash;
7use std::marker::PhantomData;
8
9use solverforge_core::score::Score;
10use solverforge_core::ImpactType;
11
12use super::collection_extract::CollectionExtract;
13use super::collector::{Accumulator, Collector};
14use super::filter::UniFilter;
15use super::relational::leaf_collector::{leaf_key, LeafCollector, LeafFilter, OptionalEntityKey};
16use super::relational::operator::{CollectionNode, ComplementNode, FilterNode, GroupNode};
17use super::relational::view_plan::{GroupKey, ViewEqualPlan};
18use super::relational::Leaf;
19use super::weighting_support::ConstraintWeight;
20use crate::constraint::relational::{
21 OperatorScorer, OptionalComplementAdapter, OptionalComplementWeight, SharedOperatorBuilder,
22 SharedOperatorTerminal,
23};
24
25pub struct ComplementedConstraintStream<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc>
115where
116 Sc: Score,
117{
118 extractor_a: EA,
119 filter: Fi,
120 extractor_b: EB,
121 key_a: KA,
122 key_b: KB,
123 collector: C,
124 default_fn: D,
125 _phantom: PhantomData<(
126 fn() -> S,
127 fn() -> A,
128 fn() -> B,
129 fn() -> K,
130 fn() -> V,
131 fn() -> R,
132 fn() -> Acc,
133 fn() -> Sc,
134 )>,
135}
136
137impl<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc>
138 ComplementedConstraintStream<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc>
139where
140 S: Send + Sync + 'static,
141 A: Send + Sync + 'static,
142 B: Send + Sync + 'static,
143 K: Clone + Eq + Hash + Send + Sync + 'static,
144 EA: CollectionExtract<S, Item = A>,
145 Fi: UniFilter<S, A>,
146 EB: CollectionExtract<S, Item = B>,
147 KA: Fn(&A) -> Option<K> + Send + Sync,
148 KB: Fn(&B) -> K + Send + Sync,
149 C: for<'i> Collector<&'i A, Value = V, Result = R, Accumulator = Acc> + Send + Sync + 'static,
150 V: Send + Sync + 'static,
151 R: Send + Sync + 'static,
152 Acc: Accumulator<V, R> + Send + Sync + 'static,
153 D: Fn(&B) -> R + Send + Sync + 'static,
154 Sc: Score + 'static,
155{
156 fn into_weighted_builder<W>(
157 self,
158 impact_type: ImpactType,
159 weight_fn: W,
160 is_hard: bool,
161 ) -> ComplementedConstraintBuilder<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, W, Sc>
162 where
163 W: Fn(&K, &R) -> Sc + Send + Sync,
164 {
165 ComplementedConstraintBuilder {
166 extractor_a: self.extractor_a,
167 filter: self.filter,
168 extractor_b: self.extractor_b,
169 key_a: self.key_a,
170 key_b: self.key_b,
171 collector: self.collector,
172 default_fn: self.default_fn,
173 impact_type,
174 weight_fn,
175 is_hard,
176 _phantom: PhantomData,
177 }
178 }
179
180 pub(crate) fn new(
182 extractor_a: EA,
183 filter: Fi,
184 extractor_b: EB,
185 key_a: KA,
186 key_b: KB,
187 collector: C,
188 default_fn: D,
189 ) -> Self {
190 Self {
191 extractor_a,
192 filter,
193 extractor_b,
194 key_a,
195 key_b,
196 collector,
197 default_fn,
198 _phantom: PhantomData,
199 }
200 }
201
202 pub fn penalize<W>(
208 self,
209 weight: W,
210 ) -> ComplementedConstraintBuilder<
211 S,
212 A,
213 B,
214 K,
215 EA,
216 Fi,
217 EB,
218 KA,
219 KB,
220 C,
221 V,
222 R,
223 Acc,
224 D,
225 impl Fn(&K, &R) -> Sc + Send + Sync,
226 Sc,
227 >
228 where
229 W: for<'w> ConstraintWeight<(&'w K, &'w R), Sc> + Send + Sync,
230 {
231 let is_hard = weight.is_hard();
232 self.into_weighted_builder(
233 ImpactType::Penalty,
234 crate::stream::closures::weight2::<K, R, Sc, _>(weight),
235 is_hard,
236 )
237 }
238
239 pub fn reward<W>(
245 self,
246 weight: W,
247 ) -> ComplementedConstraintBuilder<
248 S,
249 A,
250 B,
251 K,
252 EA,
253 Fi,
254 EB,
255 KA,
256 KB,
257 C,
258 V,
259 R,
260 Acc,
261 D,
262 impl Fn(&K, &R) -> Sc + Send + Sync,
263 Sc,
264 >
265 where
266 W: for<'w> ConstraintWeight<(&'w K, &'w R), Sc> + Send + Sync,
267 {
268 let is_hard = weight.is_hard();
269 self.into_weighted_builder(
270 ImpactType::Reward,
271 crate::stream::closures::weight2::<K, R, Sc, _>(weight),
272 is_hard,
273 )
274 }
275}
276
277impl<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc: Score> std::fmt::Debug
278 for ComplementedConstraintStream<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, Sc>
279{
280 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
281 f.debug_struct("ComplementedConstraintStream").finish()
282 }
283}
284
285pub struct ComplementedConstraintBuilder<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, W, Sc>
287where
288 Sc: Score,
289{
290 extractor_a: EA,
291 filter: Fi,
292 extractor_b: EB,
293 key_a: KA,
294 key_b: KB,
295 collector: C,
296 default_fn: D,
297 impact_type: ImpactType,
298 weight_fn: W,
299 is_hard: bool,
300 _phantom: PhantomData<(
301 fn() -> S,
302 fn() -> A,
303 fn() -> B,
304 fn() -> K,
305 fn() -> V,
306 fn() -> R,
307 fn() -> Acc,
308 fn() -> Sc,
309 )>,
310}
311
312impl<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, W, Sc>
313 ComplementedConstraintBuilder<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, W, Sc>
314where
315 S: Send + Sync + 'static,
316 A: std::fmt::Debug + Send + Sync + 'static,
317 B: std::fmt::Debug + Send + Sync + 'static,
318 K: Clone + Eq + Hash + Send + Sync + 'static,
319 EA: CollectionExtract<S, Item = A> + 'static,
320 Fi: UniFilter<S, A> + 'static,
321 EB: CollectionExtract<S, Item = B> + 'static,
322 KA: Fn(&A) -> Option<K> + Send + Sync + 'static,
323 KB: Fn(&B) -> K + Send + Sync + Clone + 'static,
324 C: for<'i> Collector<&'i A, Value = V, Result = R, Accumulator = Acc> + Send + Sync + 'static,
325 V: Send + Sync + 'static,
326 R: Send + Sync + 'static,
327 Acc: Accumulator<V, R> + Send + Sync + 'static,
328 D: Fn(&B) -> R + Send + Sync + 'static,
329 W: Fn(&K, &R) -> Sc + Send + Sync,
330 Sc: Score + 'static,
331{
332 pub fn named(
336 self,
337 name: &str,
338 ) -> SharedOperatorTerminal<
339 S,
340 ComplementNode<
341 S,
342 CollectionNode<S, EB>,
343 GroupNode<
344 FilterNode<CollectionNode<S, EA>, LeafFilter<S, A, Fi>>,
345 impl for<'l> Fn(&Leaf<'l, A>) -> Option<K> + Send + Sync,
346 LeafCollector<C, V, R, Acc>,
347 Option<K>,
348 Acc,
349 V,
350 R,
351 >,
352 ViewEqualPlan<Option<K>, OptionalEntityKey<KB>, GroupKey>,
353 impl for<'l> Fn(&S, &Leaf<'l, B>) -> R + Send + Sync,
354 R,
355 >,
356 OptionalComplementAdapter<KB, K, R>,
357 OperatorScorer<OptionalComplementWeight<W, KB>, Sc>,
358 Sc,
359 > {
360 let key_a = self.key_a;
361 let key_b = self.key_b;
362 let default_fn = self.default_fn;
363 let group = GroupNode::new(
364 FilterNode::new(
365 CollectionNode::new(self.extractor_a, 0),
366 LeafFilter::new(self.filter),
367 ),
368 leaf_key(key_a),
369 LeafCollector::new(self.collector),
370 );
371 let targets = CollectionNode::new(self.extractor_b, 1);
372 let tree = ComplementNode::new(
373 targets,
374 group,
375 ViewEqualPlan::new(OptionalEntityKey::new(key_b.clone()), GroupKey),
376 crate::stream::closures::leaf_default::<S, B, R, _>(default_fn),
377 );
378 SharedOperatorBuilder::first_with_hard(
379 tree,
380 OptionalComplementAdapter::new(key_b),
381 self.impact_type,
382 self.weight_fn,
383 self.is_hard,
384 )
385 .first_named(name)
386 }
387
388 }
390
391impl<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, W, Sc: Score> std::fmt::Debug
392 for ComplementedConstraintBuilder<S, A, B, K, EA, Fi, EB, KA, KB, C, V, R, Acc, D, W, Sc>
393{
394 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
395 f.debug_struct("ComplementedConstraintBuilder")
396 .field("impact_type", &self.impact_type)
397 .finish()
398 }
399}