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

« back to all changes in this revision

Viewing changes to src/dk/aau/cs/io/TapnXmlLoader.java

  • Committer: srba.jiri at gmail
  • Date: 2020-08-03 14:00:57 UTC
  • mfrom: (1067.2.23 tapaalSaveLoad)
  • Revision ID: srba.jiri@gmail.com-20200803140057-ut4f08qccu63dv24
merged in lp:~tapaal-contributor/tapaal/load-and-save-time-and-game-net-properties introducing lense (timed,game) for future implementation of net projections

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
import java.util.HashMap;
9
9
import java.util.List;
10
10
 
11
 
import javax.swing.JOptionPane;
 
11
import javax.swing.*;
12
12
import javax.xml.parsers.DocumentBuilder;
13
13
import javax.xml.parsers.DocumentBuilderFactory;
14
14
import javax.xml.parsers.ParserConfigurationException;
15
15
 
 
16
import dk.aau.cs.debug.Logger;
16
17
import org.w3c.dom.Document;
17
18
import org.w3c.dom.Element;
18
19
import org.w3c.dom.Node;
61
62
 
62
63
public class TapnXmlLoader {
63
64
        private static final String PLACENAME_ERROR_MESSAGE = "The keywords \"true\" and \"false\" are reserved and can not be used as place names.\nPlaces with these names will be renamed to \"_true\" and \"_false\" respectively.\n\n Note that any queries using these places may not be parsed correctly.";
64
 
        private HashMap<TimedTransitionComponent, TimedTransportArcComponent> presetArcs = new HashMap<TimedTransitionComponent, TimedTransportArcComponent>();
 
65
    private HashMap<TimedTransitionComponent, TimedTransportArcComponent> presetArcs = new HashMap<TimedTransitionComponent, TimedTransportArcComponent>();
65
66
        private HashMap<TimedTransitionComponent, TimedTransportArcComponent> postsetArcs = new HashMap<TimedTransitionComponent, TimedTransportArcComponent>();
66
67
        private HashMap<TimedTransportArcComponent, TimeInterval> transportArcsTimeIntervals = new HashMap<TimedTransportArcComponent, TimeInterval>();
67
68
 
70
71
        private boolean firstPlaceRenameWarning = true;
71
72
        private IdResolver idResolver = new IdResolver();
72
73
 
 
74
        private boolean isTimed;
 
75
        private boolean isGame;
 
76
        private boolean hasUncontrollableTransitions = false;
 
77
 
73
78
        public TapnXmlLoader() {
74
79
        }
75
80
 
124
129
                network.buildConstraints();
125
130
                
126
131
                parseBound(doc, network);
127
 
                
128
 
                return new LoadedModel(network, templates, queries);
 
132
 
 
133
                parseFeature(doc, network);
 
134
 
 
135
                return new LoadedModel(network, templates, queries, isTimed, isGame);
129
136
        }
130
137
 
131
138
        private void parseBound(Document doc, TimedArcPetriNetNetwork network){
135
142
                }
136
143
        }
137
144
 
 
145
    private void parseFeature(Document doc, TimedArcPetriNetNetwork network) {
 
146
            boolean networkIsTimed = !network.isUntimed();
 
147
 
 
148
        if (doc.getElementsByTagName("feature").getLength() > 0) {
 
149
                NodeList nodeList = doc.getElementsByTagName("feature");
 
150
 
 
151
            isTimed = Boolean.parseBoolean(nodeList.item(0).getAttributes().getNamedItem("isTimed").getNodeValue());
 
152
            isGame = Boolean.parseBoolean(nodeList.item(0).getAttributes().getNamedItem("isGame").getNodeValue());
 
153
 
 
154
            if (networkIsTimed && !isTimed) {
 
155
                isTimed = true;
 
156
                Logger.log("The net contains time features. The entire net will be changed to include time features.");
 
157
            }
 
158
            if (hasUncontrollableTransitions && !isGame) {
 
159
                isGame = true;
 
160
                Logger.log("The net contains game features. The entire net will be changed to include game features.");
 
161
 
 
162
            }
 
163
        } else {
 
164
            isTimed = networkIsTimed;
 
165
            isGame = hasUncontrollableTransitions;
 
166
        }
 
167
    }
 
168
 
138
169
        private void parseSharedPlaces(Document doc, TimedArcPetriNetNetwork network, ConstantStore constants) {
139
170
                NodeList sharedPlaceNodes = doc.getElementsByTagName("shared-place");
140
171
 
220
251
        }
221
252
 
222
253
        private Template parseTimedArcPetriNet(Node tapnNode, TimedArcPetriNetNetwork network, ConstantStore constants) throws FormatException {
223
 
                String name = getTAPNName(tapnNode);
 
254
        String name = getTAPNName(tapnNode);
224
255
 
225
256
                boolean active = getActiveStatus(tapnNode);
226
 
                
 
257
 
227
258
                TimedArcPetriNet tapn = new TimedArcPetriNet(name);
228
259
                tapn.setActive(active);
229
260
                network.add(tapn);
240
271
                        }
241
272
                }
242
273
 
243
 
 
244
274
                return template;
245
275
        }
246
276
 
247
277
        private boolean getActiveStatus(Node tapnNode) {
248
 
                if (tapnNode instanceof Element) {
249
 
                        Element element = (Element)tapnNode;
250
 
                        String activeString = element.getAttribute("active");
251
 
                        
252
 
                        if (activeString == null || activeString.equals(""))
253
 
                                return true;
254
 
                        else
255
 
                                return activeString.equals("true");
256
 
                } else {
257
 
                        return true;
258
 
                }
259
 
        }
 
278
        if (tapnNode instanceof Element) {
 
279
            Element element = (Element)tapnNode;
 
280
            String activeString = element.getAttribute("active");
 
281
 
 
282
            if (activeString == null || activeString.equals(""))
 
283
                return true;
 
284
            else
 
285
                return activeString.equals("true");
 
286
        } else {
 
287
            return true;
 
288
        }
 
289
    }
260
290
 
261
291
        private void parseElement(Element element, Template template, TimedArcPetriNetNetwork network, ConstantStore constants) throws FormatException {
262
292
                if ("labels".equals(element.getNodeName())) {
269
299
                        TimedTransitionComponent transition = parseTransition(element, network, template.model());
270
300
                        template.guiModel().addPetriNetObject(transition);
271
301
                } else if ("arc".equals(element.getNodeName())) {
272
 
                        parseAndAddArc(element, template, constants);
273
 
                }
 
302
            parseAndAddArc(element, template, constants);
 
303
        }
274
304
        }
275
305
 
276
306
        private boolean isNameAllowed(String name) {
343
373
                String idInput = transition.getAttribute("id");
344
374
                String nameInput = transition.getAttribute("name");
345
375
                boolean isUrgent = Boolean.parseBoolean(transition.getAttribute("urgent"));
 
376
 
 
377
                String player = transition.getAttribute("player");
 
378
                if (player.length() > 0 && player.equals("1")) {
 
379
                    hasUncontrollableTransitions = true;
 
380
        }
346
381
                
347
382
                idResolver.add(tapn.name(), idInput, nameInput);
348
383