labels.py
10.7 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
"""A plugin to add labels to nodes and nets. Labels are names (valid
Python identifiers) associated to arbitrary objects.
>>> import snakes.plugins
>>> snakes.plugins.load('labels', 'snakes.nets', 'nets')
<module ...>
>>> from nets import *
>>> t = Transition('t')
>>> t.label(foo='bar', spam=42)
>>> t.label('foo')
'bar'
>>> t.label('spam')
42
Note that when nodes in a Petri net are merged, their labels are
merged too, but in an arbitrary order. So, for example:
>>> n = PetriNet('N')
>>> n.add_place(Place('p1'))
>>> n.place('p1').label(foo='bar', spam='ham')
>>> n.add_place(Place('p2'))
>>> n.place('p2').label(hello='world', spam='egg')
>>> n.merge_places('p', ['p1', 'p2'])
>>> n.place('p').label('hello')
'world'
>>> n.place('p').label('foo')
'bar'
>>> n.place('p').label('spam') in ['ham', 'egg']
True
In the latter statement, we cannot know whether the label will be one
or the other value because merging has been done in an arbitrary
order.
"""
from snakes.plugins import plugin, new_instance
from snakes.pnml import Tree
@plugin("snakes.nets")
def extend (module) :
class Transition (module.Transition) :
def label (self, *get, **set) :
"""Get and set labels for the transition. The labels given
in `get` will be returned as a `tuple` and the labels
assigned in `set` will be changed accordingly. If a label
is given both in `get`and `set`, the returned value is
that it had at the beginning of the call, ie, before it is
set by the call.
@param get: labels which values have to be returned
@type get: `str`
@param set: labels which values have to be changed
@type set: `object`
@return: the tuples of values corresponding to `get`
@rtype: `tuple`
@raise KeyError: when a label given in `get` has not been
assigned previouly
"""
if not hasattr(self, "_labels") :
self._labels = {}
result = tuple(self._labels[g] for g in get)
self._labels.update(set)
if len(get) == 1 :
return result[0]
elif len(get) > 1 :
return result
elif len(set) == 0 :
return self._labels.copy()
def has_label (self, name, *names) :
"""Check is a label has been assigned to the transition.
@param name: the label to check
@type name: `str`
@param names: additional labels to check, if used, the
return value is a `tuple` of `bool` instead of a
single `bool`
@return: a Boolean indicating of the checked labels are
present or not in the transitions
@rtype: `bool`
"""
if len(names) == 0 :
return name in self._labels
else :
return tuple(n in self._labels for n in (name,) + names)
# apidoc stop
def copy (self, name=None, **options) :
if not hasattr(self, "_labels") :
self._labels = {}
result = module.Transition.copy(self, name, **options)
result._labels = self._labels.copy()
return result
def __pnmldump__ (self) :
"""
>>> t = Transition('t')
>>> t.label(foo='bar', spam=42)
>>> t.__pnmldump__()
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<transition id="t">
<label name="foo">
<object type="str">
bar
</object>
</label>
<label name="spam">
<object type="int">
42
</object>
</label>
</transition>
</pnml>
"""
t = module.Transition.__pnmldump__(self)
if hasattr(self, "_labels") :
for key, val in self._labels.items() :
t.add_child(Tree("label", None,
Tree.from_obj(val),
name=key))
return t
@classmethod
def __pnmlload__ (cls, tree) :
"""
>>> old = Transition('t')
>>> old.label(foo='bar', spam=42)
>>> p = old.__pnmldump__()
>>> new = Transition.__pnmlload__(p)
>>> new
Transition('t', Expression('True'))
>>> new.__class__
<class 'snakes.plugins.labels.Transition'>
>>> new.label('foo', 'spam')
('bar', 42)
"""
t = new_instance(cls, module.Transition.__pnmlload__(tree))
t._labels = dict((lbl["name"], lbl.child().to_obj())
for lbl in tree.get_children("label"))
return t
class Place (module.Place) :
def label (self, *get, **set) :
"See documentation for `Transition.label` above"
if not hasattr(self, "_labels") :
self._labels = {}
result = tuple(self._labels[g] for g in get)
self._labels.update(set)
if len(get) == 1 :
return result[0]
elif len(get) > 1 :
return result
elif len(set) == 0 :
return self._labels.copy()
def has_label (self, name, *names) :
"See documentation for `Transition.has_label` above"
if len(names) == 0 :
return name in self._labels
else :
return tuple(n in self._labels for n in (name,) + names)
# apidoc stop
def copy (self, name=None, **options) :
if not hasattr(self, "_labels") :
self._labels = {}
result = module.Place.copy(self, name, **options)
result._labels = self._labels.copy()
return result
def __pnmldump__ (self) :
"""
>>> p = Place('p')
>>> p.label(foo='bar', spam=42)
>>> p.__pnmldump__()
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<place id="p">
<type domain="universal"/>
<initialMarking>
<multiset/>
</initialMarking>
<label name="foo">
<object type="str">
bar
</object>
</label>
<label name="spam">
<object type="int">
42
</object>
</label>
</place>
</pnml>
"""
t = module.Place.__pnmldump__(self)
if hasattr(self, "_labels") :
for key, val in self._labels.items() :
t.add_child(Tree("label", None,
Tree.from_obj(val),
name=key))
return t
@classmethod
def __pnmlload__ (cls, tree) :
"""
>>> old = Place('p')
>>> old.label(foo='bar', spam=42)
>>> p = old.__pnmldump__()
>>> new = Place.__pnmlload__(p)
>>> new
Place('p', MultiSet([]), tAll)
>>> new.__class__
<class 'snakes.plugins.labels.Place'>
>>> new.label('foo', 'spam')
('bar', 42)
"""
p = new_instance(cls, module.Place.__pnmlload__(tree))
p._labels = dict((lbl["name"], lbl.child().to_obj())
for lbl in tree.get_children("label"))
return p
class PetriNet (module.PetriNet) :
def label (self, *get, **set) :
"See documentation for `Transition.label` above"
if not hasattr(self, "_labels") :
self._labels = {}
result = tuple(self._labels[g] for g in get)
self._labels.update(set)
if len(get) == 1 :
return result[0]
elif len(get) > 1 :
return result
elif len(set) == 0 :
return self._labels.copy()
def has_label (self, name, *names) :
"See documentation for `Transition.has_label` above"
if len(names) == 0 :
return name in self._labels
else :
return tuple(n in self._labels for n in (name,) + names)
# apidoc stop
def copy (self, name=None, **options) :
if not hasattr(self, "_labels") :
self._labels = {}
result = module.PetriNet.copy(self, name, **options)
result._labels = self._labels.copy()
return result
def __pnmldump__ (self) :
"""
>>> n = PetriNet('n')
>>> n.label(foo='bar', spam=42)
>>> n.__pnmldump__()
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<net id="n">
<label name="foo">
<object type="str">
bar
</object>
</label>
<label name="spam">
<object type="int">
42
</object>
</label>
</net>
</pnml>
"""
t = module.PetriNet.__pnmldump__(self)
if hasattr(self, "_labels") :
for key, val in self._labels.items() :
t.add_child(Tree("label", None,
Tree.from_obj(val),
name=key))
return t
@classmethod
def __pnmlload__ (cls, tree) :
"""
>>> old = PetriNet('n')
>>> old.label(foo='bar', spam=42)
>>> p = old.__pnmldump__()
>>> new = PetriNet.__pnmlload__(p)
>>> new
PetriNet('n')
>>> new.__class__
<class 'snakes.plugins.labels.PetriNet'>
>>> new.label('foo', 'spam')
('bar', 42)
"""
n = new_instance(cls, module.PetriNet.__pnmlload__(tree))
n._labels = dict((lbl["name"], lbl.child().to_obj())
for lbl in tree.get_children("label"))
return n
def merge_places (self, target, sources, **options) :
module.PetriNet.merge_places(self, target, sources, **options)
new = self.place(target)
for place in sources :
new.label(**dict(self.place(place).label()))
def merge_transitions (self, target, sources, **options) :
module.PetriNet.merge_transitions(self, target, sources, **options)
new = self.transition(target)
for trans in sources :
new.label(**dict(self.transition(trans).label()))
return Transition, Place, PetriNet