Repository navigation
Expand file tree
/
Copy pathinteractive.py
More file actions
348 lines (297 loc) · 9.86 KB
/
Copy pathinteractive.py
File metadata and controls
348 lines (297 loc) · 9.86 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# -*- coding: iso-8859-15 -*-
# (c) 2011-2012 Roland Kindermann
#
# This file is part of the Aalto Timed Model Checker ATMOC.
#
# ATMOC is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ATMOC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ATMOC. If not, see <http://www.gnu.org/licenses/>.
import random, readline, fractions
import expressions, byices, config, brind, states, signal, sys
DISCRETE_STEP = 0
ELAPSE_STEP = 1
class DelayBound(object):
def __init__(self, bound, strict=None):
assert bound == None or isinstance(bound, (int, long, fractions.Fraction))
assert strict == None or isinstance(strict, bool)
assert (bound == None) == (strict == None)
assert bound >= 0
assert bound != 0 or not strict
self.bound = bound
self.strict = strict
def __str__(self):
if self.bound == None:
return 'unbounded'
else:
return '%s %s unit%s' % ('less than' if self.strict else 'at most', str(self.bound), 's' if self.bound != 1 else '')
def satisfied(self, num):
return self.bound == None or num < self.bound or (num == self.bound and not self.strict)
def num_to_letters(num):
assert isinstance(num, (int, long))
assert num >= 0
if num == 0:
return 'A'
else:
ret = ''
while num > 0:
ret = chr(ord('A') + (num % 26)) + ret
num /= 26
return ret
def letters_to_num(strn):
assert isinstance(strn, basestring)
strn = strn.lower()
assert all((ord('a') <= ord(x) <= ord('z')) for x in strn)
return sum(((ord(x) - ord('a')) * (26**(len(strn) - 1 - i))) for i, x in enumerate(strn))
class Interactor(object):
def __init__(self, variables, clockmax, init, trans, invar):
assert isinstance(variables, set)
assert all(isinstance(x, expressions.Variable) for x in variables)
assert isinstance(clockmax, dict)
assert all(isinstance(x, expressions.Variable) for x in clockmax.keys())
assert all(isinstance(x, (int, long, fractions.Fraction)) for x in clockmax.values())
assert all(x.type == expressions.REAL for x in clockmax.keys())
assert isinstance(init, expressions.Expression)
self.varcount = len(variables)
self.clockcount = len(clockmax)
self.variables = [] #Soon to be filled
self.clockmax = []
self.init = init
self.trans = trans
self.invar = invar
# Variables
stfnc = lambda x, y : cmp(x.name, y.name)
for clk in sorted(clockmax, stfnc):
self.variables.append(clk)
self.clockmax.append(clockmax[clk])
self.clockcount = len(self.variables)
assert len(self.clockmax) == self.clockcount
for var in sorted(variables, stfnc):
self.variables.append(var)
self.varcount = len(self.variables)
### Initial stuff
def init_yices(self):
self.yi = byices.Yices(config.yicesfile)
assert len(self.yi.vars) == 0
for v in self.variables:
self.yi.add_var(v.name, v.type)
for v in self.variables:
self.yi.add_var(v.name + brind.PRIME_SUFFIX, v.type)
assert len(self.yi.vars) == len(self.variables) * 2
if self.clockmax:
self.deltaind = len(self.yi.vars)
self.yi.add_var(brind.DELTAVAR_NAME, expressions.REAL)
self.yi.encode(self.trans) # to get variables allocated
### State searching
def full_states(self, s):
'Replaces all None values with actual values'
s += [None] * (self.varcount - len(s))
# Find combinations
svals = []
for i in xrange(len(s)):
if s[i] == None:
if self.yi.vtypes[i] == expressions.BOOLEAN:
svals.append([False, True])
elif self.yi.vtypes[i] == expressions.REAL:
svals.append([0])
else:
raise Exception('Unsupported type: self.yi.vtypes[i]')
else:
svals.append([s[i]])
assert len(svals) == len(s)
assert len(svals) >= 1
# Products
comb = [[]]
for vals in svals:
ncomb = []
for c in comb:
for val in vals:
ncomb.append(c + [val])
comb = ncomb
return [states.State(x, False, self) for x in comb]
def get_all_states(self, primed):
ret = []
while self.yi.check():
# Get states
s = states.State(self.yi.get_model(), primed, self)
ret += self.full_states(s)
# Encode negation minus clocks
for i in xrange(self.clockcount):
s[i] = None
enc = s.encode_exact(self.yi, primed)
if len(enc) == 0: #'Anything possible'
break
elif len(enc) == 1:
enc = enc[0]
else:
enc = enc[0].mk_and(enc[1:])
self.yi.assertion(enc.mk_not())
return ret
def get_delay_bound(self):
'Returns delay bound for unbounded'
# Find relevant values
if not self.clockmax:
return None
relevant_values = [0]
for ci in xrange(self.clockcount):
mx = self.clockmax[ci]
v = self.state[ci]
assert v != None
rval = mx - v
while rval > 0:
relevant_values.append(rval)
rval -= 1
relevant_values = sorted(set(relevant_values))
# Check them
try:
self.yi.push()
self.yi.assertion(self.yi.encode(self.invar))
for i in xrange(len(relevant_values)):
# Values
rv = relevant_values[i]
last = i >= len(relevant_values) - 1
if last:
rvp = rv + fractions.Fraction(1, 2)
else:
rvp = (rv + relevant_values[i+1]) / fractions.Fraction(2)
# rv
try:
self.yi.push()
for enc in self.state.elapse(rv).encode_exact(self.yi, False):
self.yi.assertion(enc)
if not self.yi.check():
return DelayBound(rv, True)
finally:
self.yi.pop()
#rvp
try:
self.yi.push()
for enc in self.state.elapse(rvp).encode_exact(self.yi, False):
self.yi.assertion(enc)
if not self.yi.check():
return DelayBound(rv, False)
finally:
self.yi.pop()
finally:
self.yi.pop()
return DelayBound(None)
## Selection
def select_state(self, typ, states, delay_bound=None):
'Returns selected state (or None to quit)'
# Print options
if (not states) and ((delay_bound == None ) or (delay_bound.bound == 0)):
print 'DEADLOCK -- No %s state' % typ
sys.exit(0)
if states:
print 'Please select a%s %s state' % ('n' if typ[0].lower() in 'aiouey' else '', typ)
strns = [str(s) for s in states]
multiline = any(('\n' in x) for x in strns)
for i, strn in enumerate(strns):
print ' State %s: %s%s' % (num_to_letters(i), strn, '\n' if multiline else '')
if delay_bound != None and (delay_bound.bound == None or delay_bound.bound > 0):
print '%s perform a time elapse step by entering a number (%s)' % ('Or' if states else 'Please', str(delay_bound))
# State selection
try:
while True:
try:
print
inp = raw_input('Your choice: ')
except EOFError, e:
return None
oinp = inp
inp = inp.lower().strip()
if inp == '':
if states:
selected = random.randint(0, len(states) - 1)
print 'Selected state', num_to_letters(selected), 'randomly'
return states[selected]
elif all((ord('a') <= ord(c) <= ord('z')) for c in inp):
num = letters_to_num(inp)
if 0 <= num < len(states):
return states[num]
elif delay_bound != None:
try:
if '/' in inp:
inp = inp.split('/')
inp = fractions.Fraction(int(inp[0]), int(inp[1]))
elif '.' in inp:
[l, r] = inp.split('.')
den = 10**len(r)
if len(l) == 0:
l = '0'
inp = fractions.Fraction(int(l) * den + int(r), den) # Avoid using fractions to avoid rounding errors
else:
inp = int(inp)
if inp >= 0 and delay_bound.satisfied(inp):
return self.state.elapse(inp)
except ValueError, e:
pass
print 'INVALID SELECTION:', repr(oinp)
finally:
print
assert False
def select_successor(self):
# Delay bound
delay_bound = self.get_delay_bound()
# Successors
self.yi.push()
for enc in self.state.encode_exact(self.yi, False):
self.yi.assertion(enc)
self.yi.assertion(self.yi.encode(self.invar))
self.yi.assertion(self.yi.encode(self.trans))
if self.clockmax:
zero = self.yi.context.mk_num(0)
self.yi.assertion(self.yi.vars[self.deltaind].mk_eq(zero))
for i in xrange(self.clockcount):
self.yi.assertion(self.yi.vars[i].mk_ge(zero))
self.yi.assertion(self.yi.vars[i + self.varcount].mk_ge(zero))
successors = self.get_all_states(True)
self.yi.pop()
# Choice
self.state = self.select_state('successor', successors, delay_bound)
def get_initial_states(self):
self.yi.push()
self.yi.assertion(self.yi.encode(self.init))
self.yi.assertion(self.yi.encode(self.invar))
if self.clockmax:
# Note that all clocks are set to zero due to real valued variables being unused leading to trouble
zero = self.yi.context.mk_num(0)
for i in xrange(self.clockcount):
self.yi.assertion(self.yi.encode(expressions.AstExpression('=', self.variables[i], '0')))
self.yi.assertion(self.yi.vars[i + self.varcount].mk_eq(zero))
self.yi.assertion(self.yi.vars[self.deltaind].mk_eq(zero))
ret = self.get_all_states(False)
self.yi.pop()
return ret
### Control flow
def print_instructions(self):
print 'Commands:'
print ' <number> : Select option <number>. Supported formats: 1.5, 3/2'
print ' <enter> : Select random option'
print ' EOF : Quit'
print
def run(self):
prev_sig = signal.signal(signal.SIGINT, lambda _, __ : sys.exit(0))
try:
self.print_instructions()
self.init_yices()
inits = self.get_initial_states()
self.state = self.select_state('initial', inits)
while self.state != None:
print 'Current state:', self.state
print
self.select_successor()
finally:
signal.signal(signal.SIGINT, prev_sig)
if __name__ == '__main__':
for n in xrange(100):
strn = num_to_letters(n)
print n, strn, letters_to_num(strn)