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
use super::*;
use serde_json;
use std;
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub enum SymbolicConstraint {
Eq { v1: String, v2: String },
Neq { v1: String, v2: String },
}
impl std::fmt::Display for SymbolicConstraint {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(&self).unwrap())
}
}
impl SymbolicConstraint {
pub fn solve<T: ConstraintValue>(&self, bindings: &Bindings<T>) -> SolveResult<T> {
match *self {
SymbolicConstraint::Eq { ref v1, ref v2 } => {
println!("\n{:?}\n{:?}\n{:?}",
bindings.get_binding(v1),
bindings.get_binding(v2),
T::variable(v1).and_then(|var1| Some(SolveResult::Partial(bindings.set_binding(v2, var1)))));
match (bindings.get_binding(v1), bindings.get_binding(v2)) {
(Some(ref val1), Some(ref val2)) => {
if val1.eq(val2) {
Some(SolveResult::Success(bindings.clone()))
} else {
None
}
}
_ => Some(SolveResult::Partial(bindings.clone())),
}
}
SymbolicConstraint::Neq { ref v1, ref v2 } => {
match (bindings.get_binding(v1), bindings.get_binding(v2)) {
(Some(ref val1), Some(ref val2)) => {
if val1.eq(val2) {
None
} else {
Some(SolveResult::Success(bindings.clone()))
}
}
_ => Some(SolveResult::Partial(bindings.clone())),
}
}
}
.unwrap_or(SolveResult::Conflict)
}
pub fn rename_variables(&self, renamed_variables: &HashMap<String, String>) -> Self {
let lookup = |v: &String| -> String { renamed_variables.get(v).cloned().or_else(|| Some(v.clone())).unwrap() };
match *self {
SymbolicConstraint::Eq { ref v1, ref v2 } => {
SymbolicConstraint::Eq {
v1: lookup(v1),
v2: lookup(v2),
}
}
SymbolicConstraint::Neq { ref v1, ref v2 } => {
SymbolicConstraint::Neq {
v1: lookup(v1),
v2: lookup(v2),
}
}
}
}
pub fn variables(&self) -> Vec<String> {
match *self {
SymbolicConstraint::Eq { ref v1, ref v2 } => vec![v1.clone(), v2.clone()],
SymbolicConstraint::Neq { ref v1, ref v2 } => vec![v1.clone(), v2.clone()],
}
}
}