Skip to main content

pumpkin_core/constraints/
constraint_poster.rs

1use log::warn;
2
3use super::Constraint;
4use super::NegatableConstraint;
5use crate::Solver;
6use crate::variables::Literal;
7
8/// A structure which is responsible for adding the created [`Constraint`]s to the
9/// [`Solver`]. For an example on how to use this, see [`crate::constraints`].
10#[derive(Debug)]
11pub struct ConstraintPoster<'solver, ConstraintImpl> {
12    solver: &'solver mut Solver,
13    constraint: Option<ConstraintImpl>,
14}
15
16impl<'a, ConstraintImpl> ConstraintPoster<'a, ConstraintImpl> {
17    pub(crate) fn new(solver: &'a mut Solver, constraint: ConstraintImpl) -> Self {
18        ConstraintPoster {
19            solver,
20            constraint: Some(constraint),
21        }
22    }
23}
24
25impl<ConstraintImpl: Constraint> ConstraintPoster<'_, ConstraintImpl> {
26    /// Add the [`Constraint`] to the [`Solver`].
27    pub fn post(mut self) {
28        self.constraint.take().unwrap().post(self.solver)
29    }
30
31    /// Add the half-reified version of the [`Constraint`] to the [`Solver`]; i.e. post the
32    /// constraint `r -> constraint` where `r` is a reification literal.
33    pub fn implied_by(mut self, reification_literal: Literal) {
34        self.constraint
35            .take()
36            .unwrap()
37            .implied_by(self.solver, reification_literal)
38    }
39}
40
41impl<ConstraintImpl: NegatableConstraint> ConstraintPoster<'_, ConstraintImpl> {
42    /// Add the reified version of the [`Constraint`] to the [`Solver`]; i.e. post the constraint
43    /// `r <-> constraint` where `r` is a reification literal.
44    pub fn reify(mut self, reification_literal: Literal) {
45        self.constraint
46            .take()
47            .unwrap()
48            .reify(self.solver, reification_literal)
49    }
50}
51
52impl<ConstraintImpl> Drop for ConstraintPoster<'_, ConstraintImpl> {
53    fn drop(&mut self) {
54        if self.constraint.is_some() {
55            warn!("A constraint poster is never used, this is likely a mistake.");
56        }
57    }
58}