4
import java.io.IOException;
5
import java.math.BigDecimal;
6
import java.util.ArrayList;
7
import java.util.HashMap;
8
import java.util.TreeMap;
10
import javax.swing.JOptionPane;
11
import javax.xml.parsers.DocumentBuilder;
12
import javax.xml.parsers.DocumentBuilderFactory;
13
import javax.xml.parsers.ParserConfigurationException;
15
import org.w3c.dom.Document;
16
import org.w3c.dom.Element;
17
import org.w3c.dom.Node;
18
import org.w3c.dom.NodeList;
19
import org.xml.sax.SAXException;
21
import pipe.dataLayer.AnnotationNote;
22
import pipe.dataLayer.Arc;
23
import pipe.dataLayer.DataLayer;
24
import pipe.dataLayer.Note;
25
import pipe.dataLayer.PetriNetObject;
26
import pipe.dataLayer.Place;
27
import pipe.dataLayer.PlaceTransitionObject;
28
import pipe.dataLayer.TAPNQuery;
29
import pipe.dataLayer.Template;
30
import pipe.dataLayer.TimedInhibitorArcComponent;
31
import pipe.dataLayer.TimedInputArcComponent;
32
import pipe.dataLayer.TimedOutputArcComponent;
33
import pipe.dataLayer.TimedPlaceComponent;
34
import pipe.dataLayer.TimedTransitionComponent;
35
import pipe.dataLayer.Transition;
36
import pipe.dataLayer.TransportArcComponent;
37
import pipe.dataLayer.TAPNQuery.ExtrapolationOption;
38
import pipe.dataLayer.TAPNQuery.HashTableSize;
39
import pipe.dataLayer.TAPNQuery.SearchOption;
40
import pipe.dataLayer.TAPNQuery.TraceOption;
41
import pipe.gui.CreateGui;
42
import pipe.gui.DrawingSurfaceImpl;
45
import pipe.gui.Zoomable;
46
import pipe.gui.handler.AnimationHandler;
47
import pipe.gui.handler.AnnotationNoteHandler;
48
import pipe.gui.handler.ArcHandler;
49
import pipe.gui.handler.LabelHandler;
50
import pipe.gui.handler.PlaceHandler;
51
import pipe.gui.handler.TAPNTransitionHandler;
52
import pipe.gui.handler.TimedArcHandler;
53
import pipe.gui.handler.TransitionHandler;
54
import pipe.gui.handler.TransportArcHandler;
55
import dk.aau.cs.TCTL.TCTLAbstractProperty;
56
import dk.aau.cs.TCTL.Parsing.TAPAALQueryParser;
57
import dk.aau.cs.TCTL.visitors.AddTemplateVisitor;
58
import dk.aau.cs.TCTL.visitors.VerifyPlaceNamesVisitor;
59
import dk.aau.cs.gui.NameGenerator;
60
import dk.aau.cs.model.tapn.Constant;
61
import dk.aau.cs.model.tapn.ConstantStore;
62
import dk.aau.cs.model.tapn.LocalTimedPlace;
63
import dk.aau.cs.model.tapn.TimeInterval;
64
import dk.aau.cs.model.tapn.TimeInvariant;
65
import dk.aau.cs.model.tapn.TimedArcPetriNet;
66
import dk.aau.cs.model.tapn.TimedArcPetriNetNetwork;
67
import dk.aau.cs.model.tapn.TimedInhibitorArc;
68
import dk.aau.cs.model.tapn.TimedInputArc;
69
import dk.aau.cs.model.tapn.TimedMarking;
70
import dk.aau.cs.model.tapn.TimedOutputArc;
71
import dk.aau.cs.model.tapn.TimedPlace;
72
import dk.aau.cs.model.tapn.TimedToken;
73
import dk.aau.cs.model.tapn.TimedTransition;
74
import dk.aau.cs.model.tapn.TransportArc;
75
import dk.aau.cs.translations.ReductionOption;
76
import dk.aau.cs.util.FormatException;
77
import dk.aau.cs.util.Require;
78
import dk.aau.cs.util.Tuple;
80
public class TapnLegacyXmlLoader {
82
private static final String ERROR_PARSING_QUERY_MESSAGE = "TAPAAL encountered an error trying to parse one or more of the queries in the model.\n\nThe queries that could not be parsed will not show up in the query list.";
83
private HashMap<TimedTransitionComponent, TransportArcComponent> presetArcs;
84
private HashMap<TimedTransitionComponent, TransportArcComponent> postsetArcs;
85
private HashMap<TransportArcComponent, TimeInterval> transportArcsTimeIntervals;
86
private TimedArcPetriNet tapn;
87
private DataLayer guiModel;
88
private ArrayList<TAPNQuery> queries;
89
private TreeMap<String, Constant> constants;
90
private DrawingSurfaceImpl drawingSurface;
91
private NameGenerator nameGenerator = new NameGenerator();
92
private boolean firstQueryParsingWarning = true;
93
private boolean firstInhibitorIntervalWarning = true;
95
public TapnLegacyXmlLoader(DrawingSurfaceImpl drawingSurfaceImpl) {
96
presetArcs = new HashMap<TimedTransitionComponent, TransportArcComponent>();
97
postsetArcs = new HashMap<TimedTransitionComponent, TransportArcComponent>();
98
transportArcsTimeIntervals = new HashMap<TransportArcComponent, TimeInterval>();
99
queries = new ArrayList<TAPNQuery>();
100
constants = new TreeMap<String, Constant>();
101
this.drawingSurface = drawingSurfaceImpl;
104
public LoadedModel load(File file) throws FormatException {
105
Require.that(file != null && file.exists(), "file must be non-null and exist");
107
Document doc = loadDocument(file);
108
if(doc == null) return null;
112
private Document loadDocument(File file) {
114
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
115
return builder.parse(file);
116
} catch (ParserConfigurationException e) {
118
} catch (SAXException e) {
120
} catch (IOException e) {
125
private LoadedModel parse(Document tapnDoc) throws FormatException {
126
ArrayList<Template> templates = new ArrayList<Template>();
128
NodeList constantNodes = tapnDoc.getElementsByTagName("constant");
129
for (int i = 0; i < constantNodes.getLength(); i++) {
130
Node c = constantNodes.item(i);
132
if (c instanceof Element) {
133
parseAndAddConstant((Element) c);
137
TimedArcPetriNetNetwork network = new TimedArcPetriNetNetwork(new ConstantStore(constants.values()));
138
NodeList nets = tapnDoc.getElementsByTagName("net");
140
if(nets.getLength() <= 0)
141
throw new FormatException("File did not contain any TAPN components.");
143
templates.add(parseTimedArcPetriNetAsOldFormat(nets.item(0), network));
145
checkThatQueriesUseExistingPlaces(network);
147
return new LoadedModel(network, templates, queries);
150
private void checkThatQueriesUseExistingPlaces(TimedArcPetriNetNetwork network) {
151
ArrayList<TAPNQuery> okQueries = new ArrayList<TAPNQuery>();
152
ArrayList<Tuple<String,String>> templatePlaceNames = getTemplatePlaceNames(network);
153
for(TAPNQuery query : queries) {
154
if(!doesPlacesUsedInQueryExist(query, templatePlaceNames)) {
155
if(firstQueryParsingWarning) {
156
JOptionPane.showMessageDialog(CreateGui.getApp(), ERROR_PARSING_QUERY_MESSAGE, "Error Parsing Query", JOptionPane.ERROR_MESSAGE);
157
firstQueryParsingWarning = false;
162
okQueries.add(query);
168
private ArrayList<Tuple<String, String>> getTemplatePlaceNames(TimedArcPetriNetNetwork network) {
169
ArrayList<Tuple<String,String>> templatePlaceNames = new ArrayList<Tuple<String,String>>();
170
for(TimedArcPetriNet tapn : network.templates()) {
171
for(TimedPlace p : tapn.places()) {
172
templatePlaceNames.add(new Tuple<String, String>(tapn.name(), p.name()));
176
for(TimedPlace p : network.sharedPlaces()) {
177
templatePlaceNames.add(new Tuple<String, String>("", p.name()));
179
return templatePlaceNames;
182
private Arc parseAndAddTimedOutputArc(String idInput, boolean taggedArc,
183
String inscriptionTempStorage, PlaceTransitionObject sourceIn,
184
PlaceTransitionObject targetIn, double _startx, double _starty,
185
double _endx, double _endy) throws FormatException {
188
tempArc = new TimedOutputArcComponent(_startx, _starty, _endx, _endy,
189
sourceIn, targetIn, Integer.valueOf(inscriptionTempStorage), idInput, taggedArc);
191
TimedPlace place = tapn.getPlaceByName(targetIn.getName());
192
TimedTransition transition = tapn.getTransitionByName(sourceIn.getName());
194
TimedOutputArc outputArc = new TimedOutputArc(transition, place);
195
((TimedOutputArcComponent) tempArc).setUnderlyingArc(outputArc);
197
if(tapn.hasArcFromTransitionToPlace(outputArc.source(),outputArc.destination())) {
198
throw new FormatException("Multiple arcs between a place and a transition is not allowed");
201
guiModel.addPetriNetObject(tempArc);
202
addListeners(tempArc);
205
sourceIn.addConnectFrom(tempArc);
206
targetIn.addConnectTo(tempArc);
210
private Arc parseAndAddTransportArc(String idInput, boolean taggedArc,
211
String inscriptionTempStorage, PlaceTransitionObject sourceIn,
212
PlaceTransitionObject targetIn, double _startx, double _starty,
213
double _endx, double _endy) {
216
String[] inscriptionSplit = {};
217
if (inscriptionTempStorage.contains(":")) {
218
inscriptionSplit = inscriptionTempStorage.split(":");
220
boolean isInPreSet = false;
221
if (sourceIn instanceof Place) {
224
tempArc = new TransportArcComponent(new TimedInputArcComponent(
225
new TimedOutputArcComponent(_startx, _starty, _endx, _endy, sourceIn, targetIn, 1, idInput, taggedArc),
226
inscriptionSplit[0]), Integer.parseInt(inscriptionSplit[1]), isInPreSet);
228
sourceIn.addConnectFrom(tempArc);
229
targetIn.addConnectTo(tempArc);
232
if (postsetArcs.containsKey((TimedTransitionComponent) targetIn)) {
233
TransportArcComponent postsetTransportArc = postsetArcs.get((TimedTransitionComponent) targetIn);
234
TimedPlace sourcePlace = tapn.getPlaceByName(sourceIn.getName());
235
TimedTransition trans = tapn.getTransitionByName(targetIn.getName());
236
TimedPlace destPlace = tapn.getPlaceByName(postsetTransportArc.getTarget().getName());
237
TimeInterval interval = TimeInterval.parse(inscriptionSplit[0], constants);
239
assert (sourcePlace != null);
240
assert (trans != null);
241
assert (destPlace != null);
243
TransportArc transArc = new TransportArc(sourcePlace, trans, destPlace, interval);
245
((TransportArcComponent) tempArc).setUnderlyingArc(transArc);
246
postsetTransportArc.setUnderlyingArc(transArc);
247
guiModel.addPetriNetObject(tempArc);
248
addListeners(tempArc);
249
guiModel.addPetriNetObject(postsetTransportArc);
250
addListeners(postsetTransportArc);
253
postsetArcs.remove((TimedTransitionComponent) targetIn);
255
presetArcs.put((TimedTransitionComponent) targetIn, (TransportArcComponent) tempArc);
256
transportArcsTimeIntervals.put((TransportArcComponent) tempArc, TimeInterval.parse(inscriptionSplit[0], constants));
259
if (presetArcs.containsKey((TimedTransitionComponent) sourceIn)) {
260
TransportArcComponent presetTransportArc = presetArcs.get((TimedTransitionComponent) sourceIn);
261
TimedPlace sourcePlace = tapn.getPlaceByName(presetTransportArc.getSource().getName());
262
TimedTransition trans = tapn.getTransitionByName(sourceIn.getName());
263
TimedPlace destPlace = tapn.getPlaceByName(targetIn.getName());
264
TimeInterval interval = transportArcsTimeIntervals.get((TransportArcComponent) presetTransportArc);
266
assert (sourcePlace != null);
267
assert (trans != null);
268
assert (destPlace != null);
270
TransportArc transArc = new TransportArc(sourcePlace, trans,
271
destPlace, interval);
273
((TransportArcComponent) tempArc).setUnderlyingArc(transArc);
274
presetTransportArc.setUnderlyingArc(transArc);
275
guiModel.addPetriNetObject(presetTransportArc);
276
addListeners(presetTransportArc);
277
guiModel.addPetriNetObject(tempArc);
278
addListeners(tempArc);
281
presetArcs.remove((TimedTransitionComponent) sourceIn);
282
transportArcsTimeIntervals.remove((TransportArcComponent) presetTransportArc);
284
postsetArcs.put((TimedTransitionComponent) sourceIn, (TransportArcComponent) tempArc);
290
private Arc parseAndAddTimedInputArc(String idInput, boolean taggedArc,
291
String inscriptionTempStorage, PlaceTransitionObject sourceIn,
292
PlaceTransitionObject targetIn, double _startx, double _starty,
293
double _endx, double _endy) throws FormatException {
295
tempArc = new TimedInputArcComponent(new TimedOutputArcComponent(
296
_startx, _starty, _endx, _endy, sourceIn, targetIn, 1, idInput,
298
(inscriptionTempStorage != null ? inscriptionTempStorage : ""));
300
TimedPlace place = tapn.getPlaceByName(sourceIn.getName());
301
TimedTransition transition = tapn.getTransitionByName(targetIn.getName());
302
TimeInterval interval = TimeInterval.parse(inscriptionTempStorage, constants);
304
TimedInputArc inputArc = new TimedInputArc(place, transition, interval);
305
((TimedInputArcComponent) tempArc).setUnderlyingArc(inputArc);
307
if(tapn.hasArcFromPlaceToTransition(inputArc.source(), inputArc.destination())) {
308
throw new FormatException("Multiple arcs between a place and a transition is not allowed");
311
guiModel.addPetriNetObject(tempArc);
312
addListeners(tempArc);
315
sourceIn.addConnectFrom(tempArc);
316
targetIn.addConnectTo(tempArc);
320
private Arc parseAndAddTimedInhibitorArc(String idInput, boolean taggedArc,
321
String inscriptionTempStorage, PlaceTransitionObject sourceIn,
322
PlaceTransitionObject targetIn, double _startx, double _starty,
323
double _endx, double _endy) {
325
tempArc = new TimedInhibitorArcComponent(
326
new TimedInputArcComponent(
327
new TimedOutputArcComponent(_startx, _starty, _endx, _endy, sourceIn, targetIn, 1, idInput, taggedArc)
329
(inscriptionTempStorage != null ? inscriptionTempStorage : ""));
330
TimedPlace place = tapn.getPlaceByName(sourceIn.getName());
331
TimedTransition transition = tapn.getTransitionByName(targetIn.getName());
332
TimeInterval interval = TimeInterval.parse(inscriptionTempStorage, constants);
334
if(!interval.equals(TimeInterval.ZERO_INF) && firstInhibitorIntervalWarning) {
335
JOptionPane.showMessageDialog(CreateGui.getApp(), "The chosen model contained inhibitor arcs with unsupported intervals.\n\nTAPAAL only supports inhibitor arcs with intervals [0,inf).\n\nAny other interval on inhibitor arcs will be replaced with [0,inf).", "Unsupported Interval Detected on Inhibitor Arc", JOptionPane.INFORMATION_MESSAGE);
336
firstInhibitorIntervalWarning = false;
339
TimedInhibitorArc inhibArc = new TimedInhibitorArc(place, transition, interval);
341
((TimedInhibitorArcComponent) tempArc).setUnderlyingArc(inhibArc);
342
guiModel.addPetriNetObject(tempArc);
343
addListeners(tempArc);
346
sourceIn.addConnectFrom(tempArc);
347
targetIn.addConnectTo(tempArc);
351
private ReductionOption getQueryReductionOption(Element queryElement) {
352
ReductionOption reductionOption;
354
reductionOption = ReductionOption.valueOf(queryElement.getAttribute("reductionOption"));
355
} catch (Exception e) {
356
reductionOption = ReductionOption.STANDARD;
358
return reductionOption;
361
private ExtrapolationOption getQueryExtrapolationOption(Element queryElement) {
362
ExtrapolationOption extrapolationOption;
364
extrapolationOption = ExtrapolationOption.valueOf(queryElement.getAttribute("extrapolationOption"));
365
} catch (Exception e) {
366
extrapolationOption = ExtrapolationOption.AUTOMATIC;
368
return extrapolationOption;
371
private HashTableSize getQueryHashTableSize(Element queryElement) {
372
HashTableSize hashTableSize;
374
hashTableSize = HashTableSize.valueOf(queryElement.getAttribute("hashTableSize"));
375
} catch (Exception e) {
376
hashTableSize = HashTableSize.MB_16;
378
return hashTableSize;
381
private SearchOption getQuerySearchOption(Element queryElement) {
382
SearchOption searchOption;
384
searchOption = SearchOption.valueOf(queryElement.getAttribute("searchOption"));
385
} catch (Exception e) {
386
searchOption = SearchOption.BFS;
391
private TraceOption getQueryTraceOption(Element queryElement) {
392
TraceOption traceOption;
394
traceOption = TraceOption.valueOf(queryElement.getAttribute("traceOption"));
395
} catch (Exception e) {
396
traceOption = TraceOption.NONE;
401
private String getQueryComment(Element queryElement) {
404
comment = queryElement.getAttribute("name");
405
} catch (Exception e) {
406
comment = "No comment specified";
411
private void parseAndAddConstant(Element constantElement) {
412
String name = constantElement.getAttribute("name");
413
int value = Integer.parseInt(constantElement.getAttribute("value"));
415
if (!name.isEmpty() && !name.equals(""))
416
constants.put(name, new Constant(name, value));
419
// //////////////////////////////////////////////////////////
420
// Legacy support for old format
421
// //////////////////////////////////////////////////////////
422
private Template parseTimedArcPetriNetAsOldFormat(Node tapnNode, TimedArcPetriNetNetwork network) throws FormatException {
423
tapn = new TimedArcPetriNet(nameGenerator .getNewTemplateName());
426
guiModel = new DataLayer();
429
NodeList nodeList = null;
431
nodeList = tapnNode.getChildNodes();
432
for (int i = 0; i < nodeList.getLength(); i++) {
433
node = nodeList.item(i);
434
parseElementAsOldFormat(node, tapn.name(), network.marking());
437
return new Template(tapn, guiModel);
440
private void parseElementAsOldFormat(Node node, String templateName, TimedMarking marking) throws FormatException {
442
if (node instanceof Element) {
443
element = (Element) node;
444
if ("labels".equals(element.getNodeName())) {
445
parseAndAddAnnotationAsOldFormat(element);
446
} else if ("place".equals(element.getNodeName())) {
447
parseAndAddPlaceAsOldFormat(element, marking);
448
} else if ("transition".equals(element.getNodeName())) {
449
parseAndAddTransitionAsOldFormat(element);
450
} else if ("arc".equals(element.getNodeName())) {
451
parseAndAddArcAsOldFormat(element);
452
} else if ("queries".equals(element.getNodeName())) {
453
TAPNQuery query = parseQueryAsOldFormat(element);
457
query.getProperty().accept(new AddTemplateVisitor(templateName), null);
464
private boolean doesPlacesUsedInQueryExist(TAPNQuery query, ArrayList<Tuple<String, String>> templatePlaceNames) {
465
VerifyPlaceNamesVisitor nameChecker = new VerifyPlaceNamesVisitor(templatePlaceNames);
467
VerifyPlaceNamesVisitor.Context c = nameChecker.VerifyPlaceNames(query.getProperty());
469
return c.getResult();
472
private void parseAndAddAnnotationAsOldFormat(Element inputLabelElement) throws FormatException {
473
int positionXInput = 0;
474
int positionYInput = 0;
477
boolean borderInput = true;
479
String positionXTempStorage = inputLabelElement.getAttribute("x");
480
String positionYTempStorage = inputLabelElement.getAttribute("y");
481
String widthTemp = inputLabelElement.getAttribute("width");
482
String heightTemp = inputLabelElement.getAttribute("height");
483
String borderTemp = inputLabelElement.getAttribute("border");
485
String text = getFirstChildNodeByName(inputLabelElement, "text").getTextContent();
487
if (positionXTempStorage.length() > 0) {
488
positionXInput = Integer.valueOf(positionXTempStorage).intValue() + 1;
491
if (positionYTempStorage.length() > 0) {
492
positionYInput = Integer.valueOf(positionYTempStorage).intValue() + 1;
495
if (widthTemp.length() > 0) {
496
widthInput = Integer.valueOf(widthTemp).intValue() + 1;
499
if (heightTemp.length() > 0) {
500
heightInput = Integer.valueOf(heightTemp).intValue() + 1;
503
if (borderTemp.length() > 0) {
504
borderInput = Boolean.valueOf(borderTemp).booleanValue();
508
AnnotationNote an = new AnnotationNote(text, positionXInput,
509
positionYInput, widthInput, heightInput, borderInput);
510
guiModel.addPetriNetObject(an);
514
private void parseAndAddTransitionAsOldFormat(Element element) throws FormatException {
515
double positionXInput = getPositionAttribute(element, "x");
516
double positionYInput = getPositionAttribute(element, "y");
517
String idInput = element.getAttribute("id");
518
String nameInput = getChildNodesContentOfValueChildNodeAsString(element, "name");
519
double nameOffsetXInput = getNameOffsetAttribute(element, "x");
520
double nameOffsetYInput = getNameOffsetAttribute(element, "y");
521
boolean timedTransition = getContentOfFirstSpecificChildNodesValueChildNodeAsBoolean(element, "timed");
522
boolean infiniteServer = getContentOfFirstSpecificChildNodesValueChildNodeAsBoolean(element, "infiniteServer");
523
int angle = getContentOfFirstSpecificChildNodesValueChildNodeAsInt(element,"orientation");
524
int priority = getContentOfFirstSpecificChildNodesValueChildNodeAsInt(element,"priority");
526
positionXInput = Grid.getModifiedX(positionXInput);
527
positionYInput = Grid.getModifiedY(positionYInput);
529
if (idInput.length() == 0 && nameInput.length() > 0) {
533
if (nameInput.length() == 0 && idInput.length() > 0) {
537
TimedTransition t = new TimedTransition(nameInput);
539
TimedTransitionComponent transition = new TimedTransitionComponent(
540
positionXInput, positionYInput, idInput, nameInput,
541
nameOffsetXInput, nameOffsetYInput, timedTransition,
542
infiniteServer, angle, priority);
543
transition.setUnderlyingTransition(t);
544
transition.setTimed(true);
545
guiModel.addPetriNetObject(transition);
546
addListeners(transition);
550
private void parseAndAddPlaceAsOldFormat(Element element, TimedMarking marking) throws FormatException {
551
double positionXInput = getPositionAttribute(element, "x");
552
double positionYInput = getPositionAttribute(element, "y");
553
String idInput = element.getAttribute("id");
554
String nameInput = getChildNodesContentOfValueChildNodeAsString(element, "name");
555
double nameOffsetXInput = getNameOffsetAttribute(element, "x");
556
double nameOffsetYInput = getNameOffsetAttribute(element, "y");
557
int initialMarkingInput = getContentOfFirstSpecificChildNodesValueChildNodeAsInt(element, "initialMarking");
558
double markingOffsetXInput = getMarkingOffsetAttribute(element, "x");
559
double markingOffsetYInput = getMarkingOffsetAttribute(element, "y");
560
int capacityInput = getContentOfFirstSpecificChildNodesValueChildNodeAsInt(element, "capacity");
561
String invariant = getChildNodesContentOfValueChildNodeAsString(element, "invariant");
563
positionXInput = Grid.getModifiedX(positionXInput);
564
positionYInput = Grid.getModifiedY(positionYInput);
566
if (idInput.length() == 0 && nameInput.length() > 0) {
570
if (nameInput.length() == 0 && idInput.length() > 0) {
576
if (invariant == null || invariant == "") {
577
place = new Place(positionXInput, positionYInput, idInput,
578
nameInput, nameOffsetXInput, nameOffsetYInput,
579
initialMarkingInput, markingOffsetXInput,
580
markingOffsetYInput, capacityInput);
583
place = new TimedPlaceComponent(positionXInput, positionYInput,
584
idInput, nameInput, nameOffsetXInput, nameOffsetYInput,
585
initialMarkingInput, markingOffsetXInput,
586
markingOffsetYInput, capacityInput);
588
LocalTimedPlace p = new LocalTimedPlace(nameInput, TimeInvariant.parse(invariant, constants));
591
((TimedPlaceComponent) place).setUnderlyingPlace(p);
592
guiModel.addPetriNetObject(place);
595
for (int i = 0; i < initialMarkingInput; i++) {
596
marking.add(new TimedToken(p, new BigDecimal(0.0)));
601
private void parseAndAddArcAsOldFormat(Element inputArcElement) throws FormatException {
602
String idInput = inputArcElement.getAttribute("id");
603
String sourceInput = inputArcElement.getAttribute("source");
604
String targetInput = inputArcElement.getAttribute("target");
605
boolean taggedArc = getContentOfFirstSpecificChildNodesValueChildNodeAsBoolean(inputArcElement, "tagged");
606
String inscriptionTempStorage = getChildNodesContentOfValueChildNodeAsString(inputArcElement, "inscription");
608
PlaceTransitionObject sourceIn = guiModel.getPlaceTransitionObject(sourceInput);
609
PlaceTransitionObject targetIn = guiModel.getPlaceTransitionObject(targetInput);
611
// add the insets and offset
612
int aStartx = sourceIn.getX() + sourceIn.centreOffsetLeft();
613
int aStarty = sourceIn.getY() + sourceIn.centreOffsetTop();
615
int aEndx = targetIn.getX() + targetIn.centreOffsetLeft();
616
int aEndy = targetIn.getY() + targetIn.centreOffsetTop();
618
double _startx = aStartx;
619
double _starty = aStarty;
620
double _endx = aEndx;
621
double _endy = aEndy;
625
String type = "normal";
626
type = ((Element) getFirstChildNodeByName(inputArcElement, "type")).getAttribute("value");
628
if (type.equals("tapnInhibitor")) {
630
tempArc = parseAndAddTimedInhibitorArc(idInput, taggedArc,
631
inscriptionTempStorage, sourceIn, targetIn, _startx,
632
_starty, _endx, _endy);
635
if (type.equals("timed")) {
636
tempArc = parseAndAddTimedInputArc(idInput, taggedArc,
637
inscriptionTempStorage, sourceIn, targetIn, _startx,
638
_starty, _endx, _endy);
640
} else if (type.equals("transport")) {
641
tempArc = parseAndAddTransportArc(idInput, taggedArc,
642
inscriptionTempStorage, sourceIn, targetIn, _startx,
643
_starty, _endx, _endy);
646
tempArc = parseAndAddTimedOutputArc(idInput, taggedArc,
647
inscriptionTempStorage, sourceIn, targetIn, _startx,
648
_starty, _endx, _endy);
653
parseArcPathAsOldFormat(inputArcElement, tempArc);
656
private void parseArcPathAsOldFormat(Element inputArcElement, Arc tempArc) {
657
NodeList nodelist = inputArcElement.getElementsByTagName("arcpath");
658
if (nodelist.getLength() > 0) {
659
tempArc.getArcPath().purgePathPoints();
660
for (int i = 0; i < nodelist.getLength(); i++) {
661
Node node = nodelist.item(i);
662
if (node instanceof Element) {
663
Element element = (Element) node;
664
if ("arcpath".equals(element.getNodeName())) {
665
String arcTempX = element.getAttribute("x");
666
String arcTempY = element.getAttribute("y");
667
String arcTempType = element.getAttribute("curvePoint");
668
float arcPointX = Float.valueOf(arcTempX).floatValue();
669
float arcPointY = Float.valueOf(arcTempY).floatValue();
670
arcPointX += Pipe.ARC_CONTROL_POINT_CONSTANT + 1;
671
arcPointY += Pipe.ARC_CONTROL_POINT_CONSTANT + 1;
672
boolean arcPointType = Boolean.valueOf(arcTempType).booleanValue();
673
tempArc.getArcPath().addPoint(arcPointX, arcPointY, arcPointType);
680
private TAPNQuery parseQueryAsOldFormat(Element queryElement) throws FormatException {
681
String comment = getQueryComment(queryElement);
682
TraceOption traceOption = getQueryTraceOption(queryElement);
683
SearchOption searchOption = getQuerySearchOption(queryElement);
684
HashTableSize hashTableSize = getQueryHashTableSize(queryElement);
685
ExtrapolationOption extrapolationOption = getQueryExtrapolationOption(queryElement);
686
ReductionOption reductionOption = getQueryReductionOption(queryElement);
687
int capacity = getQueryCapacityAsOldFormat(queryElement);
689
TCTLAbstractProperty query;
690
query = parseQueryPropertyAsOldFormat(queryElement);
693
return new TAPNQuery(comment, capacity, query, traceOption,
694
searchOption, reductionOption, hashTableSize,
695
extrapolationOption);
700
private TCTLAbstractProperty parseQueryPropertyAsOldFormat(Element queryElement) throws FormatException {
701
TCTLAbstractProperty query = null;
702
TAPAALQueryParser queryParser = new TAPAALQueryParser();
704
String queryToParse = getChildNodesContentOfValueChildNodeAsString(queryElement, "query");
707
query = queryParser.parse(queryToParse);
708
} catch (Exception e) {
709
if(firstQueryParsingWarning ) {
710
JOptionPane.showMessageDialog(CreateGui.getApp(), ERROR_PARSING_QUERY_MESSAGE, "Error Parsing Query", JOptionPane.ERROR_MESSAGE);
711
firstQueryParsingWarning = false;
713
System.err.println("No query was specified: " + e.getStackTrace());
718
private int getQueryCapacityAsOldFormat(Element queryElement) throws FormatException {
719
return getContentOfFirstSpecificChildNodesValueChildNodeAsInt(queryElement, "capacity");
722
private boolean getContentOfFirstSpecificChildNodesValueChildNodeAsBoolean(Element element, String childNodeName) throws FormatException {
723
Node node = getFirstChildNodeByName(element, childNodeName);
725
if (node instanceof Element) {
726
Element e = (Element) node;
728
String value = getContentOfValueChildNode(e);
730
return Boolean.parseBoolean(value);
736
private double getNameOffsetAttribute(Element element, String coordinateName) throws FormatException {
737
Node node = getFirstChildNodeByName(element, "name");
739
if (node instanceof Element) {
740
Element e = (Element) node;
742
Element graphics = ((Element) getFirstChildNodeByName(e, "graphics"));
743
String offsetCoordinate = ((Element) getFirstChildNodeByName(graphics, "offset")).getAttribute(coordinateName);
744
if (offsetCoordinate.length() > 0) {
745
return Double.valueOf(offsetCoordinate).doubleValue();
752
private Node getFirstChildNodeByName(Element element, String childNodeName) throws FormatException {
753
Node node = element.getElementsByTagName(childNodeName).item(0);
756
throw new FormatException("TAPAAL could not recognize save format.");
761
private String getContentOfValueChildNode(Element element) throws FormatException {
762
return ((Element) getFirstChildNodeByName(element, "value")).getTextContent();
765
private String getChildNodesContentOfValueChildNodeAsString(Element element, String childNodeName) throws FormatException {
766
Node node = getFirstChildNodeByName(element, childNodeName);
768
if (node instanceof Element) {
769
Element e = (Element) node;
771
return getContentOfValueChildNode(e);
777
private double getPositionAttribute(Element element, String coordinateName) throws FormatException {
778
Node node = getFirstChildNodeByName(element, "graphics");
780
if (node instanceof Element) {
781
Element e = (Element) node;
783
String posCoordinate = ((Element) getFirstChildNodeByName(e, "position")).getAttribute(coordinateName);
784
if (posCoordinate.length() > 0) {
785
return Double.valueOf(posCoordinate).doubleValue();
792
private int getContentOfFirstSpecificChildNodesValueChildNodeAsInt(Element element, String childNodeName) throws FormatException {
793
Node node = getFirstChildNodeByName(element, childNodeName);
795
if (node instanceof Element) {
796
Element e = (Element) node;
798
String value = getContentOfValueChildNode(e);
800
if (value.length() > 0)
801
return Integer.parseInt(value);
807
private double getMarkingOffsetAttribute(Element element, String coordinateName) throws FormatException {
808
Node node = getFirstChildNodeByName(element, "initialMarking");
810
if (node instanceof Element) {
811
Element e = (Element) node;
813
Element graphics = ((Element) getFirstChildNodeByName(e, "graphics"));
814
String offsetCoordinate = ((Element) getFirstChildNodeByName(graphics, "offset")).getAttribute(coordinateName);
815
if (offsetCoordinate.length() > 0)
816
return Double.parseDouble(offsetCoordinate);
822
private void addListeners(PetriNetObject newObject) {
823
if (newObject != null) {
824
if (newObject.getMouseListeners().length == 0) {
825
if (newObject instanceof Place) {
827
if (newObject instanceof TimedPlaceComponent) {
829
LabelHandler labelHandler = new LabelHandler(((Place) newObject).getNameLabel(), (Place) newObject);
830
((Place) newObject).getNameLabel().addMouseListener(labelHandler);
831
((Place) newObject).getNameLabel().addMouseMotionListener(labelHandler);
832
((Place) newObject).getNameLabel().addMouseWheelListener(labelHandler);
834
PlaceHandler placeHandler = new PlaceHandler(drawingSurface, (Place) newObject, guiModel, tapn);
835
newObject.addMouseListener(placeHandler);
836
newObject.addMouseWheelListener(placeHandler);
837
newObject.addMouseMotionListener(placeHandler);
840
LabelHandler labelHandler = new LabelHandler(((Place) newObject).getNameLabel(), (Place) newObject);
841
((Place) newObject).getNameLabel().addMouseListener(labelHandler);
842
((Place) newObject).getNameLabel().addMouseMotionListener(labelHandler);
843
((Place) newObject).getNameLabel().addMouseWheelListener(labelHandler);
845
PlaceHandler placeHandler = new PlaceHandler(drawingSurface, (Place) newObject);
846
newObject.addMouseListener(placeHandler);
847
newObject.addMouseWheelListener(placeHandler);
848
newObject.addMouseMotionListener(placeHandler);
851
} else if (newObject instanceof Transition) {
852
TransitionHandler transitionHandler;
853
if (newObject instanceof TimedTransitionComponent) {
854
transitionHandler = new TAPNTransitionHandler(drawingSurface, (Transition) newObject, guiModel, tapn);
856
transitionHandler = new TransitionHandler(drawingSurface, (Transition) newObject);
859
LabelHandler labelHandler = new LabelHandler(((Transition) newObject).getNameLabel(), (Transition) newObject);
860
((Transition) newObject).getNameLabel().addMouseListener(labelHandler);
861
((Transition) newObject).getNameLabel().addMouseMotionListener(labelHandler);
862
((Transition) newObject).getNameLabel().addMouseWheelListener(labelHandler);
864
newObject.addMouseListener(transitionHandler);
865
newObject.addMouseMotionListener(transitionHandler);
866
newObject.addMouseWheelListener(transitionHandler);
868
newObject.addMouseListener(new AnimationHandler());
870
} else if (newObject instanceof Arc) {
871
/* CB - Joakim Byg add timed arcs */
872
if (newObject instanceof TimedInputArcComponent) {
873
if (newObject instanceof TransportArcComponent) {
874
TransportArcHandler transportArcHandler = new TransportArcHandler(drawingSurface, (Arc) newObject);
875
newObject.addMouseListener(transportArcHandler);
876
newObject.addMouseWheelListener(transportArcHandler);
877
newObject.addMouseMotionListener(transportArcHandler);
879
TimedArcHandler timedArcHandler = new TimedArcHandler(drawingSurface, (Arc) newObject);
880
newObject.addMouseListener(timedArcHandler);
881
newObject.addMouseWheelListener(timedArcHandler);
882
newObject.addMouseMotionListener(timedArcHandler);
886
ArcHandler arcHandler = new ArcHandler(drawingSurface,(Arc) newObject);
887
newObject.addMouseListener(arcHandler);
888
newObject.addMouseWheelListener(arcHandler);
889
newObject.addMouseMotionListener(arcHandler);
891
} else if (newObject instanceof AnnotationNote) {
892
AnnotationNoteHandler noteHandler = new AnnotationNoteHandler(drawingSurface, (AnnotationNote) newObject);
893
newObject.addMouseListener(noteHandler);
894
newObject.addMouseMotionListener(noteHandler);
895
((Note) newObject).getNote().addMouseListener(noteHandler);
896
((Note) newObject).getNote().addMouseMotionListener(noteHandler);
898
if (newObject instanceof Zoomable) {
899
newObject.zoomUpdate(drawingSurface.getZoom());
902
newObject.setGuiModel(guiModel);