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
use std::ffi::CStr;
use std::fmt;
use z3_sys::*;
use Ast;
use Context;
use Model;
use Optimize;
use Z3_MUTEX;
impl<'ctx> Optimize<'ctx> {
pub fn new(ctx: &'ctx Context) -> Optimize<'ctx> {
Optimize {
ctx,
z3_opt: unsafe {
let guard = Z3_MUTEX.lock().unwrap();
let opt = Z3_mk_optimize(ctx.z3_ctx);
Z3_optimize_inc_ref(ctx.z3_ctx, opt);
opt
},
}
}
pub fn assert(&self, ast: &Ast<'ctx>) {
let guard = Z3_MUTEX.lock().unwrap();
unsafe { Z3_optimize_assert(self.ctx.z3_ctx, self.z3_opt, ast.z3_ast) };
}
pub fn maximize(&self, ast: &Ast<'ctx>) {
let guard = Z3_MUTEX.lock().unwrap();
unsafe { Z3_optimize_maximize(self.ctx.z3_ctx, self.z3_opt, ast.z3_ast) };
}
pub fn minimize(&self, ast: &Ast<'ctx>) {
let guard = Z3_MUTEX.lock().unwrap();
unsafe { Z3_optimize_minimize(self.ctx.z3_ctx, self.z3_opt, ast.z3_ast) };
}
pub fn push(&self) {
let guard = Z3_MUTEX.lock().unwrap();
unsafe { Z3_optimize_push(self.ctx.z3_ctx, self.z3_opt) };
}
pub fn pop(&self) {
let guard = Z3_MUTEX.lock().unwrap();
unsafe { Z3_optimize_pop(self.ctx.z3_ctx, self.z3_opt) };
}
pub fn check(&self) -> bool {
let guard = Z3_MUTEX.lock().unwrap();
unsafe { Z3_optimize_check(self.ctx.z3_ctx, self.z3_opt) == Z3_L_TRUE }
}
pub fn get_model(&self) -> Model<'ctx> {
Model::of_optimize(self)
}
}
impl<'ctx> fmt::Display for Optimize<'ctx> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let p = unsafe {
CStr::from_ptr(Z3_optimize_to_string(self.ctx.z3_ctx, self.z3_opt) as *mut i8)
};
if p.as_ptr().is_null() {
return Result::Err(fmt::Error);
}
match p.to_str() {
Ok(s) => write!(f, "{}", s),
Err(_) => Result::Err(fmt::Error),
}
}
}
impl<'ctx> Drop for Optimize<'ctx> {
fn drop(&mut self) {
let guard = Z3_MUTEX.lock().unwrap();
unsafe { Z3_optimize_dec_ref(self.ctx.z3_ctx, self.z3_opt) };
}
}