-
Notifications
You must be signed in to change notification settings - Fork 0
/
PetrinetReaderWriter.java
533 lines (523 loc) · 13.4 KB
/
PetrinetReaderWriter.java
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import java.io.*;
import java.lang.Math;
import java.util.regex.*;
public class PetrinetReaderWriter implements PetrinetIo {
public PetrinetReaderWriter() {
//
}
public void read(Reader r, Petrinet p) throws IOException, PetrinetFormatException {
PetrinetReadMachine m = new PetrinetReadMachine(p);
int buf = r.read();
while (buf != -1) {
m.sendChar((char)buf);
buf = r.read();
}
m.sendChar('\n');
}
public void write(Writer w, Petrinet p) throws IOException {
String in = p.toString();
// places
Pattern pat = Pattern.compile("PLACE");
Matcher mat = pat.matcher(in);
in = mat.replaceAll("plc");
// transitions
pat = Pattern.compile("TRANSITION");
mat = pat.matcher(in);
in = mat.replaceAll("trn");
// arcpt
pat = Pattern.compile("ARCPT");
mat = pat.matcher(in);
in = mat.replaceAll("apt");
// arctp
pat = Pattern.compile("ARCTP");
mat = pat.matcher(in);
in = mat.replaceAll("atp");
// initial
pat = Pattern.compile("INITIAL");
mat = pat.matcher(in);
in = mat.replaceAll("ini");
// juggle arc fields
pat = Pattern.compile("(\\([0-9]+\\))(.+)\n");
mat = pat.matcher(in);
in = mat.replaceAll(" $2$1\n");
// commas & parens to spaces
pat = Pattern.compile("\\(|,|\\)");
mat = pat.matcher(in);
in = mat.replaceAll(" ");
// tidy line endings
pat = Pattern.compile(" +\n");
mat = pat.matcher(in);
in = mat.replaceAll("\n");
// write to file
w.write(in);
}
/* INDENTATION IS INTENTIONAL */
private class PetrinetReadMachine {
/* 'Generic' States */
private static final int STARTLN = 0;
private static final int IGNORELN = 1;
/* 'Place' States */
private static final int PLCNAME = 2;
private static final int PLCXPOS = 3;
private static final int PLCYPOS = 4;
/* 'Transition' States */
private static final int TRNNAME = 5;
private static final int TRNXPOS = 6;
private static final int TRNYPOS = 7;
/* 'Place-Transition Arc' States */
private static final int APTPLC = 8;
private static final int APTTRN = 9;
private static final int APTWGT = 10;
/* 'Transition-Place Arc' States */
private static final int ATPTRN = 11;
private static final int ATPPLC = 12;
private static final int ATPWGT = 13;
/* 'Initial Arc' States */
private static final int INIPLC = 14;
private static final int INIWGT = 15;
/* Class Variables */
private int linenr;
private int state;
private Petrinet pnet;
private String line;
private String name;
private String place;
private String trans;
private String xpos;
private String ypos;
private String weight;
public PetrinetReadMachine(Petrinet p) {
this.linenr = 0;
this.state = 0;
this.pnet = p;
this.line = new String();
this.name = new String();
this.place = new String();
this.trans = new String();
this.xpos = new String();
this.ypos = new String();
this.weight = new String();
next(STARTLN);
}
public void sendChar(char c) throws PetrinetFormatException {
switch (this.state) {
case STARTLN:
if (c == ' ') {
if (this.line.length() == 0) {
// arbitrary whitespace?
return;
}
// line type in buffer, change state
if (this.line.equals("plc")) {
// line is a place
next(PLCNAME);
return;
}
if (this.line.equals("trn")) {
// line is a transition
next(TRNNAME);
return;
}
if (this.line.equals("apt")) {
// line is a place-transition arc
next(APTPLC);
return;
}
if (this.line.equals("atp")) {
// line is a transition-place arc
next(ATPTRN);
return;
}
if (this.line.equals("ini")) {
// line is an initial arc
next(INIPLC);
return;
}
// invalid type, fall through to error
}
if (this.line.length() == 0 && (c == '\n' || c == '\r')) {
// ignore newline characters
return;
}
if (c >= 'a' && c <= 'z') {
// append character to buffer
this.line += c;
return;
}
if (this.line.length() == 0 && c == '#') {
// line is a comment, ignore it
next(IGNORELN);
return;
}
// handle invalid characters
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Object Type");
// return;
case IGNORELN:
if (c == '\n' || c == '\r') {
// line has ended
next(STARTLN);
return;
}
// ignore the given character
return;
case PLCNAME:
if (c == ' ') {
if (this.name.length() == 0) {
// arbitrary whitespace
return;
}
// name is in buffer, change state
next(PLCXPOS);
return;
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// append character to buffer
this.name += c;
return;
}
// handle invalid characters
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Place Name");
// return;
case PLCXPOS:
if (c == ' ') {
if (this.xpos.length() == 0) {
// arbitrary whitespace
return;
}
// value is in buffer, change state
next(PLCYPOS);
return;
}
if (c >= '0' && c <= '9') {
// append character to buffer
this.xpos += c;
return;
}
// handle invalid characters
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Place X-Coordinate "+this.line+" "+c);
// return;
case PLCYPOS:
if (this.ypos.length() == 0 && c == ' ') {
// arbitrary whitespace
return;
}
if (this.ypos.length() > 0 && (c == '\r' || c == '\n')) {
// line has ended
int x = 0;
for (int i = 0; i < this.xpos.length(); i++) {
x += (this.xpos.charAt(i) - '0') * Math.pow(10, (this.xpos.length() - (i+1)));
}
int y = 0;
for (int i = 0; i < this.ypos.length(); i++) {
y += (this.ypos.charAt(i) - '0') * Math.pow(10, (this.ypos.length() - (i+1)));
}
this.pnet.newPlace(this.name, x, y);
next(STARTLN);
return;
}
if (c >= '0' && c <= '9') {
// append character
this.ypos += c;
return;
}
// handle invalid characters
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Place Y-Coordinate");
// return;
case TRNNAME:
if (c == ' ') {
if (this.name.length() == 0) {
// arbitrary whitespace
return;
}
// name is in buffer, change state
next(TRNXPOS);
return;
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// append character
this.name += c;
return;
}
// handle invalid characters
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Transition Name");
// return;
case TRNXPOS:
if (c == ' ') {
if (this.xpos.length() == 0) {
// arbitrary whitespace
return;
}
// position is in buffer, change state
next(TRNYPOS);
return;
}
if (c >= '0' && c <= '9') {
// append character
this.xpos += c;
return;
}
// handle invalid characters
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Transition X-Coordinate");
// return;
case TRNYPOS:
if (this.ypos.length() == 0 && c == ' ') {
// arbitrary whitespace
return;
}
if (this.ypos.length() > 0 && (c == '\r' || c == '\n')) {
// line end
int x = 0;
for (int i = 0; i < xpos.length(); i++) {
x += (this.xpos.charAt(i) - '0') * Math.pow(10, (this.xpos.length() - (i+1)));
}
int y = 0;
for (int i = 0; i < ypos.length(); i++) {
y += (this.ypos.charAt(i) - '0') * Math.pow(10, (this.xpos.length() - (i+1)));
}
this.pnet.newTransition(this.name, x, y);
next(STARTLN);
return;
}
if (c >= '0' && c <= '9') {
// append character
this.ypos += c;
return;
}
// handle bad characters
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Transition Y-Coordinate");
// return;
case APTPLC:
if (c == ' ') {
if (this.place.length() == 0) {
// arbitrary whitespace
return;
}
// name in buffer, check for the place in pnet
if (this.pnet.findPlace(this.place) != null) {
next(APTTRN);
return;
}
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// append character
this.place += c;
return;
}
// handle invalid characters
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Arc Place");
// return;
case APTTRN:
if (c == ' ') {
if (this.trans.length() == 0) {
// arbitrary whitespace
return;
}
if (this.pnet.findTransition(this.trans) != null) {
// in buffer, change state
next(APTWGT);
return;
}
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// append to buffer
this.trans += c;
return;
}
// handle invlid input
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Arc Transition");
// return;
case APTWGT:
if (this.weight.length() == 0 && c == ' ') {
// arbitrary whitespace
return;
}
if (this.weight.length() > 0 && (c == '\r' || c == '\n')) {
// end of line
int w = 0;
for (int i = 0; i < this.weight.length(); i++) {
w += (this.weight.charAt(i) - '0') * Math.pow(10, (this.weight.length() - (i+1)));
}
this.pnet.addArc(this.pnet.findPlace(this.place), this.pnet.findTransition(this.trans), w);
next(STARTLN);
return;
}
if (c >= '0' && c <= '9') {
// append to buffer
this.weight += c;
return;
}
// handle invalid input
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Arc Weight");
// return;
case ATPTRN:
if (c == ' ') {
if (this.trans.length() == 0) {
// arbitrary whitespace
return;
}
if (this.pnet.findTransition(this.trans) != null) {
// change state
next(ATPPLC);
return;
}
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// append to buffer
this.trans += c;
return;
}
// handle invalid input
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Arc Transition");
// return;
case ATPPLC:
if (c == ' ') {
if (this.place.length() == 0) {
// arbitrary whitespace
return;
}
if (this.pnet.findPlace(this.place) != null) {
// change state
next(ATPWGT);
return;
}
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// append character
this.place += c;
return;
}
// handle invalid
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Arc Place");
// return;
case ATPWGT:
if (this.weight.length() == 0 && c == ' ') {
// arbitrary whitespace
return;
}
if (this.weight.length() > 0 && (c == '\r' || c == '\n')) {
// end of line
int w = 0;
for (int i = 0; i < this.weight.length(); i++) {
w += (this.weight.charAt(i) - '0') * Math.pow(10, (this.weight.length() - (i+1)));
}
this.pnet.addArc(this.pnet.findTransition(this.trans), this.pnet.findPlace(this.place), w);
next(STARTLN);
return;
}
if (c >= '0' && c <= '9') {
// apend character
this.weight += c;
return;
}
// append character
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Arc Weight");
// return;
case INIPLC:
if (c == ' ') {
if (this.place.length() == 0) {
// arbitrary whitespace
return;
}
if (this.pnet.findPlace(this.place) != null) {
// change state
next(INIWGT);
return;
}
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
// append character
this.place += c;
return;
}
// bad character
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Initial-Arc Place");
// return;
case INIWGT:
if (this.weight.length() == 0 && c == ' ') {
// arbitrary whitespace
return;
}
if (this.weight.length() > 0 && (c == '\r' || c == '\n')) {
// end of line
int w = 0;
for (int i = 0; i < this.weight.length(); i++) {
w += (this.weight.charAt(i) - '0') * Math.pow(10, (this.weight.length() - (i+1)));
}
this.pnet.addInitialArc(this.pnet.findPlace(this.place), w);
next(STARTLN);
return;
}
if (c >= '0' && c <= '9') {
// append character
this.weight += c;
return;
}
// bad character
next(STARTLN);
throw new PetrinetFormatException(this.linenr, "Invalid Initial-Arc Weight");
// return;
}
}
private void next(int newstate) {
this.state = newstate;
switch (this.state) {
case STARTLN:
// next line
this.linenr += 1;
// reset the globals
this.line = "";
this.name = "";
this.place = "";
this.trans = "";
this.xpos = "";
this.ypos = "";
this.weight = "";
return;
case IGNORELN:
return;
case PLCNAME:
return;
case PLCXPOS:
return;
case PLCYPOS:
return;
case TRNNAME:
return;
case TRNXPOS:
return;
case TRNYPOS:
return;
case APTPLC:
return;
case APTTRN:
return;
case APTWGT:
return;
case ATPPLC:
return;
case ATPTRN:
return;
case ATPWGT:
return;
case INIPLC:
return;
case INIWGT:
return;
}
}
}
} // leave this here, it's supposed to be here