solverforge_scoring/stream/
chain_stream.rs1use std::marker::PhantomData;
16
17use solverforge_core::score::Score;
18use solverforge_core::{ConstraintRef, ImpactType};
19
20use super::collection_extract::CollectionExtract;
21use super::joiner::plan::{CompileCondition, ExecutablePlan, IndexedPlan};
22use super::relational::operator::{FilterNode, FilterPredicate, JoinNode, Operator};
23use super::relational::Leaf;
24use crate::constraint::relational::OperatorTerminal;
25
26pub struct Chain<S: 'static, O, Sc>
32where
33 O: Operator<S>,
34{
35 tree: O,
36 next_binding: u32,
37 _phantom: PhantomData<fn() -> (S, Sc)>,
38}
39
40impl<S, O, Sc> Chain<S, O, Sc>
41where
42 S: Send + Sync + 'static,
43 O: Operator<S> + Send + Sync,
44 Sc: Score + 'static,
45{
46 pub fn new(tree: O, next_binding: u32) -> Self {
49 Self {
50 tree,
51 next_binding,
52 _phantom: PhantomData,
53 }
54 }
55
56 pub fn filter<P>(self, predicate: P) -> Chain<S, FilterNode<O, P>, Sc>
59 where
60 P: for<'a> FilterPredicate<S, O::View<'a>> + 'static,
61 FilterNode<O, P>: Operator<S>,
62 {
63 Chain {
64 tree: FilterNode::new(self.tree, predicate),
65 next_binding: self.next_binding,
66 _phantom: PhantomData,
67 }
68 }
69
70 pub fn join<B, EB, P>(
88 self,
89 target: (EB, P),
90 ) -> Chain<S, JoinNode<S, O, CollectionNodeAlias<S, EB>, P::Plan>, Sc>
91 where
92 B: Clone + Send + Sync + 'static,
93 EB: CollectionExtract<S, Item = B> + 'static,
94 P: CompileCondition + 'static,
95 P::Plan: IndexedPlan,
96 for<'a> CollectionNodeAlias<S, EB>: Operator<S, View<'a> = Leaf<'a, B>>,
97 for<'a> P::Plan: ExecutablePlan<O::View<'a>, Leaf<'a, B>>,
98 JoinNode<S, O, CollectionNodeAlias<S, EB>, P::Plan>: Operator<S>,
99 {
100 let (extractor, condition) = target;
101 let tree = JoinNode::new(
102 self.tree,
103 super::relational::operator::CollectionNode::new(extractor, self.next_binding),
104 condition,
105 );
106 Chain {
107 tree,
108 next_binding: self.next_binding + 1,
109 _phantom: PhantomData,
110 }
111 }
112
113 pub fn penalize<W>(self, weight: W) -> ChainBuilder<S, O, W, Sc>
116 where
117 W: for<'a> Fn(&S, &O::View<'a>) -> Sc + Send + Sync,
118 {
119 ChainBuilder {
120 tree: self.tree,
121 impact_type: ImpactType::Penalty,
122 weight,
123 is_hard: false,
124 _phantom: PhantomData,
125 }
126 }
127
128 pub fn reward<W>(self, weight: W) -> ChainBuilder<S, O, W, Sc>
130 where
131 W: for<'a> Fn(&S, &O::View<'a>) -> Sc + Send + Sync,
132 {
133 ChainBuilder {
134 tree: self.tree,
135 impact_type: ImpactType::Reward,
136 weight,
137 is_hard: false,
138 _phantom: PhantomData,
139 }
140 }
141}
142
143type CollectionNodeAlias<S, E> = super::relational::operator::CollectionNode<S, E>;
145
146pub struct ChainBuilder<S: 'static, O, W, Sc>
149where
150 O: Operator<S>,
151{
152 tree: O,
153 impact_type: ImpactType,
154 weight: W,
155 is_hard: bool,
156 _phantom: PhantomData<fn() -> (S, Sc)>,
157}
158
159impl<S, O, W, Sc> ChainBuilder<S, O, W, Sc>
160where
161 S: Send + Sync + 'static,
162 O: Operator<S> + Send + Sync,
163 W: for<'a> Fn(&S, &O::View<'a>) -> Sc + Send + Sync,
164 Sc: Score + 'static,
165 for<'a> O::View<'a>: super::relational::operator::ExplainRow,
166{
167 pub fn hard(mut self) -> Self {
169 self.is_hard = true;
170 self
171 }
172
173 pub fn named(self, name: &str) -> OperatorTerminal<S, O, W, Sc> {
175 OperatorTerminal::new(
176 ConstraintRef::new("", name),
177 self.impact_type,
178 self.tree,
179 self.weight,
180 self.is_hard,
181 )
182 }
183}