-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudbm.py
317 lines (304 loc) · 11.5 KB
/
udbm.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright 2011 Peter Bulychev
#
# This file is part of Python binding to the UPPAAL DBM library.
#
# This binding 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.
#
# This binding 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 this binding. If not, see <http://www.gnu.org/licenses/>.
import logging
import pdb
import udbm_int
my_logger = logging.getLogger('Python-UDBM')
my_logger.setLevel(logging.DEBUG)
class Clock:
def __init__(self, context, name, index):
self.context = context
self.index = index
self.name = name
self.dbm_index = index+1
def __sub__(self, var2):
assert(isinstance(var2, Clock))
return VariableDifference([self, var2])
def __le__(self, c):
assert(type(c) == int)
return Federation(Constraint(self, None, c, False))
def __ge__(self, c):
assert(type(c) == int)
return Federation(Constraint(None, self, -c, False))
def __lt__(self, c):
assert(type(c) == int)
return Federation(Constraint(self, None, c, True))
def __gt__(self, c):
assert(type(c) == int)
return Federation(Constraint(None, self, -c, True))
def __eq__(self, c):
assert(type(c) == int)
return Federation(Constraint(self, None, c, False)) & Federation(Constraint(None, self, -c, False))
def __ne__(self, c):
assert(type(c) == int)
return Federation(Constraint(self, None, c, True)) | Federation(Constraint(None, self, -c, True))
def __hash__(self):
return hash(self.getFullName())
def getFullName(self):
if self.context.name:
return self.context.name +'.' + self.name
else:
return self.name
class Valuation (dict):
def __init__(self, context):
self.context = context
def __setitem__(self, key, value):
if not isinstance(key, Clock):
assert(type(key) == str)
l = [v for v in self.context.clocks if v.name == key]
if len(l) != 1:
raise KeyError
key = l[0]
assert(key.context == self.context)
dict.__setitem__(self, key, value)
def check(self):
for v in self.context.clocks:
if not self.has_key(v):
my_logger.error("Clock %s is not present in clock valuation"%(v.name,))
class IntValuation(Valuation):
def __setitem__(self, key, value):
if not (type(value) == int):
raise TypeError
Valuation.__setitem__(self, key, value)
class FloatValuation(Valuation):
def __setitem__(self, key, value):
assert(type(value) in [float, int])
Valuation.__setitem__(self, key, value)
class VariableDifference:
def __init__(self, vars):
assert(len(vars) == 2)
assert(vars[0].context == vars[1].context)
self.vars = vars
self.context = vars[0].context
def __le__(self, c):
assert(type(c) == int)
return Federation(Constraint(self.vars[0], self.vars[1], c, False))
def __ge__(self, c):
assert(type(c) == int)
return Federation(Constraint(self.vars[1], self.vars[0], -c, False))
def __lt__(self, c):
assert(type(c) == int)
return Federation(Constraint(self.vars[0], self.vars[1], c, True))
def __gt__(self, c):
assert(type(c) == int)
return Federation(Constraint(self.vars[1], self.vars[0], -c, True))
def __eq__(self, c):
assert(type(c) == int)
return Federation(Constraint(self.vars[0], self.vars[1], c, False)) & Federation(Constraint(self.vars[1], self.vars[0], -c, False))
def __ne__(self, c):
assert(type(c) == int)
return Federation(Constraint(self.vars[0], self.vars[1], c, True)) | Federation(Constraint(self.vars[1], self.vars[0], -c, True))
class Constraint:
def __init__(self, arg1, arg2, val, isStrict):
self.arg1 = arg1
self.arg2 = arg2
assert(isinstance(arg1, Clock) or (arg1 is None))
assert(isinstance(arg2, Clock) or (arg2 is None))
assert(not (arg1 is None) or not (arg2 is None))
assert(type(val) == int)
if arg1 is None and arg2 is None:
assert(arg1.context == arg2.context)
if arg1 is None:
self.context = arg2.context
else:
self.context = arg1.context
args = [arg1, arg2]
dbm_indexes = [None, None]
for i in (0,1):
if args[i] is None:
dbm_indexes[i]=0
else:
dbm_indexes[i] = args[i].dbm_index
self._constraint = udbm_int.Constraint(dbm_indexes[0], dbm_indexes[1], val, isStrict)
class Federation:
def __init__(self, arg):
if isinstance(arg, Constraint):
self._fed = udbm_int.Federation(len(arg.context.clocks)+1, arg._constraint)
self.context = arg.context
elif isinstance(arg, Context):
self._fed = udbm_int.Federation(len(arg.clocks)+1)
self._fed.setZero()
self.context = arg
else:
assert(0)
def __str__(self):
a = udbm_int.VarNamesAccessor()
for i in range(len(self.context.clocks)):
var = self.context.clocks[i]
a.setClockName(var.dbm_index, var.getFullName())
return self._fed.toStr(a).replace(' && ', ' & ').replace(' || ', ' | ')
def copy(self):
ret = Federation(self.context)
ret._fed = udbm_int.Federation(self._fed)
return ret
def __and__(self, arg2):
assert(isinstance(arg2, Federation))
assert(arg2.context == self.context)
ret = Federation(self.context)
ret._fed = self._fed.andOp(arg2._fed)
return ret
def __iand__(self, arg2):
assert(isinstance(arg2, Federation))
assert(arg2.context == self.context)
self._fed &= arg2._fed
return self
def __or__(self, arg2):
assert(isinstance(arg2, Federation))
assert(arg2.context == self.context)
ret = Federation(self.context)
ret._fed = self._fed.orOp(arg2._fed)
return ret
def __ior__(self, arg2):
assert(isinstance(arg2, Federation))
assert(arg2.context == self.context)
self._fed |= arg2._fed
return self
def __add__(self, arg2):
assert(isinstance(arg2, Federation))
assert(arg2.context == self.context)
ret = Federation(self.context)
ret._fed = self._fed.addOp(arg2._fed)
return ret
def __iadd__(self, arg2):
assert(isinstance(arg2, Federation))
assert(arg2.context == self.context)
self._fed += arg2._fed
return self
def __sub__(self, arg2):
assert(isinstance(arg2, Federation))
assert(arg2.context == self.context)
ret = Federation(self.context)
ret._fed = self._fed.minusOp(arg2._fed)
return ret
def __isub__(self, arg2):
assert(isinstance(arg2, Federation))
assert(arg2.context == self.context)
self._fed -= arg2._fed
return self
def up(self):
ret = self.copy()
ret._fed.up()
return ret
def down(self):
ret = self.copy()
ret._fed.down()
return ret
def reduce(self, level=0):
self._fed.mergeReduce(0, level)
return self
def freeClock(self, c):
ret = self.copy()
ret._fed.freeClock(c.dbm_index)
return ret
def setZero(self):
self._fed.setZero()
return self
def hasZero(self):
return self._fed.hasZero()
def setInit(self):
self._fed.setInit()
return self
def convexHull(self):
ret =self.copy()
ret._fed.convexHull()
return ret
def __eq__(self, arg2):
assert(arg2.context == self.context)
return self._fed.eq(arg2._fed)
def __ne__(self, arg2):
assert(arg2.context == self.context)
return not (self == arg2)
def __le__(self, arg2):
assert(arg2.context == self.context)
return self._fed.le(arg2._fed)
def __ge__(self, arg2):
assert(arg2.context == self.context)
return self._fed.ge(arg2._fed)
def __lt__(self, arg2):
assert(arg2.context == self.context)
return self._fed.lt(arg2._fed)
def __gt__(self, arg2):
assert(arg2.context == self.context)
return self._fed.gt(arg2._fed)
def intern(self):
return self._fed.intern()
def predt(self, arg2):
assert(arg2.context == self.context)
ret = self.copy()
ret._fed.predt(arg2._fed)
return ret
def contains(self, valuation):
valuation.check()
if isinstance(valuation, IntValuation):
p = udbm_int.IntClockValuation(len(self.context.clocks)+1)
f = self._fed.containsIntValuation
elif isinstance(valuation, FloatValuation):
p = udbm_int.DoubleClockValuation(len(self.context.clocks)+1)
f = self._fed.containsDoubleValuation
else:
my_logger.error("Unknown valuation type")
for v in self.context.clocks:
p.setClockValue(v.dbm_index, valuation[v])
return f(p)
def updateValue(self, clock, value):
assert(clock.context == self.context)
ret = self.copy()
ret._fed.updateValue(clock.dbm_index, value)
return ret
def resetValue(self, clock):
return self.updateValue(clock, 0)
def getSize(self):
return self._fed.size()
def extrapolateMaxBounds(self, a):
if len(a) != len(self.context.clocks):
my_logger.error("extrapolateMaxBounds without all clocks setted")
ret = self.copy()
v = udbm_int.IntVector(len(self.context.clocks)+1)
v.setElement(0, 0) # we don't care about first element, but let's exclude possible nedetermenism
for clock in a.keys():
v.setElement(clock.dbm_index, a[clock])
ret._fed.myExtrapolateMaxBounds(v)
return ret
def isZero(self):
return self == self.context.getZeroFederation()
def isEmpty(self):
return self._fed.isEmpty()
def __hash__(self):
return self._fed.hash()
def hash(self):
return hash(self)
class Context:
def __init__(self, clock_names, name = None):
self.clocks = []
self.name = name
for i, clock_name in zip(range(len(clock_names)), clock_names):
clock = Clock(self, clock_name, i)
self.clocks.append(clock)
if hasattr(self, clock_name):
my_logger.warning('Class %s already has attribute %s'%(self.__class__.__name__, clock_name))
else:
setattr(self, clock_name, clock)
def setName(self, name):
self.name = name
def __getitem__(self, arg):
names = [v for v in self.clocks if v.name == arg]
if len(names)==0:
raise KeyError
assert(len(names)==1)
return names[0]
def getZeroFederation(self):
return Federation(self)