-
Notifications
You must be signed in to change notification settings - Fork 0
/
petriNet.py
58 lines (49 loc) · 1.48 KB
/
petriNet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from snakes.nets import *
import sys
graph = PetriNet('First net')
graph.add_transition(Transition("Attacker"))
err = {}
preconditions = {}
def parseFile(data):
global graph, err, preconditions
file = data.split("\n")[:-1]
for line in file:
tmpL, tmpR = line.split('==>')
tmpL = tmpL.strip()
startVertices = tmpL.split(',')
for vertex in startVertices:
if vertex not in preconditions:
nameOfVertex = ""
if vertex not in err:
nameOfVertex = vertex
err[vertex] = 1
else:
nameOfVertex = vertex + "_(" + str(err[vertex]) + ")"
err[vertex] += 1
preconditions[vertex] = nameOfVertex
graph.add_place(Place(nameOfVertex))
graph.add_output(nameOfVertex, "Attacker", Expression('x'))
endVertices, supp, conf = tmpR.split("#")
endVertices = endVertices.strip()
endVertices = endVertices.split(',')
for vertex in endVertices:
nameOfTransition = ""
if vertex not in err:
nameOfTransition = vertex
err[vertex] = 1
else:
nameOfTransition = vertex + "_(" + str(err[vertex]) + ")"
err[vertex] += 1
graph.add_transition(Transition(nameOfTransition))
for ver in startVertices:
graph.add_input(preconditions[ver], nameOfTransition, Variable('x'))
nameOfNode = vertex + "_(" + str(err[vertex]) + ")"
err[vertex] += 1
graph.add_place(Place(nameOfNode))
graph.add_output(nameOfNode, nameOfTransition, Expression(vertex))
def init():
data = open(sys.argv[1], "r")
a = data.read()
parseFile(a)
return graph
init()