Graph::PetriNet - Perl extension for Petri Nets


  # build your places objects (see DESCRIPTION)
  my %places = ('place1' => ....,
                'place2' => ....);
  # build your transition objects (see DESCRIPTION)
  my %transitions = ('trans1' => [ ... ],
                     'trans2' => [ ... ]);

  use Graph::PetriNet;
  my $pn = new Graph::PetriNet (places      => \%places,
                                transitions => \%transitions);

  # change a token setting at one place
  $pn->things ('place1')->tokens (42);

  # walk through the whole life time
  while ($pn->ignitables) {
     warn "tokens: ". $pn->things ('place1')->tokens;

  # only ignite one particular transitions
  $pn->ignite ('trans1', 'trans2'); 

  my @places = $pn->places;
  my @trans  = $pn->transitions;


This package implements a bipartite graph to represent and interpret a Petri net ( Accordingly, there are two kinds of nodes:

  • "Data nodes" carry the information to be processed and/or propagated. This package assumes that each such node has a unique label (just a string). The label is used to refer to the node.

  • "Transition nodes" carry the information how and when processing has to occur. Also transitions have unique labels and also they are objects. Every transition node has a set of incoming data nodes from which it can consume data. And it has a set of outgoing data nodes which will be fill with new data after a transition.

Processing Model

At any time the application can check which transitions are ignitable. It can ask the petri net to fire some (or all of them). It is the responsibility of the transition nodes to do what they are supposed to do.

Node Semantics

As a default behavior (not overly useful, but here it is), transition nodes consume tokens from the data nodes (actually one per node and transition) and then pass one token to the downstream data node.

To modify this behaviour, you simply implement your own data and transition nodes. To make this reasonably easy their behaviour is defined as trait: You can either take these traits as they are, or import the trait with modifications, or develop a subtrait which you import into your objects, or write the objects from scratch. For an example look at t/02_makefileish.t which implements a processing behaviour you would expect from make.


  • The roles (traits) are currently written with Class::Trait. Maybe in another time I reimplement this with Moose roles. Maybe.

  • This graph is not implemented on top of Graph, so using it as superclass. There is already a package Graph::Bipartite (not recommended) which blocks the namespace, but there are no deep reasons why this should not be possible.



The constructor expects a hash with the following fields:

transitions (mandatory, hash reference)

A hash reference, whereby the keys are labels for the transitions and the values are the transitions themselves. They can be anything but must be able to do the trait Graph::PetriNet::TransitAble.

places (mandatory, hash reference)

A hash reference, whereby the keys are labels for the places and the values are the places themselves. They can be anything but must be able to do the trait Graph::PetriNet::PlaceAble.

initialize (optional, integer)

If non-zero, then the constructor will invoke the token method on all places, setting them to 0.


  my $pn = new Graph::PetriNet (# here I want something special
                                places => { 'p1' => new My::Place (...),
                                            'p2' => new My::Place (...),
                                # too lazy, happy with the default behavior
                                transitions => {
                                            't1' => [ bless ({}, 'Whatever'), [ 'p1' ], [ 'p2' ] ],
                                            't2' => [ bless ({}, 'Whatever'), [ 'p2' ], [ 'p2' ] ]



@labels = $pn->places

Retrieve the labels of all places in the network.

@labels = $pn->transitions

Retrieve the labels of all transitions in the network.


@things = $pn->things ($label, ...)

Given some labels, this method returns the things with this label, or undef if there is none.



Resets all places to have zero tokens.


@is = $pn->ignitables

This method returns a list of transitions which can be fired. It returns the labels, not the object.


$pn->ignite $pn->ignite (label, ...)

This methods ignites those transitions which are handed in (as labels). If none is handed in, then all ignitables with be ignited.

SEE ALSO, Graph::PetriNet::PlaceAble, Graph::PetriNet::TransitionAble


Robert Barta, <>


Copyright (C) 2009 by Robert Barta

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.