~tapaal-contributor/tapaal/autodetect-lens-check2

« back to all changes in this revision

Viewing changes to src/dk/aau/cs/io/batchProcessing/BatchProcessingLoader.java

merged in lp:~yrke/tapaal/removeSpecialBatchLoading removing special loading of nets in batch processing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package dk.aau.cs.io.batchProcessing;
2
 
 
3
 
import java.io.File;
4
 
import java.io.IOException;
5
 
import java.util.ArrayList;
6
 
import java.util.Collection;
7
 
import java.util.HashMap;
8
 
import java.util.List;
9
 
 
10
 
import javax.xml.parsers.DocumentBuilder;
11
 
import javax.xml.parsers.DocumentBuilderFactory;
12
 
import javax.xml.parsers.ParserConfigurationException;
13
 
 
14
 
import org.w3c.dom.Document;
15
 
import org.w3c.dom.Element;
16
 
import org.w3c.dom.Node;
17
 
import org.w3c.dom.NodeList;
18
 
import org.xml.sax.SAXException;
19
 
 
20
 
import pipe.dataLayer.TAPNQuery;
21
 
import pipe.dataLayer.TAPNQuery.AlgorithmOption;
22
 
import pipe.dataLayer.TAPNQuery.ExtrapolationOption;
23
 
import pipe.dataLayer.TAPNQuery.HashTableSize;
24
 
import pipe.dataLayer.TAPNQuery.QueryCategory;
25
 
import pipe.dataLayer.TAPNQuery.SearchOption;
26
 
import pipe.dataLayer.TAPNQuery.TraceOption;
27
 
import pipe.gui.widgets.InclusionPlaces;
28
 
import pipe.gui.widgets.InclusionPlaces.InclusionPlacesOption;
29
 
import dk.aau.cs.TCTL.TCTLAbstractProperty;
30
 
import dk.aau.cs.TCTL.Parsing.TAPAALQueryParser;
31
 
import dk.aau.cs.TCTL.XMLParsing.XMLCTLQueryParser;
32
 
import dk.aau.cs.TCTL.XMLParsing.XMLQueryParseException;
33
 
import dk.aau.cs.TCTL.visitors.VerifyPlaceNamesVisitor;
34
 
import dk.aau.cs.gui.NameGenerator;
35
 
import dk.aau.cs.io.IdResolver;
36
 
import dk.aau.cs.io.queries.TAPNQueryLoader;
37
 
import dk.aau.cs.model.tapn.Constant;
38
 
import dk.aau.cs.model.tapn.ConstantStore;
39
 
import dk.aau.cs.model.tapn.IntWeight;
40
 
import dk.aau.cs.model.tapn.LocalTimedPlace;
41
 
import dk.aau.cs.model.tapn.SharedPlace;
42
 
import dk.aau.cs.model.tapn.SharedTransition;
43
 
import dk.aau.cs.model.tapn.TimeInterval;
44
 
import dk.aau.cs.model.tapn.TimeInvariant;
45
 
import dk.aau.cs.model.tapn.TimedArcPetriNet;
46
 
import dk.aau.cs.model.tapn.TimedArcPetriNetNetwork;
47
 
import dk.aau.cs.model.tapn.TimedInhibitorArc;
48
 
import dk.aau.cs.model.tapn.TimedInputArc;
49
 
import dk.aau.cs.model.tapn.TimedMarking;
50
 
import dk.aau.cs.model.tapn.TimedOutputArc;
51
 
import dk.aau.cs.model.tapn.TimedPlace;
52
 
import dk.aau.cs.model.tapn.TimedToken;
53
 
import dk.aau.cs.model.tapn.TimedTransition;
54
 
import dk.aau.cs.model.tapn.TransportArc;
55
 
import dk.aau.cs.model.tapn.Weight;
56
 
import dk.aau.cs.translations.ReductionOption;
57
 
import dk.aau.cs.util.FormatException;
58
 
import dk.aau.cs.util.Require;
59
 
import dk.aau.cs.util.Tuple;
60
 
 
61
 
public class BatchProcessingLoader {
62
 
        private HashMap<Tuple<TimedTransition, Integer>, TimedPlace> presetArcs;
63
 
        private HashMap<Tuple<TimedTransition, Integer>, TimedPlace> postsetArcs;
64
 
        private HashMap<String, String> placeIDToName;
65
 
        private HashMap<String, String> transitionIDToName;
66
 
        private HashMap<Tuple<TimedTransition, Integer>, TimeInterval> transportArcsTimeIntervals;
67
 
 
68
 
        private IdResolver idResolver = new IdResolver();
69
 
        
70
 
        private NameGenerator nameGenerator = new NameGenerator();
71
 
        
72
 
        public BatchProcessingLoader() {
73
 
                presetArcs = new HashMap<Tuple<TimedTransition,Integer>, TimedPlace>();
74
 
                postsetArcs = new HashMap<Tuple<TimedTransition,Integer>, TimedPlace>();
75
 
                placeIDToName = new HashMap<String, String>();
76
 
                transitionIDToName = new HashMap<String, String>();
77
 
                transportArcsTimeIntervals = new HashMap<Tuple<TimedTransition,Integer>, TimeInterval>();
78
 
        }
79
 
 
80
 
        public LoadedBatchProcessingModel load(File file) throws FormatException {
81
 
                Require.that(file != null && file.exists(), "file must be non-null and exist");
82
 
 
83
 
                Document doc = loadDocument(file);
84
 
                if(doc == null) return null;
85
 
                return parse(doc);
86
 
        }
87
 
 
88
 
        private Document loadDocument(File file) {
89
 
                try {
90
 
                        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
91
 
                        return builder.parse(file);
92
 
                } catch (ParserConfigurationException | IOException | SAXException e) {
93
 
                        return null;
94
 
                }
95
 
        }
96
 
 
97
 
        private LoadedBatchProcessingModel parse(Document doc) throws FormatException {
98
 
                ConstantStore constants = new ConstantStore(parseConstants(doc));
99
 
 
100
 
                TimedArcPetriNetNetwork network = new TimedArcPetriNetNetwork(constants);
101
 
 
102
 
                parseSharedPlaces(doc, network, constants);
103
 
                parseSharedTransitions(doc, network);
104
 
                
105
 
                parseTemplates(doc, network, constants);
106
 
                Collection<TAPNQuery> queries = parseQueries(doc, network);
107
 
 
108
 
                network.buildConstraints();
109
 
                return new LoadedBatchProcessingModel(network, queries);
110
 
        }
111
 
 
112
 
 
113
 
        private void parseSharedPlaces(Document doc, TimedArcPetriNetNetwork network, ConstantStore constants) {
114
 
                NodeList sharedPlaceNodes = doc.getElementsByTagName("shared-place");
115
 
 
116
 
                for(int i = 0; i < sharedPlaceNodes.getLength(); i++){
117
 
                        Node node = sharedPlaceNodes.item(i);
118
 
 
119
 
                        if(node instanceof Element){
120
 
                                SharedPlace place = parseSharedPlace((Element)node, network.marking(), constants);
121
 
                                network.add(place);
122
 
                        }
123
 
                }
124
 
        }
125
 
 
126
 
        private SharedPlace parseSharedPlace(Element element, TimedMarking marking, ConstantStore constants) {
127
 
                String name = element.getAttribute("name");
128
 
                TimeInvariant invariant = TimeInvariant.parse(element.getAttribute("invariant"), constants);
129
 
                int numberOfTokens = Integer.parseInt(element.getAttribute("initialMarking"));
130
 
 
131
 
                if(name.toLowerCase().equals("true") || name.toLowerCase().equals("false")) {
132
 
                        name = "_" + name;
133
 
                }
134
 
                
135
 
                SharedPlace place = new SharedPlace(name, invariant);
136
 
                place.setCurrentMarking(marking);
137
 
                for(int j = 0; j < numberOfTokens; j++){
138
 
                        marking.add(new TimedToken(place));
139
 
                }
140
 
                return place;
141
 
        }
142
 
 
143
 
        private void parseSharedTransitions(Document doc, TimedArcPetriNetNetwork network) {
144
 
                NodeList sharedTransitionNodes = doc.getElementsByTagName("shared-transition");
145
 
 
146
 
                for(int i = 0; i < sharedTransitionNodes.getLength(); i++){
147
 
                        Node node = sharedTransitionNodes.item(i);
148
 
 
149
 
                        if(node instanceof Element){
150
 
                                SharedTransition transition = parseSharedTransition((Element)node);
151
 
                                network.add(transition);
152
 
                        }
153
 
                }
154
 
        }
155
 
 
156
 
        private SharedTransition parseSharedTransition(Element element) {
157
 
                String name = element.getAttribute("name");
158
 
                
159
 
                return new SharedTransition(name);
160
 
        }
161
 
 
162
 
        private Collection<TAPNQuery> parseQueries(Document doc, TimedArcPetriNetNetwork network) {
163
 
                Collection<TAPNQuery> queries = new ArrayList<TAPNQuery>();
164
 
                NodeList queryNodes = doc.getElementsByTagName("query");
165
 
                
166
 
                ArrayList<Tuple<String, String>> templatePlaceNames = getPlaceNames(network);
167
 
                for (int i = 0; i < queryNodes.getLength(); i++) {
168
 
                        Node q = queryNodes.item(i);
169
 
 
170
 
                        if (q instanceof Element) {
171
 
                                TAPNQuery query = parseTAPNQuery((Element) q, network);
172
 
                                
173
 
                                if (query != null) {
174
 
                                        if(!doesPlacesUsedInQueryExist(query, templatePlaceNames)) {
175
 
                                                continue;
176
 
                                        }
177
 
 
178
 
                                        queries.add(query);
179
 
                                }
180
 
                        }
181
 
                }
182
 
                
183
 
                return queries;
184
 
        }
185
 
 
186
 
        private boolean doesPlacesUsedInQueryExist(TAPNQuery query, ArrayList<Tuple<String, String>> templatePlaceNames) {
187
 
                VerifyPlaceNamesVisitor nameChecker = new VerifyPlaceNamesVisitor(templatePlaceNames);
188
 
 
189
 
                VerifyPlaceNamesVisitor.Context c = nameChecker.verifyPlaceNames(query.getProperty());
190
 
                
191
 
                return c.getResult();
192
 
                
193
 
        }
194
 
 
195
 
        private ArrayList<Tuple<String, String>> getPlaceNames(TimedArcPetriNetNetwork network) {
196
 
                ArrayList<Tuple<String,String>> templatePlaceNames = new ArrayList<Tuple<String,String>>();
197
 
                for(TimedArcPetriNet tapn : network.allTemplates()) {
198
 
                        for(TimedPlace p : tapn.places()) {
199
 
                                templatePlaceNames.add(new Tuple<String, String>(tapn.name(), p.name()));
200
 
                        }
201
 
                }
202
 
                
203
 
                for(TimedPlace p : network.sharedPlaces()) {
204
 
                        templatePlaceNames.add(new Tuple<String, String>("", p.name()));
205
 
                }
206
 
                return templatePlaceNames;
207
 
        }
208
 
 
209
 
        private void parseTemplates(Document doc, TimedArcPetriNetNetwork network, ConstantStore constants) throws FormatException {
210
 
                NodeList nets = doc.getElementsByTagName("net");
211
 
                
212
 
                if(nets.getLength() <= 0)
213
 
                        throw new FormatException("File did not contain any TAPN components.");
214
 
                
215
 
                for (int i = 0; i < nets.getLength(); i++) {
216
 
                        parseTimedArcPetriNet(nets.item(i), network, constants);
217
 
                }
218
 
        }
219
 
 
220
 
        private List<Constant> parseConstants(Document doc) {
221
 
                List<Constant> constants = new ArrayList<Constant>();
222
 
                NodeList constantNodes = doc.getElementsByTagName("constant");
223
 
                for (int i = 0; i < constantNodes.getLength(); i++) {
224
 
                        Node c = constantNodes.item(i);
225
 
 
226
 
                        if (c instanceof Element) {
227
 
                                Constant constant = parseAndAddConstant((Element) c);
228
 
                                constants.add(constant);
229
 
                        }
230
 
                }
231
 
                return constants;
232
 
        }
233
 
 
234
 
        private void parseTimedArcPetriNet(Node tapnNode, TimedArcPetriNetNetwork network, ConstantStore constants) throws FormatException {
235
 
        String name = getTAPNName(tapnNode);
236
 
                boolean active = getActiveStatus(tapnNode);
237
 
 
238
 
        TimedArcPetriNet tapn = new TimedArcPetriNet(name);
239
 
                tapn.setActive(active);
240
 
                network.add(tapn);
241
 
                nameGenerator.add(tapn);
242
 
 
243
 
                NodeList nodeList = tapnNode.getChildNodes();
244
 
                for (int i = 0; i < nodeList.getLength(); i++) {
245
 
                        Node node = nodeList.item(i);
246
 
                        if(node instanceof Element){
247
 
                                parseElement((Element)node, tapn, network, constants);
248
 
                        }
249
 
                }
250
 
        }
251
 
        
252
 
        private boolean getActiveStatus(Node tapnNode) {
253
 
                if (tapnNode instanceof Element) {
254
 
                        Element element = (Element)tapnNode;
255
 
                        String activeString = element.getAttribute("active");
256
 
                        
257
 
                        if (activeString == null || activeString.equals(""))
258
 
                                return true;
259
 
                        else
260
 
                                return activeString.equals("true");
261
 
                } else {
262
 
                        return true;
263
 
                }
264
 
        }
265
 
 
266
 
    private boolean getInfo(Node tapnNode, String attribute) {
267
 
        Element element = (Element)tapnNode;
268
 
        String string = element.getAttribute(attribute);
269
 
        return string.equals("true");
270
 
    }
271
 
 
272
 
    private boolean canGetInfo(Node tapnNode) {
273
 
        if (tapnNode instanceof Element) {
274
 
            Element element = (Element)tapnNode;
275
 
            String timedString = element.getAttribute("timed");
276
 
            String gameString = element.getAttribute("game");
277
 
 
278
 
            if (timedString == null || timedString.equals("") ||
279
 
                gameString == null || gameString.equals("")) {
280
 
                return false;
281
 
            } else {
282
 
                return true;
283
 
            }
284
 
        } else {
285
 
            return false;
286
 
        }
287
 
    }
288
 
 
289
 
        private void parseElement(Element element, TimedArcPetriNet tapn, TimedArcPetriNetNetwork network, ConstantStore constants) throws FormatException {
290
 
                if ("place".equals(element.getNodeName())) {
291
 
                        parsePlace(element, network, tapn, constants);
292
 
                } else if ("transition".equals(element.getNodeName())) {
293
 
                        parseTransition(element, network, tapn);
294
 
                } else if ("arc".equals(element.getNodeName())) {
295
 
                        parseAndAddArc(element, tapn, constants);
296
 
                }
297
 
        }
298
 
 
299
 
        private boolean isNameAllowed(String name) {
300
 
                Require.that(name != null, "name was null");
301
 
 
302
 
                return !name.isEmpty() && java.util.regex.Pattern.matches("[a-zA-Z]([_a-zA-Z0-9])*", name);
303
 
        }
304
 
 
305
 
 
306
 
        private String getTAPNName(Node tapnNode) {
307
 
                if (tapnNode instanceof Element) {
308
 
                        Element element = (Element)tapnNode;
309
 
                        String name = element.getAttribute("name");
310
 
 
311
 
                        if (name == null || name.equals(""))
312
 
                                name = element.getAttribute("id");
313
 
 
314
 
                        if(!isNameAllowed(name)){
315
 
                                name = nameGenerator.getNewTemplateName();
316
 
                        }
317
 
                        nameGenerator.updateTemplateIndex(name);
318
 
                        return name;
319
 
                } else {
320
 
                        return nameGenerator.getNewTemplateName();
321
 
                }
322
 
        }
323
 
 
324
 
        private void parseTransition(Element transition, TimedArcPetriNetNetwork network, TimedArcPetriNet tapn) {
325
 
                String idInput = transition.getAttribute("id");
326
 
                String nameInput = transition.getAttribute("name");
327
 
                boolean isUrgent = Boolean.parseBoolean(transition.getAttribute("urgent"));
328
 
                
329
 
                idResolver.add(tapn.name(), idInput, nameInput);
330
 
                
331
 
                if (idInput.length() == 0 && nameInput.length() > 0) {
332
 
                        idInput = nameInput;
333
 
                }
334
 
 
335
 
                if (nameInput.length() == 0 && idInput.length() > 0) {
336
 
                        nameInput = idInput;
337
 
                }
338
 
                
339
 
                TimedTransition t = new TimedTransition(nameInput);
340
 
                t.setUrgent(isUrgent);
341
 
                if(network.isNameUsedForShared(nameInput)){
342
 
                        t.setName(nameGenerator.getNewTransitionName(tapn)); // introduce temporary name to avoid exceptions
343
 
                        tapn.add(t);
344
 
                        if(!transitionIDToName.containsKey(idInput))
345
 
                                transitionIDToName.put(idInput, nameInput);
346
 
                        network.getSharedTransitionByName(nameInput).makeShared(t);
347
 
                }else{
348
 
                        tapn.add(t);
349
 
                        transitionIDToName.put(idInput, nameInput);
350
 
                }
351
 
                nameGenerator.updateIndicesForAllModels(nameInput);
352
 
        }
353
 
 
354
 
        private void parsePlace(Element place, TimedArcPetriNetNetwork network, TimedArcPetriNet tapn, ConstantStore constants) {
355
 
                String idInput = place.getAttribute("id");
356
 
                String nameInput = place.getAttribute("name");
357
 
                
358
 
                
359
 
                int initialMarkingInput = Integer.parseInt(place.getAttribute("initialMarking"));
360
 
                String invariant = place.getAttribute("invariant");
361
 
 
362
 
                if (idInput.length() == 0 && nameInput.length() > 0) {
363
 
                        idInput = nameInput;
364
 
                }
365
 
 
366
 
                if (nameInput.length() == 0 && idInput.length() > 0) {
367
 
                        nameInput = idInput;
368
 
                }
369
 
                
370
 
                if(nameInput.toLowerCase().equals("true") || nameInput.toLowerCase().equals("false")) {
371
 
                        nameInput = "_" + nameInput;
372
 
                }
373
 
 
374
 
                idResolver.add(tapn.name(), idInput, nameInput);
375
 
 
376
 
                TimedPlace p;
377
 
                if(network.isNameUsedForShared(nameInput)){
378
 
                        p = network.getSharedPlaceByName(nameInput);
379
 
                        if(!placeIDToName.containsKey(idInput))
380
 
                                placeIDToName.put(idInput, nameInput);
381
 
                        tapn.add(p);
382
 
                }else{
383
 
                        p = new LocalTimedPlace(nameInput, TimeInvariant.parse(invariant, constants));
384
 
                        tapn.add(p);
385
 
                        placeIDToName.put(idInput, nameInput);
386
 
                        for (int i = 0; i < initialMarkingInput; i++) {
387
 
                                network.marking().add(new TimedToken(p));
388
 
                        }
389
 
                }
390
 
                nameGenerator.updateIndicesForAllModels(nameInput);
391
 
        }
392
 
 
393
 
        private void parseAndAddArc(Element arc, TimedArcPetriNet tapn, ConstantStore constants) throws FormatException {
394
 
                String sourceId = idResolver.get(tapn.name(), arc.getAttribute("source"));
395
 
                String targetId = idResolver.get(tapn.name(), arc.getAttribute("target"));
396
 
                String inscription = arc.getAttribute("inscription");
397
 
                String type = arc.getAttribute("type");
398
 
                
399
 
                //Get weight if any
400
 
                Weight weight = new IntWeight(1);
401
 
                if(arc.hasAttribute("weight")){
402
 
                        weight = Weight.parseWeight(arc.getAttribute("weight"), constants);
403
 
                }
404
 
 
405
 
                switch (type) {
406
 
                        case "tapnInhibitor":
407
 
                                parseAndAddTimedInhibitorArc(sourceId, targetId, inscription, tapn, constants, weight);
408
 
                                break;
409
 
                        case "timed":
410
 
                                parseAndAddTimedInputArc(sourceId, targetId, inscription, tapn, constants, weight);
411
 
                                break;
412
 
                        case "transport":
413
 
                                parseAndAddTransportArc(sourceId, targetId, inscription, tapn, constants, weight);
414
 
                                break;
415
 
                        default:
416
 
                                parseAndAddTimedOutputArc(sourceId, targetId, inscription, tapn, weight);
417
 
                                break;
418
 
                }
419
 
        }
420
 
 
421
 
        private void parseAndAddTimedOutputArc(String sourceId, String targetId, String inscription, TimedArcPetriNet tapn, Weight weight) throws FormatException {
422
 
                TimedTransition transition = tapn.getTransitionByName(transitionIDToName.get(sourceId));
423
 
                TimedPlace place = tapn.getPlaceByName(placeIDToName.get(targetId));
424
 
 
425
 
                TimedOutputArc outputArc = new TimedOutputArc(transition, place, weight);
426
 
                
427
 
                if(tapn.hasArcFromTransitionToPlace(outputArc.source(),outputArc.destination())) {
428
 
                        throw new FormatException("Multiple arcs between a place and a transition is not allowed");
429
 
                }
430
 
 
431
 
                tapn.add(outputArc);
432
 
        }
433
 
 
434
 
        private void parseAndAddTransportArc(String sourceId, String targetId,  String inscription, TimedArcPetriNet tapn, ConstantStore constants, Weight weight) {
435
 
                String[] inscriptionSplit = {};
436
 
                if (inscription.contains(":")) {
437
 
                        inscriptionSplit = inscription.split(":");
438
 
                }
439
 
                boolean isInPreSet = false;
440
 
                TimedPlace sourcePlace = null;
441
 
                if(placeIDToName.containsKey(sourceId))
442
 
                        sourcePlace = tapn.getPlaceByName(placeIDToName.get(sourceId));
443
 
                
444
 
                if (sourcePlace != null) {
445
 
                        isInPreSet = true;
446
 
                }
447
 
                
448
 
                if (isInPreSet) {
449
 
                        TimedTransition transition = tapn.getTransitionByName(transitionIDToName.get(targetId));
450
 
                        Tuple<TimedTransition, Integer> hashKey = new Tuple<TimedTransition, Integer>(transition, Integer.parseInt(inscriptionSplit[1]));
451
 
                        
452
 
                        if (postsetArcs.containsKey(hashKey)) {
453
 
                                TimedPlace destPlace = postsetArcs.get(hashKey);
454
 
                                TimeInterval interval = TimeInterval.parse(inscriptionSplit[0], constants);
455
 
 
456
 
                                assert (sourcePlace != null);
457
 
                                assert (transition != null);
458
 
                                assert (destPlace != null);
459
 
 
460
 
                                TransportArc transArc = new TransportArc(sourcePlace, transition, destPlace, interval, weight);
461
 
                                tapn.add(transArc);
462
 
 
463
 
                                postsetArcs.remove(hashKey);
464
 
                        } else {
465
 
                                presetArcs.put(hashKey, sourcePlace);
466
 
                                transportArcsTimeIntervals.put(hashKey, TimeInterval.parse(inscriptionSplit[0], constants));
467
 
                        }
468
 
                } else {
469
 
                        TimedTransition trans = tapn.getTransitionByName(transitionIDToName.get(sourceId));
470
 
                        TimedPlace destPlace = tapn.getPlaceByName(placeIDToName.get(targetId));
471
 
                        Tuple<TimedTransition, Integer> hashKey = new Tuple<TimedTransition, Integer>(trans,  Integer.parseInt(inscriptionSplit[1]));
472
 
                        
473
 
                        if (presetArcs.containsKey(hashKey)) {
474
 
                                sourcePlace = presetArcs.get(hashKey);
475
 
                                TimeInterval interval = transportArcsTimeIntervals.get(hashKey);
476
 
 
477
 
                                assert (sourcePlace != null);
478
 
                                assert (trans != null);
479
 
                                assert (destPlace != null);
480
 
 
481
 
                                TransportArc transArc = new TransportArc(sourcePlace, trans, destPlace, interval, weight);
482
 
                                tapn.add(transArc);
483
 
 
484
 
                                presetArcs.remove(hashKey);
485
 
                                transportArcsTimeIntervals.remove(hashKey);
486
 
                        } else {
487
 
                                postsetArcs.put(hashKey, destPlace);
488
 
                        }
489
 
                }
490
 
        }
491
 
 
492
 
        private void parseAndAddTimedInputArc(String sourceId, String targetId, String inscription, TimedArcPetriNet tapn, ConstantStore constants, Weight weight) throws FormatException {
493
 
                TimedPlace place = tapn.getPlaceByName(placeIDToName.get(sourceId));
494
 
                TimedTransition transition = tapn.getTransitionByName(transitionIDToName.get(targetId));
495
 
                TimeInterval interval = TimeInterval.parse(inscription, constants);
496
 
 
497
 
                TimedInputArc inputArc = new TimedInputArc(place, transition, interval, weight);
498
 
 
499
 
                if(tapn.hasArcFromPlaceToTransition(inputArc.source(), inputArc.destination())) {
500
 
                        throw new FormatException("Multiple arcs between a place and a transition is not allowed");
501
 
                }
502
 
 
503
 
                tapn.add(inputArc);
504
 
        }
505
 
 
506
 
        private void parseAndAddTimedInhibitorArc(String sourceId, String targetId, String inscription, TimedArcPetriNet tapn, ConstantStore constants, Weight weight) {
507
 
                TimedPlace place = tapn.getPlaceByName(placeIDToName.get(sourceId));
508
 
                TimedTransition transition = tapn.getTransitionByName(transitionIDToName.get(targetId));
509
 
                
510
 
                TimedInhibitorArc inhibArc = new TimedInhibitorArc(place, transition, TimeInterval.ZERO_INF, weight);
511
 
                tapn.add(inhibArc);
512
 
        }
513
 
 
514
 
        private TAPNQuery parseTAPNQuery(Element queryElement, TimedArcPetriNetNetwork network) {
515
 
                String comment = getQueryComment(queryElement);
516
 
                TraceOption traceOption = getQueryTraceOption(queryElement);
517
 
                SearchOption searchOption = getQuerySearchOption(queryElement);
518
 
                HashTableSize hashTableSize = getQueryHashTableSize(queryElement);
519
 
                ExtrapolationOption extrapolationOption = getQueryExtrapolationOption(queryElement);
520
 
                ReductionOption reductionOption = getQueryReductionOption(queryElement);
521
 
                int capacity = Integer.parseInt(queryElement.getAttribute("capacity"));
522
 
                boolean symmetry = getReductionOption(queryElement, "symmetry", true);
523
 
                boolean gcd = getReductionOption(queryElement, "gcd", true);
524
 
                boolean timeDarts = getReductionOption(queryElement, "timeDarts", true);
525
 
                boolean pTrie = getReductionOption(queryElement, "pTrie", true);
526
 
                boolean overApproximation = getReductionOption(queryElement, "overApproximation", false);
527
 
                boolean isOverApproximationEnabled = getApproximationOption(queryElement, "enableOverApproximation", false);
528
 
                boolean isUnderApproximationEnabled = getApproximationOption(queryElement, "enableUnderApproximation", false);
529
 
                int approximationDenominator = getApproximationValue(queryElement, "approximationDenominator", 2);
530
 
                boolean discreteInclusion = getDiscreteInclusionOption(queryElement);
531
 
                boolean active = getActiveStatus(queryElement);
532
 
                InclusionPlaces inclusionPlaces = getInclusionPlaces(queryElement, network);
533
 
                boolean reduction = getReductionOption(queryElement, "reduction", true);
534
 
                String algorithmOption = queryElement.getAttribute("algorithmOption");
535
 
                boolean isCTL = isCTLQuery(queryElement);
536
 
                boolean siphontrap = getReductionOption(queryElement, "useSiphonTrapAnalysis", false);
537
 
                boolean queryReduction = getReductionOption(queryElement, "useQueryReduction", true);
538
 
                boolean stubborn = getReductionOption(queryElement, "useStubbornReduction", true);
539
 
                
540
 
                TCTLAbstractProperty query;
541
 
                if (queryElement.getElementsByTagName("formula").item(0) != null){
542
 
                        query = parseCTLQueryProperty(queryElement);
543
 
                } else {
544
 
                        query = parseQueryProperty(queryElement.getAttribute("query"));
545
 
                }
546
 
 
547
 
                if (query != null) {
548
 
                        TAPNQuery parsedQuery = new TAPNQuery(comment, capacity, query, traceOption, searchOption, reductionOption, symmetry, gcd, timeDarts, pTrie, overApproximation, reduction, hashTableSize, extrapolationOption, inclusionPlaces, isOverApproximationEnabled, isUnderApproximationEnabled, approximationDenominator);
549
 
                        parsedQuery.setActive(active);
550
 
                        parsedQuery.setDiscreteInclusion(discreteInclusion);
551
 
                        parsedQuery.setCategory(TAPNQueryLoader.detectCategory(query, isCTL));
552
 
                        parsedQuery.setUseSiphontrap(siphontrap);
553
 
                        parsedQuery.setUseQueryReduction(queryReduction);
554
 
                        parsedQuery.setUseStubbornReduction(stubborn);
555
 
                        if (parsedQuery.getCategory() == QueryCategory.CTL && algorithmOption != null){
556
 
                                parsedQuery.setAlgorithmOption(AlgorithmOption.valueOf(algorithmOption));
557
 
//                              RenameTemplateVisitor rt = new RenameTemplateVisitor("", 
558
 
//                              network.activeTemplates().get(0).name());
559
 
//                              parsedQuery.getProperty().accept(rt, null);
560
 
                        }
561
 
                        return parsedQuery;
562
 
                } else
563
 
                        return null;
564
 
        }
565
 
        
566
 
        private int getApproximationValue(Element queryElement, String attributeName, int defaultValue)
567
 
        {
568
 
                if(!queryElement.hasAttribute(attributeName)){
569
 
                        return defaultValue;
570
 
                }
571
 
                int result;
572
 
                try {
573
 
                        result = Integer.parseInt(queryElement.getAttribute(attributeName));
574
 
                } catch(Exception e) {
575
 
                        result = defaultValue;
576
 
                }
577
 
                return result;
578
 
        }
579
 
        
580
 
        private boolean getApproximationOption(Element queryElement, String attributeName, boolean defaultValue)
581
 
        {
582
 
                if(!queryElement.hasAttribute(attributeName)){
583
 
                        return defaultValue;
584
 
                }
585
 
                boolean result;
586
 
                try {
587
 
                        result = queryElement.getAttribute(attributeName).equals("true");
588
 
                } catch(Exception e) {
589
 
                        result = defaultValue;
590
 
                }
591
 
                return result;  
592
 
        }
593
 
        
594
 
        private boolean getDiscreteInclusionOption(Element queryElement) {
595
 
                boolean discreteInclusion;
596
 
                try {
597
 
                        discreteInclusion = queryElement.getAttribute("discreteInclusion").equals("true");
598
 
                } catch(Exception e) {
599
 
                        discreteInclusion = false;
600
 
                }
601
 
                return discreteInclusion;       
602
 
        }
603
 
        
604
 
        private InclusionPlaces getInclusionPlaces(Element queryElement, TimedArcPetriNetNetwork network) {
605
 
                List<TimedPlace> places = new ArrayList<TimedPlace>();
606
 
                
607
 
                String inclusionPlaces;
608
 
                try{
609
 
                        inclusionPlaces = queryElement.getAttribute("inclusionPlaces");
610
 
                } catch(Exception e) {
611
 
                        inclusionPlaces = "*ALL*";
612
 
                }
613
 
 
614
 
                if(!queryElement.hasAttribute("inclusionPlaces") || inclusionPlaces.equals("*ALL*")) 
615
 
                        return new InclusionPlaces();
616
 
                
617
 
                if(inclusionPlaces.isEmpty() || inclusionPlaces.equals("*NONE*")) 
618
 
                        return new InclusionPlaces(InclusionPlacesOption.UserSpecified, new ArrayList<TimedPlace>());
619
 
                
620
 
                
621
 
                String[] placeNames = inclusionPlaces.split(",");
622
 
                
623
 
                for(String name : placeNames) {
624
 
                        if(name.contains(".")) {
625
 
                                String templateName = name.split("\\.")[0];
626
 
                                String placeName = name.split("\\.")[1];
627
 
                                
628
 
                                // "true" and "false" are reserved keywords and places using these names are renamed to "_true" and "_false" respectively
629
 
                                if(placeName.equalsIgnoreCase("false") || placeName.equalsIgnoreCase("true"))
630
 
                                        placeName = "_" + placeName;
631
 
                                
632
 
                                TimedPlace p = network.getTAPNByName(templateName).getPlaceByName(placeName);
633
 
                                places.add(p);
634
 
                        } else { // shared Place
635
 
                                
636
 
                                if(name.equalsIgnoreCase("false") || name.equalsIgnoreCase("true"))
637
 
                                        name = "_" + name;
638
 
                                
639
 
                                TimedPlace p = network.getSharedPlaceByName(name);
640
 
                                places.add(p);
641
 
                        }
642
 
                }
643
 
                
644
 
                return new InclusionPlaces(InclusionPlacesOption.UserSpecified, places);
645
 
        }
646
 
        
647
 
        private boolean isCTLQuery(Element queryElement) {
648
 
                if(!queryElement.hasAttribute("type")){
649
 
                        return false;
650
 
                }
651
 
                boolean result;
652
 
                try {
653
 
                        result = queryElement.getAttribute("type").equals("CTL");
654
 
                } catch(Exception e) {
655
 
                        result = false;
656
 
                }
657
 
                return result;
658
 
        }
659
 
        
660
 
        private boolean getReductionOption(Element queryElement, String attributeName, boolean defaultValue) {
661
 
                boolean result;
662
 
                try {
663
 
                        result = queryElement.getAttribute(attributeName).equals("true");
664
 
                } catch(Exception e) {
665
 
                        result = defaultValue;
666
 
                }
667
 
                return result;  
668
 
        }
669
 
        
670
 
        private TCTLAbstractProperty parseCTLQueryProperty(Node queryElement){
671
 
                TCTLAbstractProperty query = null;
672
 
                
673
 
                try {
674
 
                        query = XMLCTLQueryParser.parse(queryElement);
675
 
                } catch (XMLQueryParseException e) {
676
 
                        System.err.println("No query was specified: " + e.getStackTrace().toString());
677
 
                }
678
 
                
679
 
                return query;
680
 
        }
681
 
 
682
 
        private TCTLAbstractProperty parseQueryProperty(String queryToParse) {
683
 
                TCTLAbstractProperty query = null;
684
 
 
685
 
                try {
686
 
                        query = TAPAALQueryParser.parse(queryToParse);
687
 
                } catch (Exception e) {
688
 
                        System.err.println("No query was specified: " + e.getStackTrace().toString());
689
 
                }
690
 
                return query;
691
 
        }
692
 
 
693
 
        private ReductionOption getQueryReductionOption(Element queryElement) {
694
 
                ReductionOption reductionOption;
695
 
                try {
696
 
                        reductionOption = ReductionOption.valueOf(queryElement.getAttribute("reductionOption"));
697
 
                } catch (Exception e) {
698
 
                        reductionOption = ReductionOption.VerifyTAPN;
699
 
                }
700
 
                return reductionOption;
701
 
        }
702
 
 
703
 
        private ExtrapolationOption getQueryExtrapolationOption(Element queryElement) {
704
 
                ExtrapolationOption extrapolationOption;
705
 
                try {
706
 
                        extrapolationOption = ExtrapolationOption.valueOf(queryElement.getAttribute("extrapolationOption"));
707
 
                } catch (Exception e) {
708
 
                        extrapolationOption = ExtrapolationOption.AUTOMATIC;
709
 
                }
710
 
                return extrapolationOption;
711
 
        }
712
 
 
713
 
        private HashTableSize getQueryHashTableSize(Element queryElement) {
714
 
                HashTableSize hashTableSize;
715
 
                try {
716
 
                        hashTableSize = HashTableSize.valueOf(queryElement.getAttribute("hashTableSize"));
717
 
                } catch (Exception e) {
718
 
                        hashTableSize = HashTableSize.MB_16;
719
 
                }
720
 
                return hashTableSize;
721
 
        }
722
 
 
723
 
        private SearchOption getQuerySearchOption(Element queryElement) {
724
 
                SearchOption searchOption;
725
 
                try {
726
 
                        searchOption = SearchOption.valueOf(queryElement.getAttribute("searchOption"));
727
 
                } catch (Exception e) {
728
 
                        searchOption = SearchOption.BFS;
729
 
                }
730
 
                return searchOption;
731
 
        }
732
 
 
733
 
        private TraceOption getQueryTraceOption(Element queryElement) {
734
 
                TraceOption traceOption;
735
 
                try {
736
 
                        traceOption = TraceOption.valueOf(queryElement.getAttribute("traceOption"));
737
 
                } catch (Exception e) {
738
 
                        traceOption = TraceOption.NONE;
739
 
                }
740
 
                return traceOption;
741
 
        }
742
 
 
743
 
        private String getQueryComment(Element queryElement) {
744
 
                String comment;
745
 
                try {
746
 
                        comment = queryElement.getAttribute("name");
747
 
                } catch (Exception e) {
748
 
                        comment = "No comment specified";
749
 
                }
750
 
                return comment;
751
 
        }
752
 
 
753
 
        private Constant parseAndAddConstant(Element constantElement) {
754
 
                String name = constantElement.getAttribute("name");
755
 
                int value = Integer.parseInt(constantElement.getAttribute("value"));
756
 
 
757
 
                return new Constant(name, value);
758
 
        }
759
 
}