4
import java.io.IOException;
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;
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;
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;
43
public class TimedArcPetriNetNetworkWriter implements PNMLWriter {
45
private Iterable<Template> templates;
46
private Iterable<TAPNQuery> queries;
47
private Iterable<Constant> constants;
48
private final TimedArcPetriNetNetwork network;
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;
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");
65
Document document = null;
66
Transformer transformer = null;
69
// Build a Petri Net XML Document
70
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
71
DocumentBuilder builder = builderFactory.newDocumentBuilder();
72
document = builder.newDocument();
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);
80
appendSharedPlaces(document, pnmlRootNode);
81
appendSharedTransitions(document, pnmlRootNode);
82
appendConstants(document, pnmlRootNode);
83
appendTemplates(document, pnmlRootNode);
84
appendQueries(document, pnmlRootNode);
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);
94
StreamResult result = new StreamResult(outputObjectArrayList);
95
transformer.transform(source, result);
96
} catch (ParserConfigurationException e) {
98
.println("ParserConfigurationException thrown in savePNML() "
99
+ ": dataLayerWriter Class : dataLayer Package: filename=\""
100
+ file.getCanonicalPath() + "\" xslt=\"");
101
} catch (DOMException e) {
103
.println("DOMException thrown in savePNML() "
104
+ ": dataLayerWriter Class : dataLayer Package: filename=\""
105
+ file.getCanonicalPath() + "\" transformer=\""
106
+ transformer.getURIResolver() + "\"");
107
} catch (TransformerConfigurationException e) {
109
.println("TransformerConfigurationException thrown in savePNML() "
110
+ ": dataLayerWriter Class : dataLayer Package: filename=\""
111
+ file.getCanonicalPath()
112
+ "\" transformer=\""
113
+ transformer.getURIResolver() + "\"");
114
} catch (TransformerException e) {
116
.println("TransformerException thrown in savePNML() : dataLayerWriter Class : dataLayer Package: filename=\""
117
+ file.getCanonicalPath()
118
+ "\" transformer=\""
119
+ transformer.getURIResolver() + "\"" + e);
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);
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);
141
private void appendConstants(Document document, Element root) {
142
for (Constant constant : constants) {
143
Element elem = createConstantElement(constant, document);
144
root.appendChild(elem);
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");
152
Element constantElement = document.createElement("constant");
154
constantElement.setAttribute("name", constant.name());
155
constantElement.setAttribute("value", String.valueOf(constant.value()));
157
return constantElement;
160
private void appendTemplates(Document document, Element root) {
161
for (Template tapn : templates) {
162
DataLayer guiModel = tapn.guiModel();
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);
170
Attr netAttrType = document.createAttribute("type");
171
switch (guiModel.netType()) {
173
netAttrType.setValue("Untimed P/T net");
176
netAttrType.setValue("P/T net");
178
NET.setAttributeNode(netAttrType);
180
appendAnnotationNotes(document, guiModel, NET);
181
appendPlaces(document, guiModel, NET);
182
appendTransitions(document, guiModel, NET);
183
appendArcs(document, guiModel, NET);
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));
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));
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));
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);
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));
219
NET.appendChild(newArc);
223
private void appendQueries(Document document, Element root) {
224
for (TAPNQuery query : queries) {
225
Element newQuery = createQueryElement(query, document);
226
root.appendChild(newQuery);
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");
234
Element queryElement = document.createElement("query");
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());
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");
253
Element placeElement = document.createElement("place");
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());
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");
274
Element labelElement = document.createElement("labels");
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", );
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");
292
Element transitionElement = document.createElement("transition");
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()));
305
return transitionElement;
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");
313
Element arcElement = document.createElement("arc");
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() : ""));
319
if (inputArc instanceof TimedOutputArcComponent) {
320
if (inputArc instanceof TimedInputArcComponent) {
321
arcElement.setAttribute("type", getInputArcTypeAsString((TimedInputArcComponent)inputArc));
322
arcElement.setAttribute("inscription", getGuardAsString((TimedInputArcComponent)inputArc));
324
arcElement.setAttribute("type", "normal");
325
arcElement.setAttribute("inscription", Integer.toString((inputArc != null ? inputArc.getWeight() : 1)));
331
private String getInputArcTypeAsString(TimedInputArcComponent inputArc) {
332
if (inputArc instanceof TransportArcComponent) {
334
} else if (inputArc instanceof TimedInhibitorArcComponent) {
335
return "tapnInhibitor";
341
private String getGuardAsString(TimedInputArcComponent inputArc) {
342
if(inputArc instanceof TransportArcComponent)
343
return inputArc.getGuardAsString() + ":" + ((TransportArcComponent) inputArc).getGroupNr();
345
return inputArc.getGuardAsString();
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");
353
arcPoint.setAttribute("id", String.valueOf(id));
354
arcPoint.setAttribute("xCoord", x);
355
arcPoint.setAttribute("yCoord", y);
356
arcPoint.setAttribute("arcPointType", type);