solverforge-solver 0.18.0

Solver engine for SolverForge
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Shared typed/dynamic scalar access carrier for runtime graph compilation.
//!
//! This is deliberately below selector/provider policy.  A graph node carries
//! one `RuntimeScalarSlot`; scalar leaves and generic provider moves use
//! the same identity, legality, and metadata dispatch rather than rebuilding
//! typed and dynamic selector trees.

use std::fmt;
use std::sync::Arc;

use solverforge_core::domain::{DynamicScalarVariableSlot, EntityClassId, VariableId};

use super::{ScalarVariableSlot, ValueSource};

/// Immutable scalar slot identity used by compiler errors, provider
/// normalization, candidate ownership, and trace provenance.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct RuntimeScalarSlotId {
    pub descriptor_index: usize,
    pub variable_index: usize,
    pub entity_class: Arc<str>,
    pub variable_name: Arc<str>,
    pub dynamic_identity: Option<(EntityClassId, VariableId)>,
}

impl RuntimeScalarSlotId {
    pub fn from_static_slot<S>(slot: &ScalarVariableSlot<S>) -> Self {
        Self {
            descriptor_index: slot.descriptor_index,
            variable_index: slot.variable_index,
            entity_class: Arc::from(slot.entity_type_name),
            variable_name: Arc::from(slot.variable_name),
            dynamic_identity: None,
        }
    }

    pub fn from_dynamic_slot<S>(slot: &DynamicScalarVariableSlot<S>) -> Self {
        Self {
            descriptor_index: slot.descriptor_index(),
            variable_index: slot.descriptor_variable_index(),
            entity_class: Arc::from(slot.entity_type_name),
            variable_name: Arc::from(slot.variable_name),
            dynamic_identity: Some((slot.entity, slot.variable)),
        }
    }
}

impl fmt::Display for RuntimeScalarSlotId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}.{} (descriptor {}, variable {})",
            self.entity_class, self.variable_name, self.descriptor_index, self.variable_index
        )
    }
}

/// Structural scalar capability.  It answers whether an immutable binding
/// supplies a source, not whether a particular row produces a candidate.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ScalarAccessCapability {
    Candidates,
    NearbyValue,
    NearbyEntity,
    ConstructionEntityOrder,
    ConstructionValueOrder,
}

/// Physical payload carrier.  This is the only typed/dynamic distinction
/// retained by the shared scalar/provider kernels.
pub enum RuntimeScalarSlot<S> {
    Static(ScalarVariableSlot<S>),
    Dynamic(DynamicScalarVariableSlot<S>),
}

impl<S> Clone for RuntimeScalarSlot<S> {
    fn clone(&self) -> Self {
        match self {
            Self::Static(slot) => Self::Static(*slot),
            Self::Dynamic(slot) => Self::Dynamic(slot.clone()),
        }
    }
}

impl<S> fmt::Debug for RuntimeScalarSlot<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RuntimeScalarSlot")
            .field("id", &self.id())
            .field("dynamic", &self.is_dynamic())
            .finish()
    }
}

impl<S> RuntimeScalarSlot<S> {
    pub fn id(&self) -> RuntimeScalarSlotId {
        match self {
            Self::Static(slot) => RuntimeScalarSlotId::from_static_slot(slot),
            Self::Dynamic(slot) => RuntimeScalarSlotId::from_dynamic_slot(slot),
        }
    }

    pub fn matches_target(&self, entity_class: Option<&str>, variable_name: Option<&str>) -> bool {
        match self {
            Self::Static(slot) => slot.matches_target(entity_class, variable_name),
            Self::Dynamic(slot) => slot.matches_target(entity_class, variable_name),
        }
    }

    pub fn has_capability(&self, capability: ScalarAccessCapability) -> bool {
        match (self, capability) {
            // Every scalar binding exposes an ordinary stream, even when a
            // row's stream is empty.
            (_, ScalarAccessCapability::Candidates) => true,
            (Self::Static(slot), ScalarAccessCapability::NearbyValue) => {
                slot.nearby_value_candidates.is_some()
            }
            (Self::Static(slot), ScalarAccessCapability::NearbyEntity) => {
                slot.nearby_entity_candidates.is_some()
            }
            (Self::Static(slot), ScalarAccessCapability::ConstructionEntityOrder) => {
                slot.construction_entity_order_key.is_some()
            }
            (Self::Static(slot), ScalarAccessCapability::ConstructionValueOrder) => {
                slot.construction_value_order_key.is_some()
            }
            (Self::Dynamic(slot), ScalarAccessCapability::NearbyValue) => {
                slot.has_nearby_value_candidates()
            }
            (Self::Dynamic(slot), ScalarAccessCapability::NearbyEntity) => {
                slot.has_nearby_entity_candidates()
            }
            // Dynamic scalar decreasing/queue construction requires immutable
            // order metadata. It is intentionally not recreated by a wrapper
            // callback or ad hoc model scan.
            (Self::Dynamic(_), ScalarAccessCapability::ConstructionEntityOrder)
            | (Self::Dynamic(_), ScalarAccessCapability::ConstructionValueOrder) => false,
        }
    }

    /// Descriptor coordinate without materializing an owned identity. Runtime
    /// moves use this in their hot apply/undo and tabu paths.
    pub(crate) fn descriptor_index(&self) -> usize {
        match self {
            Self::Static(slot) => slot.descriptor_index,
            Self::Dynamic(slot) => slot.descriptor_index(),
        }
    }

    /// Descriptor-local scalar coordinate without materializing an owned
    /// identity.
    pub(crate) fn variable_index(&self) -> usize {
        match self {
            Self::Static(slot) => slot.variable_index,
            Self::Dynamic(slot) => slot.descriptor_variable_index(),
        }
    }

    /// Stable slot name held by the frozen model. Callback reasons remain
    /// owned separately; this accessor is only the planning-variable identity
    /// needed by existing core telemetry/tabu primitives.
    pub(crate) fn variable_name(&self) -> &'static str {
        match self {
            Self::Static(slot) => slot.variable_name,
            Self::Dynamic(slot) => slot.variable_name,
        }
    }

    pub(crate) fn entity_count(&self, solution: &S) -> usize {
        match self {
            Self::Static(slot) => (slot.entity_count)(solution),
            Self::Dynamic(slot) => slot.entity_count(solution),
        }
    }

    /// Whether a construction placement may retain an unassigned value. This
    /// is immutable slot metadata, shared by the descriptor-placement and
    /// global runtime-slot construction schedules.
    pub(crate) fn allows_unassigned(&self) -> bool {
        match self {
            Self::Static(slot) => slot.allows_unassigned,
            Self::Dynamic(slot) => slot.allows_unassigned,
        }
    }

    pub(crate) fn current_value(&self, solution: &S, entity_index: usize) -> Option<usize> {
        match self {
            Self::Static(slot) => slot.current_value(solution, entity_index),
            Self::Dynamic(slot) => slot.current_value(solution, entity_index),
        }
    }

    pub(crate) fn set_value(&self, solution: &mut S, entity_index: usize, value: Option<usize>) {
        match self {
            Self::Static(slot) => slot.set_value(solution, entity_index, value),
            Self::Dynamic(slot) => slot.set_value(solution, entity_index, value),
        }
    }

    pub(crate) fn value_is_legal(
        &self,
        solution: &S,
        entity_index: usize,
        value: Option<usize>,
    ) -> bool {
        match self {
            Self::Static(slot) => slot.value_is_legal(solution, entity_index, value),
            Self::Dynamic(slot) => slot.value_is_legal(solution, entity_index, value),
        }
    }

    /// Applies scalar-swap legality through the shared carrier. Native empty
    /// value sources permit exchanging assigned values;
    /// every other source uses its declared per-row legality predicate.
    pub(crate) fn swap_destination_is_legal(
        &self,
        solution: &S,
        entity_index: usize,
        value: Option<usize>,
    ) -> bool {
        match self {
            Self::Static(slot) if matches!(slot.value_source, ValueSource::Empty) => {
                value.is_some()
            }
            Self::Static(slot) => slot.value_is_legal(solution, entity_index, value),
            Self::Dynamic(slot) => slot.value_is_legal(solution, entity_index, value),
        }
    }

    /// Visits ordinary candidates in their declared source order.  `limit`
    /// limits source consumption, never a post-hoc result set, so lazy dynamic
    /// bindings and static value ranges share one ordering contract.
    pub(crate) fn visit_candidate_values(
        &self,
        solution: &S,
        entity_index: usize,
        limit: Option<usize>,
        visit: &mut dyn FnMut(usize),
    ) {
        let limit = limit.unwrap_or(usize::MAX);
        match self {
            Self::Static(slot) => {
                for value in slot.candidate_values_for_entity(solution, entity_index, Some(limit)) {
                    visit(value);
                }
            }
            Self::Dynamic(slot) => {
                for &value in slot
                    .candidate_values(solution, entity_index)
                    .iter()
                    .take(limit)
                {
                    visit(value);
                }
            }
        }
    }

    /// Visits the declared nearby-value source, returning false only when the
    /// binding has no such source and a caller may select its explicit normal
    /// candidate behavior.  It never synthesizes a wrapper-local fallback.
    pub(crate) fn visit_nearby_value_candidates(
        &self,
        solution: &S,
        entity_index: usize,
        limit: usize,
        visit: &mut dyn FnMut(usize),
    ) -> bool {
        match self {
            Self::Static(slot) => {
                let Some(source) = slot.nearby_value_candidates else {
                    return false;
                };
                for &value in source(solution, entity_index, slot.variable_index)
                    .iter()
                    .take(limit)
                {
                    visit(value);
                }
                true
            }
            Self::Dynamic(slot) => {
                slot.visit_nearby_value_candidates(solution, entity_index, limit, visit)
            }
        }
    }

    pub(crate) fn nearby_value_distance(
        &self,
        solution: &S,
        entity_index: usize,
        candidate: usize,
    ) -> Option<f64> {
        match self {
            Self::Static(slot) => slot.nearby_value_distance(solution, entity_index, candidate),
            Self::Dynamic(slot) => slot.nearby_value_distance(solution, entity_index, candidate),
        }
    }

    pub(crate) fn visit_nearby_entity_candidates(
        &self,
        solution: &S,
        entity_index: usize,
        limit: usize,
        visit: &mut dyn FnMut(usize),
    ) -> bool {
        match self {
            Self::Static(slot) => {
                let Some(source) = slot.nearby_entity_candidates else {
                    return false;
                };
                for &candidate in source(solution, entity_index, slot.variable_index)
                    .iter()
                    .take(limit)
                {
                    visit(candidate);
                }
                true
            }
            Self::Dynamic(slot) => {
                slot.visit_nearby_entity_candidates(solution, entity_index, limit, visit)
            }
        }
    }

    pub(crate) fn nearby_entity_distance(
        &self,
        solution: &S,
        left_entity_index: usize,
        right_entity_index: usize,
    ) -> Option<f64> {
        match self {
            Self::Static(slot) => {
                slot.nearby_entity_distance(solution, left_entity_index, right_entity_index)
            }
            Self::Dynamic(slot) => {
                slot.nearby_entity_distance(solution, left_entity_index, right_entity_index)
            }
        }
    }

    pub(crate) fn construction_entity_order_key(
        &self,
        solution: &S,
        entity_index: usize,
    ) -> Option<i64> {
        match self {
            Self::Static(slot) => slot.construction_entity_order_key(solution, entity_index),
            Self::Dynamic(_) => None,
        }
    }

    pub(crate) fn construction_value_order_key(
        &self,
        solution: &S,
        entity_index: usize,
        value: usize,
    ) -> Option<i64> {
        match self {
            Self::Static(slot) => slot.construction_value_order_key(solution, entity_index, value),
            Self::Dynamic(_) => None,
        }
    }

    pub(crate) fn edit(
        &self,
        entity_index: usize,
        to_value: Option<usize>,
    ) -> RuntimeScalarEdit<S> {
        RuntimeScalarEdit {
            slot: self.clone(),
            entity_index,
            to_value,
        }
    }

    pub fn is_dynamic(&self) -> bool {
        matches!(self, Self::Dynamic(_))
    }
}

/// A neutral scalar edit used by the shared provider/scalar kernels before it
/// is lowered to the outer move payload.  The kind/reason policy lives above
/// this type; apply/undo ownership never needs a second typed/dynamic edit
/// implementation.
pub struct RuntimeScalarEdit<S> {
    pub slot: RuntimeScalarSlot<S>,
    pub entity_index: usize,
    pub to_value: Option<usize>,
}

impl<S> Clone for RuntimeScalarEdit<S> {
    fn clone(&self) -> Self {
        Self {
            slot: self.slot.clone(),
            entity_index: self.entity_index,
            to_value: self.to_value,
        }
    }
}

impl<S> fmt::Debug for RuntimeScalarEdit<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RuntimeScalarEdit")
            .field("slot", &self.slot)
            .field("entity_index", &self.entity_index)
            .field("to_value", &self.to_value)
            .finish()
    }
}

impl<S> RuntimeScalarEdit<S> {
    pub fn id(&self) -> RuntimeScalarSlotId {
        self.slot.id()
    }

    pub(crate) fn descriptor_index(&self) -> usize {
        self.slot.descriptor_index()
    }

    pub(crate) fn variable_index(&self) -> usize {
        self.slot.variable_index()
    }

    pub(crate) fn variable_name(&self) -> &'static str {
        self.slot.variable_name()
    }
}