Skip to main content

solverforge_scoring/stream/
existence_target.rs

1//! Existence targets: turn a left filtered stream plus a right stream (direct
2//! or flattened) into a count-backed existence constraint.
3//!
4//! The relationship is a named pair of key adapters over the two operators'
5//! views. The left input is a `CollectionNode` over the left stream (whose
6//! membership is the stream's own filter); the right input is a `CollectionNode`
7//! over the right stream, or the parent collection with a `ParentChildKeys`
8//! adapter when the right stream is flattened. Right-side children are read
9//! straight from the solution and never become rows.
10
11use std::hash::Hash;
12use std::marker::PhantomData;
13
14use solverforge_core::score::Score;
15
16use super::collection_extract::{CollectionExtract, FlattenExtract};
17use super::existence_stream::{ExistenceMode, ExistsConstraintStream};
18use super::filter::UniFilter;
19use super::joiner::EqualJoiner;
20use super::relational::operator::{CollectionNode, ParentChildKeys, UnitKeys};
21use super::relational::view_plan::EntityKey;
22use super::uni_stream::UniConstraintStream;
23
24/// A stream flattened into child items `B`, usable as the right side of
25/// `if_exists`/`if_not_exists`. Built by
26/// [`UniConstraintStream::flattened`](crate::stream::UniConstraintStream::flattened).
27pub struct FlattenedCollectionTarget<S, P, B, EP, FP, Flatten, Sc>
28where
29    Sc: Score,
30{
31    pub(crate) right_stream: UniConstraintStream<S, P, EP, FP, Sc>,
32    pub(crate) flatten: Flatten,
33    pub(crate) _phantom: PhantomData<(fn() -> B, fn() -> Sc)>,
34}
35
36/// Right-hand side of [`if_exists`](crate::stream::UniConstraintStream::if_exists)
37/// and [`if_not_exists`](crate::stream::UniConstraintStream::if_not_exists).
38pub trait ExistenceTarget<S, A, EA, FA, Sc: Score>
39where
40    EA: CollectionExtract<S, Item = A>,
41    FA: UniFilter<S, A>,
42{
43    /// The existence stream produced.
44    type Output;
45
46    /// Builds the existence stream from the left stream's extractor and filter.
47    fn apply(self, mode: ExistenceMode, extractor_a: EA, filter_a: FA) -> Self::Output;
48}
49
50/// Direct existence: `.if_exists((other_stream, equal_bi(ka, kb)))`.
51///
52/// Both inputs are collections over their streams, so each input's membership is
53/// that stream's accumulated filter, and the relationship keys off the leaf views.
54impl<S, A, B, EA, FA, EB, FB, K, KA, KB, Mode, Sc> ExistenceTarget<S, A, EA, FA, Sc>
55    for (
56        UniConstraintStream<S, B, EB, FB, Sc>,
57        EqualJoiner<KA, KB, K, Mode>,
58    )
59where
60    S: Send + Sync + 'static,
61    A: Send + Sync + 'static,
62    B: Send + Sync + 'static,
63    EA: CollectionExtract<S, Item = A> + Send + Sync + 'static,
64    FA: UniFilter<S, A> + Send + Sync + 'static,
65    EB: CollectionExtract<S, Item = B> + Send + Sync + 'static,
66    FB: UniFilter<S, B> + Send + Sync + 'static,
67    K: Eq + Hash + Clone + Send + Sync + 'static,
68    KA: Fn(&A) -> K + Send + Sync + 'static,
69    KB: Fn(&B) -> K + Send + Sync + 'static,
70    Sc: Score + 'static,
71{
72    type Output = ExistsConstraintStream<
73        S,
74        A,
75        K,
76        CollectionNode<S, UniConstraintStream<S, A, EA, FA, Sc>>,
77        CollectionNode<S, UniConstraintStream<S, B, EB, FB, Sc>>,
78        EntityKey<KA>,
79        UnitKeys<EntityKey<KB>>,
80        Sc,
81    >;
82
83    fn apply(self, mode: ExistenceMode, extractor_a: EA, filter_a: FA) -> Self::Output {
84        let (right_stream, joiner) = self;
85        let (key_a, key_b) = joiner.into_keys();
86        let left_stream = UniConstraintStream::from_parts(extractor_a, filter_a);
87        let left = CollectionNode::new(left_stream, 0);
88        let right = CollectionNode::new(right_stream, 1);
89        ExistsConstraintStream::new(
90            mode,
91            left,
92            right,
93            EntityKey::new(key_a),
94            UnitKeys::new(EntityKey::new(key_b)),
95        )
96    }
97}
98
99/// Flattened existence: `.if_not_exists((stream.flattened(|p| ..), equal_bi(..)))`.
100///
101/// The right input is the parent collection; the key adapter expands each
102/// parent's children in place from the solution, so no child rows are created.
103impl<S, A, P, B, EA, FA, EP, FP, K, KA, KB, Mode, Flatten, Sc> ExistenceTarget<S, A, EA, FA, Sc>
104    for (
105        FlattenedCollectionTarget<S, P, B, EP, FP, Flatten, Sc>,
106        EqualJoiner<KA, KB, K, Mode>,
107    )
108where
109    S: Send + Sync + 'static,
110    A: Send + Sync + 'static,
111    P: Clone + Send + Sync + 'static,
112    B: Send + Sync + 'static,
113    EA: CollectionExtract<S, Item = A> + Send + Sync + 'static,
114    FA: UniFilter<S, A> + Send + Sync + 'static,
115    EP: CollectionExtract<S, Item = P> + Send + Sync + 'static,
116    FP: UniFilter<S, P> + Send + Sync + 'static,
117    K: Eq + Hash + Clone + Send + Sync + 'static,
118    KA: Fn(&A) -> K + Send + Sync + 'static,
119    KB: Fn(&B) -> K + Send + Sync + 'static,
120    Flatten: FlattenExtract<P, Item = B> + Send + Sync + 'static,
121    Sc: Score + 'static,
122{
123    type Output = ExistsConstraintStream<
124        S,
125        A,
126        K,
127        CollectionNode<S, UniConstraintStream<S, A, EA, FA, Sc>>,
128        CollectionNode<S, UniConstraintStream<S, P, EP, FP, Sc>>,
129        EntityKey<KA>,
130        ParentChildKeys<KB, Flatten>,
131        Sc,
132    >;
133
134    fn apply(self, mode: ExistenceMode, extractor_a: EA, filter_a: FA) -> Self::Output {
135        let (target, joiner) = self;
136        let FlattenedCollectionTarget {
137            right_stream,
138            flatten,
139            ..
140        } = target;
141        let (key_a, key_b) = joiner.into_keys();
142        let left_stream = UniConstraintStream::from_parts(extractor_a, filter_a);
143        let left = CollectionNode::new(left_stream, 0);
144        let right = CollectionNode::new(right_stream, 1);
145        ExistsConstraintStream::new(
146            mode,
147            left,
148            right,
149            EntityKey::new(key_a),
150            ParentChildKeys::new(key_b, flatten),
151        )
152    }
153}