1 package loadfromxml;
2
3 import java.awt.Dimension;
4 import java.io.ByteArrayInputStream;
5 import java.io.File;
6 import java.io.IOException;
7 import java.net.URISyntaxException;
8 import java.nio.charset.StandardCharsets;
9 import java.nio.file.Files;
10 import java.nio.file.Paths;
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.bind.JAXBException;
17 import javax.xml.parsers.ParserConfigurationException;
18
19 import org.djunits.value.vdouble.scalar.Duration;
20 import org.djunits.value.vdouble.scalar.Length;
21 import org.djunits.value.vdouble.scalar.Time;
22 import org.opentrafficsim.core.dsol.AbstractOTSModel;
23 import org.opentrafficsim.core.dsol.OTSAnimator;
24 import org.opentrafficsim.core.dsol.OTSModelInterface;
25 import org.opentrafficsim.core.dsol.OTSSimulationException;
26 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
27 import org.opentrafficsim.core.geometry.OTSGeometryException;
28 import org.opentrafficsim.core.gtu.GTUException;
29 import org.opentrafficsim.core.gtu.GTUType;
30 import org.opentrafficsim.core.network.NetworkException;
31 import org.opentrafficsim.draw.core.OTSDrawingException;
32 import org.opentrafficsim.road.gtu.lane.plan.operational.LaneOperationalPlanBuilder;
33 import org.opentrafficsim.road.network.OTSRoadNetwork;
34 import org.opentrafficsim.road.network.factory.xml.XmlParserException;
35 import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
36 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
37 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
38 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
39 import org.xml.sax.SAXException;
40
41 import nl.tudelft.simulation.dsol.SimRuntimeException;
42 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
43
44
45
46
47
48
49
50
51
52
53
54
55 public class LoadXML extends OTSSimulationApplication<OTSModelInterface>
56 {
57
58 private static final long serialVersionUID = 20170421L;
59
60
61
62
63
64
65 public LoadXML(final OTSModelInterface model, final OTSAnimationPanel animationPanel) throws OTSDrawingException
66 {
67 super(model, animationPanel);
68 }
69
70
71
72
73
74
75
76
77
78
79 public static void main(final String[] args)
80 throws IOException, SimRuntimeException, NamingException, OTSSimulationException, InputParameterException
81 {
82 LaneOperationalPlanBuilder.INSTANT_LANE_CHANGES = true;
83 String fileName;
84 String xml;
85 if (0 == args.length)
86 {
87 JFileChooser fileChooser = new JFileChooser();
88 fileChooser.addChoosableFileFilter(new FileFilter()
89 {
90
91 @Override
92 public boolean accept(final File f)
93 {
94 String name = f.getName();
95 int length = name.length();
96 if (length < 5)
97 {
98 return false;
99 }
100 String type = name.substring(length - 4);
101 return type.equalsIgnoreCase(".xml");
102 }
103
104 @Override
105 public String getDescription()
106 {
107 return "XML files";
108 }
109 });
110 fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
111 if (JFileChooser.APPROVE_OPTION != fileChooser.showOpenDialog(null))
112 {
113 System.out.println("No file chosen; exiting");
114 System.exit(0);
115 }
116 fileName = fileChooser.getSelectedFile().getAbsolutePath();
117 }
118 else
119 {
120 fileName = args[0];
121 }
122 xml = new String(Files.readAllBytes(Paths.get(fileName)));
123 try
124 {
125 OTSAnimator simulator = new OTSAnimator();
126 XMLModel xmlModel = new XMLModel(simulator, "XML model", "Model built from XML file " + fileName, xml);
127 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), xmlModel);
128 OTSAnimationPanel animationPanel = new OTSAnimationPanel(xmlModel.getNetwork().getExtent(), new Dimension(800, 600),
129 simulator, xmlModel, DEFAULT_COLORER, xmlModel.getNetwork());
130 new LoadXML(xmlModel, animationPanel);
131 }
132 catch (SimRuntimeException | OTSDrawingException sre)
133 {
134 JOptionPane.showMessageDialog(null, sre.getMessage(), "Exception occured", JOptionPane.ERROR_MESSAGE);
135 System.exit(1);
136 }
137 }
138
139
140
141
142 static class XMLModel extends AbstractOTSModel
143 {
144
145 private static final long serialVersionUID = 20170421L;
146
147
148 private OTSRoadNetwork network;
149
150
151 private final String xml;
152
153
154
155
156
157
158
159 XMLModel(final OTSSimulatorInterface simulator, final String shortName, final String description, final String xml)
160 {
161 super(simulator, shortName, description);
162 this.xml = xml;
163 }
164
165
166 @Override
167 public void constructModel() throws SimRuntimeException
168 {
169 this.network = new OTSRoadNetwork(getShortName(), true);
170 try
171 {
172 XmlNetworkLaneParser.build(new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8)),
173 this.network, getSimulator());
174 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), getSimulator(),
175 new ConflictBuilder.FixedWidthGenerator(Length.createSI(2.0)));
176 }
177 catch (NetworkException | OTSGeometryException | JAXBException | URISyntaxException | XmlParserException
178 | SAXException | ParserConfigurationException | GTUException exception)
179 {
180 exception.printStackTrace();
181
182
183 throw new SimRuntimeException(exception.getMessage());
184 }
185 }
186
187
188 @Override
189 public OTSRoadNetwork getNetwork()
190 {
191 return this.network;
192 }
193
194 }
195
196 }