This plugin defines a data structure Cluster
that allows to group
nodes hierarchically. This is used by plugin gv
to improve the
graphical layout of Petri nets obtained using plugin ops
.
In general, this plugin is probably not needed by anyone, consequently, documentation will be very terse.
Class Cluster
class Cluster (object) :
A hierarchical data structure to organise strings (intended to be node names).
Method Cluster.__init__
def __init__ (self, nodes=[], children=[]) :
Create a cluster whose to-level nodes are nodes
and with
sub-clusters given in children
.
>>> Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])])
Cluster(...)
Method Cluster.get_path
def get_path (self, name) :
Get the path of a name inside the cluster. This path is a list of indexes for each child-cluster within its parent.
>>> Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])]).get_path('C')
[1, 0]
Call API
str name
: the searched namereturn list
: the list of indexes asint
values
Method Cluster.add_node
def add_node (self, name, path=None) :
Add name
to the cluster, optionally at a given position
path
.
>>> c = Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])])
>>> c.add_node('c')
Cluster([...'c'...], ...)
>>> c.add_node('E', [1, 0])
Cluster([...'E'...], [])
>>> c
Cluster([...'c'...],
[Cluster(['...', '...'],
[Cluster(['A'], [])]),
Cluster(['...', '...', '...'],
[Cluster([...'E'...], [])])])
Call API
str name
: name to addlist path
: position wherename
should be added, given as a list of indexes
Method Cluster.remove_node
def remove_node (self, name) :
Remove a name from the cluster.
>>> c = Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])])
>>> c.remove_node('4')
>>> c
Cluster(['...', '...'],
[Cluster(['...', '...'],
[Cluster(['A'], [])]),
Cluster(['...', '...'],
[Cluster(['...', '...'], [])])])
Call API
str name
: name to remove
Method Cluster.rename_node
def rename_node (self, old, new) :
Change a name in the cluster.
>>> c = Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])])
>>> c.rename_node('4', '42')
>>> c
Cluster(['...', '...'],
[Cluster(['...', '...'],
[Cluster(['A'], [])]),
Cluster([...'42'...],
[Cluster(['...', '...'], [])])])
Call API
str old
: name to changestr new
: new name to replaceold
Method Cluster.add_child
def add_child (self, cluster=None) :
Add a child cluster
>>> c = Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])])
>>> c.add_child(c.copy())
>>> c
Cluster(['...', '...'],
[Cluster(['...', '...'],
[Cluster(['A'], [])]),
Cluster(['...', '...', '...'],
[Cluster(['...', '...'], [])]),
Cluster(['...', '...'],
[Cluster(['...', '...'],
[Cluster(['A'], [])]),
Cluster(['...', '...', '...'],
[Cluster(['...', '...'], [])])])])
Call API
Cluster cluster
: the new child, ifNone
is given, an empty child is added
Method Cluster.nodes
def nodes (self, all=False) :
Returns the nodes in the cluster: only the top-level ones
is all
is False
, or all the nodes otherwise.
>>> list(sorted(Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])]).nodes()))
['a', 'b']
>>> list(sorted(Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])]).nodes(True)))
['1', '2', '3', '4', '5', 'A', 'C', 'D', 'a', 'b']
Call API
bool all
: whether all the nodes should be returned or only the top-level onesreturn list
: list of nodes
Method Cluster.children
def children (self) :
Return the children of the cluster.
>>> Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])]).children()
(Cluster(['...', '...'],
[Cluster(['A'], [])]),
Cluster(['...', '...', '...'],
[Cluster(['...', '...'], [])]))
Call API
return tuple
: the children ofself
Method Cluster.__contains__
def __contains__ (self, name) :
Test if a name is in the cluster.
>>> c = Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])])
>>> 'a' in c
True
>>> 'x' in c
False
>>> '4' in c
True
Call API
str name
: the node to testreturn bool
: whethername
is in the cluster
Method Cluster.__iter__
def __iter__ (self) :
Iterate over the clusters and its children, yielding lists of nodes at each level.
>>> c = Cluster(['a', 'b'],
... [Cluster(['1', '2'],
... [Cluster(['A'])]),
... Cluster(['3', '4', '5'],
... [Cluster(['C', 'D'])])])
>>> for cluster in c :
... print(list(sorted(cluster.nodes())))
['a', 'b']
['1', '2']
['A']
['3', '4', '5']
['C', 'D']
Extensions
Class PetriNet
class PetriNet (module.PetriNet) :
Class PetriNet
is extended so that instances have an
attribute clusters
to which the nodes are added.
Method PetriNet.add_place
def add_place (self, place, **options) :
Call API
Place place
: the place to addobject options
: additional options for plugins- keyword
cluster
: position of the new place in the cluster
Method PetriNet.add_transition
def add_transition (self, trans, **options) :
Call API
Transition trans
: the transition to addobject options
: additional options for plugins- keyword
cluster
: position of the new transition in the cluster