Skip to content

Instantly share code, notes, and snippets.

@keveman
Created August 30, 2012 01:28
Show Gist options
  • Save keveman/3521442 to your computer and use it in GitHub Desktop.
Save keveman/3521442 to your computer and use it in GitHub Desktop.
Petri net model for a program
import snakes.plugins
snakes.plugins.load('gv', 'snakes.nets', 'nets')
from nets import *
n = PetriNet('pn')
n.add_place(Place('p1', [1,2]))
n.add_place(Place('p2'))
n.add_place(Place('p3'))
n.add_place(Place('p4_i'))
n.add_place(Place('p4_o'))
n.add_place(Place('p5'))
n.add_place(Place('p6'))
n.add_transition(Transition('t1'))
n.add_input('p4_i', 't1', MultiArc((Value(1), Value(2))))
n.add_output('p4_o', 't1', MultiArc((Value(1), Value(2))))
n.add_transition(Transition('t2'))
n.add_input('p1', 't2', Variable('x'))
n.add_output('p2', 't2', Expression('x'))
n.add_transition(Transition('t3'))
n.add_input('p1', 't3', Variable('x'))
n.add_output('p3', 't3', Expression('x'))
n.add_transition(Transition('t4'))
n.add_input('p2', 't4', Variable('x'))
n.add_output('p4_i', 't4', Expression('x'))
n.add_transition(Transition('t5'))
n.add_input('p3', 't5', Variable('x'))
n.add_output('p4_i', 't5', Expression('x'))
n.add_transition(Transition('t6'))
n.add_input('p3', 't6', Variable('x'))
n.add_output('p5', 't6', Expression('x'))
n.add_transition(Transition('t7'))
n.add_input('p4_o', 't7', Variable('x'))
n.add_output('p6', 't7', Expression('x'))
n.add_transition(Transition('t8'))
n.add_input('p5', 't8', Variable('x'))
n.add_output('p6', 't8', Expression('x'))
n.draw('/tmp/petri.ps')
sg = StateGraph(n)
sg.build()
def n_attr(n, sg, at):
sg.goto(n)
m = sg.net.get_marking()
l = []
for k in sorted(m.keys()):
for e in sorted(m[k].items()):
l = l + [str(k) + '(' + str(e) + ')']
l = ' '.join(l)
at['label'] = l
sg.draw('/tmp/sg.ps', node_attr=n_attr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment