-
Notifications
You must be signed in to change notification settings - Fork 1
/
pnml_lex.py
245 lines (220 loc) · 8.77 KB
/
pnml_lex.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import lex
class RLexerPNML(lex.RLexer):
"""
Lexer for Petri Net Markup Language (PNML)
"""
def lex(self, filename):
"""
Takes a (correctly) formatted PNML file as input, and returns
an unordered list of token objects, with properties 'label'
and 'value'. This list should then be passed to an RParser
object.
"""
from xml.dom.minidom import parseString
import parse_tokens as pat
import numpy as np
import re
self.input = filename
data = self.input.read()
self.input.close()
getpid = re.compile('(?i)place(?:.*)id\w?=\w?\"?([a-zA-Z_0-9]*)\"?')
gettid = re.compile('(?i)transition(?:.*)id\w?=\w?\"?([a-zA-Z_0-9]*)\"?')
getaid = re.compile('(?i)arc(?:.*)id\w?=\w?\"?([a-zA-Z_0-9]*)\"?')
getatype = re.compile('(?i)type\w?=\w?\"?([a-zA-Z_0-9]*)\"?')
getnid = re.compile('(?i)net(?:.*)id\w?=\w?\"?([a-zA-Z_0-9 ]*)\"?')
getcapac = re.compile('(?i)capacity\w?=\w?\"?([0-9]*)\"?')
getsource = re.compile('(?i)source\w?=\w?\"?([a-zA-Z_0-9]*)\"?')
gettarg = re.compile('(?i)target\w?=\w?\"?([a-zA-Z_0-9]*)\"?')
dom = parseString(data)
plist = []
mlist = []
clist = []
pcount = 0
for place in dom.getElementsByTagName('place'):
tag = place.toxml()
m = None
try:
# For places where the place name is encoded in the
# <place> .. <name> .. <text>
data = str(place.getElementsByTagName('name')[0]\
.getElementsByTagName('text')[0]\
.firstChild.data).rstrip()
except IndexError:
pass
try:
# For places where the place id tag is the name of the place
data = str(re.search(getpid, tag).group(1).rstrip())
except AttributeError:
pass
try:
# Try different ways of retrieving the marking, allows
# flexibility in input file. #1 = <marking><value>X</.. </..
m = int(place.getElementsByTagName('marking')[0]\
.getElementsByTagName('value')[0].firstChild.data)
except IndexError:
pass
try:
# #2 = <initialMarking><valueX</.. </..
# Takes precendence over the above
m = int(place.getElementsByTagName('initialMarking')[0]\
.getElementsByTagName('value')[0].firstChild.data)
except IndexError:
pass
try:
# #3 = <initialMarking><text>X</.. </..
# Takes precendence over the above
m = int(place.getElementsByTagName('initialMarking')[0]\
.getElementsByTagName('text')[0].firstChild.data)
except IndexError:
pass
if m == None:
print "Note: No initial markings detected, set to 0"
### WARNING NO INITIAL MARKING SET FOR PLACE ###
mlist.append(0)
else:
mlist.append(m)
try:
cap = int(re.search(getcapac, tag).group(1).rstrip())
if cap != 0:
clist.append(cap)
except:
pass
plist.append(data)
pcount +=1
tlist = []
rates = []
tcount = 0
for trans in dom.getElementsByTagName('transition'):
# Initialise to blank each time
tid = None
tag = trans.toxml()
# ALT methods of retrieving transition names:
# NOTE: arc source/target (should) use transition id as a reference
# try:
# #preferably get the name from <name>..<value> OR <text>
# tid = str(trans.getElementsByTagName('name')[0]\
# .getElementsByTagName('value')[0].firstChild.data)\
# .rstrip()
# except:
# pass
# try:
# tid = str(trans.getElementsByTagName('name')[0]\
# .getElementsByTagName('text')[0].firstChild.data)\
# .rstrip()
# except:
# pass
try:
# Is transition name encoded in id?
tid = str(re.search(gettid, tag).group(1))
except:
# else assign out own IDs
tid = 't_' + str(tcount)
tlist.append(tid)
tcount +=1
## RATES
try:
rate = str(trans.getElementsByTagName('rate')[0]\
.getElementsByTagName('value')[0].firstChild.data)
rates.append(float(rate))
except:
pass
# Get arcs
arcd = {}
stoich = None
acount = 0
for arc in dom.getElementsByTagName('arc'):
tag = arc.toxml()
try:
atype = arc.getElementsByTagName('type')[0].firstChild.data
print atype
except IndexError:
pass
try:
stoich = arc.getElementsByTagName('text')[0].firstChild.data
except IndexError:
pass
try:
stoich = arc.getElementsByTagName('value')[0]\
.firstChild.data
stoich = arc.getElementsByTagName('inscription')[0]\
.getElementsByTagName('value')[0].firstChild.data
except IndexError:
pass
if stoich == None:
### WARNING: MISSING STOICHIOMETRIES
### ASSUMED ONE
print "Note: Missing stoichiometry, set to 1"
stoich = 1
# arc ID
try:
aid = str(re.search(getaid, tag).group(1))
except:
aid = acount
afrom = str(re.search(getsource, tag).group(1))
# find test and inhibitory arcs:
try:
atype = str(re.search(getatype, tag).group(1))
except:
atype = None
if atype == 'test':
mat = 'test'
elif atype == 'inhibitory':
mat = 'inhib'
else:
if afrom in plist:
mat = 'pre'
else:
mat = 'post'
ato = str(re.search(gettarg, tag).group(1))
# ARC d
arcd[aid] = [int(stoich), afrom, ato, mat]
acount +=1
# if [1] in plist >> pre
# if [1] in tlist >> post
net = re.search(getnid, str(dom.getElementsByTagName('net')[0]\
.toxml())).group(1)
# format { arcid : \
# [stoichiometry, from (place or trans), to (p or t)], 'pre' or 'post' }
## Pre and Post ##
pre = np.zeros(shape=(len(tlist), len(plist)), dtype=int)
post = pre.copy()
# test and inhib may not be needed
test = pre.copy()
inhib = pre.copy()
zeros = pre.copy().tolist()
cols={}
for i in range(len(plist)):
# buils dict for matrices population purposes
# columns of pre/post relate to places
cols[plist[i]] = i
rows={}
for j in range(len(tlist)):
# rows relate to transitions
rows[tlist[j]] = j
try:
for arc in arcd.values():
if arc[3] == 'pre':
pre[rows[arc[2]], cols[arc[1]]] = int(arc[0])
elif arc[3] == 'post':
post[rows[arc[1]], cols[arc[2]]] = int(arc[0])
elif arc[3] == 'test':
test[rows[arc[2]], cols[arc[1]]] = int(arc[0])
elif arc[3] == 'inhib':
inhib[rows[arc[2]], cols[arc[1]]] = int(arc[0])
except KeyError:
print "Error with place or transition identifiers"
raise AttributeError
## Build token list
TOK = []
vectok = { 'p': plist, 't': tlist, 'r': rates, 'm' : mlist, 'c':clist }
mattok = {'pre': pre, 'post': post, 'inhib':inhib, 'test':test}
for key in vectok.keys():
if len(vectok[key]) != 0:
TOK.append(pat.Token(key, np.array(vectok[key])))
for ky in mattok.keys():
if mattok[ky].tolist() != zeros:
TOK.append(pat.Token(ky, np.matrix(mattok[ky])))
# run error handling
self.check(TOK)
# return unordered list of token objects
return TOK