1
use super::errors::{ReprInstantiateError, ReprUpError};
2
use super::types::{LookupFn, ReprAssignment, ReprDeclLevel, ReprGetOrInitResult, ReprResult};
3
use crate::ast::{DeclarationPtr, DomainPtr, Expression, Literal, Name};
4
use crate::representation::ReprRule;
5
use conjure_cp_core::representation::ReprError;
6
use parking_lot::MappedRwLockReadGuard;
7
use serde::Deserialize;
8
use serde_json;
9
use std::any::Any;
10
use std::collections::{HashMap, VecDeque};
11
use std::fmt::Debug;
12
use std::hash::{Hash, Hasher};
13

            
14
/// Detached representation structure for compact cost estimation.
15
/// Probing must neither modify the declaration nor commit uniform choices.
16
pub struct ReprStructureProbe {
17
    pub domains: Vec<DomainPtr>,
18
    pub constraints: Vec<Expression>,
19
}
20

            
21
pub trait ReprStateStored: Any + Send + Sync + Debug {
22
    fn rule(&self) -> &'static dyn ReprRuleStored;
23

            
24
    fn up_via(&self, lu: &LookupFn<'_>) -> Result<Literal, ReprUpError>;
25

            
26
    fn up(&self, raw_assignment: &HashMap<Name, Literal>) -> Result<Literal, ReprUpError> {
27
        let lu: LookupFn<'_> =
28
            Box::new(|decl: &DeclarationPtr| raw_assignment.get(&decl.name()).cloned());
29
        self.up_via(&lu)
30
    }
31

            
32
    fn repr_vars(&self) -> VecDeque<DeclarationPtr>;
33

            
34
    fn as_any(&self) -> &dyn Any;
35

            
36
    fn as_any_mut(&mut self) -> &mut dyn Any;
37

            
38
    fn clone_box(&self) -> Box<dyn ReprStateStored>;
39

            
40
    fn serialise(&self) -> Result<serde_json::Value, serde_json::Error>;
41
}
42

            
43
impl<D: ReprDeclLevel> ReprStateStored for D {
44
5181624
    fn rule(&self) -> &'static dyn ReprRuleStored {
45
5181624
        D::RULE
46
5181624
    }
47

            
48
19411345
    fn up_via(&self, lu: &LookupFn<'_>) -> Result<Literal, ReprUpError> {
49
19411345
        let res = self.lookup_via(lu)?;
50
19398079
        Ok(res.up())
51
19411345
    }
52

            
53
    fn repr_vars(&self) -> VecDeque<DeclarationPtr> {
54
        D::repr_vars(self)
55
    }
56

            
57
12520442
    fn as_any(&self) -> &dyn Any {
58
12520442
        self
59
12520442
    }
60

            
61
    fn as_any_mut(&mut self) -> &mut dyn Any {
62
        self
63
    }
64

            
65
10
    fn clone_box(&self) -> Box<dyn ReprStateStored> {
66
10
        Box::new(self.clone())
67
10
    }
68

            
69
    fn serialise(&self) -> Result<serde_json::Value, serde_json::Error> {
70
        serde_json::to_value(self)
71
    }
72
}
73

            
74
pub trait ReprRuleStored: Send + Sync {
75
    fn name(&self) -> &'static str;
76

            
77
    fn short_name(&self) -> &'static str;
78

            
79
    /// This representation's identity.
80
    fn id(&self) -> super::ReprId;
81

            
82
    /// Whether this representation is available when targeting `family`.
83
    fn applies_to(&self, family: crate::settings::SolverFamily) -> bool;
84

            
85
    fn is_integer_encoding(&self) -> bool;
86

            
87
    fn init_for(&self, decl: &mut DeclarationPtr) -> ReprResult;
88

            
89
    fn init_for_if_not_exists(&self, decl: &mut DeclarationPtr) -> ReprResult;
90

            
91
    /// Checks applicability without mutating the source declaration and returns its compactness
92
    /// score.
93
    fn probe_for(&self, decl: &DeclarationPtr) -> Result<usize, ReprError>;
94

            
95
    fn probe_structure_for(&self, decl: &DeclarationPtr) -> Result<ReprStructureProbe, ReprError>;
96

            
97
    fn get_or_init_for<'a>(
98
        &self,
99
        decl: &'a mut DeclarationPtr,
100
    ) -> ReprGetOrInitResult<'a, dyn ReprStateStored, ReprError>;
101

            
102
    fn get_for<'a>(
103
        &self,
104
        decl: &'a DeclarationPtr,
105
    ) -> Option<MappedRwLockReadGuard<'a, dyn ReprStateStored>>;
106

            
107
    fn deserialize_state(
108
        &self,
109
        val: serde_json::Value,
110
    ) -> Result<Box<dyn ReprStateStored>, serde_json::Error>;
111
}
112

            
113
impl<R: ReprRule> ReprRuleStored for R {
114
130323246
    fn name(&self) -> &'static str {
115
130323246
        R::NAME
116
130323246
    }
117

            
118
233526621
    fn id(&self) -> super::ReprId {
119
233526621
        <R as ReprRule>::id()
120
233526621
    }
121

            
122
147898
    fn short_name(&self) -> &'static str {
123
147898
        R::SHORT_NAME
124
147898
    }
125

            
126
3654
    fn applies_to(&self, family: crate::settings::SolverFamily) -> bool {
127
3654
        R::applies_to(family)
128
3654
    }
129

            
130
76794035
    fn is_integer_encoding(&self) -> bool {
131
76794035
        R::is_integer_encoding()
132
76794035
    }
133

            
134
744563
    fn init_for(&self, decl: &mut DeclarationPtr) -> ReprResult {
135
744563
        R::init_for(decl)
136
744563
    }
137

            
138
5478871
    fn init_for_if_not_exists(&self, decl: &mut DeclarationPtr) -> ReprResult {
139
5478871
        R::init_for_if_not_exists(decl)
140
5478871
    }
141

            
142
60794088
    fn probe_for(&self, decl: &DeclarationPtr) -> Result<usize, ReprError> {
143
60794088
        let domain = R::domain_for(decl)?;
144
        // Value lettings still need `down` to check that their value can be represented.
145
23960383
        if (decl.as_find().is_some() || decl.is_find_auxiliary())
146
23915401
            && let Some(score) = R::probe_score(domain.clone())?
147
        {
148
1294163
            return Ok(score);
149
22102649
        }
150
22102649
        let mut detached = decl.clone().detach();
151
22102649
        super::uniform::probe(|| {
152
22102649
            R::init_for(&mut detached)?;
153
2547216
            R::compactness_score(domain).map_err(Into::into)
154
22102649
        })
155
60794088
    }
156

            
157
990
    fn probe_structure_for(&self, decl: &DeclarationPtr) -> Result<ReprStructureProbe, ReprError> {
158
990
        R::domain_for(decl)?;
159
990
        let mut detached = decl.clone().detach();
160
990
        super::uniform::probe(|| {
161
990
            let (_, constraints) = R::init_for(&mut detached)?;
162
990
            let state = R::get_for(&detached).expect("probe initialised representation");
163
990
            let domains = ReprDeclLevel::repr_vars(&*state)
164
990
                .iter()
165
2517
                .map(|var| {
166
2517
                    var.domain()
167
                        .ok_or_else(|| ReprInstantiateError::NoDomain(var.clone()))
168
2517
                })
169
                .collect::<Result<Vec<_>, _>>()?;
170
990
            Ok(ReprStructureProbe {
171
990
                domains,
172
990
                constraints,
173
990
            })
174
990
        })
175
990
    }
176

            
177
    fn get_or_init_for<'a>(
178
        &self,
179
        decl: &'a mut DeclarationPtr,
180
    ) -> ReprGetOrInitResult<'a, dyn ReprStateStored, ReprError> {
181
        let (state, symbols, constraints) = R::get_or_init_for(decl)?;
182
        let state_dyn = repr_state_as_dyn(state);
183
        Ok((state_dyn, symbols, constraints))
184
    }
185

            
186
    fn get_for<'a>(
187
        &self,
188
        decl: &'a DeclarationPtr,
189
    ) -> Option<MappedRwLockReadGuard<'a, dyn ReprStateStored>> {
190
        let state = R::get_for(decl)?;
191
        Some(repr_state_as_dyn(state))
192
    }
193

            
194
    fn deserialize_state(
195
        &self,
196
        val: serde_json::Value,
197
    ) -> Result<Box<dyn ReprStateStored>, serde_json::Error> {
198
        Ok(Box::new(R::DeclLevel::deserialize(val)?))
199
    }
200
}
201

            
202
impl Debug for dyn ReprRuleStored {
203
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
204
        write!(fmt, "ReprRule({})", self.name())
205
    }
206
}
207

            
208
impl PartialEq for dyn ReprRuleStored {
209
275965313
    fn eq(&self, other: &Self) -> bool {
210
275965313
        self.id() == other.id()
211
275965313
    }
212
}
213

            
214
impl Eq for dyn ReprRuleStored {}
215

            
216
impl PartialOrd for dyn ReprRuleStored {
217
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
218
        Some(self.cmp(other))
219
    }
220
}
221

            
222
impl Ord for dyn ReprRuleStored {
223
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
224
        self.name().cmp(other.name())
225
    }
226
}
227

            
228
impl Hash for dyn ReprRuleStored {
229
145735
    fn hash<H: Hasher>(&self, state: &mut H) {
230
145735
        self.name().hash(state)
231
145735
    }
232
}
233

            
234
fn repr_state_as_dyn<'a, D>(
235
    guard: MappedRwLockReadGuard<'a, D>,
236
) -> MappedRwLockReadGuard<'a, dyn ReprStateStored>
237
where
238
    D: ReprStateStored,
239
{
240
    MappedRwLockReadGuard::map(guard, |state| state as &dyn ReprStateStored)
241
}