~tapaal-contributor/tapaal/disappearing-tokens-1940098

« back to all changes in this revision

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

  • Committer: Kenneth Yrke Jørgensen
  • Date: 2011-04-12 09:50:16 UTC
  • mfrom: (329.1.188 tapaal-1.5)
  • Revision ID: mail@yrke.dk-20110412095016-e4hqdgab5596ja09
Merged with branch addning support for new 1.5 features

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package dk.aau.cs.io;
 
2
 
 
3
import java.io.File;
 
4
import java.io.IOException;
 
5
 
 
6
import javax.xml.parsers.DocumentBuilder;
 
7
import javax.xml.parsers.DocumentBuilderFactory;
 
8
import javax.xml.parsers.ParserConfigurationException;
 
9
import javax.xml.transform.OutputKeys;
 
10
import javax.xml.transform.Transformer;
 
11
import javax.xml.transform.TransformerConfigurationException;
 
12
import javax.xml.transform.TransformerException;
 
13
import javax.xml.transform.TransformerFactory;
 
14
import javax.xml.transform.dom.DOMSource;
 
15
import javax.xml.transform.stream.StreamResult;
 
16
 
 
17
import org.w3c.dom.Attr;
 
18
import org.w3c.dom.DOMException;
 
19
import org.w3c.dom.Document;
 
20
import org.w3c.dom.Element;
 
21
import org.w3c.dom.Text;
 
22
 
 
23
import pipe.dataLayer.AnnotationNote;
 
24
import pipe.dataLayer.Arc;
 
25
import pipe.dataLayer.DataLayer;
 
26
import pipe.dataLayer.PNMLWriter;
 
27
import pipe.dataLayer.Place;
 
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 dk.aau.cs.model.tapn.Constant;
 
38
import dk.aau.cs.model.tapn.SharedPlace;
 
39
import dk.aau.cs.model.tapn.SharedTransition;
 
40
import dk.aau.cs.model.tapn.TimedArcPetriNetNetwork;
 
41
import dk.aau.cs.util.Require;
 
42
 
 
43
public class TimedArcPetriNetNetworkWriter implements PNMLWriter {
 
44
 
 
45
        private Iterable<Template> templates;
 
46
        private Iterable<TAPNQuery> queries;
 
47
        private Iterable<Constant> constants;
 
48
        private final TimedArcPetriNetNetwork network;
 
49
 
 
50
        public TimedArcPetriNetNetworkWriter(
 
51
                        TimedArcPetriNetNetwork network, 
 
52
                        Iterable<Template> templates,
 
53
                        Iterable<TAPNQuery> queries, 
 
54
                        Iterable<Constant> constants) {
 
55
                this.network = network;
 
56
                this.templates = templates;
 
57
                this.queries = queries;
 
58
                this.constants = constants;
 
59
        }
 
60
 
 
61
        // TODO: refactor this code to make it more readable
 
62
        public void savePNML(File file) throws IOException, ParserConfigurationException, DOMException, TransformerConfigurationException, TransformerException {
 
63
                Require.that(file != null, "Error: file to save to was null");
 
64
 
 
65
                Document document = null;
 
66
                Transformer transformer = null;
 
67
                
 
68
                try {
 
69
                        // Build a Petri Net XML Document
 
70
                        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
 
71
                        DocumentBuilder builder = builderFactory.newDocumentBuilder();
 
72
                        document = builder.newDocument();
 
73
 
 
74
                        Element pnmlRootNode = document.createElement("pnml"); // PNML Top Level
 
75
                        document.appendChild(pnmlRootNode);
 
76
                        Attr pnmlAttr = document.createAttribute("xmlns"); // PNML "xmlns"
 
77
                        pnmlAttr.setValue("http://www.informatik.hu-berlin.de/top/pnml/ptNetb");
 
78
                        pnmlRootNode.setAttributeNode(pnmlAttr);
 
79
 
 
80
                        appendSharedPlaces(document, pnmlRootNode);
 
81
                        appendSharedTransitions(document, pnmlRootNode);
 
82
                        appendConstants(document, pnmlRootNode);
 
83
                        appendTemplates(document, pnmlRootNode);
 
84
                        appendQueries(document, pnmlRootNode);
 
85
 
 
86
                        document.normalize();
 
87
                        // Create Transformer with XSL Source File
 
88
                        transformer = TransformerFactory.newInstance().newTransformer();
 
89
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 
90
                        // Write file and do XSLT transformation to generate correct PNML
 
91
                        File outputObjectArrayList = file;
 
92
                        DOMSource source = new DOMSource(document);
 
93
 
 
94
                        StreamResult result = new StreamResult(outputObjectArrayList);
 
95
                        transformer.transform(source, result);
 
96
                } catch (ParserConfigurationException e) {
 
97
                        System.out
 
98
                                        .println("ParserConfigurationException thrown in savePNML() "
 
99
                                                        + ": dataLayerWriter Class : dataLayer Package: filename=\""
 
100
                                                        + file.getCanonicalPath() + "\" xslt=\"");
 
101
                } catch (DOMException e) {
 
102
                        System.out
 
103
                                        .println("DOMException thrown in savePNML() "
 
104
                                                        + ": dataLayerWriter Class : dataLayer Package: filename=\""
 
105
                                                        + file.getCanonicalPath() + "\" transformer=\""
 
106
                                                        + transformer.getURIResolver() + "\"");
 
107
                } catch (TransformerConfigurationException e) {
 
108
                        System.out
 
109
                                        .println("TransformerConfigurationException thrown in savePNML() "
 
110
                                                        + ": dataLayerWriter Class : dataLayer Package: filename=\""
 
111
                                                        + file.getCanonicalPath()
 
112
                                                        + "\" transformer=\""
 
113
                                                        + transformer.getURIResolver() + "\"");
 
114
                } catch (TransformerException e) {
 
115
                        System.out
 
116
                                        .println("TransformerException thrown in savePNML() : dataLayerWriter Class : dataLayer Package: filename=\""
 
117
                                                        + file.getCanonicalPath()
 
118
                                                        + "\" transformer=\""
 
119
                                                        + transformer.getURIResolver() + "\"" + e);
 
120
                }
 
121
        }
 
122
        
 
123
        private void appendSharedPlaces(Document document, Element root) {
 
124
                for(SharedPlace place : network.sharedPlaces()){
 
125
                        Element element = document.createElement("shared-place");
 
126
                        element.setAttribute("invariant", place.invariant().toString());
 
127
                        element.setAttribute("name", place.name());
 
128
                        element.setAttribute("initialMarking", String.valueOf(place.numberOfTokens()));
 
129
                        root.appendChild(element);
 
130
                }
 
131
        }
 
132
 
 
133
        private void appendSharedTransitions(Document document, Element root) {
 
134
                for(SharedTransition transition : network.sharedTransitions()){
 
135
                        Element element = document.createElement("shared-transition");
 
136
                        element.setAttribute("name", transition.name());
 
137
                        root.appendChild(element);
 
138
                }
 
139
        }
 
140
 
 
141
        private void appendConstants(Document document, Element root) {
 
142
                for (Constant constant : constants) {
 
143
                        Element elem = createConstantElement(constant, document);
 
144
                        root.appendChild(elem);
 
145
                }
 
146
        }
 
147
        
 
148
        private Element createConstantElement(Constant constant, Document document) {
 
149
                Require.that(constant != null, "Error: constant was null");
 
150
                Require.that(document != null, "Error: document was null");
 
151
                
 
152
                Element constantElement = document.createElement("constant");
 
153
                
 
154
                constantElement.setAttribute("name", constant.name());
 
155
                constantElement.setAttribute("value", String.valueOf(constant.value()));
 
156
        
 
157
                return constantElement;
 
158
        }
 
159
 
 
160
        private void appendTemplates(Document document, Element root) {
 
161
                for (Template tapn : templates) {
 
162
                        DataLayer guiModel = tapn.guiModel();
 
163
 
 
164
                        Element NET = document.createElement("net");
 
165
                        root.appendChild(NET);
 
166
                        Attr netAttrId = document.createAttribute("id");
 
167
                        netAttrId.setValue(tapn.model().name());
 
168
                        NET.setAttributeNode(netAttrId);
 
169
 
 
170
                        Attr netAttrType = document.createAttribute("type");
 
171
                        switch (guiModel.netType()) {
 
172
                        case UNTIMED:
 
173
                                netAttrType.setValue("Untimed P/T net");
 
174
                                break;
 
175
                        default:
 
176
                                netAttrType.setValue("P/T net");
 
177
                        }
 
178
                        NET.setAttributeNode(netAttrType);
 
179
 
 
180
                        appendAnnotationNotes(document, guiModel, NET);
 
181
                        appendPlaces(document, guiModel, NET);
 
182
                        appendTransitions(document, guiModel, NET);
 
183
                        appendArcs(document, guiModel, NET);
 
184
                }
 
185
        }
 
186
 
 
187
        private void appendAnnotationNotes(Document document, DataLayer guiModel, Element NET) {
 
188
                AnnotationNote[] labels = guiModel.getLabels();
 
189
                for (int i = 0; i < labels.length; i++) {
 
190
                        NET.appendChild(createAnnotationNoteElement(labels[i], document));
 
191
                }
 
192
        }
 
193
 
 
194
        private void appendPlaces(Document document, DataLayer guiModel, Element NET) {
 
195
                Place[] places = guiModel.getPlaces();
 
196
                for (int i = 0; i < places.length; i++) {
 
197
                        NET.appendChild(createPlaceElement((TimedPlaceComponent) places[i], guiModel, document));
 
198
                }
 
199
        }
 
200
 
 
201
        private void appendTransitions(Document document, DataLayer guiModel, Element NET) {
 
202
                Transition[] transitions = guiModel.getTransitions();
 
203
                for (int i = 0; i < transitions.length; i++) {
 
204
                        NET.appendChild(createTransitionElement((TimedTransitionComponent)transitions[i],       document));
 
205
                }
 
206
        }
 
207
        
 
208
        private void appendArcs(Document document, DataLayer guiModel, Element NET) {
 
209
                Arc[] arcs = guiModel.getArcs();
 
210
                for (int i = 0; i < arcs.length; i++) {
 
211
                        Element newArc = createArcElement(arcs[i], guiModel, document);
 
212
 
 
213
                        int arcPoints = arcs[i].getArcPath().getArcPathDetails().length;
 
214
                        String[][] point = arcs[i].getArcPath().getArcPathDetails();
 
215
                        for (int j = 0; j < arcPoints; j++) {
 
216
                                newArc.appendChild(createArcPoint(point[j][0],
 
217
                                                point[j][1], point[j][2], document, j));
 
218
                        }
 
219
                        NET.appendChild(newArc);
 
220
                }
 
221
        }
 
222
        
 
223
        private void appendQueries(Document document, Element root) {
 
224
                for (TAPNQuery query : queries) {
 
225
                        Element newQuery = createQueryElement(query, document);
 
226
                        root.appendChild(newQuery);
 
227
                }
 
228
        }
 
229
 
 
230
        private Element createQueryElement(TAPNQuery query, Document document) {
 
231
                Require.that(query != null, "Error: query was null");
 
232
                Require.that(document != null, "Error: document was null");
 
233
                
 
234
                Element queryElement = document.createElement("query");
 
235
        
 
236
                queryElement.setAttribute("name", query.getName());
 
237
                queryElement.setAttribute("capacity", "" + query.getCapacity());
 
238
                queryElement.setAttribute("query", query.getProperty().toString());
 
239
                queryElement.setAttribute("traceOption", ""     + query.getTraceOption());
 
240
                queryElement.setAttribute("searchOption", "" + query.getSearchOption());
 
241
                queryElement.setAttribute("hashTableSize", "" + query.getHashTableSize());
 
242
                queryElement.setAttribute("extrapolationOption", "" + query.getExtrapolationOption());
 
243
                queryElement.setAttribute("reductionOption", "" + query.getReductionOption());
 
244
                
 
245
                return queryElement;
 
246
        }
 
247
 
 
248
        private Element createPlaceElement(TimedPlaceComponent inputPlace, DataLayer guiModel, Document document) {
 
249
                Require.that(inputPlace != null, "Error: inputPlace was null");
 
250
                Require.that(guiModel != null, "Error: guiModel was null");
 
251
                Require.that(document != null, "Error: document was null");
 
252
                
 
253
                Element placeElement = document.createElement("place");
 
254
 
 
255
                placeElement.setAttribute("positionX", (inputPlace.getPositionXObject() != null ? String.valueOf(inputPlace.getPositionXObject()) : ""));
 
256
                placeElement.setAttribute("positionY", (inputPlace.getPositionYObject() != null ? String.valueOf(inputPlace.getPositionYObject())       : ""));
 
257
                placeElement.setAttribute("name", inputPlace.underlyingPlace().name());
 
258
                placeElement.setAttribute("id", (inputPlace.getId() != null ? inputPlace.getId() : "error"));
 
259
                placeElement.setAttribute("nameOffsetX", (inputPlace.getNameOffsetXObject() != null ? String.valueOf(inputPlace.getNameOffsetXObject()) : ""));
 
260
                placeElement.setAttribute("nameOffsetY", (inputPlace.getNameOffsetYObject() != null ? String.valueOf(inputPlace.getNameOffsetYObject()) : ""));
 
261
                placeElement.setAttribute("initialMarking", ((Integer) inputPlace.getNumberOfTokens() != null ? String.valueOf((Integer) inputPlace.getNumberOfTokens()) : "0"));
 
262
                placeElement.setAttribute("markingOffsetX",     (inputPlace.getMarkingOffsetXObject() != null ? String.valueOf(inputPlace.getMarkingOffsetXObject()) : ""));
 
263
                placeElement.setAttribute("markingOffsetY",     (inputPlace.getMarkingOffsetYObject() != null ? String.valueOf(inputPlace.getMarkingOffsetYObject()) : ""));
 
264
                placeElement.setAttribute("invariant", inputPlace.underlyingPlace().invariant().toString());
 
265
 
 
266
                return placeElement;
 
267
        }
 
268
 
 
269
 
 
270
        private Element createAnnotationNoteElement(AnnotationNote inputLabel, Document document) {
 
271
                Require.that(inputLabel != null, "Error: inputLabel was null");
 
272
                Require.that(document != null, "Error: document was null");
 
273
                
 
274
                Element labelElement = document.createElement("labels");
 
275
 
 
276
                labelElement.setAttribute("positionX", (inputLabel.getOriginalX() >= 0.0 ? String.valueOf(inputLabel.getOriginalX()) : ""));
 
277
                labelElement.setAttribute("positionY", (inputLabel.getOriginalY() >= 0.0 ? String.valueOf(inputLabel.getOriginalY()) : ""));
 
278
                labelElement.setAttribute("width", (inputLabel.getNoteWidth() >= 0.0 ? String.valueOf(inputLabel.getNoteWidth()) : ""));
 
279
                labelElement.setAttribute("height", (inputLabel.getNoteHeight() >= 0.0 ? String.valueOf(inputLabel.getNoteHeight()) : ""));
 
280
                labelElement.setAttribute("border", String.valueOf(inputLabel.isShowingBorder()));
 
281
                Text text = document.createTextNode(inputLabel.getNoteText() != null ? inputLabel.getNoteText() : "");
 
282
                labelElement.appendChild(text);
 
283
                //labelElement.setAttribute("text", );
 
284
                
 
285
                return labelElement;
 
286
        }
 
287
 
 
288
        private Element createTransitionElement(TimedTransitionComponent inputTransition, Document document) {
 
289
                Require.that(inputTransition != null, "Error: inputTransition was null");
 
290
                Require.that(document != null, "Error: document was null");
 
291
                
 
292
                Element transitionElement = document.createElement("transition");
 
293
 
 
294
                transitionElement.setAttribute("positionX", (inputTransition.getPositionXObject() != null ? String.valueOf(inputTransition.getPositionXObject()) : ""));
 
295
                transitionElement.setAttribute("positionY",     (inputTransition.getPositionYObject() != null ? String.valueOf(inputTransition.getPositionYObject()) : ""));
 
296
                transitionElement.setAttribute("nameOffsetX", (inputTransition.getNameOffsetXObject() != null ? String.valueOf(inputTransition.getNameOffsetXObject()) : ""));
 
297
                transitionElement.setAttribute("nameOffsetY", (inputTransition.getNameOffsetYObject() != null ? String.valueOf(inputTransition.getNameOffsetYObject()) : ""));
 
298
                transitionElement.setAttribute("name", inputTransition.underlyingTransition().name());
 
299
                transitionElement.setAttribute("id", (inputTransition.getId() != null ? inputTransition.getId() : "error"));
 
300
                transitionElement.setAttribute("timed", String.valueOf(inputTransition.isTimed()));
 
301
                transitionElement.setAttribute("infiniteServer", String.valueOf(inputTransition.isInfiniteServer()));
 
302
                transitionElement.setAttribute("angle", String.valueOf(inputTransition.getAngle()));
 
303
                transitionElement.setAttribute("priority", String.valueOf(inputTransition.getPriority()));
 
304
 
 
305
                return transitionElement;
 
306
        }
 
307
 
 
308
        private Element createArcElement(Arc inputArc, DataLayer guiModel, Document document) {
 
309
                Require.that(inputArc != null, "Error: inputArc was null");
 
310
                Require.that(guiModel != null, "Error: guiModel was null");
 
311
                Require.that(document != null, "Error: document was null");
 
312
                
 
313
                Element arcElement = document.createElement("arc");
 
314
                
 
315
                arcElement.setAttribute("id", (inputArc.getId() != null ? inputArc.getId() : "error"));
 
316
                arcElement.setAttribute("source", (inputArc.getSource().getId() != null ? inputArc.getSource().getId() : ""));
 
317
                arcElement.setAttribute("target", (inputArc.getTarget().getId() != null ? inputArc.getTarget().getId() : ""));
 
318
 
 
319
                if (inputArc instanceof TimedOutputArcComponent) {
 
320
                        if (inputArc instanceof TimedInputArcComponent) {
 
321
                                arcElement.setAttribute("type", getInputArcTypeAsString((TimedInputArcComponent)inputArc));
 
322
                                arcElement.setAttribute("inscription", getGuardAsString((TimedInputArcComponent)inputArc));     
 
323
                        } else {
 
324
                                arcElement.setAttribute("type", "normal");
 
325
                                arcElement.setAttribute("inscription", Integer.toString((inputArc != null ? inputArc.getWeight() : 1)));
 
326
                        }
 
327
                } 
 
328
                return arcElement;
 
329
        }
 
330
 
 
331
        private String getInputArcTypeAsString(TimedInputArcComponent inputArc) {
 
332
                if (inputArc instanceof TransportArcComponent) {
 
333
                        return "transport";
 
334
                } else if (inputArc instanceof TimedInhibitorArcComponent) {
 
335
                        return "tapnInhibitor";
 
336
                } else {
 
337
                        return "timed";
 
338
                }
 
339
        }
 
340
 
 
341
        private String getGuardAsString(TimedInputArcComponent inputArc) {
 
342
                if(inputArc instanceof TransportArcComponent)
 
343
                        return inputArc.getGuardAsString() + ":" + ((TransportArcComponent) inputArc).getGroupNr();
 
344
                else {
 
345
                        return inputArc.getGuardAsString();
 
346
                }
 
347
        }
 
348
 
 
349
        private Element createArcPoint(String x, String y, String type, Document document, int id) {
 
350
                Require.that(document != null, "Error: document was null");
 
351
                Element arcPoint = document.createElement("arcpath");
 
352
                
 
353
                arcPoint.setAttribute("id", String.valueOf(id));
 
354
                arcPoint.setAttribute("xCoord", x);
 
355
                arcPoint.setAttribute("yCoord", y);
 
356
                arcPoint.setAttribute("arcPointType", type);
 
357
 
 
358
                return arcPoint;
 
359
        }
 
360
 
 
361
}