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
165
166
167
168
169
170
171
172
173
174
175
pub use self::numerical::*;
use core::{Bindings, BindingsValue};
use serde_json;
use std;
use std::collections::HashMap;
use std::ops::{Add, Div, Mul, Sub};
use utils;
mod numerical;
pub use self::symbolic::*;
mod symbolic;
pub trait ConstraintValue: BindingsValue {
fn float(f64) -> Self;
fn to_float(&self) -> Option<f64>;
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum SolveResult<T: ConstraintValue> {
Conflict,
Partial(Bindings<T>),
Success(Bindings<T>),
}
impl<T: ConstraintValue> SolveResult<T> {
pub fn ok(&self) -> Option<Bindings<T>> {
match *self {
SolveResult::Success(ref bindings) => Some(bindings.clone()),
SolveResult::Partial(ref bindings) => Some(bindings.clone()),
_ => None,
}
}
pub fn and_then(&self, f: &Fn(&Bindings<T>) -> Self) -> Self {
match *self {
SolveResult::Success(ref bindings) => f(bindings),
SolveResult::Partial(ref bindings) => f(bindings),
_ => self.clone(),
}
}
pub fn if_partial(&self, f: &Fn(&Bindings<T>) -> Self) -> Self {
match *self {
SolveResult::Partial(ref bindings) => f(bindings),
_ => self.clone(),
}
}
}
impl<T: ConstraintValue> std::fmt::Display for SolveResult<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(&self).unwrap())
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub enum Constraint {
#[serde(rename="numerical")]
Numerical(NumericalConstraint),
Symbolic(SymbolicConstraint),
}
impl Eq for Constraint {}
impl std::fmt::Display for Constraint {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(&self).unwrap())
}
}
impl Constraint {
pub fn solve<T: ConstraintValue>(&self, bindings: &Bindings<T>) -> SolveResult<T> {
match *self {
Constraint::Numerical(ref numerical_constraint) => numerical_constraint.solve(bindings),
Constraint::Symbolic(ref symbolic_constraint) => symbolic_constraint.solve(bindings),
}
}
pub fn solve_many<T: ConstraintValue>(constraints: Vec<&Constraint>, bindings: &Bindings<T>) -> SolveResult<T> {
let fold_result = utils::fold_while_some((Vec::new(), bindings.clone()),
&mut constraints.iter(),
&|(mut remaining_constraints, bindings), constraint| {
let result: SolveResult<T> = constraint.solve(&bindings);
match result {
SolveResult::Conflict => None,
SolveResult::Partial(bindings) => {
remaining_constraints.push(constraint.clone());
Some((remaining_constraints, bindings.clone()))
}
SolveResult::Success(bindings) => Some((remaining_constraints, bindings.clone())),
}
});
match fold_result {
Some((remaining_constraints, bindings)) => {
if remaining_constraints.is_empty() {
SolveResult::Success(bindings)
} else if remaining_constraints.len() == constraints.len() {
SolveResult::Partial(bindings)
} else {
Constraint::solve_many(remaining_constraints, &bindings)
}
}
None => SolveResult::Conflict,
}
}
pub fn rename_variables(&self, renamed_variables: &HashMap<String, String>) -> Self {
match *self {
Constraint::Numerical(ref numerical_constraint) => Constraint::Numerical(numerical_constraint.rename_variables(renamed_variables)),
Constraint::Symbolic(ref symbolic_constraint) => Constraint::Symbolic(symbolic_constraint.rename_variables(renamed_variables)),
}
}
pub fn variables(&self) -> Vec<String> {
match *self {
Constraint::Numerical(ref numerical_constraint) => numerical_constraint.variables(),
Constraint::Symbolic(ref symbolic_constraint) => symbolic_constraint.variables(),
}
}
}
#[cfg(test)]
mod tests;