Git Product home page Git Product logo

petrikit's Introduction

PetriKit

Getting started

Note this tutorial assumes you are using Swift 4 on a unix-like machine.

Installing PetriKit

You use PetriKit with the Swift Package Manager. Create an empty directory for your application, open a terminal inside and create a new Swift command line tool:

mkdir /some/path/to/your/app
cd /some/path/to/your/app
swift package init --type executable

You'll have to add PetriKit as a dependency to your application. Open the generated file Package.swift and edit its content as follows:

// swift-tools-version:4.2

import PackageDescription

let package = Package(
  name: "MyApp",
  dependencies: [
    .package(url: "https://github.com/kyouko-taiga/PetriKit.git", from: "2.0.0")
  ],
  targets: [
    .target(
      name: "MyApp",
      dependencies: ["PetriKit"]),
  ])

This will let you import PetriKit in your sources. Add the following statement at the beginning of Sources/MyApp/main.swift:

import PetriKit

And you can now use PetriKit.

Creating a Place/Transition Net

Petri nets in PetriKit are described by the means of two protocols: Transition and PetriNet. Those protocols only describe the minimum requirements for a type to represent a transition (resp. a petri net). They can be seen as an abstract description of the structure of a Petri net. Hence, they can't be used as is, but rather should be implemented for a particular type of Petri net.

PetriKit comes with such an implementation for Place/Transition nets (or P/T-nets). Those are one of the most simple variant of Petri nets. You can create a P/T-net as follows:

enum Place {
  case p0, p1
}

let t0 = PTTransition<Place>(
  named         : "t0",
  preconditions : [PTArc(place: .p0)],
  postconditions: [PTArc(place: .p1)])
let t1 = PTTransition<Place>(
  named         : "t1",
  preconditions : [PTArc(place: .p1)],
  postconditions: [PTArc(place: .p0)])

let pn = PTNet(transitions: [t0, t1])

The above code create a P/T-net pn composed of two places and two transitions.

Simulating Place/Transition Nets

You can simulate the firing of a transition as follows:

let m = t0.fire(from: [.p0: 1, .p1: 1])

The above code assigns to m the marking obtained after firing the transition t0 from the marking [.p0: 1, .p1: 1] (i.e. one token in each place). Note that the method fire(from:) of the TransitionProtocol protocols returns an optional. That's because if the transition is not fireable from the given marking, the method should return nil.

You can also simulate the execution of a P/T-net from a given initial marking:

let m = pn.simulate(steps: 4, from: [.p0: 1, .p1: 0])

The above code simulate 4 steps of execution, randomly choosing one fireable transition at each step, or stops prematurely if the simulation reaches a deadlock.

Visualizing Place/Transition Nets

The type PTNet comes with a special method saveAsDot that outputs the given P/T-net as a Graphviz file. Note that this will require you to import the Foundation library as well.

import Foundation

// ...

try pn.saveAsDot(to: URL(fileURLWithPath: "pn.dot"), withMarking: [.p0: 1, .p1: 2])

The above code will output the P/T-net pn with marking [.p0: 1, .p1: 2] to a file named pn.dot.

petrikit's People

Contributors

kyouko-taiga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

petrikit's Issues

Marking algebra

It would be nice to have a complete algebra on markings (type PTMarking) with + - >=

in order to be able to write:

var m : PTMarking = [...]
var mm : PTMarking = [...]

let mmm= m+ mm
let m4= mmm-m

m>=mm

Le fichier .dot ne se créer pas (aucune erreur de compilation)

Avec le code suivant dans mon main.swift, le fichier pn.dot n'est pas créée, quel que soit le réseau de Petri pn que j'essaie de créer. De même, print() ne fonctionne pas ... Pourtant, si je rajoute une erreur volontaire, des messages d'erreurs apparaissent, ce qui veut dire que je compile bien le bon dossier. Le fichier pn.dot résiste à être créée, alors que le package est bien importé et que le terminal ne m'indique aucune erreur de compilation.

import Foundation
import PetriKit
import TaskManagerLib

let taskManager = createTaskManager()
let taskPool = taskManager.places.first { $0.name == "taskPool" }!
let create = taskManager.transitions.first { $0.name == "create" }!

print("Hello ?")

let pn = PTNet(places: [taskPool], transitions: [create])
try pn.saveAsDot(to: URL(fileURLWithPath: "pn.dot"), withMarking: [taskPool: 1])

(noob en swift, et noob en github, mon erreur est peut-être flagrante pour des yeux plus affutés)

Port the code to Swift 4

The code was written for Swift 3.1. It should be updated to the latest version of Swift (currently 4.2).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.