Skip to content

Instantly share code, notes, and snippets.

@FRex
Created March 1, 2018 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FRex/084aabd74597b6a2ad617a9b9a95a784 to your computer and use it in GitHub Desktop.
Save FRex/084aabd74597b6a2ad617a9b9a95a784 to your computer and use it in GitHub Desktop.
from collections import defaultdict
class Transition:
def __init__(self, label, ins, outs):
self.label = label
self.inputs = frozenset(ins)
self.outputs = frozenset(outs)
def __repr__(self):
return f'Transition({repr(self.label)}, {list(self.inputs)}, {list(self.outputs)})'
transitions = []
transitions.append(Transition('1', ['a', 'b'], ['c', 'd']))
transitions.append(Transition('2', ['c'], ['a']))
transitions.append(Transition('3', ['d'], ['b']))
marking = frozenset(['a', 'b'])
visited = defaultdict(dict)
unfinished_markings = set([marking])
while True:
enabled = [t for t in transitions if marking.intersection(t.inputs) == t.inputs]
enabled = [t for t in enabled if t.label not in visited[marking]]
#add detection of 1 safety violation here
if len(enabled):
e = enabled[0]
new_marking = marking.difference(e.inputs).union(e.outputs)
visited[marking][e.label] = new_marking
marking = new_marking
unfinished_markings.add(marking)
else:
unfinished_markings.remove(marking)
if len(unfinished_markings):
marking = next(iter(unfinished_markings))
else:
break
for i, v in visited.items():
for i2, v2 in v.items():
m1 = sorted(list(i))
tr = i2
m2 = sorted(list(v2))
print(f"{m1} ---{tr}--> {m2}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment