-
Notifications
You must be signed in to change notification settings - Fork 2
/
timescales.py
293 lines (238 loc) · 10.9 KB
/
timescales.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import sys
from collections import namedtuple
from properties.bounded_absence import *
from properties.bounded_universality import *
from properties.bounded_recurrence import *
from properties.bounded_response import *
def eliminate_stuttering(rows, cap=None):
if cap == None:
cap = sys.maxsize
prev = rows[0]
nrows = [rows[0]]
for row in rows[1:-1]:
if (row.data != prev.data) or (prev.time - nrows[-1].time >= cap):
nrows += [prev]
prev = row
nrows += [rows[-1]]
return nrows[1:]
def forward_eliminate_stuttering(rows, cap=None):
if cap == None:
cap = sys.maxsize
prev = rows[0]
nrows = [rows[0]]
for row in rows[1:]:
if (row.data != prev.data) or (prev.time - nrows[-1].time >= cap):
nrows += [row]
prev = row
return nrows
def write_spec(spec, filename, directory=""):
spec = '''---
name : "{name}"
pattern : "{spec}"
'''.format(name=filename, spec=spec)
with open(os.path.join(directory, '{filename}.yaml'.format(filename=filename)), 'w') as f:
f.write(spec)
def write_csv_trace(rows, filename, directory=""):
import csv
with open(os.path.join(directory, '{filename}.csv'.format(filename=filename)), 'w') as f:
w = csv.writer(f)
w.writerows([["time"] + [f for f in rows[0].data._fields]])
w.writerows([[row.time] + [d for d in row.data] for row in rows])
def write_json_trace(rows, filename, directory="", persistent=True, with_time=True):
import json
try:
os.remove(os.path.join(
directory, '{filename}.jsonl'.format(filename=filename)))
except OSError:
pass
with open(os.path.join(directory, '{filename}.jsonl'.format(filename=filename)), 'a') as f:
previous = dict()
for row in rows:
if with_time:
obj = dict(time=row.time)
else:
obj = dict()
if not persistent:
obj.update(row.data._asdict())
else:
current = row.data._asdict()
current_diff = {(k, current[k]) for k in current.keys()
if not k in previous or previous[k] != current[k]}
obj.update(current_diff)
previous = current
json.dump(obj, f)
f.write('\n')
def write_flatbuffers_trace(rows, filename, directory="", persistent=True, with_time=True):
import flatbuffers
from serialization.Timescales import Message, NullableInt64, NullableBool
try:
os.remove(os.path.join(
directory, '{filename}.bin'.format(filename=filename)))
except OSError:
pass
with open(os.path.join(directory, '{filename}.bin'.format(filename=filename)), 'w+b') as f:
previous = dict()
for row in rows:
builder = flatbuffers.Builder(512)
Message.MessageStart(builder)
if with_time:
Message.MessageAddTime(
builder, NullableInt64.CreateNullableInt64(builder, row.time))
if not persistent:
current = row.data._asdict()
if 'p' in current:
Message.MessageAddPropP(
builder, NullableBool.CreateNullableBool(builder, current['p']))
if 'q' in current:
Message.MessageAddPropQ(
builder, NullableBool.CreateNullableBool(builder, current['q']))
if 'r' in current:
Message.MessageAddPropR(
builder, NullableBool.CreateNullableBool(builder, current['r']))
if 's' in current:
Message.MessageAddPropS(
builder, NullableBool.CreateNullableBool(builder, current['s']))
else:
current = row.data._asdict()
if 'p' in current and (not 'p' in previous or current['p'] != previous['p']):
Message.MessageAddPropP(
builder, NullableBool.CreateNullableBool(builder, current['p']))
if 'q' in current and (not 'q' in previous or current['q'] != previous['q']):
Message.MessageAddPropQ(
builder, NullableBool.CreateNullableBool(builder, current['q']))
if 'r' in current and (not 'r' in previous or current['r'] != previous['r']):
Message.MessageAddPropR(
builder, NullableBool.CreateNullableBool(builder, current['r']))
if 's' in current and (not 's' in previous or current['s'] != previous['s']):
Message.MessageAddPropS(
builder, NullableBool.CreateNullableBool(builder, current['s']))
previous = current
msg = Message.MessageEnd(builder)
builder.FinishSizePrefixed(msg)
buf = builder.Output()
f.write(buf)
def write_protobuf_trace(rows, filename, directory="", persistent=True, with_time=True):
import serialization.Timescales_pb2 as Timescales
import struct
try:
os.remove(os.path.join(
directory, '{filename}.bin'.format(filename=filename)))
except OSError:
pass
with open(os.path.join(directory, '{filename}.bin'.format(filename=filename)), 'w+b') as f:
previous = dict()
for row in rows:
msg = Timescales.Message()
if with_time:
msg.time = row.time
if not persistent:
current = row.data._asdict()
if 'p' in current:
msg.p = current['p']
if 'q' in current:
msg.q = current['q']
if 'r' in current:
msg.r = current['r']
if 's' in current:
msg.s = current['s']
else:
current = row.data._asdict()
if 'p' in current and (not 'p' in previous or current['p'] != previous['p']):
msg.p = current['p']
if 'q' in current and (not 'q' in previous or current['q'] != previous['q']):
msg.q = current['q']
if 'r' in current and (not 'r' in previous or current['r'] != previous['r']):
msg.r = current['r']
if 's' in current and (not 's' in previous or current['s'] != previous['s']):
msg.s = current['s']
# Pack little-endian UInt32
protobuf = msg.SerializeToString()
buf_length = struct.pack('<I', len(protobuf))
f.write(buf_length + protobuf)
def generate(property, lower_bound, upper_bound, min_recur, max_recur, duration, limit_stutter, failing_end):
past_spec = ""
future_spec = ""
rows = []
if property == "absence_after_q":
past_spec = bounded_absence_after_q.generate_past_formula(upper_bound)
future_spec = bounded_absence_after_q.generate_future_formula(
upper_bound)
rows = bounded_absence_after_q.generate_trace(
upper_bound, duration, failing_end)
elif property == "absence_before_r":
past_spec = bounded_absence_before_r.generate_past_formula(upper_bound)
future_spec = bounded_absence_before_r.generate_future_formula(
upper_bound)
rows = bounded_absence_before_r.generate_trace(
upper_bound, duration, failing_end)
elif property == "absence_between_q_and_r":
past_spec = bounded_absence_between_q_and_r.generate_past_formula(
lower_bound, upper_bound)
future_spec = bounded_absence_between_q_and_r.generate_future_formula(
lower_bound, upper_bound)
rows = bounded_absence_between_q_and_r.generate_trace(
lower_bound, upper_bound, duration, failing_end)
elif property == "always_after_q":
past_spec = bounded_universality_after_q.generate_past_formula(
upper_bound)
future_spec = bounded_universality_after_q.generate_future_formula(
upper_bound)
rows = bounded_universality_after_q.generate_trace(
upper_bound, duration, failing_end)
elif property == "always_before_r":
past_spec = bounded_universality_before_r.generate_past_formula(
upper_bound)
future_spec = bounded_universality_before_r.generate_future_formula(
upper_bound)
rows = bounded_universality_before_r.generate_trace(
upper_bound, duration, failing_end)
elif property == "always_between_q_and_r":
past_spec = bounded_universality_between_q_and_r.generate_past_formula(
lower_bound, upper_bound)
future_spec = bounded_universality_between_q_and_r.generate_future_formula(
lower_bound, upper_bound)
rows = bounded_universality_between_q_and_r.generate_trace(
lower_bound, upper_bound, duration, failing_end)
elif property == "recurrence_globally":
past_spec = bounded_recurrence_globally.generate_past_formula(
upper_bound)
future_spec = bounded_recurrence_globally.generate_future_formula(
upper_bound)
rows = bounded_recurrence_globally.generate_trace(
upper_bound, duration, failing_end)
elif property == "recurrence_between_q_and_r":
past_spec = bounded_recurrence_between_q_and_r.generate_past_formula(
upper_bound)
future_spec = bounded_recurrence_between_q_and_r.generate_future_formula(
upper_bound)
rows = bounded_recurrence_between_q_and_r.generate_trace(
upper_bound, duration, min_recur, max_recur, failing_end)
elif property == "response_globally":
past_spec = bounded_response_globally.generate_past_formula(
lower_bound, upper_bound)
future_spec = bounded_response_globally.generate_future_formula(
lower_bound, upper_bound)
rows = bounded_response_globally.generate_trace(
lower_bound, upper_bound, duration, failing_end)
elif property == "response_between_q_and_r":
past_spec = bounded_response_between_q_and_r.generate_past_formula(
lower_bound, upper_bound)
future_spec = bounded_response_between_q_and_r.generate_future_formula(
lower_bound, upper_bound)
rows = bounded_response_between_q_and_r.generate_trace(
lower_bound, upper_bound, min_recur, max_recur, duration, failing_end)
else:
raise ValueError(
"Unknown property. Please see the help for supported properties.")
if limit_stutter == -1:
rows = forward_eliminate_stuttering(rows, None) # Maximum condensation
elif limit_stutter == 0:
pass # No condensation
elif limit_stutter >= 1:
rows = forward_eliminate_stuttering(
rows, limit_stutter) # Limited condensation
else:
raise ValueError(
"Illegal value for condensation. Must be -1, 0, 1+")
return past_spec, future_spec, rows