npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pseuco/colored-petri-nets

v1.5.1

Published

A library to express colored petri nets and analyze their behavior.

Downloads

12

Readme

colored-petri-nets

This is a library to describe colored Petri nets and CPN++, an enhanced version of colored Petri nets for modeling programs.

© Felix Freiberger, 2018-2019, Saarland University

A Word of Caution

This library is quite experimental. For now, there is no proper documentation, and the API shouldn't be considered stable.

Petri Net Semantics

In this library, the colors of tokens in the nets are simple JavaScript values, consisting of booleans, integers, arrays and objects. For example, these are valid colors:

  • true
  • 42
  • "Hello World!
  • [42, []]
  • { object: {}, array: [] }

Transitions work by having patterns on incoming flows and expressions on outgoing flows. A transition is enabled if

  • all incoming flows can find a token that matches the pattern,
  • the incoming flows produce bindings that are compatible and
  • the resulting binding fulfills the guard.

If a transition is executed, the outgoing flows produce tokens corresponding to their expressions, which may reference the binding.

As an example, a transition with two inflows with the patterns [ x ] and x, the guard x > 10 and an outflow with the expression (x + 1) * 2 behaves like this:

  • If the inflows try to read 5 and 5, respectively, the transition is not enabled, because the first pattern expects an array.
  • If the inflows try to read [ 4 ] and 5, respectively, the transition is not enabled, because the bindings are not compatible.
  • If the inflows try to read [ 5 ] and 5, respectively, the transition is not enabled, because the binding does not satisfy the guard. If the inflows try to read [ 20 ] and 20, respectively, the transition is enabled, and the outflow produces a token colored 42.

The syntax for patterns and expressions is inspired by JavaScript. Here are examples of valid patterns:

  • x
  • _ (a wildcard, drops the value)
  • []
  • [a, b, c]
  • [a, b, [c, d]]
  • [a, , c] (equivalent to [a, _, c])
  • [a, b, _]
  • {}
  • { a: a, b: c }
  • { a, b }
  • { a }
  • [a, { b : c, d: [e, f] }]
  • { one, two, ...rest } (rest will be an object with all properties except one and two)
  • { ...obj } (equivalent to obj except it rejects non-objects)
  • [ one, two, ...rest ] (rest will be an array with the all remaining items)
  • [ ...arr ] (equivalent to arr except it rejects non-arrays)

Here are examples of valid expressions:

  • var
  • 1337
  • true
  • "hello world"
  • "hello \"world"
  • "back\\slash"
  • 1 + 28
  • 1 - 28
  • "hello" + " " + "world"
  • 3 * 9
  • a * ( b + c)
  • 1 > 2
  • 2 >= 2
  • 2 <= 1
  • 2 < 1
  • 2 == 1
  • 2 != 1
  • a || b
  • a && b
  • !a
  • []
  • [ 42, 1337-1336 ]
  • [1, 1, 0].length
  • [9, 8] @ [7] @ [6, 5] (array concatenation)
  • {}
  • {a:7, b : x, c }
  • { a, ...b, c }
  • { a: 1, ...x, b: 2, ...y, a: 2 }
  • { x, y }.y.z
  • true ? 42 : 1337
  • eval(a + b, vars) (evaluates a + b in the binding expressed by the object vars)

An Example

The following Petri net computes the first 10 numbers of the Fibonacci sequence and puts a token with an array of them in the place done:

{
    "places": [
        {
            "key": "calc",
            "displayName": "calculate Fibonacci sequence",
            "extensions": {}
        },
        {
            "key": "done",
            "displayName": "calculation finished",
            "extensions": {}
        }
    ],
    "transitions": [
        {
            "key": "add-fib",
            "displayName": "add one fibonacci number",
            "guard": "list.length < 10",
            "inFlows": [
                {
                    "source": "calc",
                    "pattern": "{ a, b, list }"
                }
            ],
            "outFlows": [
                {
                    "target": "calc",
                    "expression": "{ a: b, b: a + b, list: list @ [a] }"
                }
            ],
            "extensions": {}
        },
        {
            "key": "exit",
            "displayName": "exit the main loop",
            "guard": "list.length == 10",
            "inFlows": [
                {
                    "source": "calc",
                    "pattern": "{ list }"
                }
            ],
            "outFlows": [
                {
                    "target": "done",
                    "expression": "list"
                }
            ],
            "extensions": {}
        }
    ],
    "initialMarking": {
        "tokens": {
            "calc": [
                {
                    "color": {
                        "a": 0,
                        "b": 1,
                        "list": []
                    }
                }
            ]
        },
        "extensions": {}
    }
}

More specifially, the above is the JSON object representation of the net. The following code parses this to a JavaScript object, converts it to a real PetriNet instance, then uses the API to query the behavior of the net:

const { PetriNet } = require('@pseuco/colored-petri-nets');

const fs = require('fs');

const netString = fs.readFileSync('net.json');
const netObject = JSON.parse(netString);

// build an actual PetriNet object from the plain object representation.
const net = PetriNet.fromObject(netObject);

let marking = net.initialMarking;

while (true) {
    console.log(JSON.stringify(marking.toObject()));

    let successors = marking.enabledMoves();
    if (successors.length > 1) throw new Error("Found nondeterminism!");
    if (successors.length < 1) {
        console.log("Terminated.");
        return;
    }

    console.log(` ↓ ${successors[0].transition.displayName}`);

    marking = successors[0].marking;
}