1 package org.opentrafficsim.road.network.factory.vissim;
2
3 import java.awt.geom.Rectangle2D;
4 import java.io.File;
5 import java.io.IOException;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.ArrayList;
9
10 import javax.naming.NamingException;
11 import javax.swing.SwingUtilities;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.djunits.unit.DurationUnit;
15 import org.djunits.value.vdouble.scalar.Duration;
16 import org.djunits.value.vdouble.scalar.Time;
17 import org.opengis.feature.Property;
18 import org.opentrafficsim.base.modelproperties.PropertyException;
19 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
20 import org.opentrafficsim.core.dsol.OTSModelInterface;
21 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
22 import org.opentrafficsim.core.geometry.OTSGeometryException;
23 import org.opentrafficsim.core.gtu.GTUException;
24 import org.opentrafficsim.core.gtu.animation.GTUColorer;
25 import org.opentrafficsim.core.network.NetworkException;
26 import org.opentrafficsim.core.network.OTSNetwork;
27 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
28 import org.opentrafficsim.simulationengine.OTSSimulationException;
29 import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
30 import org.xml.sax.SAXException;
31
32 import nl.tudelft.simulation.dsol.SimRuntimeException;
33 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
34
35 public class TestVissimParser extends AbstractWrappableAnimation
36 {
37
38
39
40
41
42
43 public static void main(final String[] args) throws SimRuntimeException
44 {
45 SwingUtilities.invokeLater(new Runnable()
46 {
47 @Override
48 public void run()
49 {
50 try
51 {
52 TestVissimParser xmlModel = new TestVissimParser();
53
54 xmlModel.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
55 new ArrayList<org.opentrafficsim.base.modelproperties.Property<?>>(), null, true);
56 }
57 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
58 {
59 exception.printStackTrace();
60 }
61 }
62 });
63 }
64
65
66 @Override
67 public final String shortName()
68 {
69 return "TestXMLModel";
70 }
71
72
73 @Override
74 public final String description()
75 {
76 return "TestXMLModel";
77 }
78
79
80 @Override
81 public final void stopTimersThreads()
82 {
83 super.stopTimersThreads();
84 }
85
86
87 @Override
88 protected final void addTabs(final SimpleSimulatorInterface simulator)
89 {
90 return;
91 }
92
93
94 @Override
95 protected final OTSModelInterface makeModel(final GTUColorer colorer)
96 {
97 return new VissimImport();
98 }
99
100
101 @Override
102 protected final java.awt.geom.Rectangle2D.Double makeAnimationRectangle()
103 {
104
105 return new Rectangle2D.Double(162000, 384500, 2000, 2000);
106 }
107
108
109 @Override
110 public final String toString()
111 {
112 return "TestVissimParser []";
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 class VissimImport implements OTSModelInterface
131 {
132
133 private static final long serialVersionUID = 20141121L;
134
135
136 private OTSDEVSSimulatorInterface simulator;
137
138
139 private OTSNetwork network = new OTSNetwork("test Vissim network");
140
141
142 @Override
143 public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> pSimulator)
144 throws SimRuntimeException
145 {
146
147
148 boolean OpenTrafficSim = false;
149 String sinkKillClassName;
150 String sensorClassName;
151 String trafficLightName;
152 if (OpenTrafficSim)
153 {
154 sinkKillClassName = "org.opentrafficsim.road.network.lane.object.sensor.SinkSensor";
155 sensorClassName = "org.opentrafficsim.road.network.lane.object.sensor.SimpleReportingSensor";
156 trafficLightName = "org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight";
157 }
158 else
159 {
160 sinkKillClassName = "nl.grontmij.smarttraffic.model.KillSensor";
161 sensorClassName = "nl.grontmij.smarttraffic.model.CheckSensor";
162 trafficLightName = "org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight";
163 }
164 this.simulator = (OTSDEVSSimulatorInterface) pSimulator;
165 ClassLoader classLoader = getClass().getClassLoader();
166 URL inputUrl = null;
167 try
168 {
169 inputUrl = new URL(classLoader.getResource("ehv_eisen1_VA.inpx").toString());
170 }
171 catch (MalformedURLException e1)
172 {
173
174 e1.printStackTrace();
175 }
176 String path = classLoader.getResource("").getPath().toString();
177 File outputFile = new File(path, "/testEindhoven.xml");
178 try
179 {
180 outputFile.createNewFile();
181 }
182 catch (IOException e1)
183 {
184 e1.printStackTrace();
185 }
186 VissimNetworkLaneParser nlp = new VissimNetworkLaneParser(this.simulator);
187
188 try
189 {
190 this.network = nlp.build(inputUrl, outputFile, network, sinkKillClassName, sensorClassName, trafficLightName);
191 }
192 catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException | GTUException
193 | OTSGeometryException exception)
194 {
195 exception.printStackTrace();
196 }
197
198 }
199
200
201
202
203
204 private Double parseDouble(Property property)
205 {
206 if (property.getValue() != null)
207 {
208 if (property.getValue().toString() != null)
209 {
210 return Double.parseDouble(property.getValue().toString());
211 }
212 }
213 return Double.NaN;
214 }
215
216
217 @Override
218 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator()
219 {
220 return this.simulator;
221 }
222
223
224 @Override
225 public OTSNetwork getNetwork()
226 {
227 return this.network;
228 }
229
230
231 @Override
232 public final String toString()
233 {
234 return "TestVissimParser [simulator=" + this.simulator + "]";
235 }
236
237 }
238
239 }