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