pos.py
13.1 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#-*- encoding: latin-1
"""A plugin to add positions to the nodes.
`Place` and `Transition` are added an optional argument for the
constructor `pos=(x,y)` to set their position. Moreover, these classes
are added an attribute `pos` that holds a pair of numbers with
attributes `x` and `y` and methods `shift(dx, dy)` and `moveto(x, y)`.
So, when the plugin is loaded, we can specify and retreive nodes
positions:
>>> import snakes.plugins
>>> snakes.plugins.load('pos', 'snakes.nets', 'nets')
<module ...>
>>> from nets import PetriNet, Place, Transition
>>> n = PetriNet('N')
>>> n.add_place(Place('p00'))
>>> t10 = Transition('t10', pos=(1, 0))
>>> n.add_transition(t10)
>>> n.add_place(Place('p11', pos=(1, 1)))
>>> n.add_transition(Transition('t01', pos=(0, 1)))
>>> t10.pos
Position(1, 0)
>>> t10.pos.x
1
>>> t10.pos.y
0
>>> t10.pos()
(1, 0)
Nodes positions is immutable, we must use method `moveto` to change
positions:
>>> t10.pos.y = 1
Traceback (most recent call last):
...
AttributeError: readonly attribute
>>> t10.pos.moveto(t10.pos.x, 1)
>>> t10.pos
Position(1, 1)
Petri nets are added methods `bbox()` that returns a pair of extrema
`((xmin, ymin), (xmax, ymax))`, a method `shift(dx, dy)` that shift
all the nodes, and a method `transpose()` that rotates the net in such
a way that the top-down direction becomes left-right:
>>> n.bbox()
((0, 0), (1, 1))
>>> n.shift(1, 2)
>>> n.bbox()
((1, 2), (2, 3))
>>> n.node('t01').copy().pos
Position(1, 3)
>>> n.transpose()
>>> n.node('t01').pos
Position(-3, 1)
"""
from snakes import SnakesError
from snakes.compat import *
from snakes.plugins import plugin, new_instance
from snakes.pnml import Tree
class Position (object) :
"The position of a node"
def __init__ (self, x, y) :
"""Constructor expects the Cartesian coordinates of the node,
they can be provided as `float` or `int`.
@param x: horizontal position
@type x: `float`
@param y: vertical position
@type y: `float`
"""
self.__dict__["x"] = x
self.__dict__["y"] = y
# apidoc skip
def __str__ (self) :
return "(%s, %s)" % (str(self.x), str(self.y))
# apidoc skip
def __repr__ (self) :
return "Position(%s, %s)" % (str(self.x), str(self.y))
# apidoc skip
def __setattr__ (self, name, value) :
if name in ("x", "y") :
raise AttributeError("readonly attribute")
else :
self.__dict__[name] = value
def moveto (self, x, y) :
"""Change current coordinates to the specified position
@param x: horizontal position
@type x: `float`
@param y: vertical position
@type y: `float`
"""
self.__init__(x, y)
def shift (self, dx, dy) :
"""Shift current coordinates by the specified amount.
@param dx: horizontal shift
@type dx: `float`
@param dy: vertical shift
@type dy: `float`
"""
self.__init__(self.x + dx, self.y + dy)
def __getitem__ (self, index) :
"""Access coordinates by index
>>> Position(1, 2)[0]
1
>>> Position(1, 2)[1]
2
>>> Position(1, 2)[42]
Traceback (most recent call last):
...
IndexError: Position index out of range
@param index: 0 for `x` coordinate, 1 for `y`
@type index: `int`
@raise IndexError: when `index not in {0, 1}`
"""
if index == 0 :
return self.x
elif index == 1 :
return self.y
else :
raise IndexError("Position index out of range")
def __iter__ (self) :
"""Successively yield `x` and `y` coordinates
>>> list(Position(1, 2))
[1, 2]
"""
yield self.x
yield self.y
def __call__ (self) :
"""Return the position as a pair of values
>>> Position(1, 2.0)()
(1, 2.0)
@return: the pair of coordinates `(x, y)`
@rtype: `tuple`
"""
return (self.x, self.y)
@plugin("snakes.nets")
def extend (module) :
class Place (module.Place) :
def __init__ (self, name, tokens=[], check=None, **args) :
"""If no position is given `(0, 0)` is chosen
>>> Place('p').pos
Position(0, 0)
>>> Place('p', pos=(1,2)).pos
Position(1, 2)
@keyword pos: the position of the new place
@type pos: `tuple`
"""
x, y = args.pop("pos", (0, 0))
self.pos = Position(x, y)
module.Place.__init__(self, name, tokens, check, **args)
# apidoc skip
def copy (self, name=None, **args) :
x, y = args.pop("pos", self.pos())
result = module.Place.copy(self, name, **args)
result.pos.moveto(x, y)
return result
# apidoc skip
def __pnmldump__ (self) :
"""
>>> p = Place('p', pos=(1, 2))
>>> p.__pnmldump__()
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<place id="p">
<type domain="universal"/>
<initialMarking>
<multiset/>
</initialMarking>
<graphics>
<position x="1" y="2"/>
</graphics>
</place>
</pnml>
"""
t = module.Place.__pnmldump__(self)
try :
gfx = t.child("graphics")
except SnakesError :
gfx = Tree("graphics", None)
t.add_child(gfx)
gfx.add_child(Tree("position", None,
x=str(self.pos.x),
y=str(self.pos.y)))
return t
# apidoc skip
@classmethod
def __pnmlload__ (cls, tree) :
"""
>>> old = Place('p', pos=(1, 2))
>>> p = old.__pnmldump__()
>>> new = Place.__pnmlload__(p)
>>> new.pos
Position(1, 2)
>>> new
Place('p', MultiSet([]), tAll)
>>> new.__class__
<class 'snakes.plugins.pos.Place'>
"""
result = new_instance(cls, module.Place.__pnmlload__(tree))
try :
p = tree.child("graphics").child("position")
x, y = eval(p["x"]), eval(p["y"])
result.pos = Position(x, y)
except SnakesError :
result.pos = Position(0, 0)
return result
class Transition (module.Transition) :
def __init__ (self, name, guard=None, **args) :
"""If no position is given `(0, 0)` is chosen
>>> Transition('t').pos
Position(0, 0)
>>> Transition('t', pos=(1,2)).pos
Position(1, 2)
@keyword pos: the position of the new transition
@type pos: `tuple`
"""
x, y = args.pop("pos", (0, 0))
self.pos = Position(x, y)
module.Transition.__init__(self, name, guard, **args)
# apidoc skip
def copy (self, name=None, **args) :
x, y = args.pop("pos", self.pos())
result = module.Transition.copy(self, name, **args)
result.pos.moveto(x, y)
return result
# apidoc skip
def __pnmldump__ (self) :
"""
>>> t = Transition('t', pos=(2, 1))
>>> t.__pnmldump__()
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<transition id="t">
<graphics>
<position x="2" y="1"/>
</graphics>
</transition>
</pnml>
"""
t = module.Transition.__pnmldump__(self)
t.add_child(Tree("graphics", None,
Tree("position", None,
x=str(self.pos.x),
y=str(self.pos.y))))
return t
# apidoc skip
@classmethod
def __pnmlload__ (cls, tree) :
"""
>>> old = Transition('t', pos=(2, 1))
>>> p = old.__pnmldump__()
>>> new = Transition.__pnmlload__(p)
>>> new.pos
Position(2, 1)
>>> new
Transition('t', Expression('True'))
>>> new.__class__
<class 'snakes.plugins.pos.Transition'>
"""
result = new_instance(cls, module.Transition.__pnmlload__(tree))
try :
p = tree.child("graphics").child("position")
x, y = eval(p["x"]), eval(p["y"])
result.pos = Position(x, y)
except SnakesError :
result.pos = Position(0, 0)
return result
class PetriNet (module.PetriNet) :
def add_place (self, place, **args) :
"""Position can be set also when a place is added to the
net.
>>> n = PetriNet('n')
>>> n.add_place(Place('a', pos=(1, 2)))
>>> n.node('a').pos
Position(1, 2)
>>> n.add_place(Place('b'), pos=(3,4))
>>> n.node('b').pos
Position(3, 4)
>>> n.add_place(Place('c', pos=(42, 42)), pos=(5, 6))
>>> n.node('c').pos
Position(5, 6)
@keyword pos: the position of the added place
@type pos: `tuple`
"""
if "pos" in args :
x, y = args.pop("pos")
place.pos.moveto(x, y)
module.PetriNet.add_place(self, place, **args)
def add_transition (self, trans, **args) :
"""Position can be set also when a transitions is added to
the net. See method `add_place` above.
@keyword pos: the position of the added transition
@type pos: `tuple`
"""
if "pos" in args :
x, y = args.pop("pos")
trans.pos.moveto(x, y)
module.PetriNet.add_transition(self, trans, **args)
def merge_places (self, target, sources, **args) :
"""When places are merged, the position of the new place
is the barycentre of the positions of the merged nodes.
Optionally, position can be specified in the call the
`merge_places`.
>>> n = PetriNet('n')
>>> n.add_place(Place('a', pos=(1,2)))
>>> n.add_place(Place('b', pos=(3,4)))
>>> n.merge_places('c', ['a', 'b'])
>>> n.node('c').pos
Position(2.0, 3.0)
>>> n.merge_places('d', ['a', 'b'], pos=(0, 0))
>>> n.node('d').pos
Position(0, 0)
@keyword pos: the position of the added transition
@type pos: `tuple`
"""
pos = args.pop("pos", None)
module.PetriNet.merge_places(self, target, sources, **args)
if pos is None :
pos = reduce(complex.__add__,
(complex(*self._place[name].pos())
for name in sources)) / len(sources)
x, y = pos.real, pos.imag
else :
x, y = pos
self._place[target].pos.moveto(x, y)
def merge_transitions (self, target, sources, **args) :
"""See method `merge_places` above.
"""
pos = args.pop("pos", None)
module.PetriNet.merge_transitions(self, target, sources, **args)
if pos is None :
pos = reduce(complex.__add__,
(complex(*self._trans[name].pos())
for name in sources)) / len(sources)
x, y = pos.real, pos.imag
else :
x, y = pos
self._trans[target].pos.moveto(x, y)
def bbox (self) :
"""The bounding box of the net, that is, the smallest
rectangle that contains all nodes coordinates.
@return: rectangle coordinates as `((xmin, ymin), (xmax,
ymax))`
@rtype: tuple
"""
if len(self._node) == 0 :
return (0, 0), (0, 0)
else :
nodes = iter(self._node.values())
xmin, ymin = next(nodes).pos()
xmax, ymax = xmin, ymin
for n in nodes :
x, y = n.pos()
xmin = min(xmin, x)
xmax = max(xmax, x)
ymin = min(ymin, y)
ymax = max(ymax, y)
return (xmin, ymin), (xmax, ymax)
def shift (self, dx, dy) :
"""Shift every node by `(dx, dy)`
@param dx: horizontal shift
@type dx: `float`
@param dy: vertical shift
@type dy: `float`
"""
for node in self.node() :
node.pos.shift(dx, dy)
def transpose (self) :
"""Perform a clockwise 90° rotation of node coordinates,
ie, change every position `(x, y)` to `(-y, x)`
"""
for node in self.node() :
x, y = node.pos()
node.pos.moveto(-y, x)
return Place, Transition, PetriNet, Position