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

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

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

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

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

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

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

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

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 PetriNetis 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

Method PetriNet.add_transition

def add_transition (self, trans, **options) :
Call API