Petri nets are a mathematical modeling scheme for the description of distributed systems.
Petri nets were invented in 1962 by Carl Adam Petri. They have been used to model various kinds of systems, including computer networks, manufacturing systems, and biological systems.
CarlAdam
is a Python library for working with Petri nets, named after their inventor.
It provides a simple, Python-oriented API for defining and manipulating Petri nets.
It is also a simulator for Petri nets, so you can run your Petri net models and see how they behave.
Here is an example of a Petri net being used to model a simple pull-style production workflow. It models three stages, each with their own capacity. Each stage signals demand for intermediate products they need, and provides products their own work is done.
The above example and more are included, and can all be interacted with by cloning this repo.
Use Docker, which runs the required Kroki and Niolesk services locally as well:
$ docker compose up
Or, run the simulator directly, which uses the public Kroki and Niolesk websites by default:
$ poetry install
$ make simulator
Then browse to http://localhost:8000 to see the simulator in action.
Petri net functionality can be included in your own code.
Just add CarlAdam
as a dependency.
>>> from carladam import PetriNet, Place, Token, Transition, arc_path
>>> class MyNet(PetriNet):
... class Structure:
... on = Place()
... off = Place()
... turn_on = Transition()
... turn_off = Transition()
... arcs = {*arc_path(turn_on, on, turn_off, off, turn_on)}
>>> net = MyNet.new()
>>> ns = net.Structure
>>> ns.arcs
{□ Turn on → ⬭ On, □ Turn off → ⬭ Off, ⬭ Off → □ Turn on, ⬭ On → □ Turn off}
>>> before = {ns.on: {Token()}}
>>> before
{⬭ On: {⚫️}}
>>> list(net.enabled_transitions(before))
[□ Turn off]
>>> after = net.marking_after_transition(before, ns.turn_off)
>>> after
pmap({⬭ Off: pset([⚫️])})
You can also include Petri net diagrams in Jupyter notebooks.
More documentation will be provided for this in a future release.
Initial work on the CarlAdam package was sponsored by Routable.