-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLWriterDOM.java
607 lines (391 loc) · 19.1 KB
/
XMLWriterDOM.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Beliefs beliefs1 = new Beliefs();
beliefs1.addBelief("cucciolo", "piccolo");
beliefs1.addBelief("amre", "piccolo");
beliefs1.addBelief("nabbo", "piccolo");
beliefs1.addBelief("fortnite", "piccolo");
Belief belief1, belief2;
belief1 = beliefs1.getBelief("fortnite");
//belief2 = beliefs1.getBeliefValue("piccolo");
String name = belief1.getName();
String value = beliefs1.getBeliefValue("amre");
beliefs1.changeBeliefValue("amre", "lacasadicarta");
String value2 = beliefs1.getBeliefValue("amre");
System.out.println("il val del belief2 e= "+value);
System.out.println("il nuovo val del belief2 e= "+value2);*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLWriterDOM {
private static int currmaxID = 0;
public static ArrayList <AutomaState> StateList = new ArrayList<AutomaState>();
public static ArrayList<AutomaTransition> TransitionList = new ArrayList<AutomaTransition>();
private static Agent agent = new Agent("mimmo2");
public static void main(String[] args) {
int stateid = 0;
ArrayList<AutomaState> retList = new ArrayList<AutomaState>();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document automa = dBuilder.parse("/Users/Carmi/Desktop/WorkspaceTesi/Agent/" + agent.getAgentName() + ".xml");
automa.getDocumentElement().normalize();
NodeList template = automa.getElementsByTagName("template");
for (int i = 0;i < StateList.size();i++)
{
NodeList lastlist = (template instanceof Element ? (Element)template : null).getElementsByTagName("location");
Element last = (Element) lastlist.item(lastlist.getLength() - 1);
Element loc = automa.createElement("location");
(loc instanceof Element ? (Element)loc : null).setAttribute("id",StateList.get(i).myID);
Element locname = automa.createElement("name");
locname.setTextContent(StateList.get(i).myID);
Element comment = automa.createElement("label");
(comment instanceof Element ? (Element)comment : null).setAttribute("kind","comments");
comment.setTextContent(StateList.get(i).stringBeliefs);
Node ciccio = template.item(i);
Element cicc = (Element) ciccio;
//template.insertAfter(loc,last);
loc.appendChild(locname);
loc.appendChild(comment);
}
for (int i = 0;i < TransitionList.size();i++)
{
Element trans = automa.createElement("transition");
Element from = automa.createElement("source");
(from instanceof Element ? (Element)from : null).setAttribute("ref",TransitionList.get(i).sourceID);
Element to = automa.createElement("target");
(to instanceof Element ? (Element)to : null).setAttribute("ref",TransitionList.get(i).targetID);
Element comment = automa.createElement("label");
(comment instanceof Element ? (Element)comment : null).setAttribute("kind","comments");
comment.setTextContent(TransitionList.get(i).actionDescription);
Element guard = automa.createElement("label");
(guard instanceof Element ? (Element)guard : null).setAttribute("kind","guard");
String guardText = "";
MyNotationConverter mync = new MyNotationConverter();
for (int j = 0;j < TransitionList.get(i).transitionAction.getPreconditions().size();j++)
{
if (j == TransitionList.get(i).transitionAction.getPreconditions().size() - 1)
{
guardText = guardText + "(" + mync.toUppaalGuard(TransitionList.get(i).transitionAction.getPreconditions().get(j).getStringPrecondition()) + ")";
}
else
{
guardText = "(" + guardText + mync.toUppaalGuard(TransitionList.get(i).transitionAction.getPreconditions().get(j).getStringPrecondition()) + ")" + "&&";
}
}
//*/
guard.setTextContent(guardText);
Element updates = automa.createElement("label");
(updates instanceof Element ? (Element)updates : null).setAttribute("kind","assignment");
String[] BeliefsToChangeTranslated = new String[TransitionList.get(i).transitionAction.getPost().getBeliefNames().length];
String[] newValuesTranslated = new String[TransitionList.get(i).transitionAction.getPost().getBeliefNames().length];
Beliefs belsToTranslate = TransitionList.get(i).transitionAction.getPost().ApplyPostCondition(TransitionList.get(i).source.stateBeliefs);
for (int j = 0;j < BeliefsToChangeTranslated.length;j++)
{
String tmp = TransitionList.get(i).transitionAction.getPost().getBeliefNames()[j];
tmp = tmp.replace("(","_");
tmp = tmp.replace(")","_");
tmp = tmp.replace(",","_");
BeliefsToChangeTranslated[j] = tmp;
tmp = belsToTranslate.getBeliefValue(TransitionList.get(i).transitionAction.getPost().getBeliefNames()[j]);
if (tmp.equals("true") || tmp.equals("false"))
{
tmp = "_" + tmp;
}
newValuesTranslated[j] = tmp;
}
String updateValue = "";
for (int j = 0;j < BeliefsToChangeTranslated.length;j++)
{
if (j == BeliefsToChangeTranslated.length - 1)
{
updateValue = updateValue + BeliefsToChangeTranslated[j] + "=" + newValuesTranslated[j];
}
else
{
updateValue = updateValue + BeliefsToChangeTranslated[j] + "=" + newValuesTranslated[j] + ",";
}
}
updates.setTextContent( updateValue ) ;
template.appendChild(trans);
trans.appendChild(from);
trans.appendChild(to);
trans.appendChild(comment);
trans.appendChild(guard);
trans.appendChild(updates);
}
automa.Save("C:/Users/Dario/Desktop/Exchange/" + agent.agentName + ".xml");
}
}
/*
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document automa = dBuilder.parse("/Users/Carmi/Desktop/WorkspaceTesi/Agent/" + agent.getAgentName() + ".xml");
automa.getDocumentElement().normalize();
NodeList states = automa.getElementsByTagName("location");
NodeList states2 = automa.getElementsByTagName("label");
//Node thisNode = eventList.item(i);
//Element section = (Element) states.item(i);
// System.out.println("il nodo è " + section.getFirstChild());
for (int j = 0;j < states.getLength();j++)
{
String temp = (states.item(j) instanceof Element ? (Element)states.item(j) : null).getAttribute("id");
String[] temp1 = temp.split("[d]", -1);
stateid = Integer.parseInt(temp1[1]);
//System.out.println("currmax = " + currmaxID);
//System.out.println("id numero = " + stateid);
if (stateid > currmaxID)
{
//System.out.println("sono entrato qui");
//System.out.println("allora ID = " + stateid + " e invece currmax = " + currmaxID);
currmaxID = stateid;
}
Beliefs ret = new Beliefs();
String rep = (states2.item(j) instanceof Element ? (Element)states2.item(j) : null).getTextContent();
//String text = (states.item(j) instanceof Element ? (Element)states.item(j): null).getElementsByTagName("label");
//System.out.println(rep);
String[] tempbel = rep.split("[;]", -1);
for (int i1 = 0;i1 < tempbel.length;i1++)
{
String [] t1 = tempbel[i1].split("[=]", -1);
//System.out.println("questa è t1 di zero = " + t1[0]);
//System.out.println("questa è t1 di uno = " + t1[1]);
ret.addBelief(t1[0],t1[1]);
System.out.println(ret.getBels().get(i1).getName());
}
retList.add(new AutomaState(ret,stateid));
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
}
*/
}
/*
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
Beliefs b1 = new Beliefs();
AutomaState s1 = new AutomaState(b1,1);
StateList.add(s1);
try {
dBuilder = dbFactory.newDocumentBuilder();
Document automa = dBuilder.newDocument();
//add elements to Document
Element rootElement =
automa.createElement("nta");
//append root element to document
automa.appendChild(rootElement);
Element globalDeclarations = automa.createElement("declaration");//CreateNode("element","declaration","");
Element template = automa.createElement("template");
Element system = automa.createElement("system");
system.setTextContent(agent.getAgentName() + "Process" + "=" + agent.getAgentName() + "_automa()" + ";" + "system " + agent.getAgentName() + "Process;");
rootElement.appendChild(globalDeclarations);
rootElement.appendChild(template);
rootElement.appendChild(system);
Element templateName=automa.createElement("name");
templateName.setTextContent(agent.getAgentName()+"_automa");
Element localDeclarations=automa.createElement ("declaration");
template.appendChild(templateName);
template.appendChild(localDeclarations);
for (int i = 0;i < StateList.size();i++)
{
Element loc = automa.createElement("location");
(loc instanceof Element ? (Element)loc : null).setAttribute("id",StateList.get(i).myID);
Element locname = automa.createElement("name");
locname.setTextContent(StateList.get(i).myID);
Element comment = automa.createElement("label");
(comment instanceof Element ? (Element)comment : null).setAttribute("kind","comments");
comment.setTextContent( StateList.get(i).stringBeliefs) ;
template.appendChild(loc);
loc.appendChild(locname);
loc.appendChild(comment);
}
for (int i = 0;i < TransitionList.size();i++)
{
Element trans = automa.createElement("transition");
Element from = automa.createElement("source");
(from instanceof Element ? (Element)from : null).setAttribute("ref",TransitionList.get(i).sourceID);
Element to = automa.createElement("target");
(to instanceof Element ? (Element)to : null).setAttribute("ref",TransitionList.get(i).targetID);
Element comment = automa.createElement("label");
(comment instanceof Element ? (Element)comment : null).setAttribute("kind","comments");
comment.setTextContent(TransitionList.get(i).actionDescription);
Element guard = automa.createElement("label");
(guard instanceof Element ? (Element)guard : null).setAttribute("kind","guard");
String guardText = "";
MyNotationConverter mync = new MyNotationConverter();
for (int j = 0;j < TransitionList.get(i).transitionAction.getPreconditions().size();j++)
{
if (j == TransitionList.get(i).transitionAction.getPreconditions().size() - 1)
{
guardText = guardText + "(" + mync.toUppaalGuard(TransitionList.get(i).transitionAction.getPreconditions().get(j).getStringPrecondition()) + ")";
}
else
{
guardText = "(" + guardText + mync.toUppaalGuard(TransitionList.get(i).transitionAction.getPreconditions().get(j).getStringPrecondition()) + ")" + "&&";
}
}
//*/
/*
guard.setTextContent(guardText);
Element updates = automa.createElement("label");
(updates instanceof Element ? (Element)updates : null).setAttribute("kind","assignment");
String[] BeliefsToChangeTranslated = new String[TransitionList.get(i).transitionAction.getPost().getBeliefNames().length];
String[] newValuesTranslated = new String[TransitionList.get(i).transitionAction.getPost().getBeliefNames().length];
Beliefs belsToTranslate = TransitionList.get(i).transitionAction.getPost().ApplyPostCondition(TransitionList.get(i).source.stateBeliefs);
for (int j = 0;j < BeliefsToChangeTranslated.length;j++)
{
String tmp = TransitionList.get(i).transitionAction.getPost().getBeliefNames()[j];
tmp = tmp.replace("(","_");
tmp = tmp.replace(")","_");
tmp = tmp.replace(",","_");
BeliefsToChangeTranslated[j] = tmp;
tmp = belsToTranslate.getBeliefValue(TransitionList.get(i).transitionAction.getPost().getBeliefNames()[j]);
if (tmp.equals("true") || tmp.equals("false"))
{
tmp = "_" + tmp;
}
newValuesTranslated[j] = tmp;
}
String updateValue = "";
for (int j = 0;j < BeliefsToChangeTranslated.length;j++)
{
if (j == BeliefsToChangeTranslated.length - 1)
{
updateValue = updateValue + BeliefsToChangeTranslated[j] + "=" + newValuesTranslated[j];
}
else
{
updateValue = updateValue + BeliefsToChangeTranslated[j] + "=" + newValuesTranslated[j] + ",";
}
}
updates.setTextContent(updateValue);
template.appendChild(trans);
trans.appendChild(from);
trans.appendChild(to);
trans.appendChild(comment);
trans.appendChild(guard);
trans.appendChild(updates);
}
//automa.Save("C:/Users/Dario/Desktop/Exchange/" + agent.agentName + ".xml");
//for output to file, console
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//for pretty print
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(automa);
//write to console or file
StreamResult console = new StreamResult(System.out);
StreamResult file = new StreamResult(new File("/Users/Carmi/Desktop/WorkspaceTesi/Agent/" + agent.getAgentName() + ".xml"));//Users\carmi\Desktop\WorkspaceTesi\Agent
//test lettura file da path
Document automa2 = dBuilder.parse("/Users/Carmi/Desktop/WorkspaceTesi/Agent/" + agent.getAgentName() + ".xml");
DOMSource source2 = new DOMSource(automa);
System.out.println("Source2");
//transformer.transform(source2,console);
automa2.getDocumentElement().normalize();
System.out.println("Root element :" + automa2.getDocumentElement().getNodeName());
//NodeList states = automa2.getElementsByTagName("location");
NodeList states = automa2.getElementsByTagName("location");
NodeList states2 = automa2.getElementsByTagName("label");
for (int i=0; i< states.getLength(); i++) {
//Node thisNode = eventList.item(i);
Element section = (Element) states.item(i);
// System.out.println("il nodo è " + section.getFirstChild());
currmaxID = 0;
for (int j = 0;j < states.getLength();j++)
{
String temp = (states.item(j) instanceof Element ? (Element)states.item(j) : null).getAttribute("id");
String[] temp1 = temp.split("[d]", -1);
int stateid = Integer.parseInt(temp1[1]);
System.out.println("currmax = " + currmaxID);
System.out.println("id numero = " + stateid);
if (stateid > currmaxID)
{
System.out.println("sono entrato qui");
System.out.println("allora ID = " + stateid + " e invece currmax = " + currmaxID);
currmaxID = stateid;
}
Beliefs ret = new Beliefs();
String rep = (states2.item(j) instanceof Element ? (Element)states2.item(j) : null).getTextContent();
//String text = (states.item(j) instanceof Element ? (Element)states.item(j): null).getElementsByTagName("label");
System.out.println(rep);
String[] tempbel = rep.split("[;]", -1);
for (int i1 = 0;i1 < tempbel.length;i1++)
{
String [] t1 = tempbel[i1].split("[=]", -1);
System.out.println("questa è t1 di zero = " + t1[0]);
System.out.println("questa è t1 di uno = " + t1[1]);
ret.addBelief(t1[0],t1[1]);
}
//retList.add(new AutomaState(ret,stateid));
}
//return retList;
}
//NodeList states = ((tempVar instanceof XmlElement ? (XmlElement)tempVar : null).GetElementsByTagName("template")[0] instanceof XmlElement ? (XmlElement)(tempVar instanceof XmlElement ? (XmlElement)tempVar : null).GetElementsByTagName("template")[0] : null).GetElementsByTagName("location");
// for (int temp = 0; temp < states.getLength(); temp++) {
// Node nNode = states.item(temp);
// System.out.println("\nCurrent Element :" + nNode.getNodeName());
//write data
//transformer.transform(source, console);
//transformer.transform(source, file);
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
*/
/*
private static Node getEmployee(Document doc, String id, String name, String age, String role,
String gender) {
Element employee = doc.createElement("Employee");
//set id attribute
employee.setAttribute("id", id);
//create name element
employee.appendChild(getEmployeeElements(doc, employee, "name", name));
//create age element
employee.appendChild(getEmployeeElements(doc, employee, "age", age));
//create role element
employee.appendChild(getEmployeeElements(doc, employee, "role", role));
//create gender element
employee.appendChild(getEmployeeElements(doc, employee, "gender", gender));
return employee;
}
//utility method to create text node
private static Node getEmployeeElements(Document doc, Element element, String name, String value) {
Element node = doc.createElement(name);
node.appendChild(doc.createTextNode(value));
return node;
}
*/