1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Arbitrary-depth typed join chain, past the named arities.
//!
//! `Bi`/`Tri`/`Quad`/`Penta` are ergonomic adapters with hand-written entity
//! accessors. Beyond Penta there is no per-arity type to write, so this is the
//! typed row form the arity adapters are built on: a `Chain` owns any
//! `Operator<S>` tree plus the next binding index, and `.join()` nests another
//! `JoinNode` whose plan receives the whole borrowed left row. No erasure, no
//! fixed ceiling — depth is whatever the type nests to.
//!
//! Entry is `Penta::join`, which continues the fluent chain at depth six; a
//! `Chain` then joins indefinitely. Filtering wraps the tree in a `FilterNode`
//! (identity-preserving, view-transparent); finalizing hands the tree to the
//! generic `OperatorTerminal`.
use std::marker::PhantomData;
use solverforge_core::score::Score;
use solverforge_core::{ConstraintRef, ImpactType};
use super::collection_extract::CollectionExtract;
use super::joiner::plan::{CompileCondition, ExecutablePlan, IndexedPlan};
use super::relational::operator::{FilterNode, FilterPredicate, JoinNode, Operator};
use super::relational::Leaf;
use crate::constraint::relational::OperatorTerminal;
/// A typed join chain of unbounded depth.
///
/// `O` is the concrete operator tree (a `JoinNode`, optionally wrapped in a
/// `FilterNode`); `next_binding` is the binding index the next joined collection
/// receives, so repeated descriptors stay distinguishable.
pub struct Chain<S: 'static, O, Sc>
where
O: Operator<S>,
{
tree: O,
next_binding: u32,
_phantom: PhantomData<fn() -> (S, Sc)>,
}
impl<S, O, Sc> Chain<S, O, Sc>
where
S: Send + Sync + 'static,
O: Operator<S> + Send + Sync,
Sc: Score + 'static,
{
/// Wraps an existing operator tree as a chain whose next joined
/// collection takes `next_binding`.
pub fn new(tree: O, next_binding: u32) -> Self {
Self {
tree,
next_binding,
_phantom: PhantomData,
}
}
/// Adds a row filter. The filter receives the whole borrowed typed row and
/// the solution, and is applied exactly once on every path.
pub fn filter<P>(self, predicate: P) -> Chain<S, FilterNode<O, P>, Sc>
where
P: for<'a> FilterPredicate<S, O::View<'a>> + 'static,
FilterNode<O, P>: Operator<S>,
{
Chain {
tree: FilterNode::new(self.tree, predicate),
next_binding: self.next_binding,
_phantom: PhantomData,
}
}
/// Extends the chain with one more source.
///
/// The target's condition compiles fresh, so its left key receives the whole
/// borrowed row (`O::View<'a>`) and `target`'s key domain is independent of
/// every earlier relationship.
///
/// # Key shapes
///
/// Later joins of a chain take VIEW keys, not entity keys:
///
/// - left key — the whole borrowed row so far, spelled here as
/// `|row: &O::View<'_>| ...` (e.g. `row.left.entity.key` on a `Pair`)
/// - right key — `|c: &Leaf<'_, B>| c.entity.key`, reading fields via `.entity`
///
/// `equal_bi` with entity closures does NOT typecheck here (E0631, expected
/// a closure over the row view) — use `equal_on`. The first join of a chain
/// keeps `equal_bi` with plain entity keys.
pub fn join<B, EB, P>(
self,
target: (EB, P),
) -> Chain<S, JoinNode<S, O, CollectionNodeAlias<S, EB>, P::Plan>, Sc>
where
B: Send + Sync + 'static,
EB: CollectionExtract<S, Item = B> + 'static,
P: CompileCondition + 'static,
P::Plan: IndexedPlan,
for<'a> CollectionNodeAlias<S, EB>: Operator<S, View<'a> = Leaf<'a, B>>,
for<'a> P::Plan: ExecutablePlan<O::View<'a>, Leaf<'a, B>>,
JoinNode<S, O, CollectionNodeAlias<S, EB>, P::Plan>: Operator<S>,
{
let (extractor, condition) = target;
let tree = JoinNode::new(
self.tree,
super::relational::operator::CollectionNode::new(extractor, self.next_binding),
condition,
);
Chain {
tree,
next_binding: self.next_binding + 1,
_phantom: PhantomData,
}
}
/// Finalizes into the generic operator terminal, scoring with a weight that
/// receives the whole borrowed typed row.
pub fn penalize<W>(self, weight: W) -> ChainBuilder<S, O, W, Sc>
where
W: for<'a> Fn(&S, &O::View<'a>) -> Sc + Send + Sync,
{
ChainBuilder {
tree: self.tree,
impact_type: ImpactType::Penalty,
weight,
is_hard: false,
_phantom: PhantomData,
}
}
/// Finalizes into the generic operator terminal as a reward.
pub fn reward<W>(self, weight: W) -> ChainBuilder<S, O, W, Sc>
where
W: for<'a> Fn(&S, &O::View<'a>) -> Sc + Send + Sync,
{
ChainBuilder {
tree: self.tree,
impact_type: ImpactType::Reward,
weight,
is_hard: false,
_phantom: PhantomData,
}
}
}
/// Alias kept short at the use site; the concrete node type.
type CollectionNodeAlias<S, E> = super::relational::operator::CollectionNode<S, E>;
/// Finalizer for a chain: holds the tree, impact, and row weight until
/// `named()` attaches a constraint name.
pub struct ChainBuilder<S: 'static, O, W, Sc>
where
O: Operator<S>,
{
tree: O,
impact_type: ImpactType,
weight: W,
is_hard: bool,
_phantom: PhantomData<fn() -> (S, Sc)>,
}
impl<S, O, W, Sc> ChainBuilder<S, O, W, Sc>
where
S: Send + Sync + 'static,
O: Operator<S> + Send + Sync,
W: for<'a> Fn(&S, &O::View<'a>) -> Sc + Send + Sync,
Sc: Score + 'static,
O: super::relational::operator::ExplainOperator<S>,
{
/// Marks the constraint hard.
pub fn hard(mut self) -> Self {
self.is_hard = true;
self
}
/// Names the constraint, producing the terminal.
pub fn named(self, name: &str) -> OperatorTerminal<S, O, W, Sc> {
OperatorTerminal::new(
ConstraintRef::new("", name),
self.impact_type,
self.tree,
self.weight,
self.is_hard,
)
}
}