pumpkin_solver/engine/predicates/
integer_predicate.rs

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
use thiserror::Error;

use super::predicate::Predicate;
use crate::engine::variables::DomainId;

/// Representation of a domain operation
///
/// It can either be in the form of atomic constraints over
/// [`DomainId`]s (in the form of [`IntegerPredicate::LowerBound`],
/// [`IntegerPredicate::UpperBound`], [`IntegerPredicate::NotEqual`] or [`IntegerPredicate::Equal`])
#[derive(Clone, PartialEq, Eq, Copy, Hash)]
pub enum IntegerPredicate {
    LowerBound {
        domain_id: DomainId,
        lower_bound: i32,
    },
    UpperBound {
        domain_id: DomainId,
        upper_bound: i32,
    },
    NotEqual {
        domain_id: DomainId,
        not_equal_constant: i32,
    },
    Equal {
        domain_id: DomainId,
        equality_constant: i32,
    },
}

impl IntegerPredicate {
    pub fn is_equality_predicate(&self) -> bool {
        matches!(
            *self,
            IntegerPredicate::Equal {
                domain_id: _,
                equality_constant: _
            }
        )
    }

    pub fn is_lower_bound_predicate(&self) -> bool {
        matches!(
            *self,
            IntegerPredicate::LowerBound {
                domain_id: _,
                lower_bound: _
            }
        )
    }

    pub fn is_not_equal_predicate(&self) -> bool {
        matches!(
            *self,
            IntegerPredicate::NotEqual {
                domain_id: _,
                not_equal_constant: _
            }
        )
    }

    /// Returns the [`DomainId`] of the [`IntegerPredicate`]
    pub fn get_domain(&self) -> DomainId {
        match *self {
            IntegerPredicate::LowerBound {
                domain_id,
                lower_bound: _,
            } => domain_id,
            IntegerPredicate::UpperBound {
                domain_id,
                upper_bound: _,
            } => domain_id,
            IntegerPredicate::NotEqual {
                domain_id,
                not_equal_constant: _,
            } => domain_id,
            IntegerPredicate::Equal {
                domain_id,
                equality_constant: _,
            } => domain_id,
        }
    }
}

impl std::ops::Not for IntegerPredicate {
    type Output = IntegerPredicate;

    fn not(self) -> Self::Output {
        match self {
            IntegerPredicate::LowerBound {
                domain_id,
                lower_bound,
            } => IntegerPredicate::UpperBound {
                domain_id,
                upper_bound: lower_bound - 1,
            },
            IntegerPredicate::UpperBound {
                domain_id,
                upper_bound,
            } => IntegerPredicate::LowerBound {
                domain_id,
                lower_bound: upper_bound + 1,
            },
            IntegerPredicate::NotEqual {
                domain_id,
                not_equal_constant,
            } => IntegerPredicate::Equal {
                domain_id,
                equality_constant: not_equal_constant,
            },
            IntegerPredicate::Equal {
                domain_id,
                equality_constant,
            } => IntegerPredicate::NotEqual {
                domain_id,
                not_equal_constant: equality_constant,
            },
        }
    }
}

#[derive(Debug, Error, Copy, Clone)]
#[error("Attempt to transform non-integer predicate to integer predicate")]
pub struct IntegerPredicateConversionError;

impl TryFrom<Predicate> for IntegerPredicate {
    type Error = IntegerPredicateConversionError;

    fn try_from(value: Predicate) -> Result<Self, Self::Error> {
        match value {
            Predicate::IntegerPredicate(integer_predicate) => Ok(integer_predicate),
            _ => Err(IntegerPredicateConversionError),
        }
    }
}

impl std::fmt::Display for IntegerPredicate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IntegerPredicate::LowerBound {
                domain_id,
                lower_bound,
            } => write!(f, "[{} >= {}]", domain_id, lower_bound),
            IntegerPredicate::UpperBound {
                domain_id,
                upper_bound,
            } => write!(f, "[{} <= {}]", domain_id, upper_bound),
            IntegerPredicate::NotEqual {
                domain_id,
                not_equal_constant,
            } => write!(f, "[{} != {}]", domain_id, not_equal_constant),
            IntegerPredicate::Equal {
                domain_id,
                equality_constant,
            } => write!(f, "[{} == {}]", domain_id, equality_constant),
        }
    }
}

impl std::fmt::Debug for IntegerPredicate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}