1 package loadfromxml;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.io.IOException;
6 import java.nio.charset.StandardCharsets;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
9 import java.rmi.RemoteException;
10 import java.util.ArrayList;
11
12 import javax.naming.NamingException;
13 import javax.swing.JFileChooser;
14 import javax.swing.JOptionPane;
15 import javax.swing.filechooser.FileFilter;
16 import javax.xml.parsers.ParserConfigurationException;
17
18 import org.djunits.unit.DurationUnit;
19 import org.djunits.value.vdouble.scalar.Duration;
20 import org.djunits.value.vdouble.scalar.Time;
21 import org.opentrafficsim.base.modelproperties.Property;
22 import org.opentrafficsim.base.modelproperties.PropertyException;
23 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
24 import org.opentrafficsim.core.dsol.OTSModelInterface;
25 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
26 import org.opentrafficsim.core.geometry.OTSGeometryException;
27 import org.opentrafficsim.core.gtu.GTUException;
28 import org.opentrafficsim.core.gtu.animation.GTUColorer;
29 import org.opentrafficsim.core.network.NetworkException;
30 import org.opentrafficsim.core.network.OTSNetwork;
31 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
32 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
33 import org.opentrafficsim.simulationengine.OTSSimulationException;
34 import org.xml.sax.SAXException;
35
36 import nl.tudelft.simulation.dsol.SimRuntimeException;
37 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
38 import nl.tudelft.simulation.event.EventProducer;
39
40
41
42
43
44
45
46
47
48
49
50
51 public class LoadXML extends AbstractWrappableAnimation
52 {
53
54
55 private static final long serialVersionUID = 20170421L;
56
57
58 private String fileName = null;
59
60
61 private String xml = null;
62
63
64
65
66
67
68
69
70
71
72 public static void main(final String[] args) throws IOException, SimRuntimeException, NamingException,
73 OTSSimulationException, PropertyException
74 {
75 JFileChooser fileChooser = new JFileChooser();
76 fileChooser.addChoosableFileFilter(new FileFilter()
77 {
78
79 @Override
80 public boolean accept(final File f)
81 {
82 String name = f.getName();
83 int length = name.length();
84 if (length < 5)
85 {
86 return false;
87 }
88 String type = name.substring(length - 4);
89 return type.equalsIgnoreCase(".xml");
90 }
91
92 @Override
93 public String getDescription()
94 {
95 return "XML files";
96 }
97 });
98 fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
99 if (JFileChooser.APPROVE_OPTION != fileChooser.showOpenDialog(null))
100 {
101 System.out.println("No file chosen; exiting");
102 System.exit(0);
103 }
104 LoadXML loadXML = new LoadXML();
105 loadXML.fileName = fileChooser.getSelectedFile().getAbsolutePath();
106 loadXML.xml = new String(Files.readAllBytes(Paths.get(loadXML.fileName)));
107 try
108 {
109 loadXML.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(3600, DurationUnit.SI), new ArrayList<Property<?>>(),
110 null, true);
111 }
112 catch (SimRuntimeException sre)
113 {
114 JOptionPane.showMessageDialog(null, sre.getMessage(), "Exception occured", JOptionPane.ERROR_MESSAGE);
115 System.exit(1);
116 }
117 }
118
119
120 @Override
121 public final String shortName()
122 {
123 return this.fileName;
124 }
125
126
127 @Override
128 public final String description()
129 {
130 return "OTS network from " + this.fileName;
131 }
132
133
134 private XMLModel model = null;
135
136
137 @Override
138 protected final OTSModelInterface makeModel(final GTUColorer colorer) throws OTSSimulationException
139 {
140 this.model = new XMLModel();
141 return this.model;
142 }
143
144
145
146
147 class XMLModel extends EventProducer implements OTSModelInterface
148 {
149
150
151 private static final long serialVersionUID = 20170421L;
152
153
154 private OTSNetwork network;
155
156
157 private SimulatorInterface<Time, Duration, OTSSimTimeDouble> simulator;
158
159
160 @SuppressWarnings("synthetic-access")
161 @Override
162 public void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> theSimulator)
163 throws SimRuntimeException, RemoteException
164 {
165 this.simulator = theSimulator;
166 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser((OTSDEVSSimulatorInterface) theSimulator);
167 try
168 {
169 this.network = nlp.build(new ByteArrayInputStream(LoadXML.this.xml.getBytes(StandardCharsets.UTF_8)));
170 }
171 catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException
172 | GTUException | OTSGeometryException exception)
173 {
174 exception.printStackTrace();
175
176
177 throw new SimRuntimeException(exception.getMessage());
178 }
179 }
180
181
182 @Override
183 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
184 {
185 return this.simulator;
186 }
187
188
189 @Override
190 public OTSNetwork getNetwork()
191 {
192 return this.network;
193 }
194
195 }
196 }