-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialization.py
105 lines (85 loc) · 3.4 KB
/
serialization.py
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
"""
Read/write results in libsmt.
"""
import json
from pysmt.smtlib.parser import SmtLibParser
from sabbath.lzz.serialization import (
_get_smt_vars,
_get_smt_formula,
_get_smt_formula_pred,
fromStringFormula, readVar,
parse_dyn_sys
)
def importSynthesis(problem_json, env):
"""
"""
parser = SmtLibParser(env)
dyn_systems = [None,None]
dyn_systems[0],_,_ = parse_dyn_sys(env, problem_json["sys_m0"], True, True)
dyn_systems[1],_,_ = parse_dyn_sys(env, problem_json["sys_m1"], True, True)
all_vars = []
vars_decl_str = None
for l in [problem_json["sys_m0"]["varsDecl"],problem_json["sys_m1"]["varsDecl"]]:
for var_decl in l:
app = []
readVar(parser, var_decl, app)
if not app[0] in all_vars:
all_vars += app
vars_decl_str = var_decl if vars_decl_str is None else "%s\n%s" % (vars_decl_str, var_decl)
theta = fromStringFormula(parser, vars_decl_str, problem_json["theta"]).args()[0]
reference_values = []
for json_r in problem_json["reference_values"]:
r = fromStringFormula(parser, vars_decl_str, json_r).args()[0]
reference_values.append(r)
if "lyapunov_m0" in problem_json:
lyapunov_m0 = fromStringFormula(parser, vars_decl_str, problem_json["lyapunov_m0"]).args()[0]
else:
lyapunov_m0 = None
if "lyapunov_m1" in problem_json:
lyapunov_m1 = fromStringFormula(parser, vars_decl_str, problem_json["lyapunov_m1"]).args()[0]
else:
lyapunov_m1 = None
if "assumption" in problem_json:
assumption = fromStringFormula(parser, vars_decl_str, problem_json["assumption"])
else:
assumption = None
return (dyn_systems,
theta,
reference_values,
lyapunov_m0,
lyapunov_m1,
assumption)
def serializeSynthesis(outstream,
dyn_systems,
theta,
reference_values,
lyapunov_m0,
lyapunov_m1,
assumption,
env):
"""
Writes invar_problem problem on outstream
"""
cont_vars_0_smt = [_get_smt_vars(v, env) for v in dyn_systems[0].states()]
cont_vars_1_smt = [_get_smt_vars(v, env) for v in dyn_systems[1].states()]
json_data = {
"sys_m0" : {
"contVars" : cont_vars_0_smt,
"varsDecl" : cont_vars_0_smt + [_get_smt_vars(v, env) for v in dyn_systems[0].inputs()],
"vectorField": [_get_smt_formula_pred(dyn_systems[0].get_ode(v), env) for v in dyn_systems[0].states()],
},
"sys_m1" : {
"contVars" : cont_vars_1_smt,
"varsDecl" : cont_vars_1_smt + [_get_smt_vars(v, env) for v in dyn_systems[1].inputs()],
"vectorField": [_get_smt_formula_pred(dyn_systems[1].get_ode(v), env) for v in dyn_systems[1].states()],
},
"theta" : _get_smt_formula_pred(theta, env),
"reference_values" : [_get_smt_formula_pred(v, env) for v in reference_values]
}
if not lyapunov_m0 is None:
json_data["lyapunov_m0"] = _get_smt_formula_pred(lyapunov_m0, env)
if not lyapunov_m1 is None:
json_data["lyapunov_m1"] = _get_smt_formula_pred(lyapunov_m1, env)
if not assumption is None:
json_data["assumption"] = _get_smt_formula(assumption, env)
json.dump(json_data, outstream)