Skip to main content

triblespace_core/query/
constantconstraint.rs

1use super::*;
2
3/// Pins a variable to a single known value.
4///
5/// Created by [`Variable::is`]. The estimate is always 1, propose yields
6/// exactly the constant, and confirm retains only matching proposals.
7/// This is the simplest possible constraint and is used by the macro
8/// layer to bind attribute IDs and literal values.
9pub struct ConstantConstraint {
10    variable: VariableId,
11    constant: RawValue,
12}
13
14impl ConstantConstraint {
15    /// Creates a constraint that binds `variable` to `constant`.
16    pub fn new<T: ValueSchema>(variable: Variable<T>, constant: Value<T>) -> Self {
17        ConstantConstraint {
18            variable: variable.index,
19            constant: constant.raw,
20        }
21    }
22}
23
24impl<'a> Constraint<'a> for ConstantConstraint {
25    fn variables(&self) -> VariableSet {
26        VariableSet::new_singleton(self.variable)
27    }
28
29    /// Always returns `Some(1)` for the constrained variable.
30    fn estimate(&self, variable: VariableId, _binding: &Binding) -> Option<usize> {
31        if self.variable == variable {
32            Some(1)
33        } else {
34            None
35        }
36    }
37
38    /// Pushes the single constant value.
39    fn propose(&self, variable: VariableId, _binding: &Binding, proposals: &mut Vec<RawValue>) {
40        if self.variable == variable {
41            proposals.push(self.constant);
42        }
43    }
44
45    /// Retains only proposals that match the constant exactly.
46    fn confirm(&self, variable: VariableId, _binding: &Binding, proposals: &mut Vec<RawValue>) {
47        if self.variable == variable {
48            proposals.retain(|v| *v == self.constant);
49        }
50    }
51
52    /// Returns `false` when the variable is bound to a different value.
53    fn satisfied(&self, binding: &Binding) -> bool {
54        match binding.get(self.variable) {
55            Some(v) => *v == self.constant,
56            None => true,
57        }
58    }
59}