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.
Extensions
Class Transition
class Transition (module.Transition) :
Method Transition.label
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.
Call API
str get
: labels which values have to be returnedobject set
: labels which values have to be changedreturn tuple
: the tuples of values corresponding toget
Exceptions
KeyError
: when a label given inget
has not been assigned previouly
Method Transition.has_label
def has_label (self, name, *names) :
Check is a label has been assigned to the transition.
Call API
str name
: the label to checkobject names
: additional labels to check, if used, the return value is atuple
ofbool
instead of a singlebool
return bool
: a Boolean indicating of the checked labels are present or not in the transitions
Class Place
class Place (module.Place) :
Method Place.label
def label (self, *get, **set) :
See documentation for Transition.label
above
Method Place.has_label
def has_label (self, name, *names) :
See documentation for Transition.has_label
above
Class PetriNet
class PetriNet (module.PetriNet) :
Method PetriNet.label
def label (self, *get, **set) :
See documentation for Transition.label
above
Method PetriNet.has_label
def has_label (self, name, *names) :
See documentation for Transition.has_label
above