Skip to main content

solverforge_scoring/stream/
collection_extract.rs

1//! `CollectionExtract`: the source of a stream's rows.
2//!
3//! A stream source is a planning-model collection: the accessor that
4//! `#[planning_solution]` generates for each entity, fact and list-element
5//! collection (`Plan::shifts()`). Each accessor reports which descriptor owns its
6//! rows, so incremental scoring can map an entity notification to the rows it
7//! changes. A raw closure or `fn` item carries no such identity and is not a
8//! source.
9
10/// Extracts a slice of entities from the solution.
11///
12/// The associated type `Item` names the entity type, allowing callers to
13/// write `E: CollectionExtract<S, Item = A>` when `A` must be inferred from `E`
14/// rather than stated as a separate generic parameter.
15#[diagnostic::on_unimplemented(
16    message = "`{Self}` is not a stream source",
17    label = "stream sources must come from the planning model",
18    note = "pass the generated accessor, e.g. `Plan::shifts()`"
19)]
20pub trait CollectionExtract<S>: Send + Sync {
21    /// The entity type yielded by this extractor.
22    type Item;
23
24    /// Extracts the entity slice from the solution.
25    fn extract<'s>(&self, s: &'s S) -> &'s [Self::Item];
26
27    /// Reports whether this extractor's own source-level predicate accepts an item.
28    fn contains(&self, _s: &S, _item: &Self::Item) -> bool {
29        true
30    }
31
32    /// The descriptor that owns this source's rows, or `Static` when no
33    /// planning-variable change can alter them. Macro-generated solution source
34    /// methods attach it through hidden internal support.
35    fn change_source(&self) -> ChangeSource;
36}
37
38/// Preserves a unary stream's membership at the source boundary, before
39/// indexed joins build keys or materialize pairs. The original slice and index
40/// mapping remain unchanged so descriptor-local mutations keep their identity.
41#[doc(hidden)]
42pub struct FilteredExtract<E, F> {
43    extractor: E,
44    filter: F,
45}
46
47impl<E, F> FilteredExtract<E, F> {
48    pub fn new(extractor: E, filter: F) -> Self {
49        Self { extractor, filter }
50    }
51}
52
53impl<S, E, F> CollectionExtract<S> for FilteredExtract<E, F>
54where
55    E: CollectionExtract<S>,
56    F: super::filter::UniFilter<S, E::Item>,
57{
58    type Item = E::Item;
59
60    fn extract<'s>(&self, s: &'s S) -> &'s [Self::Item] {
61        self.extractor.extract(s)
62    }
63
64    fn contains(&self, s: &S, item: &Self::Item) -> bool {
65        self.extractor.contains(s, item) && self.filter.test(s, item)
66    }
67
68    fn change_source(&self) -> ChangeSource {
69        self.extractor.change_source()
70    }
71}
72
73#[doc(hidden)]
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum ChangeSource {
76    Static,
77    Descriptor(usize),
78}
79
80impl ChangeSource {
81    #[inline]
82    pub fn owns_descriptor(self, descriptor_index: usize) -> bool {
83        matches!(self, Self::Descriptor(index) if index == descriptor_index)
84    }
85
86    #[inline]
87    pub fn same_index_domain(self, other: Self) -> bool {
88        matches!((self, other), (Self::Descriptor(left), Self::Descriptor(right)) if left == right)
89    }
90}
91
92/// Borrows the child items of one parent entity, for flattened streams.
93pub trait FlattenExtract<P>: Send + Sync {
94    /// The child item type.
95    type Item;
96
97    /// Returns `parent`'s children.
98    fn extract<'s>(&self, parent: &'s P) -> &'s [Self::Item];
99}
100
101impl<P, B, F> FlattenExtract<P> for F
102where
103    F: for<'a> Fn(&'a P) -> &'a [B] + Send + Sync,
104{
105    type Item = B;
106
107    #[inline]
108    fn extract<'s>(&self, parent: &'s P) -> &'s [B] {
109        self(parent)
110    }
111}
112
113/// Adapts a closure returning `&Vec<B>` to [`FlattenExtract`].
114#[derive(Clone, Copy)]
115pub struct FlattenVecExtract<F>(pub F);
116
117impl<P, B, F> FlattenExtract<P> for FlattenVecExtract<F>
118where
119    F: for<'a> Fn(&'a P) -> &'a Vec<B> + Send + Sync,
120{
121    type Item = B;
122
123    #[inline]
124    fn extract<'s>(&self, parent: &'s P) -> &'s [B] {
125        (self.0)(parent).as_slice()
126    }
127}
128
129#[doc(hidden)]
130#[derive(Clone, Copy)]
131pub struct SourceExtract<E> {
132    extractor: E,
133    change_source: ChangeSource,
134}
135
136impl<E> SourceExtract<E> {
137    pub fn new(extractor: E, change_source: ChangeSource) -> Self {
138        Self {
139            extractor,
140            change_source,
141        }
142    }
143
144    pub fn extractor(&self) -> &E {
145        &self.extractor
146    }
147}
148
149impl<S, A, E> CollectionExtract<S> for SourceExtract<E>
150where
151    E: for<'a> Fn(&'a S) -> &'a [A] + Send + Sync,
152{
153    type Item = A;
154
155    #[inline]
156    fn extract<'s>(&self, s: &'s S) -> &'s [A] {
157        (self.extractor)(s)
158    }
159
160    fn change_source(&self) -> ChangeSource {
161        self.change_source
162    }
163}
164
165#[doc(hidden)]
166pub fn source<E>(extractor: E, change_source: ChangeSource) -> SourceExtract<E> {
167    SourceExtract::new(extractor, change_source)
168}