Skip to main content

Place/transition net (petri net) simulator that uses asyncio utilities as backend

Project description

SoyutNet

SoyutNet is a place/transition net (PT net, Petri net) simulator that uses Python's asyncio task and synchronization utilities as backend. (Soyut means abstract in Turkish.)

Its documentation can be found at https://soyutnet.readthedocs.io

Building

python3 -m venv venv
source venv/bin/activate
pip install -e '.[dev]'
pytest

Installing

python3 -m venv venv
source venv/bin/activate
pip install soyutnet

An example

This example simulates the PT net given in the diagram below.

PT net example

import sys
import asyncio

import soyutnet
from soyutnet import SoyutNet
from soyutnet.constants import GENERIC_ID, GENERIC_LABEL


def main():
    async def scheduled():
        await asyncio.sleep(1)
        soyutnet.terminate()

    with SoyutNet(extra_routines=[scheduled()]) as net:
        net.DEBUG_ENABLED = True

        LABEL = 1
        initial_tokens = {
            GENERIC_LABEL: [GENERIC_ID],
            LABEL: [1000, 990],
        }
        o1 = net.Observer(verbose=True)
        p1 = net.Place("p1", initial_tokens=initial_tokens, observer=o1)
        o2 = net.Observer(verbose=True)
        p2 = net.Place("p2", observer=o2)
        t1 = net.Transition("t1")
        """Define places and transitions (PTs)"""

        _ = net.Arc(labels=(GENERIC_LABEL, LABEL))
        p1 >> _ >> t1 >> _ >> p2
        """Connect PTs"""

    records = net.registry.get_merged_records()
    graph = net.registry.generate_graph(
        indent="  ", label_names={LABEL: "🤔", GENERIC_LABEL: "🤌"}
    )

    print("\nRecorded events:")
    {net.print(rec) for rec in records}
    print("\nNet graph:")
    print(graph, file=sys.stderr)

    return records, graph


if __name__ == "__main__":
    main()

outputs:

$ python tests/behavior/readme_example.py

loop(t1, 3): REC: O{(p1, 1)}: (112199.881220, ((0, 1, ), (1, 2, ), ), t1, )
loop(t1, 3): REC: O{(p1, 1)}: (112199.881402, ((0, 0, ), (1, 2, ), ), t1, )
loop(t1, 3): REC: O{(p1, 1)}: (112199.881550, ((0, 0, ), (1, 1, ), ), t1, )

Recorded events:
(p1, (112199.881220, ((0, 1, ), (1, 2, ), ), t1, ), )
(p1, (112199.881402, ((0, 0, ), (1, 2, ), ), t1, ), )
(p1, (112199.881550, ((0, 0, ), (1, 1, ), ), t1, ), )

Net graph:
digraph Net {
  subgraph cluster_0 {
    p1_0 [shape="circle",fontsize="20",style="filled",color="#000000",fillcolor="#dddddd",label="",xlabel="p1",height="1",width="1",penwidth=3];
    p2_0 [shape="circle",fontsize="20",style="filled",color="#000000",fillcolor="#dddddd",label="",xlabel="p2",height="1",width="1",penwidth=3];
    t1_0 [shape="box",fontsize="20",style="filled",color="#cccccc",fillcolor="#000000",label="",xlabel="t1",height="0.25",width="1.25",penwidth=3];
    t1_0 -> p2_0 [fontsize="20",label="{🤌,🤔}",minlen="2",penwidth="3"];
    p1_0 -> t1_0 [fontsize="20",label="{🤌,🤔}",minlen="2",penwidth="3"];
  }
  clusterrank=none;
}

How to interpret events

('p1', (188597.931257369, ((0, 1), (1, 2)), 't1'))

A list of place markings that show token counts for each label recorded just before a transition is fired:
[('<name of the place>', (<event timestamp in seconds>, ((<token label>, <token count>),), '<firing transition>')), ...]

How to generate the graph

sudo apt install graphviz # Which provides 'dot'
python tests/behavior/readme_example.py 2>&1 > /dev/null | dot -Tpng > readme_example.png

Outputs:

PT net graph

Credits

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

soyutnet-0.4.0b1.tar.gz (472.8 kB view details)

Uploaded Source

Built Distribution

soyutnet-0.4.0b1-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

Details for the file soyutnet-0.4.0b1.tar.gz.

File metadata

  • Download URL: soyutnet-0.4.0b1.tar.gz
  • Upload date:
  • Size: 472.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for soyutnet-0.4.0b1.tar.gz
Algorithm Hash digest
SHA256 22f17c29fd03e10e467e113701f10b13774bb5862ca5b9f33e5d2a4b3009b931
MD5 209f59590413571e694b9ce6841262fe
BLAKE2b-256 bebeb2f81cafd5b47159add3178c2469fa76853c4020db880de1145f764f8aec

See more details on using hashes here.

Provenance

The following attestation bundles were made for soyutnet-0.4.0b1.tar.gz:

Publisher: build.yml on dmrokan/soyutnet

Attestations:

File details

Details for the file soyutnet-0.4.0b1-py3-none-any.whl.

File metadata

  • Download URL: soyutnet-0.4.0b1-py3-none-any.whl
  • Upload date:
  • Size: 23.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for soyutnet-0.4.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 c601892bf1c43844d1aed2ab0d557e215a458185dfffa4f466edcf9661a7bbbe
MD5 32e9c778f6a7bf53a60b3e89a1f8c9fb
BLAKE2b-256 49fa45a809e8066ed2f41963ee855c43fc9897c54bf49699b7894020cbfeefca

See more details on using hashes here.

Provenance

The following attestation bundles were made for soyutnet-0.4.0b1-py3-none-any.whl:

Publisher: build.yml on dmrokan/soyutnet

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page